Publishing R3
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / src / include / xml_object.h
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
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
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16 #pragma once
17 #include <string>
18 #include <type_traits>
19 #include <unordered_map>
20 #include <map>
21 #include <algorithm>
22 #include <iostream>
23 #include <memory>
24
25 namespace cldnn
26 {
27     class xml_base;
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>;
31
32     class xml_base
33     {
34     public:
35         virtual void dump(std::ostream& out, int offset) = 0;
36     };
37
38     template<class Type>
39     class xml_leaf : public xml_base
40     {
41     private:
42         Type value;
43     public:
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
47         {
48             out << value;
49         }
50     };
51
52     template<class Type>
53     class xml_basic_array : public xml_base
54     {
55     private:
56         std::vector<Type> values;
57     public:
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
61         {
62             const char* delim = "";
63             for (size_t i = 0; i < values.size(); i++)
64             {
65                 out << delim << values[i];
66                 delim = ",";
67             }
68         }
69     };
70
71     class xml_composite : public xml_base
72     {
73     private:
74         xml_map children;
75     public:
76         void dump(std::ostream& out, int offset = -1) override
77         {
78             offset++;
79             bool first = true;
80             static int offset_temp;
81             std::string spaces(offset * 4, ' ');
82             if (offset!=0) out << "\n";
83             for (const auto& it : children)
84             {
85                 if (first)
86                 {
87                     out << spaces << "<" << it.first << ">";
88                     first = false;
89                 }
90                 else
91                     out << "\n" << spaces << "<" << it.first << ">";
92
93                 offset_temp = offset;
94                 it.second->dump(out, offset);
95
96                 std::string spaces_behind(0, ' ');
97                 if (offset_temp != offset)
98                     spaces_behind = spaces;
99                 out << spaces_behind << "</" << it.first << ">";
100                 if (offset == 1)
101                 {
102                     out << spaces << "\n";
103                 }
104             };
105
106             if (offset > 0)
107             {
108                 out << spaces << "\n";
109                 offset--;
110             }
111         }
112
113         template<class Type>
114         void add(xml_key key, Type value)
115         {
116             children[key] = std::make_shared<xml_leaf<Type>>(value);
117         }
118         void add(xml_key key, xml_composite comp)
119         {
120             children[key] = std::make_shared<xml_composite>(comp);
121         }
122         template<class Type>
123         void add(xml_key key, std::vector<Type> array)
124         {
125             children[key] = std::make_shared<xml_basic_array<Type>>(array);
126         }
127     };
128
129
130 }
131