summaryrefslogtreecommitdiff
path: root/inference-engine/tests/helpers/xml_father.hpp
blob: 90b7d732a6de24e32ae9c08a0900165cb31184cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (C) 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once
#include <string>
#include <list>
#include <sstream>
#include <memory>

namespace testing {
    template<typename Type> inline std::string convert2string(Type& input) {
		std::ostringstream convertStream;
		convertStream << input;
		return convertStream.str();
	}

#if _MSC_VER> 1800
    inline std::string operator ""_s(const char * str, std::size_t len) {
        return std::string(str, str + len);
    }

    inline std::string make_content(const std::string & tag, const std::string & content) {
        return"<"_s + tag + ">" + content + "</" + tag + ">";
    }

    inline std::string make_content(const std::string & tag, const std::string & attribute, const std::string & content) {
        return attribute.empty() ? make_content(tag, content) :
            ("<"_s + tag + attribute + ">" + content + "</" + tag + ">");
    }

#else
    inline std::string make_content(const std::string & tag, const std::string & content) {
        return std::string("<") + tag +">" + content + "</" + tag +">" ;
    }

    inline std::string make_content(const std::string & tag, const std::string & attribute, const std::string & content) {
        return attribute.empty() ? make_content(tag, content):
        (std::string("<") + tag + attribute + ">" + content + "</" + tag +">");
    }
#endif

    class ConstCharMaker {
        std::string  ref;
    public:
        ConstCharMaker(const std::string & ref) : ref(ref){}
        operator const char * () {
            return ref.c_str();
        }
    };

    template <class T>
    class Token {
        template <class S>
        friend class Token;
        T* _f;
        mutable std::string _content;
        std::string _tag;
        std::string _attr;
        mutable std::shared_ptr<Token<Token<T>>> lastTag;

    public:
        Token(T* f, const std::string & tag) : _f(f), _tag(tag){}

        T & close() const {
            return const_cast<T&>(_f->closeToken());
        }
        //direct node content description - without subnodes
        template <class ...Args>
        Token<T> & node(const std::string & name, Args ... content) {
            _content += make_content(name, merge({convert2string(content)...}));
            return *this;
        }

        //combine call to close and creation of new node
        template <class ...Args>
        Token<T> & newnode(const std::string & name, Args ... content) {
            return close().node(name, content ...);
        }


        std::string tag () const {
            return _tag;
        }

        std::string content () const {
            return _content;
        }

        void add_content (std::string content) {
            _content += content;
        }

        std::string attr () const {
            return _attr;
        }

        template<typename Arg>
        Token<T> & attr (const std::string & attributeType, const Arg & attribute) {
            _attr = merge({_attr, attributeType + "=\"" + convert2string(attribute) + "\""});
            return *this;
        }

        operator std::string () {
            return closeAll().please();
        }

        ConstCharMaker c_str() const {
            return ConstCharMaker(closeAll().please());
        }

        Token<Token<T>>& node(const std::string & tag) {
            lastTag = std::make_shared<Token<Token<T>>>(this, tag);
            return *lastTag;
        }


        private :
        auto  closeAll () const -> decltype(_f->closeAll()){
            closeToken();
            return _f->closeAll();
        }
        std::string please() {

            if (lastTag.get() != nullptr) {
                lastTag->close();
            }

            return _content;
        }
        const Token<T>&  closeToken() const {
            if (lastTag) {
                _content += make_content(lastTag->tag(), lastTag->attr(), lastTag->content());
                lastTag.reset();
            }
            return *this;
        }
        std::string merge(std::initializer_list<std::string> strList)const
        {
            std::stringstream ret ;

            for (auto it = strList.begin(); it != strList.end(); it++) {
                ret  << *it;
                if ((it + 1) != strList.end()) {
                    ret<<" ";
                }
            }
            return ret.str();
        }

    };

    class XMLFather {
        friend class Token<XMLFather>;
        std::list<std::string> tokens;
        std::shared_ptr<Token<XMLFather>> lastTag;
        std::string _please;

    public:
        static XMLFather make_without_schema() {
            auto x = XMLFather();
            x.tokens.clear();
            return x;
        }
        XMLFather () {
            tokens.push_back("<?xml version=\"1.0\"?>");
        }
        Token<XMLFather>& node(const  std::string & tag) {
            lastTag = std::make_shared<Token<XMLFather>>(this, tag);
            return *lastTag;
        }

        std::string please() {
            if (!_please.empty()) {
                return _please;
            }
            if (lastTag.get() != nullptr) {
                lastTag->close();
            }
            std::stringstream ss;
            for (auto s : tokens) {
                ss << s << std::endl;
            }
            return _please=ss.str();
        }

        operator std::string (){
            return please();
        }

    protected:
        XMLFather & closeAll()  {
            return closeToken();
        }
        XMLFather & closeToken()  {
            tokens.push_back(make_content(lastTag->tag(), lastTag->attr(), lastTag->content()));
            lastTag.reset();
            return *this;
        }
    };

}