Skip to content

Commit

Permalink
+namevalue2 visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
asherikov committed Jul 14, 2024
1 parent 01cca4f commit b3a21b7
Show file tree
Hide file tree
Showing 15 changed files with 541 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ representation formats, in particular:
<https://asherikov.github.io/ariles/2/group__rosparam.html>

* A set of flattened key-value pairs, output only, no dependencies:
<https://asherikov.github.io/ariles/2/group__namevalue.html>
<https://asherikov.github.io/ariles/2/group__namevalue2.html>

* `graphviz` dot files for diagram generation:
<https://asherikov.github.io/ariles/2/group__graphviz.html>
Expand Down
12 changes: 6 additions & 6 deletions extra_visitors/graphviz/ariles2/visitors/graphviz.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ namespace ariles2
void endArrayElement();
void endArray();


template <class t_Ariles>
const Parameters getParameters(const t_Ariles &ariles_class) const
{
return (ariles_class.arilesGetParameters(*this));
}
/*
template <class t_Ariles>
const Parameters getParameters(const t_Ariles &ariles_class) const
{
return (ariles_class.arilesGetParameters(*this));
}*/

template <class t_Entry>
void startMap(t_Entry &, const Parameters &parameters)
Expand Down
2 changes: 2 additions & 0 deletions extra_visitors/namevalue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecated, use namevalue2.

5 changes: 5 additions & 0 deletions extra_visitors/namevalue2/CMakeLists.txt
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)
39 changes: 39 additions & 0 deletions extra_visitors/namevalue2/ariles2/visitors/namevalue2.h
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 extra_visitors/namevalue2/ariles2/visitors/namevalue2/writer.h
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 &param)
{
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 &parameters)
{
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
2 changes: 2 additions & 0 deletions extra_visitors/protobuf3/ariles2/visitors/protobuf3/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ namespace ariles2


public:
/*
template <class t_Ariles>
const Parameters &getParameters(const t_Ariles &ariles_class) const
{
return (ariles_class.arilesGetParameters(*this));
}
*/


template <class t_Left, class t_Right>
Expand Down
2 changes: 2 additions & 0 deletions extra_visitors/protobuf3/ariles2/visitors/protobuf3/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ namespace ariles2


public:
/*
template <class t_Ariles>
const Parameters &getParameters(const t_Ariles &ariles_class) const
{
return (ariles_class.arilesGetParameters(*this));
}
*/


template <class t_Left, class t_Right>
Expand Down
2 changes: 1 addition & 1 deletion include/ariles2/visitors/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace ariles2
using Parameters = t_Parameters;

public:
const t_Parameters &getDefaultParameters() const
virtual const t_Parameters &getDefaultParameters() const
{
const static t_Parameters parameters(false);
return parameters;
Expand Down
Loading

0 comments on commit b3a21b7

Please sign in to comment.