Support conversion between archive and string
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 18 Nov 2019 10:50:09 +0000 (19:50 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Fri, 22 Nov 2019 01:52:17 +0000 (10:52 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/archive.hpp
src/vist/common/tests/archive.cpp

index f1d525a..4086652 100644 (file)
@@ -44,6 +44,12 @@ using IsArchival = typename std::enable_if<std::is_base_of<Archival, T>::value,
 
 class Archive final {
 public:
+       explicit Archive() = default;
+       /// Use only when flatten data comes from Archive (by casting to string)
+       explicit Archive(const std::string& flatten) : buffer(flatten.cbegin(), flatten.cend())
+       {
+       }
+
        template<typename Front, typename... Rest>
        void pack(const Front& front, const Rest&... rest);
        inline void pack(void) {}
@@ -55,7 +61,7 @@ public:
        template<typename... Ts>
        void transform(std::tuple<Ts...>& tuple);
 
-       // serialize method
+       /// serialize method
        template<typename T, IsFundamental<T> = 0>
        Archive& operator<<(const T& value);
        template<typename T, IsArchival<T> = 0>
@@ -67,7 +73,7 @@ public:
        Archive& operator<<(const std::string& value);
        Archive& operator<<(const Archive& archive);
 
-       // deserialize method
+       /// deserialize method
        template<typename T, IsFundamental<T> = 0>
        Archive& operator>>(T& value);
        template<typename T, IsArchival<T> = 0>
@@ -83,9 +89,15 @@ public:
        std::size_t size(void) const noexcept;
        void reserve(std::size_t size) noexcept;
 
+       /// It is not safe when Archive includes binary formats.
+       operator std::string() const
+       {
+               return std::string(this->buffer.begin(), this->buffer.end());
+       }
+
 protected:
-       virtual void save(const void* bytes, std::size_t size);
-       virtual void load(void* bytes, std::size_t size);
+       void save(const void* bytes, std::size_t size);
+       void load(void* bytes, std::size_t size);
 
 private:
        template<typename T>
index ab8c69b..508eb83 100644 (file)
@@ -310,3 +310,21 @@ TEST(ArchiveTests, parameter_pack_transform_empty)
        Archive archive;
        archive.transform(tuple);
 }
+
+TEST(ArchiveTests, string_conversion)
+{
+       int input1 = std::numeric_limits<int>::lowest();
+       int input2 = std::numeric_limits<int>::max();
+
+       Archive archive;
+       archive << input1 << input2;
+
+       std::string flatten = archive;
+       Archive restore(flatten);
+
+       int output1, output2;
+       restore >> output1 >> output2;
+
+       EXPECT_EQ(input1, output1);
+       EXPECT_EQ(input2, output2);
+}