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 #include <iostream>
10 
11 namespace geos
12 {
13 
14 namespace internal
15 {
16 
25 template< typename T,
26  typename Allocator = std::allocator< T >,
27  bool USE_STD_CONTAINER_BOUNDS_CHECKING = false
28  >
29 class StdVectorWrapper : public std::vector< T, Allocator >
30 {
31 public:
33  using Base = std::vector< T, Allocator >;
34 
36  using Base::Base; // Inherit constructors
37 
38 
44  StdVectorWrapper( size_t const count, T const & value ):
45  Base( count, value )
46  {}
47 
48 
53  StdVectorWrapper( Base const & other ):
54  Base( other )
55  {}
56 
61  StdVectorWrapper( Base && other ):
62  Base( std::move( other ) )
63  {}
64 
71  T const & operator[]( size_t const index ) const
72  {
73  if constexpr (USE_STD_CONTAINER_BOUNDS_CHECKING)
74  {
75  if( index >= this->size() )
76  {
77  std::cout<< "Index out of bounds in StdVectorWrapper::operator[]: index = " + std::to_string( index ) + ", size = " + std::to_string( this->size());
78  }
79  return Base::at( index ); // Throws std::out_of_range if out of bounds.
80  }
81  else
82  {
83  return Base::operator[]( index );
84  }
85  }
86 
93  T & operator[]( size_t const index )
94  {
95  if constexpr (USE_STD_CONTAINER_BOUNDS_CHECKING)
96  {
97  if( index >= this->size() )
98  {
99  std::cout<< "Index out of bounds in StdVectorWrapper::operator[]: index = " + std::to_string( index ) + ", size = " + std::to_string( this->size());
100  }
101  return Base::at( index ); // Throws std::out_of_range if out of bounds.
102  }
103  else
104  {
105  return Base::operator[]( index ); // No bounds checking
106  }
107  }
108 };
109 } // namespace internal
110 
111 #if defined( GEOS_USE_BOUNDS_CHECK )
117 template< typename T, typename Allocator = std::allocator< T > >
118 using stdVector = internal::StdVectorWrapper< T, Allocator, true >;
119 #else
125 template< typename T, typename Allocator = std::allocator< T > >
126 using stdVector = std::vector< T, Allocator >;
127 #endif
128 
129 // template< typename MapType,
130 // bool USE_STD_CONTAINER_BOUNDS_CHECKING >
131 // class StdMapWrapper : public MapType
132 // {
133 // public:
134 // using Base = MapType;
135 // using Base::Base; // Inherit constructors
136 // using KeyType = typename Base::key_type;
137 // using MappedType = typename Base::mapped_type;
138 // using ValueType = typename Base::value_type;
139 
140 // // Override operator[]
141 // MappedType & operator[]( KeyType const & key)
142 // {
143 // if constexpr(USE_STD_CONTAINER_BOUNDS_CHECKING)
144 // {
145 // return this->at(key); // Throws std::out_of_range if key is missing
146 // }
147 // else
148 // {
149 // return Base::operator[](key); // Inserts default-constructed value if missing
150 // }
151 // }
152 
153 // MappedType const & operator[]( KeyType const & key) const
154 // {
155 // if constexpr(USE_STD_CONTAINER_BOUNDS_CHECKING)
156 // {
157 // return this->at(key);
158 // }
159 // else
160 // {
161 // return Base::operator[](key);
162 // }
163 // }
164 // };
165 
166 //} //namespace internal
167 
168 // template< typename Key,
169 // typename T,
170 // typename Compare = std::less<Key>,
171 // typename Allocator = std::allocator<std::pair<const Key, T>>,
172 // bool USE_STD_CONTAINER_BOUNDS_CHECKING = false>
173 // using map = internal::StdMapWrapper< std::map< Key, T, Compare, Allocator> ,
174 // USE_STD_CONTAINER_BOUNDS_CHECKING >;
175 
176 
177 // template< typename Key,
178 // typename T,
179 // typename Hash = std::hash<Key>,
180 // typename KeyEqual = std::equal_to<Key>,
181 // typename Allocator = std::allocator<std::pair<const Key, T>>,
182 // bool USE_STD_CONTAINER_BOUNDS_CHECKING = false>
183 // using unordered_map = internal::StdMapWrapper< std::unordered_map< Key, T, Hash, KeyEqual, Allocator >,
184 // USE_STD_CONTAINER_BOUNDS_CHECKING >;
185 
186 
187 } // namespace geos
188 
189 #endif /* GEOS_COMMON_STD_CONTAINER_WRAPPERS_HPP */
std::vector< T, Allocator > stdVector