forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_wrapper.cpp
66 lines (51 loc) · 2.27 KB
/
value_wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <opentibiabr@outlook.com>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/
#include "pch.hpp"
#include "kv/value_wrapper.hpp"
#include "utils/tools.hpp"
ValueWrapper::ValueWrapper(uint64_t timestamp) :
timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }
ValueWrapper::ValueWrapper(const ValueVariant &value, uint64_t timestamp) :
data_(value), timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }
ValueWrapper::ValueWrapper(const std::string &value, uint64_t timestamp) :
data_(value), timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }
ValueWrapper::ValueWrapper(bool value, uint64_t timestamp) :
data_(value), timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }
ValueWrapper::ValueWrapper(int value, uint64_t timestamp) :
data_(value), timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }
ValueWrapper::ValueWrapper(double value, uint64_t timestamp) :
data_(value), timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }
ValueWrapper::ValueWrapper(const phmap::flat_hash_map<std::string, ValueWrapper> &value, uint64_t timestamp) :
data_(createMapFromRange(value.begin(), value.end(), timestamp)),
timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }
ValueWrapper::ValueWrapper(const std::initializer_list<std::pair<const std::string, ValueWrapper>> &init_list, uint64_t timestamp) :
data_(createMapFromRange(init_list.begin(), init_list.end(), timestamp)),
timestamp_(timestamp == 0 ? getTimeMsNow() : timestamp) { }
std::optional<ValueWrapper> ValueWrapper::get(const std::string &key) const {
auto pval = std::get_if<MapType>(&data_);
if (!pval) {
return std::nullopt;
}
if (!pval->contains(key)) {
return std::nullopt;
}
const auto &[_, valuePtr] = *pval->find(key);
if (!valuePtr) {
return std::nullopt;
}
return *valuePtr;
}
std::optional<ValueWrapper> ValueWrapper::get(size_t index) const {
if (auto pval = std::get_if<ArrayType>(&data_)) {
if (index < pval->size()) {
return (*pval)[index];
}
}
return std::nullopt;
}