GEOS
StdContainerWrappers.hpp
1 #ifndef GEOS_COMMON_STD_CONTAINER_WRAPPERS_HPP
2 #define GEOS_COMMON_STD_CONTAINER_WRAPPERS_HPP
3 
4 #include <vector>
5 #include <map>
6 #include <unordered_map>
7 #include <memory>
8 
9 namespace geos
10 {
11 
12 namespace internal
13 {
14 
23 template< typename T,
24  typename Allocator = std::allocator< T >,
25  bool USE_STD_CONTAINER_BOUNDS_CHECKING = false
26  >
27 class StdVectorWrapper : public std::vector< T, Allocator >
28 {
29 public:
31  using Base = std::vector< T, Allocator >;
32 
39  T const & operator[]( size_t const index ) const
40  {
41  if constexpr (USE_STD_CONTAINER_BOUNDS_CHECKING)
42  {
43  return Base::at( index );
44  }
45  else
46  {
47  return Base::operator[]( index );
48  }
49  }
50 
57  T & operator[]( size_t const index )
58  {
59  if constexpr (USE_STD_CONTAINER_BOUNDS_CHECKING)
60  {
61  return Base::at( index ); // Throws std::out_of_range if out of bounds
62  }
63  else
64  {
65  return Base::operator[]( index ); // No bounds checking
66  }
67  }
68 };
69 } // namespace internal
70 
71 #if defined( GEOS_USE_BOUNDS_CHECK )
77 template< typename T, typename Allocator = std::allocator< T > >
78 using stdVector = internal::StdVectorWrapper< T, Allocator, true >;
79 #else
85 template< typename T, typename Allocator = std::allocator< T > >
86 using stdVector = std::vector< T, Allocator >;
87 #endif
88 
89 // template< typename MapType,
90 // bool USE_STD_CONTAINER_BOUNDS_CHECKING >
91 // class StdMapWrapper : public MapType
92 // {
93 // public:
94 // using Base = MapType;
95 // using Base::Base; // Inherit constructors
96 // using KeyType = typename Base::key_type;
97 // using MappedType = typename Base::mapped_type;
98 // using ValueType = typename Base::value_type;
99 
100 // // Override operator[]
101 // MappedType & operator[]( KeyType const & key)
102 // {
103 // if constexpr(USE_STD_CONTAINER_BOUNDS_CHECKING)
104 // {
105 // return this->at(key); // Throws std::out_of_range if key is missing
106 // }
107 // else
108 // {
109 // return Base::operator[](key); // Inserts default-constructed value if missing
110 // }
111 // }
112 
113 // MappedType const & operator[]( KeyType const & key) const
114 // {
115 // if constexpr(USE_STD_CONTAINER_BOUNDS_CHECKING)
116 // {
117 // return this->at(key);
118 // }
119 // else
120 // {
121 // return Base::operator[](key);
122 // }
123 // }
124 // };
125 
126 //} //namespace internal
127 
128 // template< typename Key,
129 // typename T,
130 // typename Compare = std::less<Key>,
131 // typename Allocator = std::allocator<std::pair<const Key, T>>,
132 // bool USE_STD_CONTAINER_BOUNDS_CHECKING = false>
133 // using map = internal::StdMapWrapper< std::map< Key, T, Compare, Allocator> ,
134 // USE_STD_CONTAINER_BOUNDS_CHECKING >;
135 
136 
137 // template< typename Key,
138 // typename T,
139 // typename Hash = std::hash<Key>,
140 // typename KeyEqual = std::equal_to<Key>,
141 // typename Allocator = std::allocator<std::pair<const Key, T>>,
142 // bool USE_STD_CONTAINER_BOUNDS_CHECKING = false>
143 // using unordered_map = internal::StdMapWrapper< std::unordered_map< Key, T, Hash, KeyEqual, Allocator >,
144 // USE_STD_CONTAINER_BOUNDS_CHECKING >;
145 
146 
147 } // namespace geos
148 
149 #endif /* GEOS_COMMON_STD_CONTAINER_WRAPPERS_HPP */
std::vector< T, Allocator > stdVector