Skip to content

Commit

Permalink
+ serialization::NodeStackBase
Browse files Browse the repository at this point in the history
  • Loading branch information
asherikov committed Jun 21, 2024
1 parent 3b183b1 commit 3b67c35
Show file tree
Hide file tree
Showing 19 changed files with 320 additions and 338 deletions.
81 changes: 39 additions & 42 deletions extra_visitors/graphviz/src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Visitor
class ARILES2_VISIBILITY_ATTRIBUTE Visitor : public serialization::NodeStackBase<NodeWrapper>
{
public:
std::vector<NodeWrapper> node_stack_;
/// output file stream
std::ofstream config_ofs_;

Expand All @@ -84,6 +83,8 @@ namespace ariles2
std::set<std::string> all_ids_;
const Parameters *parameters_;

const std::string separator_ = "_";


public:
explicit Visitor(const std::string &file_name)
Expand Down Expand Up @@ -114,9 +115,9 @@ namespace ariles2
ARILES2_ASSERT(0 < stack_size, "Internal error: stack must contain at least 2 entries.");

// node
node_stack_.back().actual_id_ = node_options.id_;
back().actual_id_ = node_options.id_;

if (all_ids_.insert(node_stack_.back().actual_id_).second)
if (all_ids_.insert(back().actual_id_).second)
{
*output_stream_ << node_options.id_;
*output_stream_ << "[";
Expand All @@ -137,7 +138,7 @@ namespace ariles2
*output_stream_ //
<< node_stack_[stack_size - 2].actual_id_ //
<< "->" //
<< node_stack_.back().actual_id_ << ";\n";
<< back().actual_id_ << ";\n";
}
}
};
Expand Down Expand Up @@ -175,16 +176,16 @@ namespace ariles2
impl_->parameters_ = &parameters;
if (name.empty())
{
impl_->node_stack_.emplace_back("ariles");
impl_->emplace("ariles");
}
else
{
impl_->node_stack_.emplace_back(name);
impl_->emplace(name);
}
*impl_->output_stream_ //
<< "digraph graph_" << impl_->node_stack_.back().node_ //
<< " {\n" //
<< parameters.graph_options_; //
*impl_->output_stream_ //
<< "digraph graph_" << impl_->back().node_ //
<< " {\n" //
<< parameters.graph_options_; //
}


Expand All @@ -196,22 +197,20 @@ namespace ariles2

std::string Visitor::getDefaultNodeId() const
{
if (impl_->node_stack_.back().isArray())
if (impl_->back().isArray())
{
return (impl_->node_stack_.back().node_ + "_"
+ boost::lexical_cast<std::string>(impl_->node_stack_.back().index_));
return (impl_->back().node_ + "_" + boost::lexical_cast<std::string>(impl_->back().index_));
}
return (impl_->node_stack_.back().node_);
return (impl_->back().node_);
}

std::string Visitor::getDefaultNodeLabel() const
{
if (impl_->node_stack_.back().isArray())
if (impl_->back().isArray())
{
return (impl_->node_stack_.back().label_ + "_"
+ boost::lexical_cast<std::string>(impl_->node_stack_.back().index_));
return (impl_->back().label_ + "_" + boost::lexical_cast<std::string>(impl_->back().index_));
}
return (impl_->node_stack_.back().label_);
return (impl_->back().label_);
}

void Visitor::startMap(const Parameters &parameters, const Parameters::NodeOptions &node_options)
Expand All @@ -238,66 +237,64 @@ namespace ariles2
void Visitor::startMapEntry(const std::string &name)
{
ARILES2_TRACE_FUNCTION;
if (impl_->node_stack_.back().isArray())
if (impl_->back().isArray())
{
std::string node = impl_->node_stack_.back().node_;
node += "_";
node += boost::lexical_cast<std::string>(impl_->node_stack_.back().index_);
node += "_";
node += name;
impl_->node_stack_.emplace_back(node, name);
impl_->emplace(
impl_->concatWithNode(
impl_->separator_,
boost::lexical_cast<std::string>(impl_->back().index_),
impl_->separator_,
name),
name);
}
else
{
impl_->node_stack_.emplace_back(impl_->node_stack_.back().node_ + "_" + name, name);
impl_->emplace(impl_->concatWithNode(impl_->separator_, name), name);
}
}

void Visitor::endMapEntry()
{
ARILES2_TRACE_FUNCTION;
impl_->node_stack_.pop_back();
impl_->pop();
}


void Visitor::startArray(const std::size_t size, const bool compact)
{
ARILES2_TRACE_FUNCTION;
ARILES2_ASSERT(not impl_->node_stack_.empty(), "Internal error: empty stack.");
ARILES2_ASSERT(not impl_->empty(), "Internal error: empty stack.");

if (size > 0 || not compact)
{
impl_->writeNodeAndConnection(
impl_->parameters_->getDefaultNodeOptions(getDefaultNodeId(), getDefaultNodeLabel()));
}

if (impl_->node_stack_.back().isArray())
if (impl_->back().isArray())
{
std::string node = impl_->node_stack_.back().node_;
std::string label = impl_->node_stack_.back().label_;
node += "_";
node += boost::lexical_cast<std::string>(impl_->node_stack_.back().index_);
label += "_";
label += boost::lexical_cast<std::string>(impl_->node_stack_.back().index_);
impl_->node_stack_.emplace_back(node, label, 0, size);
const std::string index = boost::lexical_cast<std::string>(impl_->back().index_);
impl_->emplace(
impl_->concatWithNode(impl_->separator_, index),
impl_->concatenate(impl_->back().label_, impl_->separator_, index),
0,
size);
}
else
{
impl_->node_stack_.emplace_back(
impl_->node_stack_.back().node_, impl_->node_stack_.back().label_, 0, size);
impl_->emplace(impl_->back().node_, impl_->back().label_, 0, size);
}
}

void Visitor::endArrayElement()
{
ARILES2_ASSERT(impl_->node_stack_.back().isArray(), "Internal error: array expected.");
++impl_->node_stack_.back().index_;
impl_->shiftArray();
}

void Visitor::endArray()
{
ARILES2_TRACE_FUNCTION;
impl_->node_stack_.pop_back();
impl_->pop();
}


Expand Down
22 changes: 9 additions & 13 deletions extra_visitors/msgpack/src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Reader
class ARILES2_VISIBILITY_ATTRIBUTE Reader : public serialization::NodeStackBase<NodeWrapper>
{
public:
std::string buffer_;

std::vector<std::shared_ptr<::msgpack::object_handle>> handles_;

/// Stack of nodes.
std::vector<NodeWrapper> node_stack_;

std::size_t nameless_counter_;


Expand Down Expand Up @@ -138,7 +135,7 @@ namespace ariles2
{
ARILES2_TRACE_FUNCTION;
ARILES2_TRACE_VALUE(child_name);
if (impl_->node_stack_.empty())
if (impl_->empty())
{
for (std::size_t i = 0; i < impl_->handles_.size(); ++i)
{
Expand All @@ -148,7 +145,7 @@ namespace ariles2
{
if (::msgpack::type::MAP == impl_->handles_[i]->get().via.map.ptr[0].val.type)
{
impl_->node_stack_.emplace_back(&(impl_->handles_[i]->get().via.map.ptr[0].val));
impl_->emplace(&(impl_->handles_[i]->get().via.map.ptr[0].val));
return (true);
}
}
Expand All @@ -163,7 +160,7 @@ namespace ariles2
{
if (child_name == impl_->getRawNode().via.map.ptr[i].key.as<std::string>())
{
impl_->node_stack_.emplace_back(&(impl_->getRawNode().via.map.ptr[i].val));
impl_->emplace(&(impl_->getRawNode().via.map.ptr[i].val));
return (true);
}
}
Expand All @@ -177,15 +174,15 @@ namespace ariles2
void Reader::endMapEntry()
{
ARILES2_TRACE_FUNCTION;
impl_->node_stack_.pop_back();
impl_->pop();
}


std::size_t Reader::startArray()
{
ARILES2_TRACE_FUNCTION;
const std::size_t size = impl_->getRawNode().via.array.size;
impl_->node_stack_.emplace_back(0, size);
impl_->emplace(0, size);

return (size);
}
Expand All @@ -194,23 +191,22 @@ namespace ariles2
void Reader::endArray()
{
ARILES2_TRACE_FUNCTION;
impl_->node_stack_.pop_back();
impl_->pop();
}


void Reader::startArrayElement()
{
ARILES2_ASSERT(
impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
impl_->back().index_ < impl_->back().size_,
"Internal error: array has more elements than expected.");
}


void Reader::endArrayElement()
{
ARILES2_TRACE_FUNCTION;
ARILES2_ASSERT(impl_->node_stack_.back().isArray(), "Internal error: expected array.");
++impl_->node_stack_.back().index_;
impl_->shiftArray();
}


Expand Down
27 changes: 11 additions & 16 deletions extra_visitors/msgpack/src/reader_compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ namespace ariles2
{
namespace impl
{
class ARILES2_VISIBILITY_ATTRIBUTE Reader
class ARILES2_VISIBILITY_ATTRIBUTE Reader : public serialization::NodeStackBase<NodeWrapper>
{
public:
std::string buffer_;

::msgpack::object_handle handle_;

/// Stack of nodes.
std::vector<NodeWrapper> node_stack_;


public:
/**
Expand All @@ -59,7 +56,7 @@ namespace ariles2
try
{
unpack(handle_, buffer_.data(), buffer_.size(), nullptr);
node_stack_.emplace_back(&handle_.get());
emplace(&handle_.get());
}
catch (const std::exception &e)
{
Expand Down Expand Up @@ -118,12 +115,12 @@ namespace ariles2
{
const std::size_t size = impl_->getRawNode().via.array.size;
checkSize(limit_type, size, min, max);
impl_->node_stack_.emplace_back(0, size);
impl_->emplace(0, size);
}

bool Reader::startMapEntry(const std::string &)
{
if (impl_->node_stack_.back().isArray())
if (impl_->back().isArray())
{
startArrayElement();
}
Expand All @@ -132,7 +129,7 @@ namespace ariles2

void Reader::endMapEntry()
{
if (impl_->node_stack_.back().isArray())
if (impl_->back().isArray())
{
endArrayElement();
}
Expand All @@ -141,39 +138,37 @@ namespace ariles2
void Reader::endMap()
{
ARILES2_ASSERT(
impl_->node_stack_.back().isCompleted(),
"Some entries were not parsed, which is not allowed by this visitor.");
impl_->node_stack_.pop_back();
impl_->back().isCompleted(), "Some entries were not parsed, which is not allowed by this visitor.");
impl_->pop();
}


std::size_t Reader::startArray()
{
const std::size_t size = impl_->getRawNode().via.array.size;
impl_->node_stack_.emplace_back(0, size);
impl_->emplace(0, size);

return (size);
}


void Reader::endArray()
{
impl_->node_stack_.pop_back();
impl_->pop();
}


void Reader::startArrayElement()
{
ARILES2_ASSERT(
impl_->node_stack_.back().index_ < impl_->node_stack_.back().size_,
impl_->back().index_ < impl_->back().size_,
"Internal error: array has more elements than expected.");
}


void Reader::endArrayElement()
{
ARILES2_ASSERT(impl_->node_stack_.back().isArray(), "Internal error: expected array.");
++impl_->node_stack_.back().index_;
impl_->shiftArray();
}


Expand Down
Loading

0 comments on commit 3b67c35

Please sign in to comment.