From 627d135bf5108f514161e1d37acf6b97c4b3c4a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Wed, 18 Mar 2020 22:53:40 +0100 Subject: Move UUID and time definitions to separate modules --- include/erebos/message.h | 2 +- include/erebos/service.h | 6 ++++-- include/erebos/storage.h | 29 +++------------------------ include/erebos/time.h | 20 +++++++++++++++++++ include/erebos/uuid.h | 20 +++++++++++++++++++ src/CMakeLists.txt | 2 ++ src/message.h | 1 + src/storage.cpp | 52 ------------------------------------------------ src/time.cpp | 35 ++++++++++++++++++++++++++++++++ src/uuid.cpp | 31 +++++++++++++++++++++++++++++ 10 files changed, 117 insertions(+), 81 deletions(-) create mode 100644 include/erebos/time.h create mode 100644 include/erebos/uuid.h create mode 100644 src/time.cpp create mode 100644 src/uuid.cpp diff --git a/include/erebos/message.h b/include/erebos/message.h index 6415f92..0b5e257 100644 --- a/include/erebos/message.h +++ b/include/erebos/message.h @@ -15,7 +15,7 @@ class DirectMessage { public: const Identity & from() const; - const ZonedTime & time() const; + const struct ZonedTime & time() const; const std::string & text() const; private: diff --git a/include/erebos/service.h b/include/erebos/service.h index 7a6f646..2d6fbce 100644 --- a/include/erebos/service.h +++ b/include/erebos/service.h @@ -1,6 +1,8 @@ #pragma once -#include +#include + +#include namespace erebos { @@ -17,7 +19,7 @@ public: Context(Priv *); Priv & priv(); - const Ref & ref() const; + const class Ref & ref() const; const class Peer & peer() const; private: diff --git a/include/erebos/storage.h b/include/erebos/storage.h index 4d7c691..d5ae45a 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -1,8 +1,10 @@ #pragma once +#include +#include + #include #include -#include #include #include #include @@ -11,8 +13,6 @@ #include #include -#include - namespace erebos { class Storage; @@ -162,29 +162,6 @@ protected: Ref(const std::shared_ptr p): PartialRef(p) {} }; -struct ZonedTime -{ - explicit ZonedTime(std::string); - ZonedTime(std::chrono::system_clock::time_point t): time(t), zone(0) {} - explicit operator std::string() const; - - static ZonedTime now(); - - std::chrono::system_clock::time_point time; - std::chrono::minutes zone; // zone offset -}; - -struct UUID -{ - explicit UUID(std::string); - explicit operator std::string() const; - - bool operator==(const UUID &) const; - bool operator!=(const UUID &) const; - - uuid_t uuid; -}; - template class RecordT { diff --git a/include/erebos/time.h b/include/erebos/time.h new file mode 100644 index 0000000..d8ff5b1 --- /dev/null +++ b/include/erebos/time.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace erebos { + +struct ZonedTime +{ + explicit ZonedTime(std::string); + ZonedTime(std::chrono::system_clock::time_point t): time(t), zone(0) {} + explicit operator std::string() const; + + static ZonedTime now(); + + std::chrono::system_clock::time_point time; + std::chrono::minutes zone; // zone offset +}; + +} diff --git a/include/erebos/uuid.h b/include/erebos/uuid.h new file mode 100644 index 0000000..31322ea --- /dev/null +++ b/include/erebos/uuid.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include + +namespace erebos { + +struct UUID +{ + explicit UUID(std::string); + explicit operator std::string() const; + + bool operator==(const UUID &) const; + bool operator!=(const UUID &) const; + + uuid_t uuid; +}; + +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d6d5441..64c14b5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,4 +10,6 @@ add_library(erebos pubkey service storage + time + uuid ) diff --git a/src/message.h b/src/message.h index 48487ba..7e0f597 100644 --- a/src/message.h +++ b/src/message.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include diff --git a/src/storage.cpp b/src/storage.cpp index 49bac54..81a70e1 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -535,58 +535,6 @@ const Storage & Ref::storage() const } -ZonedTime::ZonedTime(string str) -{ - intmax_t t; - unsigned int h, m; - char sign[2]; - if (sscanf(str.c_str(), "%jd %1[+-]%2u%2u", &t, sign, &h, &m) != 4) - throw runtime_error("invalid zoned time"); - - time = std::chrono::system_clock::time_point(std::chrono::seconds(t)); - zone = std::chrono::minutes((sign[0] == '-' ? -1 : 1) * (60 * h + m)); -} - -ZonedTime::operator string() const -{ - char buf[32]; - unsigned int az = std::chrono::abs(zone).count(); - snprintf(buf, sizeof(buf), "%jd %c%02u%02u", - (intmax_t) std::chrono::duration_cast(time.time_since_epoch()).count(), - zone < decltype(zone)::zero() ? '-' : '+', az / 60, az % 60); - return string(buf); -} - -ZonedTime ZonedTime::now() -{ - return ZonedTime(std::chrono::system_clock::now()); -} - - -UUID::UUID(string str) -{ - if (uuid_parse(str.c_str(), uuid) != 0) - throw runtime_error("invalid UUID"); -} - -UUID::operator string() const -{ - string str(UUID_STR_LEN - 1, '\0'); - uuid_unparse_lower(uuid, str.data()); - return str; -} - -bool UUID::operator==(const UUID & other) const -{ - return std::equal(std::begin(uuid), std::end(uuid), std::begin(other.uuid)); -} - -bool UUID::operator!=(const UUID & other) const -{ - return !(*this == other); -} - - template RecordT::Item::operator bool() const { diff --git a/src/time.cpp b/src/time.cpp new file mode 100644 index 0000000..631e0f8 --- /dev/null +++ b/src/time.cpp @@ -0,0 +1,35 @@ +#include + +#include + +using namespace erebos; + +using std::runtime_error; +using std::string; + +ZonedTime::ZonedTime(string str) +{ + intmax_t t; + unsigned int h, m; + char sign[2]; + if (sscanf(str.c_str(), "%jd %1[+-]%2u%2u", &t, sign, &h, &m) != 4) + throw runtime_error("invalid zoned time"); + + time = std::chrono::system_clock::time_point(std::chrono::seconds(t)); + zone = std::chrono::minutes((sign[0] == '-' ? -1 : 1) * (60 * h + m)); +} + +ZonedTime::operator string() const +{ + char buf[32]; + unsigned int az = std::chrono::abs(zone).count(); + snprintf(buf, sizeof(buf), "%jd %c%02u%02u", + (intmax_t) std::chrono::duration_cast(time.time_since_epoch()).count(), + zone < decltype(zone)::zero() ? '-' : '+', az / 60, az % 60); + return string(buf); +} + +ZonedTime ZonedTime::now() +{ + return ZonedTime(std::chrono::system_clock::now()); +} diff --git a/src/uuid.cpp b/src/uuid.cpp new file mode 100644 index 0000000..e1f044a --- /dev/null +++ b/src/uuid.cpp @@ -0,0 +1,31 @@ +#include + +#include + +using namespace erebos; + +using std::runtime_error; +using std::string; + +UUID::UUID(string str) +{ + if (uuid_parse(str.c_str(), uuid) != 0) + throw runtime_error("invalid UUID"); +} + +UUID::operator string() const +{ + string str(UUID_STR_LEN - 1, '\0'); + uuid_unparse_lower(uuid, str.data()); + return str; +} + +bool UUID::operator==(const UUID & other) const +{ + return std::equal(std::begin(uuid), std::end(uuid), std::begin(other.uuid)); +} + +bool UUID::operator!=(const UUID & other) const +{ + return !(*this == other); +} -- cgit v1.2.3