Changes in / [4563a95:ee1635c8]


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/utility.h

    r4563a95 ree1635c8  
    1818
    1919#include <cctype>
    20 #include <algorithm>
    2120#include <iostream>
    2221#include <iterator>
     
    114113}
    115114
     115template< class T, typename ResultType, ResultType( T::* memfunc )() >
     116ResultType dispatch( T *pT ) {
     117        return (pT->*memfunc)();
     118}
     119
    116120template < typename T >
    117121std::list<T> tail( std::list<T> l ) {
     
    158162}
    159163
     164template< class Constructed, typename Arg >
     165Constructed *ctor( Arg arg ) {
     166        Constructed *c = new Constructed( arg );
     167        return c;
     168}
     169
     170template< class Constructed, typename Arg >
     171Constructed ctor_noptr( Arg arg ) {
     172        return Constructed( arg );
     173}
     174
    160175template< typename T >
    161176void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
     
    170185}
    171186
    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);
     187template< typename T1, typename T2 >
     188T2 *cast_ptr( T1 *from ) {
     189        return dynamic_cast< T2 * >( from );
     190}
     191
     192template< class Exception, typename Arg >
     193void inline assert_throw( bool pred, Arg arg ) {
     194        if (pred) throw Exception( arg );
     195}
     196
     197template< typename T >
     198struct is_null_pointer {
     199        bool operator()( const T *ptr ) { return ( ptr == 0 ); }
     200};
     201
     202template< class InputIterator, class OutputIterator, class Predicate >
     203void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) {
     204        while ( begin++ != end )
     205                if ( pred(*begin) ) *out++ = *begin;
     206
     207        return;
     208}
     209
     210template< class InputIterator1, class InputIterator2, class OutputIterator >
     211void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
     212        while ( b1 != e1 && b2 != e2 )
     213                *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++);
    180214}
    181215
     
    187221
    188222// 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;
     223template< typename Iterator >
     224Iterator operator+(Iterator i, int inc) {
     225        while ( inc > 0 ) {
     226                ++i;
     227                --inc;
     228        }
     229        return i;
    193230}
    194231
     
    209246        warn_single( params... );
    210247}
    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.
    216248
    217249template< typename ThisType >
     
    233265std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
    234266
    235 // -----------------------------------------------------------------------------
    236267// RAII object to regulate "save and restore" behaviour, e.g.
    237268// void Foo::bar() {
     
    248279};
    249280
    250 // -----------------------------------------------------------------------------
    251 // Helper struct and function to support
    252 // for ( val : reverseIterate( container ) ) {}
    253 // syntax to have a for each that iterates backwards
    254 
    255281template< typename T >
    256282struct reverseIterate_t {
Note: See TracChangeset for help on using the changeset viewer.