diff --git a/include/toml/toml.h b/include/toml/toml.h index 1945674..9bf161e 100644 --- a/include/toml/toml.h +++ b/include/toml/toml.h @@ -28,7 +28,20 @@ namespace toml { class Value; typedef std::chrono::system_clock::time_point Time; typedef std::vector Array; -typedef std::map Table; + +class Table : public std::map { +public: + inline void setInline(bool flag) + { + isInline_ = flag; + } + inline bool IsInline() const + { + return isInline_; + } +private: + bool isInline_ = false; +}; namespace internal { template struct call_traits_value { @@ -156,6 +169,10 @@ class Value { Value* setChild(const std::string& key, Value&& v); bool eraseChild(const std::string& key); + // + void setInline(bool flag); + bool IsInline() const; + // ---------------------------------------------------------------------- // For Array value @@ -1367,23 +1384,50 @@ inline void Value::write(std::ostream* os, const std::string& keyPrefix, int ind (*os) << ']'; break; case TABLE_TYPE: - for (const auto& kv : *table_) { + if (table_->IsInline()) { + (*os) << "{ "; + } + for (auto iter = table_->begin(); iter != table_->end(); ++iter) { + const auto& kv = *iter; if (kv.second.is()) continue; if (kv.second.is() && kv.second.size() > 0 && kv.second.find(0)->is
()) continue; (*os) << spaces(indent) << escapeKey(kv.first) << " = "; kv.second.write(os, keyPrefix, indent >= 0 ? indent + 1 : indent); - (*os) << '\n'; + if (table_->IsInline()) { + ++iter; + if (iter != table_->end()) { + (*os) << ", "; + } + --iter; + } else { + (*os) << '\n'; + } } - for (const auto& kv : *table_) { + for (auto iter = table_->begin(); iter != table_->end(); ++iter) { + const auto& kv = *iter; if (kv.second.is
()) { std::string key(keyPrefix); if (!keyPrefix.empty()) key += "."; key += escapeKey(kv.first); - (*os) << "\n" << spaces(indent) << "[" << key << "]\n"; + if (kv.second.IsInline()) { + if (!table_->IsInline()) { + (*os) << "\n"; + } + (*os) << escapeKey(kv.first) << " = "; + } else { + (*os) << "\n" << spaces(indent) << "[" << key << "]\n"; + } kv.second.write(os, key, indent >= 0 ? indent + 1 : indent); + if (table_->IsInline()) { + ++iter; + if (iter != table_->end()) { + (*os) << ", "; + } + --iter; + } } if (kv.second.is() && kv.second.size() > 0 && kv.second.find(0)->is
()) { std::string key(keyPrefix); @@ -1396,6 +1440,9 @@ inline void Value::write(std::ostream* os, const std::string& keyPrefix, int ind } } } + if (table_->IsInline()) { + (*os) << " }"; + } break; default: failwith("writing unknown type"); @@ -1588,6 +1635,20 @@ inline bool Value::eraseChild(const std::string& key) return table_->erase(key) > 0; } +inline void Value::setInline(bool flag) +{ + if (!is
()) + failwith("type must be table to do setInline(flag)."); + table_->setInline(flag); +} + +inline bool Value::IsInline() const +{ + if(!is
()) + failwith("type must be table to do IsInline."); + return table_->IsInline(); +} + inline Value& Value::operator[](const std::string& key) { if (!valid()) @@ -2040,6 +2101,7 @@ inline bool Parser::parseInlineTable(Value* value) if (!consumeForValue(TokenType::RBRACE)) return false; + t.setInline(true); *value = std::move(t); return true; }