summaryrefslogtreecommitdiff
path: root/inference-engine/thirdparty/clDNN/src/include/xml_object.h
blob: 66ae93ebfff5ae45f825a6a2a6b3dc663938863a (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
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#pragma once
#include <string>
#include <type_traits>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <iostream>
#include <memory>

namespace cldnn
{
    class xml_base;
    using xml_key = std::string;
    using xml_base_ptr = std::shared_ptr<xml_base>;
    using xml_map = std::unordered_map<xml_key, xml_base_ptr>;

    class xml_base
    {
    public:
        virtual void dump(std::ostream& out, int offset) = 0;
    };

    template<class Type>
    class xml_leaf : public xml_base
    {
    private:
        Type value;
    public:
        xml_leaf(const Type& val) : value(val) {}
        xml_leaf(Type&& val) : value(std::move(val)) {}
        void dump(std::ostream& out, int) override
        {
            out << value;
        }
    };

    template<class Type>
    class xml_basic_array : public xml_base
    {
    private:
        std::vector<Type> values;
    public:
        xml_basic_array(const std::vector<Type>& arr) : values(arr) {}
        xml_basic_array(std::vector<Type>&& arr) : values(std::move(arr)) {}
        void dump(std::ostream& out, int) override
        {
            const char* delim = "";
            for (size_t i = 0; i < values.size(); i++)
            {
                out << delim << values[i];
                delim = ",";
            }
        }
    };

    class xml_composite : public xml_base
    {
    private:
        xml_map children;
    public:
        void dump(std::ostream& out, int offset = -1) override
        {
            offset++;
            bool first = true;
            static int offset_temp;
            std::string spaces(offset * 4, ' ');
            if (offset!=0) out << "\n";
            for (const auto& it : children)
            {
                if (first)
                {
                    out << spaces << "<" << it.first << ">";
                    first = false;
                }
                else
                    out << "\n" << spaces << "<" << it.first << ">";

                offset_temp = offset;
                it.second->dump(out, offset);

                std::string spaces_behind(0, ' ');
                if (offset_temp != offset)
                    spaces_behind = spaces;
                out << spaces_behind << "</" << it.first << ">";
                if (offset == 1)
                {
                    out << spaces << "\n";
                }
            };

            if (offset > 0)
            {
                out << spaces << "\n";
                offset--;
            }
        }

        template<class Type>
        void add(xml_key key, Type value)
        {
            children[key] = std::make_shared<xml_leaf<Type>>(value);
        }
        void add(xml_key key, xml_composite comp)
        {
            children[key] = std::make_shared<xml_composite>(comp);
        }
        template<class Type>
        void add(xml_key key, std::vector<Type> array)
        {
            children[key] = std::make_shared<xml_basic_array<Type>>(array);
        }
    };


}