Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / helpers / xml_father.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6 #include <string>
7 #include <list>
8 #include <sstream>
9 #include <memory>
10
11 namespace testing {
12     template<typename Type> inline std::string convert2string(Type& input) {
13                 std::ostringstream convertStream;
14                 convertStream << input;
15                 return convertStream.str();
16         }
17
18 #if _MSC_VER> 1800
19     inline std::string operator ""_s(const char * str, std::size_t len) {
20         return std::string(str, str + len);
21     }
22
23     inline std::string make_content(const std::string & tag, const std::string & content) {
24         return"<"_s + tag + ">" + content + "</" + tag + ">";
25     }
26
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 + ">");
30     }
31
32 #else
33     inline std::string make_content(const std::string & tag, const std::string & content) {
34         return std::string("<") + tag +">" + content + "</" + tag +">" ;
35     }
36
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 +">");
40     }
41 #endif
42
43     class ConstCharMaker {
44         std::string  ref;
45     public:
46         ConstCharMaker(const std::string & ref) : ref(ref){}
47         operator const char * () {
48             return ref.c_str();
49         }
50     };
51
52     template <class T>
53     class Token {
54         template <class S>
55         friend class Token;
56         T* _f;
57         mutable std::string _content;
58         std::string _tag;
59         std::string _attr;
60         mutable std::shared_ptr<Token<Token<T>>> lastTag;
61
62     public:
63         Token(T* f, const std::string & tag) : _f(f), _tag(tag){}
64
65         T & close() const {
66             return const_cast<T&>(_f->closeToken());
67         }
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)...}));
72             return *this;
73         }
74
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 ...);
79         }
80
81
82         std::string tag () const {
83             return _tag;
84         }
85
86         std::string content () const {
87             return _content;
88         }
89
90         void add_content (std::string content) {
91             _content += content;
92         }
93
94         std::string attr () const {
95             return _attr;
96         }
97
98         template<typename Arg>
99         Token<T> & attr (const std::string & attributeType, const Arg & attribute) {
100             _attr = merge({_attr, attributeType + "=\"" + convert2string(attribute) + "\""});
101             return *this;
102         }
103
104         operator std::string () {
105             return closeAll().please();
106         }
107
108         ConstCharMaker c_str() const {
109             return ConstCharMaker(closeAll().please());
110         }
111
112         Token<Token<T>>& node(const std::string & tag) {
113             lastTag = std::make_shared<Token<Token<T>>>(this, tag);
114             return *lastTag;
115         }
116
117
118         private :
119         auto  closeAll () const -> decltype(_f->closeAll()){
120             closeToken();
121             return _f->closeAll();
122         }
123         std::string please() {
124
125             if (lastTag.get() != nullptr) {
126                 lastTag->close();
127             }
128
129             return _content;
130         }
131         const Token<T>&  closeToken() const {
132             if (lastTag) {
133                 _content += make_content(lastTag->tag(), lastTag->attr(), lastTag->content());
134                 lastTag.reset();
135             }
136             return *this;
137         }
138         std::string merge(std::initializer_list<std::string> strList)const
139         {
140             std::stringstream ret ;
141
142             for (auto it = strList.begin(); it != strList.end(); it++) {
143                 ret  << *it;
144                 if ((it + 1) != strList.end()) {
145                     ret<<" ";
146                 }
147             }
148             return ret.str();
149         }
150
151     };
152
153     class XMLFather {
154         friend class Token<XMLFather>;
155         std::list<std::string> tokens;
156         std::shared_ptr<Token<XMLFather>> lastTag;
157         std::string _please;
158
159     public:
160         static XMLFather make_without_schema() {
161             auto x = XMLFather();
162             x.tokens.clear();
163             return x;
164         }
165         XMLFather () {
166             tokens.push_back("<?xml version=\"1.0\"?>");
167         }
168         Token<XMLFather>& node(const  std::string & tag) {
169             lastTag = std::make_shared<Token<XMLFather>>(this, tag);
170             return *lastTag;
171         }
172
173         std::string please() {
174             if (!_please.empty()) {
175                 return _please;
176             }
177             if (lastTag.get() != nullptr) {
178                 lastTag->close();
179             }
180             std::stringstream ss;
181             for (auto s : tokens) {
182                 ss << s << std::endl;
183             }
184             return _please=ss.str();
185         }
186
187         operator std::string (){
188             return please();
189         }
190
191     protected:
192         XMLFather & closeAll()  {
193             return closeToken();
194         }
195         XMLFather & closeToken()  {
196             tokens.push_back(make_content(lastTag->tag(), lastTag->attr(), lastTag->content()));
197             lastTag.reset();
198             return *this;
199         }
200     };
201
202 }