1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
12 template<typename Type> inline std::string convert2string(Type& input) {
13 std::ostringstream convertStream;
14 convertStream << input;
15 return convertStream.str();
19 inline std::string operator ""_s(const char * str, std::size_t len) {
20 return std::string(str, str + len);
23 inline std::string make_content(const std::string & tag, const std::string & content) {
24 return"<"_s + tag + ">" + content + "</" + tag + ">";
27 inline std::string make_content(const std::string & tag, const std::string & attribute, const std::string & content) {
28 return attribute.empty() ? make_content(tag, content) :
29 ("<"_s + tag + attribute + ">" + content + "</" + tag + ">");
33 inline std::string make_content(const std::string & tag, const std::string & content) {
34 return std::string("<") + tag +">" + content + "</" + tag +">" ;
37 inline std::string make_content(const std::string & tag, const std::string & attribute, const std::string & content) {
38 return attribute.empty() ? make_content(tag, content):
39 (std::string("<") + tag + attribute + ">" + content + "</" + tag +">");
43 class ConstCharMaker {
46 ConstCharMaker(const std::string & ref) : ref(ref){}
47 operator const char * () {
57 mutable std::string _content;
60 mutable std::shared_ptr<Token<Token<T>>> lastTag;
63 Token(T* f, const std::string & tag) : _f(f), _tag(tag){}
66 return const_cast<T&>(_f->closeToken());
68 //direct node content description - without subnodes
69 template <class ...Args>
70 Token<T> & node(const std::string & name, Args ... content) {
71 _content += make_content(name, merge({convert2string(content)...}));
75 //combine call to close and creation of new node
76 template <class ...Args>
77 Token<T> & newnode(const std::string & name, Args ... content) {
78 return close().node(name, content ...);
82 std::string tag () const {
86 std::string content () const {
90 void add_content (std::string content) {
94 std::string attr () const {
98 template<typename Arg>
99 Token<T> & attr (const std::string & attributeType, const Arg & attribute) {
100 _attr = merge({_attr, attributeType + "=\"" + convert2string(attribute) + "\""});
104 operator std::string () {
105 return closeAll().please();
108 ConstCharMaker c_str() const {
109 return ConstCharMaker(closeAll().please());
112 Token<Token<T>>& node(const std::string & tag) {
113 lastTag = std::make_shared<Token<Token<T>>>(this, tag);
119 auto closeAll () const -> decltype(_f->closeAll()){
121 return _f->closeAll();
123 std::string please() {
125 if (lastTag.get() != nullptr) {
131 const Token<T>& closeToken() const {
133 _content += make_content(lastTag->tag(), lastTag->attr(), lastTag->content());
138 std::string merge(std::initializer_list<std::string> strList)const
140 std::stringstream ret ;
142 for (auto it = strList.begin(); it != strList.end(); it++) {
144 if ((it + 1) != strList.end()) {
154 friend class Token<XMLFather>;
155 std::list<std::string> tokens;
156 std::shared_ptr<Token<XMLFather>> lastTag;
160 static XMLFather make_without_schema() {
161 auto x = XMLFather();
166 tokens.push_back("<?xml version=\"1.0\"?>");
168 Token<XMLFather>& node(const std::string & tag) {
169 lastTag = std::make_shared<Token<XMLFather>>(this, tag);
173 std::string please() {
174 if (!_please.empty()) {
177 if (lastTag.get() != nullptr) {
180 std::stringstream ss;
181 for (auto s : tokens) {
182 ss << s << std::endl;
184 return _please=ss.str();
187 operator std::string (){
192 XMLFather & closeAll() {
195 XMLFather & closeToken() {
196 tokens.push_back(make_content(lastTag->tag(), lastTag->attr(), lastTag->content()));