#ifndef RFL_XML_OUTPUTVALUE_HPP_ #define RFL_XML_OUTPUTVALUE_HPP_ #include #include #include #include "rfl/always_false.hpp" #include "rfl/xml/OutputVar.hpp" namespace rfl { namespace xml { template class OutputValue : public OutputVar { public: static constexpr const char* XML_CONTENT = "xml_content"; public: OutputValue(T _val, bool _is_attribute) : is_attribute_(_is_attribute), val_(_val) {} ~OutputValue() = default; /// Inserts all elements into the builder. void insert(const std::string& _key, pugi::xml_node* _parent) final { const auto str = to_string(); if (is_attribute_) { _parent->append_attribute(_key.c_str()) = str.c_str(); } else if (_key == XML_CONTENT) { _parent->append_child(pugi::node_pcdata).set_value(str.c_str()); } else { auto node_child = _parent->append_child(_key.c_str()); node_child.append_child(pugi::node_pcdata).set_value(str.c_str()); } }; /// Whether this is null. bool is_null() const final { return false; } private: /// Transforms the underlying value into a string. std::string to_string() { if constexpr (std::is_same, std::string>()) { return val_; } else if constexpr (std::is_same, bool>()) { return val_ ? "true" : "false"; } else if constexpr (std::is_floating_point>() || std::is_integral>()) { return std::to_string(val_); } else { static_assert(always_false_v, "Unsupported type"); } } private: /// Whether you want this encoded as an attribute. bool is_attribute_; /// The underlying value. T val_; }; } // namespace xml } // namespace rfl #endif