HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
const_pointer_container.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <type_traits>
7 
8 namespace onnxruntime {
9 /**
10  Container has T* entries. e.g. std::vector<T*>, and this class provides const access to those
11  via iterators and direct access, as the standard behavior only makes the pointer constant,
12  and not what is pointed too. i.e. you get a const pointer to T not a pointer to const T without this wrapper.
13  See https://stackoverflow.com/questions/8017036/understanding-const-iterator-with-pointers
14 */
15 template <typename Container>
17  public:
19 
20  class ConstIterator {
21  public:
22  using const_iterator = typename Container::const_iterator;
23  using iterator_category = std::input_iterator_tag;
24  using value_type = T*;
25  using difference_type = std::ptrdiff_t;
26  using pointer = T**;
27  using reference = T*&;
28 
29  /** Construct iterator for container that will return const T* entries.*/
30  explicit ConstIterator(const_iterator position) noexcept : current_{position}, item_{nullptr} {}
31  ConstIterator(const ConstIterator& other) = default;
32  ConstIterator& operator=(const ConstIterator& other) = default;
33 
34  bool operator==(const ConstIterator& other) const noexcept { return current_ == other.current_; }
35  bool operator!=(const ConstIterator& other) const noexcept { return current_ != other.current_; }
36 
38  ++current_;
39  return *this;
40  }
41 
43  ConstIterator tmp{*this};
44  ++(*this);
45  return tmp;
46  }
47 
48  const T*& operator*() const {
49  item_ = *current_;
50  return item_;
51  }
52 
53  const T** operator->() const { return &(operator*()); };
54 
55  private:
56  const_iterator current_;
57  mutable const T* item_;
58  };
59 
60  /**
61  Construct wrapper class that will provide const access to the pointers in a container of non-const pointers.
62  @param data Container with non-const pointers. e.g. std::vector<T*>
63  */
64  explicit ConstPointerContainer(const Container& data) noexcept : data_(data) {}
65 
66  size_t size() const noexcept { return data_.size(); }
67  bool empty() const noexcept { return data_.empty(); }
68 
69  ConstIterator cbegin() const noexcept { return ConstIterator(data_.cbegin()); }
70  ConstIterator cend() const noexcept { return ConstIterator(data_.cend()); }
71 
72  ConstIterator begin() const noexcept { return ConstIterator(data_.cbegin()); }
73  ConstIterator end() const noexcept { return ConstIterator(data_.cend()); }
74 
75  const T* operator[](size_t index) const { return data_[index]; }
76 
77  const T* at(size_t index) const {
78  ORT_ENFORCE(index < data_.size());
79  return data_[index];
80  }
81 
82  private:
83  const Container& data_;
84 };
85 } // namespace onnxruntime
#define ORT_ENFORCE(condition,...)
Definition: common.h:173
ConstIterator cbegin() const noexcept
ConstIterator end() const noexcept
const T * operator[](size_t index) const
typename std::remove_pointer< typename Container::value_type >::type T
ConstPointerContainer(const Container &data) noexcept
ConstIterator begin() const noexcept
ConstIterator cend() const noexcept
GLuint index
Definition: glcorearb.h:786
ConstIterator & operator=(const ConstIterator &other)=default
SIM_API const UT_StringHolder position
bool operator!=(const ConstIterator &other) const noexcept
bool operator==(const ConstIterator &other) const noexcept
#define const
Definition: zconf.h:214
const T * at(size_t index) const
type
Definition: core.h:1059
Definition: format.h:895