|
| 1 | +/** |
| 2 | + @file |
| 3 | + @author Alexander Sherikov |
| 4 | +
|
| 5 | + @copyright 2019 Alexander Sherikov, Licensed under the Apache License, Version 2.0. |
| 6 | + (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0) |
| 7 | +
|
| 8 | + @brief |
| 9 | +*/ |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | +#include <utility> |
| 16 | +#include <memory> |
| 17 | +#include <boost/lexical_cast.hpp> |
| 18 | + |
| 19 | + |
| 20 | +namespace ariles2 |
| 21 | +{ |
| 22 | + namespace ns_namevalue2 |
| 23 | + { |
| 24 | + class NameValueVector |
| 25 | + { |
| 26 | + public: |
| 27 | + using SharedPtr = std::shared_ptr<NameValueVector>; |
| 28 | + static inline SharedPtr make_shared() |
| 29 | + { |
| 30 | + return (std::make_shared<NameValueVector>()); |
| 31 | + } |
| 32 | + |
| 33 | + public: |
| 34 | + std::vector<std::pair<std::string, double>> name_value_pairs_; |
| 35 | + |
| 36 | + public: |
| 37 | + std::string &name(const std::size_t index) |
| 38 | + { |
| 39 | + return (name_value_pairs_[index].first); |
| 40 | + } |
| 41 | + double &value(const std::size_t index) |
| 42 | + { |
| 43 | + return (name_value_pairs_[index].second); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + void expand() |
| 48 | + { |
| 49 | + name_value_pairs_.resize(name_value_pairs_.size() + 1); |
| 50 | + } |
| 51 | + void reserve(const std::size_t size) |
| 52 | + { |
| 53 | + name_value_pairs_.reserve(size); |
| 54 | + } |
| 55 | + void clear() |
| 56 | + { |
| 57 | + name_value_pairs_.clear(); |
| 58 | + } |
| 59 | + std::size_t size() const |
| 60 | + { |
| 61 | + return (name_value_pairs_.size()); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * @brief Configuration writer class |
| 68 | + */ |
| 69 | + template <class t_NameValueContainer> |
| 70 | + class ARILES2_VISIBILITY_ATTRIBUTE GenericWriter |
| 71 | + : public ariles2::write::Visitor, |
| 72 | + public serialization::NodeStackBase<serialization::Node<std::string>> |
| 73 | + { |
| 74 | + protected: |
| 75 | + std::size_t reserve_; |
| 76 | + |
| 77 | + bool initialize_structure_; |
| 78 | + |
| 79 | + const std::string separator_ = "."; |
| 80 | + const std::string bracket_left_ = "{"; |
| 81 | + const std::string bracket_right_ = "}"; |
| 82 | + |
| 83 | + |
| 84 | + public: |
| 85 | + std::shared_ptr<t_NameValueContainer> name_value_pairs_; |
| 86 | + std::size_t index_; |
| 87 | + |
| 88 | + |
| 89 | + protected: |
| 90 | + void expand() |
| 91 | + { |
| 92 | + if (index_ == name_value_pairs_->size()) |
| 93 | + { |
| 94 | + name_value_pairs_->expand(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + void expandReserve(const std::size_t size) |
| 99 | + { |
| 100 | + reserve_ += size; |
| 101 | + name_value_pairs_->reserve(reserve_); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + public: |
| 106 | + explicit GenericWriter(const std::size_t reserve = 0) |
| 107 | + { |
| 108 | + name_value_pairs_ = std::make_shared<t_NameValueContainer>(); |
| 109 | + |
| 110 | + if (reserve > 0) |
| 111 | + { |
| 112 | + expandReserve(reserve); |
| 113 | + } |
| 114 | + reset(); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + explicit GenericWriter( |
| 119 | + const std::shared_ptr<t_NameValueContainer> &name_value_pairs, |
| 120 | + const std::size_t reserve = 0) |
| 121 | + { |
| 122 | + name_value_pairs_ = name_value_pairs; |
| 123 | + |
| 124 | + if (reserve > 0) |
| 125 | + { |
| 126 | + expandReserve(reserve); |
| 127 | + } |
| 128 | + reset(); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + void flush() |
| 133 | + { |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + void reset(const bool initialize_structure = true) |
| 138 | + { |
| 139 | + if (initialize_structure) |
| 140 | + { |
| 141 | + name_value_pairs_->clear(); |
| 142 | + } |
| 143 | + initialize_structure_ = initialize_structure; |
| 144 | + index_ = 0; |
| 145 | + reserve_ = 0; |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + virtual void startMap(const Parameters &, const std::size_t num_entries) |
| 150 | + { |
| 151 | + if (initialize_structure_) |
| 152 | + { |
| 153 | + expandReserve(num_entries); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + virtual void startMapEntry(const std::string &map_name) |
| 158 | + { |
| 159 | + if (initialize_structure_) |
| 160 | + { |
| 161 | + if (empty()) |
| 162 | + { |
| 163 | + emplace(map_name); |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + if (back().isArray()) |
| 168 | + { |
| 169 | + concatWithNodeAndEmplace( |
| 170 | + bracket_left_, |
| 171 | + boost::lexical_cast<std::string>(back().index_), |
| 172 | + bracket_right_, |
| 173 | + separator_, |
| 174 | + map_name); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + concatWithNodeAndEmplace(separator_, map_name); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + virtual void endMapEntry() |
| 185 | + { |
| 186 | + if (initialize_structure_) |
| 187 | + { |
| 188 | + pop(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + virtual void endMap() |
| 193 | + { |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + virtual bool startIteratedMap(const std::size_t /*num_entries*/, const Parameters &) |
| 198 | + { |
| 199 | + return (false); |
| 200 | + } |
| 201 | + |
| 202 | + virtual void startArray(const std::size_t size, const bool /*compact*/ = false) |
| 203 | + { |
| 204 | + if (initialize_structure_) |
| 205 | + { |
| 206 | + expandReserve(size); |
| 207 | + if (back().isArray()) |
| 208 | + { |
| 209 | + emplace(concatWithNode(std::string("_"), boost::lexical_cast<std::string>(back().index_)), |
| 210 | + 0, |
| 211 | + size); |
| 212 | + } |
| 213 | + else |
| 214 | + { |
| 215 | + emplace(back().node_, 0, size); |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + virtual void endArrayElement() |
| 221 | + { |
| 222 | + if (initialize_structure_) |
| 223 | + { |
| 224 | + shiftArray(); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + virtual void endArray() |
| 229 | + { |
| 230 | + if (initialize_structure_) |
| 231 | + { |
| 232 | + pop(); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + |
| 237 | +#define ARILES2_BASIC_TYPE(type) \ |
| 238 | + void writeElement(const type &element, const Parameters &) \ |
| 239 | + { \ |
| 240 | + expand(); \ |
| 241 | + if (initialize_structure_) \ |
| 242 | + { \ |
| 243 | + name_value_pairs_->name(index_) = back().node_; \ |
| 244 | + if (back().isArray()) \ |
| 245 | + { \ |
| 246 | + name_value_pairs_->name(index_) += "_"; \ |
| 247 | + name_value_pairs_->name(index_) += boost::lexical_cast<std::string>(back().index_); \ |
| 248 | + } \ |
| 249 | + } \ |
| 250 | + name_value_pairs_->value(index_) = element; \ |
| 251 | + ++index_; \ |
| 252 | + } |
| 253 | + |
| 254 | + CPPUT_MACRO_SUBSTITUTE(ARILES2_BASIC_NUMERIC_TYPES_LIST) |
| 255 | + |
| 256 | +#undef ARILES2_BASIC_TYPE |
| 257 | + |
| 258 | + |
| 259 | + void writeElement(const std::string & /*element*/, const Parameters &) |
| 260 | + { |
| 261 | + } |
| 262 | + }; |
| 263 | + |
| 264 | + |
| 265 | + using Writer = GenericWriter<NameValueVector>; |
| 266 | + } // namespace ns_namevalue2 |
| 267 | +} // namespace ariles2 |
0 commit comments