source: src/Common/utility.h @ aa8f9df

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since aa8f9df was 940bba3, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

made some minor cleanup in utility.h

  • Property mode set to 100644
File size: 7.0 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// utility.h --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Jun  8 17:33:59 2016
13// Update Count     : 22
14//
15
16#ifndef _UTILITY_H
17#define _UTILITY_H
18
19#include <cctype>
20#include <algorithm>
21#include <iostream>
22#include <iterator>
23#include <list>
24#include <memory>
25#include <sstream>
26#include <string>
27
28template< typename T >
29static inline T * maybeClone( const T *orig ) {
30        if ( orig ) {
31                return orig->clone();
32        } else {
33                return 0;
34        } // if
35}
36
37template<typename T, typename U>
38struct maybeBuild_t {
39        static T * doit( const U *orig ) {
40                if ( orig ) {
41                        return orig->build();
42                } else {
43                        return 0;
44                } // if
45        }
46};
47
48template< typename T, typename U >
49static inline T * maybeBuild( const U *orig ) {
50        return maybeBuild_t<T,U>::doit(orig);
51}
52
53template< typename T, typename U >
54static inline T * maybeMoveBuild( const U *orig ) {
55        T* ret = maybeBuild<T>(orig);
56        delete orig;
57        return ret;
58}
59
60template< typename Input_iterator >
61void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
62        for ( Input_iterator i = begin; i != end; ++i ) {
63                os << name_array[ *i ] << ' ';
64        } // for
65}
66
67template< typename Container >
68void deleteAll( Container &container ) {
69        for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
70                delete *i;
71        } // for
72}
73
74template< typename Container >
75void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
76        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
77                if ( *i ) {
78                        os << std::string( indent,  ' ' );
79                        (*i)->print( os, indent + 2 );
80                        // need an endl after each element because it's not easy to know when each individual item should end
81                        os << std::endl;
82                } // if
83        } // for
84}
85
86template< typename SrcContainer, typename DestContainer >
87void cloneAll( const SrcContainer &src, DestContainer &dest ) {
88        typename SrcContainer::const_iterator in = src.begin();
89        std::back_insert_iterator< DestContainer > out( dest );
90        while ( in != src.end() ) {
91                *out++ = (*in++)->clone();
92        } // while
93}
94
95template< typename Container >
96void assertAll( const Container &container ) {
97        int count = 0;
98        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
99                if ( !(*i) ) {
100                        std::cerr << count << " is null" << std::endl;
101                } // if
102        } // for
103}
104
105static inline std::string assign_strptr( const std::string *str ) {
106        if ( str == 0 ) {
107                return "";
108        } else {
109                std::string tmp;
110                tmp = *str;
111                delete str;
112                return tmp;
113        } // if
114}
115
116template < typename T >
117std::list<T> tail( std::list<T> l ) {
118        if ( ! l.empty() ) {
119                std::list<T> ret(++(l.begin()), l.end());
120                return ret;
121        } // if
122}
123
124template < typename T >
125std::list<T> flatten( std::list < std::list<T> > l) {
126        typedef std::list <T> Ts;
127
128        Ts ret;
129
130        switch ( l.size() ) {
131          case 0:
132                return ret;
133          case 1:
134                return l.front();
135          default:
136                ret = flatten(tail(l));
137                ret.insert(ret.begin(), l.front().begin(), l.front().end());
138                return ret;
139        } // switch
140}
141
142template < typename T >
143void toString_single ( std::ostream & os, const T & value ) {
144        os << value;
145}
146
147template < typename T, typename... Params >
148void toString_single ( std::ostream & os, const T & value, const Params & ... params ) {
149        os << value;
150        toString_single( os, params ... );
151}
152
153template < typename ... Params >
154std::string toString ( const Params & ... params ) {
155        std::ostringstream os;
156        toString_single( os, params... );
157        return os.str();
158}
159
160template< typename T >
161void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
162        typename std::list< T >::iterator next = pos; advance( next, 1 );
163
164        //if ( next != org.end() ) {
165        org.erase( pos );
166        org.splice( next, with );
167        //}
168
169        return;
170}
171
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);
180}
181
182template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
183void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
184        while ( b1 != e1 && b2 != e2 )
185                *out++ = func(*b1++, *b2++);
186}
187
188// it's nice to actually be able to increment iterators by an arbitrary amount
189template< class InputIt, class Distance >
190InputIt operator+( InputIt it, Distance n ) {
191        advance(it, n);
192        return it;
193}
194
195template< typename T >
196void warn_single( const T & arg ) {
197        std::cerr << arg << std::endl;
198}
199
200template< typename T, typename... Params >
201void warn_single(const T & arg, const Params & ... params ) {
202        std::cerr << arg;
203        warn_single( params... );
204}
205
206template< typename... Params >
207void warn( const Params & ... params ) {
208        std::cerr << "Warning: ";
209        warn_single( params... );
210}
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
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// -----------------------------------------------------------------------------
236// RAII object to regulate "save and restore" behaviour, e.g.
237// void Foo::bar() {
238//   ValueGuard<int> guard(var); // var is a member of type Foo
239//   var = ...;
240// } // var's original value is restored
241template< typename T >
242struct ValueGuard {
243        T old;
244        T& ref;
245
246        ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
247        ~ValueGuard() { ref = old; }
248};
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
255template< typename T >
256struct reverseIterate_t {
257        T& ref;
258
259        reverseIterate_t( T & ref ) : ref(ref) {}
260
261        typedef typename T::reverse_iterator iterator;
262        iterator begin() { return ref.rbegin(); }
263        iterator end() { return ref.rend(); }
264};
265
266template< typename T >
267reverseIterate_t< T > reverseIterate( T & ref ) {
268        return reverseIterate_t< T >( ref );
269}
270
271#endif // _UTILITY_H
272
273// Local Variables: //
274// tab-width: 4 //
275// mode: c++ //
276// compile-command: "make install" //
277// End: //
Note: See TracBrowser for help on using the repository browser.