-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
501 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Deprecated, use namevalue2. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
set (TGT_ARILES_VISITOR_LIB "${PROJECT_NAME}_visitor_${ARILES_VISITOR}") | ||
|
||
add_library(${TGT_ARILES_VISITOR_LIB} INTERFACE) | ||
|
||
include(ariles_install_component) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
@file | ||
@author Alexander Sherikov | ||
@copyright 2018-2024 Alexander Sherikov, Licensed under the Apache License, Version 2.0. | ||
(see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0) | ||
@brief | ||
*/ | ||
|
||
/** | ||
@defgroup namevalue2 NameValue2 | ||
@ingroup config | ||
@brief Generates a set of <std::string, double> pairs with flattened member names, | ||
e.g., <"ariles_class.class_member.real_member", 3.4>. | ||
*/ | ||
|
||
|
||
#pragma once | ||
|
||
#define ARILES2_VISITOR_INCLUDED_namevalue2 | ||
|
||
#include <ariles2/internal/helpers.h> | ||
#include <ariles2/visitors/config.h> | ||
|
||
#include "./namevalue2/writer.h" | ||
|
||
namespace ariles2 | ||
{ | ||
/** | ||
* @brief NameValue2 visitor. | ||
* @ingroup namevalue2 | ||
*/ | ||
struct ARILES2_VISIBILITY_ATTRIBUTE namevalue2 | ||
{ | ||
using Writer = ariles2::cfgwrite::Visitor<ns_namevalue2::Writer>; | ||
}; | ||
} // namespace ariles2 |
226 changes: 226 additions & 0 deletions
226
extra_visitors/namevalue2/ariles2/visitors/namevalue2/writer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
/** | ||
@file | ||
@author Alexander Sherikov | ||
@copyright 2019 Alexander Sherikov, Licensed under the Apache License, Version 2.0. | ||
(see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0) | ||
@brief | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <utility> | ||
#include <memory> | ||
#include <boost/lexical_cast.hpp> | ||
|
||
|
||
namespace ariles2 | ||
{ | ||
namespace ns_namevalue2 | ||
{ | ||
class NameValueVector | ||
{ | ||
public: | ||
std::vector<std::pair<std::string, double>> name_value_pairs_; | ||
|
||
public: | ||
std::string &name(const std::size_t index) | ||
{ | ||
return (name_value_pairs_[index].first); | ||
} | ||
double &value(const std::size_t index) | ||
{ | ||
return (name_value_pairs_[index].second); | ||
} | ||
|
||
void reserve(const std::size_t size) | ||
{ | ||
name_value_pairs_.reserve(size); | ||
} | ||
std::size_t size() const | ||
{ | ||
return (name_value_pairs_.size()); | ||
} | ||
void resize(const std::size_t size) | ||
{ | ||
name_value_pairs_.resize(size); | ||
} | ||
}; | ||
|
||
|
||
/** | ||
* @brief Configuration writer class | ||
*/ | ||
template <class t_NameValueContainer> | ||
class ARILES2_VISIBILITY_ATTRIBUTE GenericWriter | ||
: public ariles2::write::Visitor, | ||
public serialization::NodeStackBase<serialization::Node<std::string>> | ||
{ | ||
public: | ||
using NameValueContainer = t_NameValueContainer; | ||
|
||
|
||
protected: | ||
bool initialize_names_; | ||
|
||
const std::string separator_ = "."; | ||
const std::string bracket_left_ = "{"; | ||
const std::string bracket_right_ = "}"; | ||
|
||
|
||
public: | ||
std::size_t index_; | ||
t_NameValueContainer name_value_pairs_; | ||
|
||
|
||
public: | ||
explicit GenericWriter(const std::size_t reserve = 0) | ||
{ | ||
name_value_pairs_.reserve(reserve); | ||
reset(); | ||
} | ||
|
||
|
||
void flush() | ||
{ | ||
name_value_pairs_.resize(index_); | ||
} | ||
|
||
|
||
void reset(const bool initialize_names = true) | ||
{ | ||
if (initialize_names) | ||
{ | ||
name_value_pairs_.resize(0); | ||
} | ||
initialize_names_ = initialize_names; | ||
index_ = 0; | ||
} | ||
|
||
|
||
virtual void startMap(const Parameters &, const std::size_t num_entries) | ||
{ | ||
if (initialize_names_) | ||
{ | ||
name_value_pairs_.reserve(num_entries); | ||
} | ||
} | ||
|
||
virtual void startMapEntry(const std::string &map_name) | ||
{ | ||
if (initialize_names_) | ||
{ | ||
if (empty()) | ||
{ | ||
emplace(map_name); | ||
} | ||
else | ||
{ | ||
if (back().isArray()) | ||
{ | ||
concatWithNodeAndEmplace( | ||
bracket_left_, | ||
boost::lexical_cast<std::string>(back().index_), | ||
bracket_right_, | ||
separator_, | ||
map_name); | ||
} | ||
else | ||
{ | ||
concatWithNodeAndEmplace(separator_, map_name); | ||
} | ||
} | ||
} | ||
} | ||
|
||
virtual void endMapEntry() | ||
{ | ||
if (initialize_names_) | ||
{ | ||
pop(); | ||
} | ||
} | ||
|
||
virtual void endMap() | ||
{ | ||
} | ||
|
||
|
||
virtual bool startIteratedMap(const std::size_t /*num_entries*/, const Parameters &) | ||
{ | ||
return (false); | ||
} | ||
|
||
virtual void startArray(const std::size_t size, const bool /*compact*/ = false) | ||
{ | ||
if (initialize_names_) | ||
{ | ||
name_value_pairs_.reserve(size); | ||
if (back().isArray()) | ||
{ | ||
emplace(concatWithNode(std::string("_"), boost::lexical_cast<std::string>(back().index_)), | ||
0, | ||
size); | ||
} | ||
else | ||
{ | ||
emplace(back().node_, 0, size); | ||
} | ||
} | ||
} | ||
|
||
virtual void endArrayElement() | ||
{ | ||
if (initialize_names_) | ||
{ | ||
shiftArray(); | ||
} | ||
} | ||
|
||
virtual void endArray() | ||
{ | ||
if (initialize_names_) | ||
{ | ||
pop(); | ||
} | ||
} | ||
|
||
|
||
#define ARILES2_BASIC_TYPE(type) \ | ||
void writeElement(const type &element, const Parameters &) \ | ||
{ \ | ||
if (index_ == name_value_pairs_.size()) \ | ||
{ \ | ||
name_value_pairs_.resize(index_ + 1); \ | ||
} \ | ||
if (initialize_names_) \ | ||
{ \ | ||
name_value_pairs_.name(index_) = back().node_; \ | ||
if (back().isArray()) \ | ||
{ \ | ||
name_value_pairs_.name(index_) += "_"; \ | ||
name_value_pairs_.name(index_) += boost::lexical_cast<std::string>(back().index_); \ | ||
} \ | ||
} \ | ||
name_value_pairs_.value(index_) = element; \ | ||
++index_; \ | ||
} | ||
|
||
CPPUT_MACRO_SUBSTITUTE(ARILES2_BASIC_NUMERIC_TYPES_LIST) | ||
|
||
#undef ARILES2_BASIC_TYPE | ||
|
||
|
||
void writeElement(const std::string &element, const Parameters ¶meters) | ||
{ | ||
writeElement(element.size(), parameters); | ||
} | ||
}; | ||
|
||
|
||
using Writer = GenericWriter<NameValueVector>; | ||
} // namespace ns_namevalue2 | ||
} // namespace ariles2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.