Finished migrating to unique_ref instead of unique_ptr

This commit is contained in:
Sebastian Messmer 2015-07-21 18:19:34 +02:00
parent f4d9d271ea
commit 37bdbd907a
32 changed files with 69 additions and 104 deletions

View File

@ -1,7 +1,6 @@
#include "CachedBlock.h"
#include "CachingBlockStore.h"
using std::unique_ptr;
using cpputils::unique_ref;
namespace blockstore {

View File

@ -6,8 +6,6 @@
#include <algorithm>
#include <messmer/cpp-utils/pointer/cast.h>
using std::unique_ptr;
using std::make_unique;
using cpputils::dynamic_pointer_move;
using cpputils::Data;
using boost::optional;
@ -18,7 +16,7 @@ using boost::none;
namespace blockstore {
namespace caching {
CachingBlockStore::CachingBlockStore(std::unique_ptr<BlockStore> baseBlockStore)
CachingBlockStore::CachingBlockStore(cpputils::unique_ref<BlockStore> baseBlockStore)
:_baseBlockStore(std::move(baseBlockStore)), _cache(), _numNewBlocks(0) {
}

View File

@ -11,7 +11,7 @@ namespace caching {
//TODO Check that this blockstore allows parallel destructing of blocks (otherwise we won't encrypt blocks in parallel)
class CachingBlockStore: public BlockStore {
public:
explicit CachingBlockStore(std::unique_ptr<BlockStore> baseBlockStore);
explicit CachingBlockStore(cpputils::unique_ref<BlockStore> baseBlockStore);
Key createKey() override;
boost::optional<cpputils::unique_ref<Block>> tryCreate(const Key &key, cpputils::Data data) override;
@ -25,7 +25,7 @@ public:
void removeFromBaseStore(cpputils::unique_ref<Block> block);
private:
std::unique_ptr<BlockStore> _baseBlockStore;
cpputils::unique_ref<BlockStore> _baseBlockStore;
Cache<Key, cpputils::unique_ref<Block>> _cache;
uint32_t _numNewBlocks;

View File

@ -1,7 +1,6 @@
#include "NewBlock.h"
#include "CachingBlockStore.h"
using std::unique_ptr;
using cpputils::Data;
using boost::none;

View File

@ -14,7 +14,7 @@ namespace encrypted {
template<class Cipher>
class EncryptedBlockStore: public BlockStore {
public:
EncryptedBlockStore(std::unique_ptr<BlockStore> baseBlockStore, const typename Cipher::EncryptionKey &encKey);
EncryptedBlockStore(cpputils::unique_ref<BlockStore> baseBlockStore, const typename Cipher::EncryptionKey &encKey);
//TODO Are createKey() tests included in generic BlockStoreTest? If not, add it!
Key createKey() override;
@ -27,7 +27,7 @@ public:
void __setKey(const typename Cipher::EncryptionKey &encKey);
private:
std::unique_ptr<BlockStore> _baseBlockStore;
cpputils::unique_ref<BlockStore> _baseBlockStore;
typename Cipher::EncryptionKey _encKey;
DISALLOW_COPY_AND_ASSIGN(EncryptedBlockStore);
@ -36,7 +36,7 @@ private:
template<class Cipher>
EncryptedBlockStore<Cipher>::EncryptedBlockStore(std::unique_ptr<BlockStore> baseBlockStore, const typename Cipher::EncryptionKey &encKey)
EncryptedBlockStore<Cipher>::EncryptedBlockStore(cpputils::unique_ref<BlockStore> baseBlockStore, const typename Cipher::EncryptionKey &encKey)
: _baseBlockStore(std::move(baseBlockStore)), _encKey(encKey) {
}

View File

@ -2,7 +2,6 @@
#include "InMemoryBlockStore.h"
#include <cstring>
using std::unique_ptr;
using std::make_shared;
using std::istream;
using std::ostream;

View File

@ -2,7 +2,6 @@
#include "InMemoryBlockStore.h"
#include <memory>
using std::unique_ptr;
using std::make_unique;
using std::string;
using std::mutex;
@ -43,7 +42,7 @@ optional<unique_ref<Block>> InMemoryBlockStore::load(const Key &key) {
void InMemoryBlockStore::remove(unique_ref<Block> block) {
Key key = block->key();
cpputils::to_unique_ptr(std::move(block)).reset(); // Call destructor
cpputils::destruct(std::move(block));
int numRemoved = _blocks.erase(key.ToString());
assert(1==numRemoved);
}

View File

@ -5,8 +5,6 @@
#include "OnDiskBlockStore.h"
#include "../../utils/FileDoesntExistException.h"
using std::unique_ptr;
using std::make_unique;
using std::istream;
using std::ostream;
using std::ifstream;

View File

@ -1,8 +1,6 @@
#include "OnDiskBlock.h"
#include "OnDiskBlockStore.h"
using std::unique_ptr;
using std::make_unique;
using std::string;
using cpputils::Data;
using cpputils::unique_ref;
@ -32,7 +30,7 @@ optional<unique_ref<Block>> OnDiskBlockStore::load(const Key &key) {
void OnDiskBlockStore::remove(unique_ref<Block> block) {
Key key = block->key();
cpputils::to_unique_ptr(std::move(block)).reset(); // Call destructor
cpputils::destruct(std::move(block));
OnDiskBlock::RemoveFromDisk(_rootdir, key);
}

View File

@ -4,9 +4,6 @@
#include <cassert>
#include <messmer/cpp-utils/pointer/cast.h>
using std::unique_ptr;
using std::make_unique;
using std::string;
using std::mutex;
using std::lock_guard;

View File

@ -2,7 +2,6 @@
#include "FakeBlockStore.h"
#include <cstring>
using std::unique_ptr;
using std::shared_ptr;
using std::istream;
using std::ostream;

View File

@ -1,8 +1,6 @@
#include "FakeBlock.h"
#include "FakeBlockStore.h"
using std::unique_ptr;
using std::make_unique;
using std::make_shared;
using std::string;
using std::mutex;
@ -42,7 +40,7 @@ optional<unique_ref<Block>> FakeBlockStore::load(const Key &key) {
void FakeBlockStore::remove(unique_ref<Block> block) {
Key key = block->key();
cpputils::to_unique_ptr(std::move(block)).reset(); // Call destructor
cpputils::destruct(std::move(block));
int numRemoved = _blocks.erase(key.ToString());
assert(numRemoved == 1);
}

View File

@ -7,14 +7,13 @@
using blockstore::BlockStore;
using blockstore::caching::CachingBlockStore;
using blockstore::testfake::FakeBlockStore;
using std::unique_ptr;
using std::make_unique;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
class CachingBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<CachingBlockStore>(make_unique<FakeBlockStore>());
unique_ref<BlockStore> createBlockStore() override {
return make_unique_ref<CachingBlockStore>(make_unique_ref<FakeBlockStore>());
}
};

View File

@ -1,23 +1,22 @@
#include <google/gtest/gtest.h>
#include <memory>
#include <messmer/cpp-utils/pointer/unique_ref.h>
#include "../../../../implementations/caching/cache/Cache.h"
#include "testutils/MinimalKeyType.h"
#include "testutils/CopyableMovableValueType.h"
using namespace blockstore::caching;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using ::testing::Test;
using std::unique_ptr;
using std::make_unique;
//Test that Cache uses a move constructor for Value if possible
class CacheTest_MoveConstructor: public Test {
public:
CacheTest_MoveConstructor() {
CacheTest_MoveConstructor(): cache(make_unique_ref<Cache<MinimalKeyType, CopyableMovableValueType>>()) {
CopyableMovableValueType::numCopyConstructorCalled = 0;
cache = make_unique<Cache<MinimalKeyType, CopyableMovableValueType>>();
}
unique_ptr<Cache<MinimalKeyType, CopyableMovableValueType>> cache;
unique_ref<Cache<MinimalKeyType, CopyableMovableValueType>> cache;
};
TEST_F(CacheTest_MoveConstructor, MoveIntoCache) {

View File

@ -1,5 +1,5 @@
#include <google/gtest/gtest.h>
#include <memory>
#include <messmer/cpp-utils/pointer/unique_ref.h>
#include "../../../../implementations/caching/cache/QueueMap.h"
#include "testutils/MinimalKeyType.h"
#include "testutils/CopyableMovableValueType.h"
@ -7,17 +7,16 @@
using namespace blockstore::caching;
using ::testing::Test;
using std::unique_ptr;
using std::make_unique;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
//Test that QueueMap uses a move constructor for Value if possible
class QueueMapTest_MoveConstructor: public Test {
public:
QueueMapTest_MoveConstructor() {
QueueMapTest_MoveConstructor(): map(make_unique_ref<QueueMap<MinimalKeyType, CopyableMovableValueType>>()) {
CopyableMovableValueType::numCopyConstructorCalled = 0;
map = make_unique<QueueMap<MinimalKeyType, CopyableMovableValueType>>();
}
unique_ptr<QueueMap<MinimalKeyType, CopyableMovableValueType>> map;
unique_ref<QueueMap<MinimalKeyType, CopyableMovableValueType>> map;
};
TEST_F(QueueMapTest_MoveConstructor, PushingAndPopping_MoveIntoMap) {

View File

@ -1,13 +1,12 @@
#include "QueueMapTest.h"
QueueMapTest::QueueMapTest() {
QueueMapTest::QueueMapTest(): _map(cpputils::make_unique_ref<blockstore::caching::QueueMap<MinimalKeyType, MinimalValueType>>()) {
MinimalKeyType::instances = 0;
MinimalValueType::instances = 0;
_map = std::make_unique<blockstore::caching::QueueMap<MinimalKeyType, MinimalValueType>>();
}
QueueMapTest::~QueueMapTest() {
_map.reset();
cpputils::destruct(std::move(_map));
EXPECT_EQ(0, MinimalKeyType::instances);
EXPECT_EQ(0, MinimalValueType::instances);
}

View File

@ -3,7 +3,7 @@
#define BLOCKS_MESSMER_BLOCKSTORE_TEST_IMPLEMENTATIONS_CACHING_CACHE_TESTUTILS_QUEUEMAPTEST_H_
#include <google/gtest/gtest.h>
#include <memory>
#include <messmer/cpp-utils/pointer/unique_ref.h>
#include "../../../../../implementations/caching/cache/QueueMap.h"
#include "MinimalKeyType.h"
#include "MinimalValueType.h"
@ -24,9 +24,8 @@ public:
int size();
private:
std::unique_ptr<blockstore::caching::QueueMap<MinimalKeyType, MinimalValueType>> _map;
cpputils::unique_ref<blockstore::caching::QueueMap<MinimalKeyType, MinimalValueType>> _map;
};
#endif

View File

@ -15,17 +15,16 @@ using blockstore::testfake::FakeBlockStore;
using blockstore::encrypted::AES256_GCM;
using blockstore::encrypted::AES256_CFB;
using std::unique_ptr;
using std::make_unique;
using cpputils::Data;
using cpputils::DataFixture;
using cpputils::make_unique_ref;
using cpputils::unique_ref;
template<class Cipher>
class EncryptedBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<EncryptedBlockStore<Cipher>>(make_unique<FakeBlockStore>(), createKeyFixture());
unique_ref<BlockStore> createBlockStore() override {
return make_unique_ref<EncryptedBlockStore<Cipher>>(make_unique_ref<FakeBlockStore>(), createKeyFixture());
}
private:

View File

@ -7,11 +7,10 @@
using ::testing::Test;
using std::unique_ptr;
using std::make_unique;
using cpputils::DataFixture;
using cpputils::Data;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using blockstore::testfake::FakeBlockStore;
@ -21,14 +20,12 @@ class EncryptedBlockStoreTest: public Test {
public:
static constexpr unsigned int BLOCKSIZE = 1024;
EncryptedBlockStoreTest():
_baseBlockStore(make_unique<FakeBlockStore>()),
baseBlockStore(_baseBlockStore.get()),
blockStore(make_unique<EncryptedBlockStore<FakeAuthenticatedCipher>>(std::move(_baseBlockStore), FakeAuthenticatedCipher::Key1())),
baseBlockStore(new FakeBlockStore),
blockStore(make_unique_ref<EncryptedBlockStore<FakeAuthenticatedCipher>>(std::move(cpputils::nullcheck(std::unique_ptr<FakeBlockStore>(baseBlockStore)).value()), FakeAuthenticatedCipher::Key1())),
data(DataFixture::generate(BLOCKSIZE)) {
}
unique_ptr<FakeBlockStore> _baseBlockStore;
FakeBlockStore *baseBlockStore;
unique_ptr<EncryptedBlockStore<FakeAuthenticatedCipher>> blockStore;
unique_ref<EncryptedBlockStore<FakeAuthenticatedCipher>> blockStore;
Data data;
blockstore::Key CreateBlockDirectlyWithFixtureAndReturnKey() {

View File

@ -9,14 +9,13 @@
using blockstore::BlockStore;
using blockstore::BlockStoreWithRandomKeys;
using blockstore::inmemory::InMemoryBlockStore;
using std::unique_ptr;
using std::make_unique;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
class InMemoryBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<InMemoryBlockStore>();
unique_ref<BlockStore> createBlockStore() override {
return make_unique_ref<InMemoryBlockStore>();
}
};
@ -24,8 +23,8 @@ INSTANTIATE_TYPED_TEST_CASE_P(InMemory, BlockStoreTest, InMemoryBlockStoreTestFi
class InMemoryBlockStoreWithRandomKeysTestFixture: public BlockStoreWithRandomKeysTestFixture {
public:
unique_ptr<BlockStoreWithRandomKeys> createBlockStore() override {
return make_unique<InMemoryBlockStore>();
unique_ref<BlockStoreWithRandomKeys> createBlockStore() override {
return make_unique_ref<InMemoryBlockStore>();
}
};

View File

@ -11,15 +11,14 @@ using blockstore::BlockStore;
using blockstore::BlockStoreWithRandomKeys;
using blockstore::ondisk::OnDiskBlockStore;
using std::unique_ptr;
using std::make_unique;
using cpputils::TempDir;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
class OnDiskBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<OnDiskBlockStore>(tempdir.path());
unique_ref<BlockStore> createBlockStore() override {
return make_unique_ref<OnDiskBlockStore>(tempdir.path());
}
private:
TempDir tempdir;
@ -29,8 +28,8 @@ INSTANTIATE_TYPED_TEST_CASE_P(OnDisk, BlockStoreTest, OnDiskBlockStoreTestFixtur
class OnDiskBlockStoreWithRandomKeysTestFixture: public BlockStoreWithRandomKeysTestFixture {
public:
unique_ptr<BlockStoreWithRandomKeys> createBlockStore() override {
return make_unique<OnDiskBlockStore>(tempdir.path());
unique_ref<BlockStoreWithRandomKeys> createBlockStore() override {
return make_unique_ref<OnDiskBlockStore>(tempdir.path());
}
private:
TempDir tempdir;

View File

@ -7,8 +7,6 @@
using ::testing::Test;
using ::testing::WithParamInterface;
using ::testing::Values;
using std::unique_ptr;
using cpputils::Data;
using cpputils::TempFile;
using cpputils::TempDir;

View File

@ -9,7 +9,6 @@ using ::testing::Test;
using ::testing::WithParamInterface;
using ::testing::Values;
using std::unique_ptr;
using cpputils::Data;
using cpputils::DataFixture;
using cpputils::TempFile;

View File

@ -14,7 +14,6 @@ using ::testing::WithParamInterface;
using ::testing::Values;
using std::ofstream;
using std::unique_ptr;
using std::ios;
using cpputils::Data;
using cpputils::DataFixture;

View File

@ -8,14 +8,13 @@ using blockstore::BlockStore;
using blockstore::parallelaccess::ParallelAccessBlockStore;
using blockstore::testfake::FakeBlockStore;
using std::unique_ptr;
using std::make_unique;
using cpputils::make_unique_ref;
using cpputils::unique_ref;
class ParallelAccessBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<ParallelAccessBlockStore>(make_unique_ref<FakeBlockStore>());
unique_ref<BlockStore> createBlockStore() override {
return make_unique_ref<ParallelAccessBlockStore>(make_unique_ref<FakeBlockStore>());
}
};

View File

@ -8,14 +8,13 @@
using blockstore::BlockStore;
using blockstore::BlockStoreWithRandomKeys;
using blockstore::testfake::FakeBlockStore;
using std::unique_ptr;
using std::make_unique;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
class FakeBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<FakeBlockStore>();
unique_ref<BlockStore> createBlockStore() override {
return make_unique_ref<FakeBlockStore>();
}
};
@ -23,8 +22,8 @@ INSTANTIATE_TYPED_TEST_CASE_P(TestFake, BlockStoreTest, FakeBlockStoreTestFixtur
class FakeBlockStoreWithRandomKeysTestFixture: public BlockStoreWithRandomKeysTestFixture {
public:
unique_ptr<BlockStoreWithRandomKeys> createBlockStore() override {
return make_unique<FakeBlockStore>();
unique_ref<BlockStoreWithRandomKeys> createBlockStore() override {
return make_unique_ref<FakeBlockStore>();
}
};

View File

@ -11,8 +11,6 @@ using ::testing::Eq;
using ::testing::ByRef;
using std::string;
using std::unique_ptr;
using std::make_unique;
using cpputils::Data;
using cpputils::DataFixture;
using cpputils::unique_ref;
@ -23,11 +21,11 @@ using namespace blockstore;
class BlockStoreWithRandomKeysMock: public BlockStoreWithRandomKeys {
public:
optional<unique_ref<Block>> tryCreate(const Key &key, Data data) {
return cpputils::nullcheck(unique_ptr<Block>(do_create(key, data)));
return cpputils::nullcheck(std::unique_ptr<Block>(do_create(key, data)));
}
MOCK_METHOD2(do_create, Block*(const Key &, const Data &data));
optional<unique_ref<Block>> load(const Key &key) {
return cpputils::nullcheck(unique_ptr<Block>(do_load(key)));
return cpputils::nullcheck(std::unique_ptr<Block>(do_load(key)));
}
MOCK_METHOD1(do_load, Block*(const Key &));
void remove(unique_ref<Block> block) {UNUSED(block);}

View File

@ -8,7 +8,7 @@
class BlockStoreTestFixture {
public:
virtual std::unique_ptr<blockstore::BlockStore> createBlockStore() = 0;
virtual cpputils::unique_ref<blockstore::BlockStore> createBlockStore() = 0;
};
template<class ConcreteBlockStoreTestFixture>

View File

@ -8,7 +8,7 @@ struct DataRange {
class BlockStoreDataParametrizedTest {
public:
BlockStoreDataParametrizedTest(std::unique_ptr<blockstore::BlockStore> blockStore_, const DataRange &testData_)
BlockStoreDataParametrizedTest(cpputils::unique_ref<blockstore::BlockStore> blockStore_, const DataRange &testData_)
: blockStore(std::move(blockStore_)),
testData(testData_),
foregroundData(cpputils::DataFixture::generate(testData.count, 0)),
@ -40,7 +40,7 @@ public:
}
private:
std::unique_ptr<blockstore::BlockStore> blockStore;
cpputils::unique_ref<blockstore::BlockStore> blockStore;
DataRange testData;
cpputils::Data foregroundData;
cpputils::Data backgroundData;

View File

@ -5,7 +5,7 @@
class BlockStoreSizeParameterizedTest {
public:
BlockStoreSizeParameterizedTest(std::unique_ptr<blockstore::BlockStore> blockStore_, size_t size_): blockStore(std::move(blockStore_)), size(size_) {}
BlockStoreSizeParameterizedTest(cpputils::unique_ref<blockstore::BlockStore> blockStore_, size_t size_): blockStore(std::move(blockStore_)), size(size_) {}
void TestCreatedBlockHasCorrectSize() {
auto block = CreateBlock();
@ -93,7 +93,7 @@ public:
private:
const blockstore::Key key = blockstore::Key::FromString("1491BB4932A389EE14BC7090AC772972");
std::unique_ptr<blockstore::BlockStore> blockStore;
cpputils::unique_ref<blockstore::BlockStore> blockStore;
size_t size;
cpputils::Data ZEROES(size_t size) {

View File

@ -8,7 +8,7 @@
class BlockStoreWithRandomKeysTestFixture {
public:
virtual std::unique_ptr<blockstore::BlockStoreWithRandomKeys> createBlockStore() = 0;
virtual cpputils::unique_ref<blockstore::BlockStoreWithRandomKeys> createBlockStore() = 0;
};
template<class ConcreteBlockStoreWithRandomKeysTestFixture>

View File

@ -9,10 +9,10 @@ using ::testing::Test;
using ::testing::WithParamInterface;
using ::testing::Values;
using std::make_unique;
using std::unique_ptr;
using cpputils::Data;
using cpputils::DataFixture;
using cpputils::unique_ref;
using cpputils::make_unique_ref;
using namespace blockstore;
using namespace blockstore::utils;
@ -25,13 +25,13 @@ public:
BlockStoreUtilsTest():
ZEROES(SIZE),
dataFixture(DataFixture::generate(SIZE)),
blockStore(make_unique<FakeBlockStore>()) {
blockStore(make_unique_ref<FakeBlockStore>()) {
ZEROES.FillWithZeroes();
}
Data ZEROES;
Data dataFixture;
unique_ptr<BlockStore> blockStore;
unique_ref<BlockStore> blockStore;
};
TEST_F(BlockStoreUtilsTest, FillWithZeroes) {