clDNN
compounds.h
1 /*
2 // Copyright (c) 2016 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 
18 #include <vector>
19 #include <cassert>
20 #include <iterator>
21 #include <cstring>
22 #include <string>
23 
24 #include "meta_utils.hpp"
25 
26 
27 namespace cldnn {
28 
31 
33 
36 
37 template<typename T>
38 class mutable_array_ref
39 {
40 public:
41  typedef size_t size_type;
42 
43  mutable_array_ref() :_data(nullptr), _size(0) {}
44  mutable_array_ref(T& val) :_data(&val), _size(1) {}
45  mutable_array_ref(T* data, size_t size) :_data(data), _size(size) {}
46 
47  template<size_t N>
48  mutable_array_ref(T(&arr)[N]) : _data(arr), _size(N) {}
49 
50  mutable_array_ref(const mutable_array_ref& other) : _data(other._data), _size(other._size) {}
51 
52  mutable_array_ref& operator=(const mutable_array_ref& other)
53  {
54  if (this == &other)
55  return *this;
56  _data = other._data;
57  _size = other._size;
58  return *this;
59  }
60 
61  T* data() const { return _data; }
62  size_t size() const { return _size; }
63  bool empty() const { return _size == 0; }
64 
65 #if defined(_SECURE_SCL) && (_SECURE_SCL > 0)
66  typedef stdext::checked_array_iterator<T*> iterator;
67  typedef stdext::checked_array_iterator<const T*> const_iterator;
68  iterator begin() const { return stdext::make_checked_array_iterator(_data, _size); }
69  iterator end() const { return stdext::make_checked_array_iterator(_data, _size, _size); }
70  const_iterator cbegin() const { return stdext::make_checked_array_iterator(_data, _size); }
71  const_iterator cend() const { return stdext::make_checked_array_iterator(_data, _size, _size); }
72 #else
73  typedef T* iterator;
74  typedef T* const_iterator;
75  iterator begin() const { return _data; }
76  iterator end() const { return _data + _size; }
77  const_iterator cbegin() const { return _data; }
78  const_iterator cend() const { return _data + _size; }
79 #endif
80 
81 
82  T& operator[](size_t idx) const
83  {
84  assert(idx < _size);
85  return _data[idx];
86  }
87 
88  T& at(size_t idx) const
89  {
90  if (idx >= _size) throw std::out_of_range("idx");
91  return _data[idx];
92  }
93 
94  std::vector<T> vector() const { return std::vector<T>(_data, _data + _size); }
95 private:
96  T* _data;
97  size_t _size;
98 };
99 
100 template<typename T>
101 class array_ref
102 {
103 public:
104  typedef size_t size_type;
105 
106  array_ref() :_data(nullptr), _size(0) {}
107  array_ref(const T& val) :_data(&val), _size(1) {}
108  array_ref(const T* data, size_t size) :_data(data), _size(size) {}
109 
110  template<typename A>
111  array_ref(const std::vector<T, A>& vec) : _data(vec.data()), _size(vec.size()) {}
112 
113  template<size_t N>
114  array_ref(const T(&arr)[N]) : _data(arr), _size(N) {}
115 
116  array_ref(const mutable_array_ref<T>& other) : _data(other.data()), _size(other.size()){}
117 
118  array_ref(const array_ref& other) : _data(other._data), _size(other._size) {}
119 
120  array_ref& operator=(const array_ref& other)
121  {
122  if (this == &other)
123  return *this;
124  _data = other._data;
125  _size = other._size;
126  return *this;
127  }
128 
129  const T* data() const { return _data; }
130  size_t size() const { return _size; }
131  bool empty() const { return _size == 0; }
132 
133 #if defined(_SECURE_SCL) && (_SECURE_SCL > 0)
134  typedef stdext::checked_array_iterator<const T*> iterator;
135  typedef stdext::checked_array_iterator<const T*> const_iterator;
136  iterator begin() const { return stdext::make_checked_array_iterator(_data, _size); }
137  iterator end() const { return stdext::make_checked_array_iterator(_data, _size, _size); }
138  const_iterator cbegin() const { return stdext::make_checked_array_iterator(_data, _size); }
139  const_iterator cend() const { return stdext::make_checked_array_iterator(_data, _size, _size); }
140 #else
141  typedef const T* iterator;
142  typedef const T* const_iterator;
143  iterator begin() const { return _data; }
144  iterator end() const { return _data + _size; }
145  const_iterator cbegin() const { return _data; }
146  const_iterator cend() const { return _data + _size; }
147 #endif
148 
149  const T& operator[](size_t idx) const
150  {
151  assert(idx < _size);
152  return _data[idx];
153  }
154 
155  const T& at(size_t idx) const
156  {
157  if (idx >= _size) throw std::out_of_range("idx");
158  return _data[idx];
159  }
160 
161  std::vector<T> vector() const { return std::vector<T>(_data, _data + _size); }
162 private:
163  const T* _data;
164  size_t _size;
165 };
166 
167 // NOTE: It seems that clang before version 3.9 has bug that treates non-member template function with deleted function
168 // body as non-template or non-specializable (specializations are treated as redefinitions).
169 //template<typename Char> size_t basic_strlen(const Char* str) = delete;
170 template<typename Char> size_t basic_strlen(const Char*)
171 {
172  static_assert(meta::always_false<Char>::value, "basic_strlen<Char> for selected Char type is deleted.");
173  return 0;
174 }
175 
176 template<>
177 inline size_t basic_strlen(const char* str) { return std::strlen(str); }
178 
179 template<>
180 inline size_t basic_strlen(const wchar_t* str) { return std::wcslen(str); }
181 
182 template<typename Char>
183 class basic_string_ref
184 {
185 public:
186  typedef const Char* iterator;
187  typedef const Char* const_iterator;
188  typedef size_t size_type;
189 
190 private:
191  const Char* _data;
192  size_t _size;
193 public:
194  basic_string_ref() :_data(nullptr), _size(0) {}
195  basic_string_ref(const Char* str) : _data(str), _size(basic_strlen(str)) {}
196 
197  template<typename T, typename A>
198  basic_string_ref(const std::basic_string<Char, T, A>& str) : _data(str.c_str()), _size(str.size()) {}
199 
200  basic_string_ref(const basic_string_ref& other) : _data(other._data), _size(other._size) {}
201 
202  basic_string_ref& operator=(const basic_string_ref& other)
203  {
204  if (this == &other)
205  return *this;
206  _data = other._data;
207  _size = other._size;
208  return *this;
209  }
210 
211  const Char* data() const { return _data; }
212  const Char* c_str() const { return _data; }
213  size_t size() const { return _size; }
214  size_t length() const { return _size; }
215  bool empty() const { return _size == 0; }
216 
217  iterator begin() const { return _data; }
218  iterator end() const { return _data + _size; }
219  const_iterator cbegin() const { return begin(); }
220  const_iterator cend() const { return end(); }
221 
222  const Char& operator[](size_t idx)
223  {
224  assert(idx < _size);
225  return _data[idx];
226  }
227 
228  std::basic_string<Char> str() const { return std::basic_string<Char>(_data, _size); }
229  operator std::basic_string<Char>() const { return str(); }
230 };
231 
232 typedef basic_string_ref<char> string_ref;
233 typedef basic_string_ref<wchar_t> wstring_ref;
234 
236 
238 
240 }