summaryrefslogtreecommitdiff
path: root/inference-engine/thirdparty/clDNN/api/CPP/compounds.h
blob: cd8f4aac26bbc11a7a81a52b5ad6b42ff3b27f3b (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
// Copyright (c) 2016 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 <vector>
#include <cassert>
#include <iterator>
#include <cstring>
#include <string>

#include "meta_utils.hpp"


namespace cldnn {

/// @addtogroup cpp_api C++ API
/// @{

/// @cond CPP_HELPERS

/// @defgroup cpp_helpers Helpers
/// @{

template<typename T>
class mutable_array_ref
{
public:
    typedef size_t size_type;

    mutable_array_ref() :_data(nullptr), _size(0) {}
    mutable_array_ref(T& val) :_data(&val), _size(1) {}
    mutable_array_ref(T* data, size_t size) :_data(data), _size(size) {}

    template<size_t N>
    mutable_array_ref(T(&arr)[N]) : _data(arr), _size(N) {}

    mutable_array_ref(const mutable_array_ref& other) : _data(other._data), _size(other._size) {}

    mutable_array_ref& operator=(const mutable_array_ref& other)
    {
        if (this == &other)
            return *this;
        _data = other._data;
        _size = other._size;
        return *this;
    }

    T* data() const { return _data; }
    size_t size() const { return _size; }
    bool empty() const { return _size == 0; }

#if defined(_SECURE_SCL) && (_SECURE_SCL > 0)
    typedef stdext::checked_array_iterator<T*> iterator;
    typedef stdext::checked_array_iterator<const T*> const_iterator;
    iterator begin() const { return stdext::make_checked_array_iterator(_data, _size); }
    iterator end() const { return stdext::make_checked_array_iterator(_data, _size, _size); }
    const_iterator cbegin() const { return stdext::make_checked_array_iterator(_data, _size); }
    const_iterator cend() const { return stdext::make_checked_array_iterator(_data, _size, _size); }
#else
    typedef T* iterator;
    typedef T* const_iterator;
    iterator begin() const { return _data; }
    iterator end() const { return _data + _size; }
    const_iterator cbegin() const { return _data; }
    const_iterator cend() const { return _data + _size; }
#endif


    T& operator[](size_t idx) const
    {
        assert(idx < _size);
        return _data[idx];
    }

    T& at(size_t idx) const
    {
        if (idx >= _size) throw std::out_of_range("idx");
        return _data[idx];
    }

    std::vector<T> vector() const { return std::vector<T>(_data, _data + _size); }
private:
    T* _data;
    size_t _size;
};

template<typename T>
class array_ref
{
public:
    typedef size_t size_type;

    array_ref() :_data(nullptr), _size(0) {}
    array_ref(const T& val) :_data(&val), _size(1) {}
    array_ref(const T* data, size_t size) :_data(data), _size(size) {}

    template<typename A>
    array_ref(const std::vector<T, A>& vec) : _data(vec.data()), _size(vec.size()) {}

    template<size_t N>
    array_ref(const T(&arr)[N]) : _data(arr), _size(N) {}

    array_ref(const mutable_array_ref<T>& other) : _data(other.data()), _size(other.size()){}

    array_ref(const array_ref& other) : _data(other._data), _size(other._size) {}

    array_ref& operator=(const array_ref& other)
    {
        if (this == &other)
            return *this;
        _data = other._data;
        _size = other._size;
        return *this;
    }

    const T* data() const { return _data; }
    size_t size() const { return _size; }
    bool empty() const { return _size == 0; }

#if defined(_SECURE_SCL) && (_SECURE_SCL > 0)
    typedef stdext::checked_array_iterator<const T*> iterator;
    typedef stdext::checked_array_iterator<const T*> const_iterator;
    iterator begin() const { return stdext::make_checked_array_iterator(_data, _size); }
    iterator end() const { return stdext::make_checked_array_iterator(_data, _size, _size); }
    const_iterator cbegin() const { return stdext::make_checked_array_iterator(_data, _size); }
    const_iterator cend() const { return stdext::make_checked_array_iterator(_data, _size, _size); }
#else
    typedef const T* iterator;
    typedef const T* const_iterator;
    iterator begin() const { return _data; }
    iterator end() const { return _data + _size; }
    const_iterator cbegin() const { return _data; }
    const_iterator cend() const { return _data + _size; }
#endif

    const T& operator[](size_t idx) const
    {
        assert(idx < _size);
        return _data[idx];
    }

    const T& at(size_t idx) const
    {
        if (idx >= _size) throw std::out_of_range("idx");
        return _data[idx];
    }

    std::vector<T> vector() const { return std::vector<T>(_data, _data + _size); }
private:
    const T* _data;
    size_t _size;
};

// NOTE: It seems that clang before version 3.9 has bug that treates non-member template function with deleted function
//       body as non-template or non-specializable (specializations are treated as redefinitions).
//template<typename Char> size_t basic_strlen(const Char* str) = delete;
template<typename Char> size_t basic_strlen(const Char*)
{
    static_assert(meta::always_false<Char>::value, "basic_strlen<Char> for selected Char type is deleted.");
    return 0;
}

template<>
inline size_t basic_strlen(const char* str) { return std::strlen(str); }

template<>
inline size_t basic_strlen(const wchar_t* str) { return std::wcslen(str); }

template<typename Char>
class basic_string_ref
{
public:
    typedef const Char* iterator;
    typedef const Char* const_iterator;
    typedef size_t size_type;

private:
    const Char* _data;
    size_t _size;
public:
    basic_string_ref() :_data(nullptr), _size(0) {}
    basic_string_ref(const Char* str) : _data(str), _size(basic_strlen(str)) {}

    template<typename T, typename A>
    basic_string_ref(const std::basic_string<Char, T, A>& str) : _data(str.c_str()), _size(str.size()) {}

    basic_string_ref(const basic_string_ref& other) : _data(other._data), _size(other._size) {}

    basic_string_ref& operator=(const basic_string_ref& other)
    {
        if (this == &other)
            return *this;
        _data = other._data;
        _size = other._size;
        return *this;
    }

    const Char* data() const { return _data; }
    const Char* c_str() const { return _data; }
    size_t size() const { return _size; }
    size_t length() const { return _size; }
    bool empty() const { return _size == 0; }

    iterator begin() const { return _data; }
    iterator end() const { return _data + _size; }
    const_iterator cbegin() const { return begin(); }
    const_iterator cend() const { return end(); }

    const Char& operator[](size_t idx)
    {
        assert(idx < _size);
        return _data[idx];
    }

    std::basic_string<Char> str() const { return std::basic_string<Char>(_data, _size); }
    operator std::basic_string<Char>() const { return str(); }
};

typedef basic_string_ref<char> string_ref;
typedef basic_string_ref<wchar_t> wstring_ref;

/// @}

/// @endcond

/// @}
}