Skip to content

Commit

Permalink
+namevalue2 visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
asherikov committed Jul 12, 2024
1 parent 01cca4f commit 0a0c2f1
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 2 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
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 = ariles2::cfgwrite::Visitor<ns_namevalue2::Writer>;
};
} // namespace ariles2
226 changes: 226 additions & 0 deletions extra_visitors/namevalue2/ariles2/visitors/namevalue2/writer.h
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 &parameters)
{
writeElement(element.size(), parameters);
}
};


using Writer = GenericWriter<NameValueVector>;
} // namespace ns_namevalue2
} // namespace ariles2
1 change: 1 addition & 0 deletions tests/api_v2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ ariles_define_regression_test("232" "yaml_cpp") # No
ariles_define_regression_test("233" "ANY" "${ARILES_TEST_ALL_VISITORS}") # subtree read
ariles_define_regression_test("234" "ANY" "${ARILES_TEST_ALL_VISITORS}") # OptionalPointer
ariles_define_regression_test("235" "ros2param") # Parameter declaration
ariles_define_regression_test("236" "namevalue2") # namevalue2
6 changes: 5 additions & 1 deletion tests/api_v2/all_enabled_visitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
// #include <ariles2/visitors/namevalue.h>
// #endif
//
// #ifdef ARILES_VISITOR_namevalue2
// #include <ariles2/visitors/namevalue2.h>
// #endif
//
// #ifdef ARILES_VISITOR_graphviz
// #include <ariles2/visitors/namevalue.h>
// #include <ariles2/visitors/graphviz.h>
// #endif
Loading

0 comments on commit 0a0c2f1

Please sign in to comment.