-
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
15 changed files
with
541 additions
and
15 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
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 = ns_namevalue2::Writer; | ||
}; | ||
} // namespace ariles2 |
263 changes: 263 additions & 0 deletions
263
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,263 @@ | ||
/** | ||
@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); | ||
} | ||
|
||
void finalize() | ||
{ | ||
} | ||
}; | ||
|
||
|
||
/** | ||
* @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) | ||
{ | ||
initialize_names_ = true; | ||
name_value_pairs_.reserve(reserve); | ||
index_ = 0; | ||
} | ||
|
||
virtual void startRoot(const std::string &name, const Parameters ¶m) | ||
{ | ||
CPPUT_TRACE_FUNCTION; | ||
if (not param.persistent_structure_ or 0 == name_value_pairs_.size()) | ||
{ | ||
initialize_names_ = true; | ||
} | ||
|
||
if (not name.empty()) | ||
{ | ||
startMapEntry(name); | ||
} | ||
} | ||
|
||
void flush() | ||
{ | ||
} | ||
|
||
|
||
template<class... t_Args> | ||
void finalize(t_Args &&... args) | ||
{ | ||
if (initialize_names_) | ||
{ | ||
// drop trailing leftovers | ||
name_value_pairs_.resize(index_); | ||
initialize_names_ = false; | ||
} | ||
index_ = 0; | ||
|
||
name_value_pairs_.finalize(std::forward<t_Args...>(args)...); | ||
} | ||
|
||
|
||
virtual void startMap(const Parameters &, const std::size_t num_entries) | ||
{ | ||
if (initialize_names_) | ||
{ | ||
name_value_pairs_.reserve(index_ + 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(index_ + 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); | ||
} | ||
|
||
virtual const Parameters &getDefaultParameters() const | ||
{ | ||
static Parameters parameters(/*override_parameters=*/true); | ||
|
||
parameters.sloppy_maps_ = true; | ||
parameters.sloppy_pairs_ = true; | ||
parameters.explicit_matrix_size_ = false; | ||
parameters.fallback_to_string_floats_ = false; | ||
parameters.flat_matrices_ = false; | ||
parameters.allow_missing_entries_ = true; | ||
|
||
parameters.persistent_structure_ = false; | ||
|
||
return 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
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.