Changes in src/Common/utility.h [1ba88a0:940bba3]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/utility.h
r1ba88a0 r940bba3 17 17 #define _UTILITY_H 18 18 19 #include <cctype> 20 #include <algorithm> 19 21 #include <iostream> 22 #include <iterator> 23 #include <list> 24 #include <memory> 20 25 #include <sstream> 21 #include <iterator>22 26 #include <string> 23 #include <cctype>24 #include <list>25 27 26 28 template< typename T > … … 110 112 return tmp; 111 113 } // if 112 }113 114 template< class T, typename ResultType, ResultType( T::* memfunc )() >115 ResultType dispatch( T *pT ) {116 return (pT->*memfunc)();117 114 } 118 115 … … 161 158 } 162 159 163 template< class Constructed, typename Arg >164 Constructed *ctor( Arg arg ) {165 Constructed *c = new Constructed( arg );166 return c;167 }168 169 template< class Constructed, typename Arg >170 Constructed ctor_noptr( Arg arg ) {171 return Constructed( arg );172 }173 174 160 template< typename T > 175 161 void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) { … … 184 170 } 185 171 186 template< typename T1, typename T2 > 187 T2 *cast_ptr( T1 *from ) { 188 return dynamic_cast< T2 * >( from ); 189 } 190 191 template< class Exception, typename Arg > 192 void inline assert_throw( bool pred, Arg arg ) { 193 if (pred) throw Exception( arg ); 194 } 195 196 template< typename T > 197 struct is_null_pointer { 198 bool operator()( const T *ptr ) { return ( ptr == 0 ); } 199 }; 200 201 template< class InputIterator, class OutputIterator, class Predicate > 202 void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) { 203 while ( begin++ != end ) 204 if ( pred(*begin) ) *out++ = *begin; 205 206 return; 207 } 208 209 template< class InputIterator1, class InputIterator2, class OutputIterator > 210 void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) { 211 while ( b1 != e1 && b2 != e2 ) 212 *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++); 172 template< typename... Args > 173 auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) { 174 return std::copy_if(std::forward<Args>(args)...); 175 } 176 177 template< typename... Args > 178 auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) { 179 return zipWith(std::forward<Args>(args)..., std::make_pair); 213 180 } 214 181 … … 220 187 221 188 // it's nice to actually be able to increment iterators by an arbitrary amount 222 template< typename Iterator > 223 Iterator operator+(Iterator i, int inc) { 224 while ( inc > 0 ) { 225 ++i; 226 --inc; 227 } 228 return i; 189 template< class InputIt, class Distance > 190 InputIt operator+( InputIt it, Distance n ) { 191 advance(it, n); 192 return it; 229 193 } 230 194 … … 246 210 } 247 211 212 // ----------------------------------------------------------------------------- 213 // Ref Counted Singleton class 214 // Objects that inherit from this class will have at most one reference to it 215 // but if all references die, the object will be deleted. 216 217 template< typename ThisType > 218 class RefCountSingleton { 219 public: 220 static std::shared_ptr<ThisType> get() { 221 if( global_instance.expired() ) { 222 std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>(); 223 global_instance = new_instance; 224 return std::move(new_instance); 225 } 226 return global_instance.lock(); 227 } 228 private: 229 static std::weak_ptr<ThisType> global_instance; 230 }; 231 232 template< typename ThisType > 233 std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance; 234 235 // ----------------------------------------------------------------------------- 248 236 // RAII object to regulate "save and restore" behaviour, e.g. 249 237 // void Foo::bar() { … … 260 248 }; 261 249 250 // ----------------------------------------------------------------------------- 251 // Helper struct and function to support 252 // for ( val : reverseIterate( container ) ) {} 253 // syntax to have a for each that iterates backwards 254 262 255 template< typename T > 263 256 struct reverseIterate_t {
Note: See TracChangeset
for help on using the changeset viewer.