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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
#ifndef RFL_OBJECT_HPP_
#define RFL_OBJECT_HPP_
#include <algorithm>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "Result.hpp"
namespace rfl {
/// Used to embed additional fields for which the names cannot be known in
/// advance and can therefore not be encoded in the struct.
template <class T>
class Object {
public:
using DataType = std::vector<std::pair<std::string, T>>;
using Type = T;
/// We want this to behave as similarly to C++ standard containers as
/// possible.
using key_type = std::string;
using mapped_type = T;
using value_type = std::pair<std::string, T>;
using size_type = typename DataType::size_type;
using difference_type = typename DataType::size_type;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = typename DataType::pointer;
using const_pointer = typename DataType::const_pointer;
using iterator = typename DataType::iterator;
using const_iterator = typename DataType::const_iterator;
using reverse_iterator = typename DataType::reverse_iterator;
using const_reverse_iterator = typename DataType::const_reverse_iterator;
Object() : data_(), i_(0) {}
Object(const Object<T>& _f) = default;
Object(Object<T>&& _f) noexcept = default;
~Object() = default;
/// Iterator to the beginning.
auto begin() { return data_.begin(); }
/// Iterator to the beginning.
auto begin() const { return data_.begin(); }
/// Const iterator to the beginning.
auto cbegin() const { return data_.cbegin(); }
/// Iterator to the end.
auto end() { return data_.end(); }
/// Iterator to the end.
auto end() const { return data_.end(); }
/// Const iterator to the end.
auto cend() const { return data_.cend(); }
/// Reverse iterator.
auto rbegin() { return data_.rbegin(); }
/// Reverse iterator.
auto rbegin() const { return data_.rbegin(); }
/// Const reverse iterator.
auto crbegin() const { return data_.crbegin(); }
/// Reverse iterator.
auto rend() { return data_.rend(); }
/// Reverse iterator.
auto rend() const { return data_.rend(); }
/// Const reverse iterator.
auto crend() const { return data_.crend(); }
Object<T>& operator=(const Object<T>& _f) = default;
Object<T>& operator=(Object<T>&& _f) = default;
/// Whether the object is empty.
auto empty() const { return data_.size() == 0; }
/// The number of elements currently inside the object.
auto size() const { return data_.size(); }
/// The maximum possible size.
auto max_size() const { return data_.max_size(); }
/// Inserts a new element at the end.
void insert(const value_type& _value) {
data_.push_back(_value);
i_ = 0;
}
/// Inserts a new element at the end.
void insert(value_type&& _value) {
data_.emplace_back(std::move(_value));
i_ = 0;
}
/// Inserts several new elements at the end.
template <class InputIt>
void insert(InputIt _first, InputIt _last) {
for (auto it = _first; it != _last; ++it) {
insert(*it);
}
}
/// Inserts a new element at the end.
void insert(const std::string& _k, const T& _v) {
insert(std::make_pair(_k, _v));
}
/// Inserts a new element at the end.
void insert(const std::string& _k, T&& _v) {
insert(std::make_pair(_k, std::move(_v)));
}
/// Inserts a new element at the end.
void insert(std::string&& _k, T&& _v) {
insert(std::make_pair(std::move(_k), std::move(_v)));
}
/// Inserts a new element at the end.
void insert(const std::string_view& _k, const T& _v) {
insert(std::make_pair(std::string(_k), _v));
}
/// Inserts a new element at the end.
void insert(const std::string_view& _k, T&& _v) {
insert(std::make_pair(std::string(_k), std::move(_v)));
}
/// Alias for insert that primarily exists for compatability with standard
/// containers.
template <class... Args>
void emplace(Args&&... _args) {
insert(std::forward<Args>(_args)...);
}
/// Alias for insert that primarily exists for compatability with standard
/// containers.
template <class... Args>
void emplace(const Args&... _args) {
insert(_args...);
}
/// Returns the element signified by the key or creates a new one.
T& operator[](const std::string& _key) {
const auto i = find(_key);
if (i != size()) {
return data_[i].second;
}
data_.emplace_back(std::make_pair(_key, T()));
i_ = 0;
return data_.back().second;
}
/// Returns the element signified by the key or creates a new one.
T& operator[](std::string&& _key) {
const auto i = find(_key);
if (i != size()) {
return data_[i].second;
}
data_.emplace_back(std::make_pair(std::move(_key), T()));
i_ = 0;
return data_.back().second;
}
/// Deletes all elements.
void clear() {
data_.clear();
i_ = 0;
}
/// Returns the element signified by the key or throws an exception.
T& at(const std::string& _key) {
const auto i = find(_key);
if (i == size()) {
throw std::runtime_error("Key named '" + _key + "' not found.");
}
return data_[i].second;
}
/// Returns the element signified by the key or throws an exception.
const T& at(const std::string& _key) const {
const auto i = find(_key);
if (i == size()) {
throw std::runtime_error("Key named '" + _key + "' not found.");
}
return data_[i].second;
}
/// Returns a result wrapping the element signified by the key.
Result<T> get(const std::string& _key) const noexcept {
const auto i = find(_key);
if (i == size()) {
return error("Key named '" + _key + "' not found.");
}
return data_[i].second;
}
private:
size_t find(const std::string& _key) const {
for (size_t i = i_; i < size(); ++i) {
if (data_[i].first == _key) {
i_ = i + 1;
return i;
}
}
for (size_t i = 0; i < i_; ++i) {
if (data_[i].first == _key) {
i_ = i + 1;
return i;
}
}
return size();
}
private:
DataType data_;
/// Allows faster access
mutable size_t i_;
};
} // namespace rfl
#endif
|