File size: 2,867 Bytes
c206440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#pragma once

#include <type_traits>

namespace onnxruntime {
/**
   Container has T* entries. e.g. std::vector<T*>, and this class provides const access to those
   via iterators and direct access, as the standard behavior only makes the pointer constant,
   and not what is pointed too. i.e. you get a const pointer to T not a pointer to const T without this wrapper.
   See https://stackoverflow.com/questions/8017036/understanding-const-iterator-with-pointers
*/
template <typename Container>
class ConstPointerContainer {
 public:
  using T = typename std::remove_pointer<typename Container::value_type>::type;

  class ConstIterator {
   public:
    using const_iterator = typename Container::const_iterator;
    using iterator_category = std::input_iterator_tag;
    using value_type = T*;
    using difference_type = std::ptrdiff_t;
    using pointer = T**;
    using reference = T*&;

    /** Construct iterator for container that will return const T* entries.*/
    explicit ConstIterator(const_iterator position) noexcept : current_{position}, item_{nullptr} {}
    ConstIterator(const ConstIterator& other) = default;
    ConstIterator& operator=(const ConstIterator& other) = default;

    bool operator==(const ConstIterator& other) const noexcept { return current_ == other.current_; }
    bool operator!=(const ConstIterator& other) const noexcept { return current_ != other.current_; }

    ConstIterator& operator++() {
      ++current_;
      return *this;
    }

    ConstIterator operator++(int) {
      ConstIterator tmp{*this};
      ++(*this);
      return tmp;
    }

    const T*& operator*() const {
      item_ = *current_;
      return item_;
    }

    const T** operator->() const { return &(operator*()); };

   private:
    const_iterator current_;
    mutable const T* item_;
  };

  /**
     Construct wrapper class that will provide const access to the pointers in a container of non-const pointers.
     @param data Container with non-const pointers. e.g. std::vector<T*>
  */
  explicit ConstPointerContainer(const Container& data) noexcept : data_(data) {}

  size_t size() const noexcept { return data_.size(); }
  bool empty() const noexcept { return data_.empty(); }

  ConstIterator cbegin() const noexcept { return ConstIterator(data_.cbegin()); }
  ConstIterator cend() const noexcept { return ConstIterator(data_.cend()); }

  ConstIterator begin() const noexcept { return ConstIterator(data_.cbegin()); }
  ConstIterator end() const noexcept { return ConstIterator(data_.cend()); }

  const T* operator[](size_t index) const { return data_[index]; }

  const T* at(size_t index) const {
    ORT_ENFORCE(index < data_.size());
    return data_[index];
  }

 private:
  const Container& data_;
};
}  // namespace onnxruntime