Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/utility.h

    r940bba3 r1ba88a0  
    1717#define _UTILITY_H
    1818
     19#include <iostream>
     20#include <sstream>
     21#include <iterator>
     22#include <string>
    1923#include <cctype>
    20 #include <algorithm>
    21 #include <iostream>
    22 #include <iterator>
    2324#include <list>
    24 #include <memory>
    25 #include <sstream>
    26 #include <string>
    2725
    2826template< typename T >
     
    112110                return tmp;
    113111        } // if
     112}
     113
     114template< class T, typename ResultType, ResultType( T::* memfunc )() >
     115ResultType dispatch( T *pT ) {
     116        return (pT->*memfunc)();
    114117}
    115118
     
    158161}
    159162
     163template< class Constructed, typename Arg >
     164Constructed *ctor( Arg arg ) {
     165        Constructed *c = new Constructed( arg );
     166        return c;
     167}
     168
     169template< class Constructed, typename Arg >
     170Constructed ctor_noptr( Arg arg ) {
     171        return Constructed( arg );
     172}
     173
    160174template< typename T >
    161175void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
     
    170184}
    171185
    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);
     186template< typename T1, typename T2 >
     187T2 *cast_ptr( T1 *from ) {
     188        return dynamic_cast< T2 * >( from );
     189}
     190
     191template< class Exception, typename Arg >
     192void inline assert_throw( bool pred, Arg arg ) {
     193        if (pred) throw Exception( arg );
     194}
     195
     196template< typename T >
     197struct is_null_pointer {
     198        bool operator()( const T *ptr ) { return ( ptr == 0 ); }
     199};
     200
     201template< class InputIterator, class OutputIterator, class Predicate >
     202void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) {
     203        while ( begin++ != end )
     204                if ( pred(*begin) ) *out++ = *begin;
     205
     206        return;
     207}
     208
     209template< class InputIterator1, class InputIterator2, class OutputIterator >
     210void 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++);
    180213}
    181214
     
    187220
    188221// 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;
     222template< typename Iterator >
     223Iterator operator+(Iterator i, int inc) {
     224        while ( inc > 0 ) {
     225                ++i;
     226                --inc;
     227        }
     228        return i;
    193229}
    194230
     
    210246}
    211247
    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 // -----------------------------------------------------------------------------
    236248// RAII object to regulate "save and restore" behaviour, e.g.
    237249// void Foo::bar() {
     
    248260};
    249261
    250 // -----------------------------------------------------------------------------
    251 // Helper struct and function to support
    252 // for ( val : reverseIterate( container ) ) {}
    253 // syntax to have a for each that iterates backwards
    254 
    255262template< typename T >
    256263struct reverseIterate_t {
Note: See TracChangeset for help on using the changeset viewer.