2 // Copyright (c) 2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 #include <type_traits>
19 #include <unordered_map>
28 using xml_key = std::string;
29 using xml_base_ptr = std::shared_ptr<xml_base>;
30 using xml_map = std::unordered_map<xml_key, xml_base_ptr>;
35 virtual void dump(std::ostream& out, int offset) = 0;
39 class xml_leaf : public xml_base
44 xml_leaf(const Type& val) : value(val) {}
45 xml_leaf(Type&& val) : value(std::move(val)) {}
46 void dump(std::ostream& out, int) override
53 class xml_basic_array : public xml_base
56 std::vector<Type> values;
58 xml_basic_array(const std::vector<Type>& arr) : values(arr) {}
59 xml_basic_array(std::vector<Type>&& arr) : values(std::move(arr)) {}
60 void dump(std::ostream& out, int) override
62 const char* delim = "";
63 for (size_t i = 0; i < values.size(); i++)
65 out << delim << values[i];
71 class xml_composite : public xml_base
76 void dump(std::ostream& out, int offset = -1) override
80 static int offset_temp;
81 std::string spaces(offset * 4, ' ');
82 if (offset!=0) out << "\n";
83 for (const auto& it : children)
87 out << spaces << "<" << it.first << ">";
91 out << "\n" << spaces << "<" << it.first << ">";
94 it.second->dump(out, offset);
96 std::string spaces_behind(0, ' ');
97 if (offset_temp != offset)
98 spaces_behind = spaces;
99 out << spaces_behind << "</" << it.first << ">";
102 out << spaces << "\n";
108 out << spaces << "\n";
114 void add(xml_key key, Type value)
116 children[key] = std::make_shared<xml_leaf<Type>>(value);
118 void add(xml_key key, xml_composite comp)
120 children[key] = std::make_shared<xml_composite>(comp);
123 void add(xml_key key, std::vector<Type> array)
125 children[key] = std::make_shared<xml_basic_array<Type>>(array);