MVault 1.0.0
Simple c++ database
Loading...
Searching...
No Matches
VaultRecordRef.hpp
1#pragma once
2
3#include "VaultRecordRef.h"
4#include "VaultParamInput.hpp"
5
6// This file contains an implementation of the VaultRecordRef template methods
7namespace mvlt
8{
9 template <class T>
10 VaultOperationResult VaultRecordRef::SetData(const std::string& key, const T& data) noexcept
11 {
12 DBG_LOG_ENTER();
13
14 static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
15 If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
16 SetData<std::string>(\"Key\", \"Value\") or SetData(\"Key\", std::string(\"Value\"))");
17
19 res.Key = key;
20 res.RequestedType = typeid(T);
21
22 if (VaultRecordPtr == nullptr)
23 {
24 res.IsOperationSuccess = false;
26 return res;
27 }
28
29 WriteLock<RecursiveReadWriteMutex> Lock(Vlt->RecursiveReadWriteMtx);
30
31 // Check if Vault still accessable
32 if (!VaultRecordPtr->GetIsValid())
33 {
34 res.IsOperationSuccess = false;
36 return res;
37 }
38
39 res = Vlt->SetDataToRecord(VaultRecordPtr, key, data);
40
41 return res;
42 }
43
44 template <class T>
45 VaultOperationResult VaultRecordRef::GetData(const std::string& key, T& data) const noexcept
46 {
47 DBG_LOG_ENTER();
48
49 static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
50 If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
51 GetData<std::string>(\"Key\", \"Value\") or GetData(\"Key\", std::string(\"Value\"))");
52
53 // Fill res info known at start
55 res.Key = key;
56 res.RequestedType = typeid(T);
57
58
59 if (VaultRecordPtr == nullptr)
60 {
61 res.IsOperationSuccess = false;
63
64 return res;
65 }
66
67 ReadLock<RecursiveReadWriteMutex> readLock(Vlt->RecursiveReadWriteMtx);
68
69 // Check if Vault still accessable
70 if (!VaultRecordPtr->GetIsValid())
71 {
72 res.IsOperationSuccess = false;
74 return res;
75 }
76
77 // If key not exist
78 if(!Vlt->GetKeyType(key, res.SavedType))
79 {
80 res.IsOperationSuccess = false;
82 return res;
83 }
84
85 // Check types
86 if (res.SavedType != res.RequestedType)
87 {
88 res.IsOperationSuccess = false;
90 return res;
91 }
92
93 VaultRecordPtr->GetData(key, data);
94 res.IsOperationSuccess = true;
96 return res;
97 }
98}
Iterator class for all library maps.
Definition Map.h:18
VaultOperationResult SetData(const std::string &key, const T &data) noexcept
Method for updating data inside Vault.
Definition VaultRecordRef.hpp:10
VaultOperationResult GetData(const std::string &key, T &data) const noexcept
A method for getting data using a key.
Definition VaultRecordRef.hpp:45
@ Success
This value is returned when the request was completely successful.
@ DataRecordNotValid
This code is returned when data is requested from the VaultRecordRef and the record it refers to is n...
@ WrongKey
This code is returned when the requested key has not been found.
@ WrongType
This code is returned when the requested type does not match the saved type.
Structure for storing the results of MVault operations.
Definition VaultOperationResult.h:44