Make StorageSerializer a template class 19/33519/28
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 12 Jan 2015 14:01:54 +0000 (15:01 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Wed, 4 Mar 2015 10:28:25 +0000 (11:28 +0100)
This patch modifies StorageSerializer so that it will be able to use
other streams than std::ostream and its derivatives. Within current
class hierarchy custom output streams with overloaded insertion operator
(operator<<) cannot be used, as it is non-virtual in std::ostream.

Change-Id: I3e713329c55aacfbb8daa23a5c4579d4c5db9f52

src/storage/CMakeLists.txt
src/storage/InMemoryStorageBackend.cpp
src/storage/InMemoryStorageBackend.h
src/storage/StorageSerializer.cpp [deleted file]
src/storage/StorageSerializer.h
test/CMakeLists.txt
test/storage/serializer/dump.cpp
test/storage/serializer/dump_load.cpp
test/storage/serializer/serialize.cpp

index 996b37a..72f0ec2 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
 #
 #    Licensed under the Apache License, Version 2.0 (the "License");
 #    you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@ SET(LIB_CYNARA_STORAGE_SOURCES
     ${CYNARA_LIB_CYNARA_STORAGE_PATH}/Integrity.cpp
     ${CYNARA_LIB_CYNARA_STORAGE_PATH}/Storage.cpp
     ${CYNARA_LIB_CYNARA_STORAGE_PATH}/StorageDeserializer.cpp
-    ${CYNARA_LIB_CYNARA_STORAGE_PATH}/StorageSerializer.cpp
     )
 
 INCLUDE_DIRECTORIES(
index a5e08b1..d6655e2 100644 (file)
@@ -100,7 +100,7 @@ void InMemoryStorageBackend::save(void) {
     std::string indexFilename = m_dbPath + m_indexFilename;
     openDumpFileStream(indexStream, indexFilename + m_backupFilenameSuffix);
 
-    StorageSerializer storageSerializer(indexStream);
+    StorageSerializer<std::ofstream> storageSerializer(indexStream);
     storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener,
                            this, std::placeholders::_1));
 
@@ -253,14 +253,14 @@ std::shared_ptr<BucketDeserializer> InMemoryStorageBackend::bucketStreamOpener(
     }
 }
 
-std::shared_ptr<StorageSerializer> InMemoryStorageBackend::bucketDumpStreamOpener(
+std::shared_ptr<StorageSerializer<std::ofstream> > InMemoryStorageBackend::bucketDumpStreamOpener(
         const PolicyBucketId &bucketId) {
     std::string bucketFilename = m_dbPath + m_bucketFilenamePrefix +
                                  bucketId + m_backupFilenameSuffix;
     auto bucketStream = std::make_shared<std::ofstream>();
 
     openDumpFileStream(bucketStream, bucketFilename);
-    return std::make_shared<StorageSerializer>(bucketStream);
+    return std::make_shared<StorageSerializer<std::ofstream> >(bucketStream);
 }
 
 void InMemoryStorageBackend::postLoadCleanup(bool isBackupValid) {
index c9e63c2..10251c7 100644 (file)
@@ -71,7 +71,8 @@ protected:
 
     virtual void openDumpFileStream(std::shared_ptr<std::ofstream> stream,
                                     const std::string &filename);
-    std::shared_ptr<StorageSerializer> bucketDumpStreamOpener(const PolicyBucketId &bucketId);
+    std::shared_ptr<StorageSerializer<std::ofstream> > bucketDumpStreamOpener(
+            const PolicyBucketId &bucketId);
 
     virtual void postLoadCleanup(bool isBackupValid);
 
diff --git a/src/storage/StorageSerializer.cpp b/src/storage/StorageSerializer.cpp
deleted file mode 100644 (file)
index 9f701dd..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *    Licensed under the Apache License, Version 2.0 (the "License");
- *    you may not use this file except in compliance with the License.
- *    You may obtain a copy of the License at
- *
- *        http://www.apache.org/licenses/LICENSE-2.0
- *
- *    Unless required by applicable law or agreed to in writing, software
- *    distributed under the License is distributed on an "AS IS" BASIS,
- *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *    See the License for the specific language governing permissions and
- *    limitations under the License.
- */
-/**
- * @file        src/storage/StorageSerializer.cpp
- * @author      Aleksander Zdyb <a.zdyb@samsung.com>
- * @version     1.0
- * @brief       Implementation of Cynara::StorageSerializer methods
- */
-
-#include <fstream>
-#include <memory>
-#include <ios>
-
-#include <exceptions/BucketSerializationException.h>
-#include <types/Policy.h>
-#include <types/PolicyBucket.h>
-#include <types/PolicyBucketId.h>
-#include <types/PolicyCollection.h>
-#include <types/PolicyType.h>
-
-#include "StorageSerializer.h"
-
-namespace Cynara {
-
-StorageSerializer::StorageSerializer(std::shared_ptr<std::ostream> os) : m_outStream(os) {
-}
-
-void StorageSerializer::dump(const Buckets &buckets,
-                             BucketStreamOpener streamOpener) {
-
-    for (const auto bucketIter : buckets) {
-        const auto &bucket = bucketIter.second;
-
-        dumpFields(bucket.id(), bucket.defaultPolicy().policyType(),
-                   bucket.defaultPolicy().metadata());
-    }
-
-    for (const auto bucketIter : buckets) {
-        const auto &bucketId = bucketIter.first;
-        const auto &bucket = bucketIter.second;
-        auto bucketSerializer = streamOpener(bucketId);
-
-        if (bucketSerializer != nullptr) {
-            bucketSerializer->dump(bucket);
-        } else {
-            throw BucketSerializationException(bucketId);
-        }
-    }
-}
-
-void StorageSerializer::dump(const PolicyBucket& bucket) {
-    for (auto it = std::begin(bucket); it != std::end(bucket); ++it) {
-        const auto &policy = *it;
-        dump(policy);
-    }
-}
-
-void StorageSerializer::dump(const PolicyKeyFeature &keyFeature) {
-    *m_outStream << keyFeature.toString();
-}
-
-void StorageSerializer::dump(const PolicyType &policyType) {
-    auto oldFormat = m_outStream->flags();
-    *m_outStream << "0x" << std::uppercase <<  std::hex << policyType;
-    m_outStream->flags(oldFormat);
-}
-
-void StorageSerializer::dump(const PolicyResult::PolicyMetadata &metadata) {
-    *m_outStream << metadata;
-}
-
-void StorageSerializer::dump(const PolicyCollection::value_type &policy) {
-    const auto &key = policy->key();
-    const auto &result = policy->result();
-
-    dumpFields(key.client(), key.user(), key.privilege(), result.policyType(), result.metadata());
-}
-
-} /* namespace Cynara */
index 42982b2..40eb784 100644 (file)
 
 #include <functional>
 #include <fstream>
+#include <ios>
 #include <memory>
+#include <sstream>
 
 #include <config/PathConfig.h>
+#include <exceptions/BucketSerializationException.h>
+#include <types/Policy.h>
 #include <types/PolicyBucket.h>
 #include <types/PolicyBucketId.h>
 #include <types/PolicyCollection.h>
 
 namespace Cynara {
 
+template<typename StreamType>
 class StorageSerializer {
 
 public:
     typedef std::function<std::shared_ptr<StorageSerializer>(const PolicyBucketId &)>
             BucketStreamOpener;
 
-    StorageSerializer(std::shared_ptr<std::ostream> os);
+    StorageSerializer(std::shared_ptr<StreamType> os);
     virtual ~StorageSerializer() {};
 
     virtual void dump(const Buckets &buckets,
@@ -72,9 +77,68 @@ protected:
     void dump(const PolicyCollection::value_type &policy);
 
 private:
-    std::shared_ptr<std::ostream> m_outStream;
+    std::shared_ptr<StreamType> m_outStream;
 };
 
+template<typename StreamType>
+StorageSerializer<StreamType>::StorageSerializer(std::shared_ptr<StreamType> os) : m_outStream(os) {
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const Buckets &buckets, BucketStreamOpener streamOpener) {
+    for (const auto bucketIter : buckets) {
+        const auto &bucket = bucketIter.second;
+
+        dumpFields(bucket.id(), bucket.defaultPolicy().policyType(),
+                   bucket.defaultPolicy().metadata());
+    }
+
+    for (const auto bucketIter : buckets) {
+        const auto &bucketId = bucketIter.first;
+        const auto &bucket = bucketIter.second;
+        auto bucketSerializer = streamOpener(bucketId);
+
+        if (bucketSerializer != nullptr) {
+            bucketSerializer->dump(bucket);
+        } else {
+            throw BucketSerializationException(bucketId);
+        }
+    }
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyBucket &bucket) {
+    for (auto it = std::begin(bucket); it != std::end(bucket); ++it) {
+        const auto &policy = *it;
+        dump(policy);
+    }
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyKeyFeature &keyFeature) {
+    *m_outStream << keyFeature.toString();
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyType &policyType) {
+    auto oldFormat = m_outStream->flags();
+    *m_outStream << "0x" << std::uppercase <<  std::hex << policyType;
+    m_outStream->flags(oldFormat);
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyResult::PolicyMetadata &metadata) {
+    *m_outStream << metadata;
+}
+
+template<typename StreamType>
+void StorageSerializer<StreamType>::dump(const PolicyCollection::value_type &policy) {
+    const auto &key = policy->key();
+    const auto &result = policy->result();
+
+    dumpFields(key.client(), key.user(), key.privilege(), result.policyType(), result.metadata());
+}
+
 } /* namespace Cynara */
 
 #endif /* SRC_STORAGE_STORAGESERIALIZER_H_ */
index 5050634..9cf89d7 100644 (file)
@@ -68,7 +68,6 @@ SET(CYNARA_SOURCES_FOR_TESTS
     ${CYNARA_SRC}/storage/Integrity.cpp
     ${CYNARA_SRC}/storage/Storage.cpp
     ${CYNARA_SRC}/storage/StorageDeserializer.cpp
-    ${CYNARA_SRC}/storage/StorageSerializer.cpp
 )
 
 SET(CYNARA_TESTS_SOURCES
index 2eafcf7..1daaf32 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ TEST(serializer_dump, dump_empty_bucket) {
     auto oss = std::make_shared<std::ostringstream>();
     PolicyBucket bucket("empty");
 
-    StorageSerializer serializer(oss);
+    StorageSerializer<std::ostringstream> serializer(oss);
     serializer.dump(bucket);
 
     ASSERT_EQ("", oss->str());
@@ -69,7 +69,7 @@ TEST(serializer_dump, dump_bucket) {
                                                           Policy::simpleWithKey(pk2, DENY) }));
 
     auto outStream = std::make_shared<std::stringstream>();
-    StorageSerializer serializer(outStream);
+    StorageSerializer<std::stringstream> serializer(outStream);
     serializer.dump(bucket);
 
     // Split stream into records
@@ -99,7 +99,7 @@ TEST(serializer_dump, dump_bucket_bucket) {
                                                    Policy::bucketWithKey(pk3, bucketId) }};
 
     auto outStream = std::make_shared<std::stringstream>();
-    StorageSerializer serializer(outStream);
+    StorageSerializer<std::stringstream> serializer(outStream);
     serializer.dump(bucket);
 
     // Split stream into records
index f3eff9b..cd4ce5c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ TEST(dump_load, bucket) {
 
     auto ioStream = std::make_shared<std::stringstream>();
 
-    StorageSerializer serializer(ioStream);
+    StorageSerializer<std::stringstream> serializer(ioStream);
     serializer.dump(bucket);
 
     BucketDeserializer deserializer(ioStream);
index 938283d..1681a5b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
 
 class FakeStreamForBucketId {
 public:
+    typedef std::shared_ptr<Cynara::StorageSerializer<std::stringstream> >
+            StringstreamStorageSerializerPtr;
+
     MOCK_METHOD1(streamForBucketId,
-                 std::shared_ptr<Cynara::StorageSerializer>(const Cynara::PolicyBucketId &));
+                 StringstreamStorageSerializerPtr(const Cynara::PolicyBucketId &));
 
-    Cynara::StorageSerializer::BucketStreamOpener streamOpener() {
+    Cynara::StorageSerializer<std::stringstream>::BucketStreamOpener streamOpener() {
         return std::bind(&FakeStreamForBucketId::streamForBucketId, this, std::placeholders::_1);
     }
 };
 
 // Fake StorageSerializer for Cynara::PolicyBucket
-class FakeStorageSerializer : public Cynara::StorageSerializer {
+class FakeStorageSerializer : public Cynara::StorageSerializer<std::stringstream> {
 public:
-    FakeStorageSerializer(std::shared_ptr<std::ostringstream> o) : Cynara::StorageSerializer(o),
-                              outStream(o) {}
+    FakeStorageSerializer(std::shared_ptr<std::stringstream> o)
+    : Cynara::StorageSerializer<std::stringstream>(o), outStream(o) {}
     MOCK_METHOD1(dump, void(const Cynara::PolicyBucket &bucket));
-    std::shared_ptr<std::ostringstream> outStream;
+    std::shared_ptr<std::stringstream> outStream;
 };
 
 class StorageSerializerFixture : public ::testing::Test {
@@ -67,8 +70,8 @@ using namespace Cynara;
 // Be sure no calls to streamForBucketId() are made
 // and output stream is not touched
 TEST_F(StorageSerializerFixture, dump_buckets_empty) {
-    auto outStream = std::make_shared<std::ostringstream>();
-    StorageSerializer serializer(outStream);
+    auto outStream = std::make_shared<std::stringstream>();
+    StorageSerializer<std::stringstream> serializer(outStream);
     serializer.dump(Buckets(), fakeStreamOpener.streamOpener());
 
     // Stream should be empty
@@ -83,7 +86,7 @@ TEST_F(StorageSerializerFixture, dump_buckets) {
 
     // Will be returned as serializer for buckets
     auto fakeBucketSerializer = std::make_shared<FakeStorageSerializer>(
-            std::make_shared<std::ostringstream>());
+            std::make_shared<std::stringstream>());
 
     buckets = {
         { "bucket1", PolicyBucket("bucket1", PredefinedPolicyType::DENY) },
@@ -93,7 +96,7 @@ TEST_F(StorageSerializerFixture, dump_buckets) {
     };
 
     auto outStream = std::make_shared<std::stringstream>();
-    StorageSerializer dbSerializer(outStream);
+    StorageSerializer<std::stringstream> dbSerializer(outStream);
 
     // Make sure stream was opened for each bucket
     EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))
@@ -128,7 +131,7 @@ TEST_F(StorageSerializerFixture, dump_buckets_io_error) {
     };
 
     auto outStream = std::make_shared<std::stringstream>();
-    StorageSerializer dbSerializer(outStream);
+    StorageSerializer<std::stringstream> dbSerializer(outStream);
 
     // Make sure stream was opened for each bucket
     EXPECT_CALL(fakeStreamOpener, streamForBucketId(_))