Skip to content

Commit

Permalink
Add move constructor to vault
Browse files Browse the repository at this point in the history
  • Loading branch information
mrognor committed Dec 13, 2024
1 parent c8c9f72 commit f3d5756
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 16 deletions.
56 changes: 55 additions & 1 deletion Source/Vault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ namespace mvlt

Vault::Vault(const Vault& other) noexcept
{
ReadLock<RecursiveReadWriteMutex> readLock(other.RecursiveReadWriteMtx);

VaultDerivedClass = VaultDerivedClasses::VaultBase;

// Copy keys
Expand Down Expand Up @@ -215,6 +217,9 @@ namespace mvlt
{
DropVault();

ReadLock<RecursiveReadWriteMutex> readLock(RecursiveReadWriteMtx);
ReadLock<RecursiveReadWriteMutex> otherReadLock(other.RecursiveReadWriteMtx);

VaultDerivedClass = VaultDerivedClasses::VaultBase;

// Copy keys
Expand Down Expand Up @@ -244,6 +249,55 @@ namespace mvlt
return *this;
}

Vault::Vault(Vault&& other) noexcept
{
ReadLock<RecursiveReadWriteMutex> otherReadLock(other.RecursiveReadWriteMtx);

VaultDerivedClass = VaultDerivedClasses::VaultBase;

RecordTemplate = std::move(other.RecordTemplate);
VaultHashMapStructure = std::move(other.VaultHashMapStructure);
VaultMapStructure = std::move(other.VaultMapStructure);
KeysTypes = std::move(other.KeysTypes);
VaultRecordAdders = std::move(other.VaultRecordAdders);
VaultRecordClearers = std::move(other.VaultRecordClearers);
VaultRecordErasers = std::move(other.VaultRecordErasers);
VaultRecordSorters = std::move(other.VaultRecordSorters);
VaultKeyCopiers = std::move(other.VaultKeyCopiers);
KeysOrder = std::move(other.KeysOrder);
UniqueKeys = std::move(other.UniqueKeys);
InvalidFileRecords = std::move(other.InvalidFileRecords);
RecordsSet = std::move(other.RecordsSet);
RecordSetsSet = std::move(other.RecordSetsSet);
}

Vault& Vault::operator= (Vault&& other) noexcept
{
if (this != &other)
{
ReadLock<RecursiveReadWriteMutex> otherReadLock(other.RecursiveReadWriteMtx);

VaultDerivedClass = VaultDerivedClasses::VaultBase;

RecordTemplate = std::move(other.RecordTemplate);
VaultHashMapStructure = std::move(other.VaultHashMapStructure);
VaultMapStructure = std::move(other.VaultMapStructure);
KeysTypes = std::move(other.KeysTypes);
VaultRecordAdders = std::move(other.VaultRecordAdders);
VaultRecordClearers = std::move(other.VaultRecordClearers);
VaultRecordErasers = std::move(other.VaultRecordErasers);
VaultRecordSorters = std::move(other.VaultRecordSorters);
VaultKeyCopiers = std::move(other.VaultKeyCopiers);
KeysOrder = std::move(other.KeysOrder);
UniqueKeys = std::move(other.UniqueKeys);
InvalidFileRecords = std::move(other.InvalidFileRecords);
RecordsSet = std::move(other.RecordsSet);
RecordSetsSet = std::move(other.RecordSetsSet);
}

return *this;
}

bool Vault::IsKeyExist(const std::string& key) const noexcept
{
bool res;
Expand Down Expand Up @@ -562,7 +616,7 @@ namespace mvlt
res.emplace_back(vaultRecordRef);
++counter;
return true;
}, isReverse);
}, const_cast<Vault*>(this), isReverse);
}

return res;
Expand Down
20 changes: 18 additions & 2 deletions Source/Vault.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ namespace mvlt
// Unordered_map of functions for getting sorted data.
// The key is a string with the name of the key from the Vault.
// The std::function is used as the value, in which the lambda function is written at the time of adding the key.
// Lambda accepts a function that is called for each record inside. VaultRecordRef is passed to her.
// Lambda accepts a function that is called for each record inside. VaultRecordRef and Vault pointer is passed to it.
// By default, iteration by records occurs in ascending order.
// isReverse parameter is used for the reverse order.
std::unordered_map<std::string, std::function<void( std::function<bool(const VaultRecordRef&)> functionToSortedData, bool isReverse )>> VaultRecordSorters;
std::unordered_map<std::string, std::function<void( std::function<bool(const VaultRecordRef&)> functionToSortedData, Vault* vltPtr, bool isReverse )>> VaultRecordSorters;

// Unordered_map of functions that copy keys from this to VaultRecordSet
std::unordered_map<std::string, std::function<void(Vault* vaultRecordSet)>> VaultKeyCopiers;
Expand Down Expand Up @@ -251,6 +251,22 @@ namespace mvlt
*/
Vault& operator= (const Vault& other) noexcept;

/**
\brief Move constructor
\param [in] other object to move data from
*/
Vault(Vault&& other) noexcept;

/**
\brief Move assignment operator
\param [in] other object to move data from
\return reference to moved object
*/
Vault& operator= (Vault&& other) noexcept;

/**
\brief Template method to add new key with default value to Vault
Expand Down
16 changes: 8 additions & 8 deletions Source/Vault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,6 @@ namespace mvlt
VaultOperationResult Vault::AddKey(const std::string& key, const T& defaultKeyValue, const bool& isUniqueKey, const bool& isUniqueKeyWithoutLambda,
std::function<T(std::size_t, const VaultRecordRef&)> uniqueKeyFunction) noexcept
{
static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
AddKey<std::string>(\"Key\", \"Value\") or AddKey(\"Key\", std::string(\"Value\"))");

VaultOperationResult res;
res.Key = key;
res.RequestedType = typeid(T);
Expand Down Expand Up @@ -524,18 +520,18 @@ namespace mvlt
}
);

VaultRecordSorters.emplace(key, [=](std::function<bool(const VaultRecordRef&)> functionToSortedData, bool isReverse)
VaultRecordSorters.emplace(key, [=](std::function<bool(const VaultRecordRef&)> functionToSortedData, Vault* vltPtr, bool isReverse)
{
if (!isReverse)
{
for (const auto& TtoVaultRecordMapIt : *TtoVaultRecordMap)
if(!functionToSortedData(VaultRecordRef(TtoVaultRecordMapIt.second, this)))
if(!functionToSortedData(VaultRecordRef(TtoVaultRecordMapIt.second, vltPtr)))
break;
}
else
{
for (auto TtoVaultRecordMapIt = TtoVaultRecordMap->Rbegin(); TtoVaultRecordMapIt != TtoVaultRecordMap->Rend(); ++TtoVaultRecordMapIt)
if(!functionToSortedData(VaultRecordRef((*TtoVaultRecordMapIt).second, this)))
if(!functionToSortedData(VaultRecordRef((*TtoVaultRecordMapIt).second, vltPtr)))
break;
}
});
Expand Down Expand Up @@ -565,6 +561,10 @@ namespace mvlt
template <class T>
bool Vault::AddKey(const std::string& key, const T& defaultKeyValue) noexcept
{
static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
AddKey<std::string>(\"Key\", \"Value\") or AddKey(\"Key\", std::string(\"Value\"))");

return AddKey(key, defaultKeyValue, false, false, {[&](std::size_t counter, const VaultRecordRef&) -> T{ return defaultKeyValue; }}).IsOperationSuccess;
}

Expand Down Expand Up @@ -1046,7 +1046,7 @@ namespace mvlt

++counter;
return true;
}, isReverse);
}, const_cast<Vault*>(this), isReverse);
}
}
}
27 changes: 23 additions & 4 deletions Source/VaultRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ namespace mvlt

VaultRecord::VaultRecord(const VaultRecord& other) noexcept
{
IsValid = true;
RefCounter = 0;

other.VaultRecordMutex.lock();
for (const auto& containerIt : other.Container)
Container.emplace(containerIt);
other.VaultRecordMutex.unlock();
}

VaultRecord& VaultRecord::operator= (const VaultRecord& other) noexcept
VaultRecord& VaultRecord::operator=(const VaultRecord& other) noexcept
{
if (&other != this)
{
Expand All @@ -33,6 +30,28 @@ namespace mvlt
return *this;
}

VaultRecord::VaultRecord(VaultRecord&& other) noexcept
{
other.VaultRecordMutex.lock();
Container = std::move(other.Container);
other.VaultRecordMutex.unlock();
}

VaultRecord& VaultRecord::operator=(VaultRecord&& other) noexcept
{
if (&other != this)
{
IsValid = true;
RefCounter = 0;

other.VaultRecordMutex.lock();
Container = std::move(other.Container);
other.VaultRecordMutex.unlock();
}

return *this;
}

void VaultRecord::AddRef() noexcept
{
VaultRecordMutex.lock();
Expand Down
18 changes: 17 additions & 1 deletion Source/VaultRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,23 @@ namespace mvlt
\return reference to copied object
*/
VaultRecord& operator= (const VaultRecord& other) noexcept;
VaultRecord& operator=(const VaultRecord& other) noexcept;

/**
\brief Move constructor
\param [in] other object to move data from
*/
VaultRecord(VaultRecord&& other) noexcept;

/**
\brief Move assignment operator
\param [in] other object to move data from
\return reference to moved object
*/
VaultRecord& operator=(VaultRecord&& other) noexcept;

/// \brief A method for increasing the number of references to an object
void AddRef() noexcept;
Expand Down
Loading

0 comments on commit f3d5756

Please sign in to comment.