Skip to content

Commit

Permalink
Serializer adds indentation after value-only tag
Browse files Browse the repository at this point in the history
- The closing state of the current tag and if a line break is required for the
  closing tag, is maintained as a small class with attributes of speaking
  names where previously only a bool was used to reflect the closed or !closed
  state.
  • Loading branch information
mdoerfel authored and srhenning committed Jul 6, 2018
1 parent a9bc8ab commit 6bd3fe9
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions emf4cpp/ecorecpp/serializer/greedy_serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ struct greedy_serializer
private:
std::ostream& out;
unsigned int level;
std::list< bool > has_value;
enum class ClosingState { Open, Closed };
enum class NewLineRequired { NoLineBreak, LineBreak };
struct TagManagement {
TagManagement(ClosingState c, NewLineRequired n) : closing(c), newLine(n) { }
ClosingState closing;
NewLineRequired newLine;
};
std::list< TagManagement > has_value;
bool indent;

inline void _indent()
Expand All @@ -81,13 +88,14 @@ struct greedy_serializer

inline void open_object(const string_t& _name, bool silent = false)
{
if(has_value.size() && !has_value.back())
if (has_value.size() && has_value.back().closing == ClosingState::Open)
{
has_value.back() = true;
has_value.back().closing = ClosingState::Closed;
has_value.back().newLine = NewLineRequired::LineBreak;
out << ">\n";
}

has_value.push_back(false);
has_value.push_back(TagManagement(ClosingState::Open, NewLineRequired::NoLineBreak));

if (indent)
_indent();
Expand All @@ -102,11 +110,11 @@ struct greedy_serializer
--level;

if (!silent) {
if (!has_value.back())
if (has_value.back().closing == ClosingState::Open)
out << "/>\n";
else
{
if (indent)
if (indent && has_value.back().newLine == NewLineRequired::LineBreak)
_indent();

out << "</" << _name << ">\n";
Expand All @@ -124,16 +132,17 @@ struct greedy_serializer
inline void add_value(const string_t& _value)
{
assert(has_value.size());
assert(!has_value.back());
assert(has_value.back().closing == ClosingState::Open);

has_value.back() = true;
has_value.back().closing = ClosingState::Closed;
has_value.back().newLine = NewLineRequired::NoLineBreak;
out << ">" << _value;
}

inline void append(const string_t& _value)
{
assert(has_value.size());
has_value.back() = true;
has_value.back().closing = ClosingState::Closed;
out << _value;
}
};
Expand Down

0 comments on commit 6bd3fe9

Please sign in to comment.