Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/utility.h

    r1ba88a0 r940bba3  
    1717#define _UTILITY_H
    1818
     19#include <cctype>
     20#include <algorithm>
    1921#include <iostream>
     22#include <iterator>
     23#include <list>
     24#include <memory>
    2025#include <sstream>
    21 #include <iterator>
    2226#include <string>
    23 #include <cctype>
    24 #include <list>
    2527
    2628template< typename T >
     
    110112                return tmp;
    111113        } // if
    112 }
    113 
    114 template< class T, typename ResultType, ResultType( T::* memfunc )() >
    115 ResultType dispatch( T *pT ) {
    116         return (pT->*memfunc)();
    117114}
    118115
     
    161158}
    162159
    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 
    174160template< typename T >
    175161void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
     
    184170}
    185171
    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++);
     172template< typename... Args >
     173auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
     174  return std::copy_if(std::forward<Args>(args)...);
     175}
     176
     177template< typename... Args >
     178auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) {
     179  return zipWith(std::forward<Args>(args)..., std::make_pair);
    213180}
    214181
     
    220187
    221188// 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;
     189template< class InputIt, class Distance >
     190InputIt operator+( InputIt it, Distance n ) {
     191        advance(it, n);
     192        return it;
    229193}
    230194
     
    246210}
    247211
     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
     217template< typename ThisType >
     218class 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
     232template< typename ThisType >
     233std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
     234
     235// -----------------------------------------------------------------------------
    248236// RAII object to regulate "save and restore" behaviour, e.g.
    249237// void Foo::bar() {
     
    260248};
    261249
     250// -----------------------------------------------------------------------------
     251// Helper struct and function to support
     252// for ( val : reverseIterate( container ) ) {}
     253// syntax to have a for each that iterates backwards
     254
    262255template< typename T >
    263256struct reverseIterate_t {
Note: See TracChangeset for help on using the changeset viewer.