MVault 0.0.1
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 static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
13 If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
14 SetData<std::string>(\"Key\", \"Value\") or SetData(\"Key\", std::string(\"Value\"))");
15
17 res.Key = key;
18 res.RequestedType = typeid(T);
19
20 if (VaultRecordPtr == nullptr)
21 {
22 res.IsOperationSuccess = false;
24 return res;
25 }
26
27 WriteLock<RecursiveReadWriteMutex> Lock(Vlt->RecursiveReadWriteMtx);
28
29 // Check if Vault still accessable
30 if (!VaultRecordPtr->GetIsValid())
31 {
32 res.IsOperationSuccess = false;
34 return res;
35 }
36
37 res = Vlt->SetDataToRecord(VaultRecordPtr, key, data);
38
39 return res;
40 }
41
42 template <class T>
43 VaultOperationResult VaultRecordRef::GetData(const std::string& key, T& data) const noexcept
44 {
45 static_assert(!std::is_array<T>::value, "It is not possible to use a c array as a key value. \n\
46 If you want to use a string as a key, you must specialize the function with a string. Like this: \n\
47 GetData<std::string>(\"Key\", \"Value\") or GetData(\"Key\", std::string(\"Value\"))");
48
49 // Fill res info known at start
51 res.Key = key;
52 res.RequestedType = typeid(T);
53
54
55 if (VaultRecordPtr == nullptr)
56 {
57 res.IsOperationSuccess = false;
59
60 return res;
61 }
62
63 ReadLock<RecursiveReadWriteMutex> readLock(Vlt->RecursiveReadWriteMtx);
64
65 // Check if Vault still accessable
66 if (!VaultRecordPtr->GetIsValid())
67 {
68 res.IsOperationSuccess = false;
70 return res;
71 }
72
73 // If key not exist
74 if(!Vlt->GetKeyType(key, res.SavedType))
75 {
76 res.IsOperationSuccess = false;
78 return res;
79 }
80
81 // Check types
82 if (res.SavedType != res.RequestedType)
83 {
84 res.IsOperationSuccess = false;
86 return res;
87 }
88
89 VaultRecordPtr->GetData(key, data);
90 res.IsOperationSuccess = true;
92 return res;
93 }
94}
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:43
@ 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:40