Changes in src/Common/utility.h [940bba3:1ba88a0]
- File:
-
- 1 edited
-
src/Common/utility.h (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Common/utility.h
r940bba3 r1ba88a0 17 17 #define _UTILITY_H 18 18 19 #include <iostream> 20 #include <sstream> 21 #include <iterator> 22 #include <string> 19 23 #include <cctype> 20 #include <algorithm>21 #include <iostream>22 #include <iterator>23 24 #include <list> 24 #include <memory>25 #include <sstream>26 #include <string>27 25 28 26 template< typename T > … … 112 110 return tmp; 113 111 } // if 112 } 113 114 template< class T, typename ResultType, ResultType( T::* memfunc )() > 115 ResultType dispatch( T *pT ) { 116 return (pT->*memfunc)(); 114 117 } 115 118 … … 158 161 } 159 162 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 160 174 template< typename T > 161 175 void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) { … … 170 184 } 171 185 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); 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++); 180 213 } 181 214 … … 187 220 188 221 // it's nice to actually be able to increment iterators by an arbitrary amount 189 template< class InputIt, class Distance > 190 InputIt operator+( InputIt it, Distance n ) { 191 advance(it, n); 192 return it; 222 template< typename Iterator > 223 Iterator operator+(Iterator i, int inc) { 224 while ( inc > 0 ) { 225 ++i; 226 --inc; 227 } 228 return i; 193 229 } 194 230 … … 210 246 } 211 247 212 // -----------------------------------------------------------------------------213 // Ref Counted Singleton class214 // Objects that inherit from this class will have at most one reference to it215 // 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 // -----------------------------------------------------------------------------236 248 // RAII object to regulate "save and restore" behaviour, e.g. 237 249 // void Foo::bar() { … … 248 260 }; 249 261 250 // -----------------------------------------------------------------------------251 // Helper struct and function to support252 // for ( val : reverseIterate( container ) ) {}253 // syntax to have a for each that iterates backwards254 255 262 template< typename T > 256 263 struct reverseIterate_t {
Note:
See TracChangeset
for help on using the changeset viewer.