source: src/Common/utility.h @ 1b77274

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 1b77274 was 1b77274, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code

  • 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 : Fri Sep 16 19:13:21 2016
13// Update Count     : 26
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#include <cassert>
28
29template< typename T >
30static inline T * maybeClone( const T *orig ) {
31        if ( orig ) {
32                return orig->clone();
33        } else {
34                return 0;
35        } // if
36}
37
38template<typename T, typename U>
39struct maybeBuild_t {
40        static T * doit( const U *orig ) {
41                if ( orig ) {
42                        return orig->build();
43                } else {
44                        return 0;
45                } // if
46        }
47};
48
49template< typename T, typename U >
50static inline T * maybeBuild( const U *orig ) {
51        return maybeBuild_t<T,U>::doit(orig);
52}
53
54template< typename T, typename U >
55static inline T * maybeMoveBuild( const U *orig ) {
56        T* ret = maybeBuild<T>(orig);
57        delete orig;
58        return ret;
59}
60
61template< typename Input_iterator >
62void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
63        for ( Input_iterator i = begin; i != end; ++i ) {
64                os << name_array[ *i ] << ' ';
65        } // for
66}
67
68template< typename Container >
69void deleteAll( Container &container ) {
70        for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
71                delete *i;
72        } // for
73}
74
75template< typename Container >
76void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
77        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
78                if ( *i ) {
79                        os << std::string( indent,  ' ' );
80                        (*i)->print( os, indent + 2 );
81                        // need an endl after each element because it's not easy to know when each individual item should end
82                        os << std::endl;
83                } // if
84        } // for
85}
86
87template< typename SrcContainer, typename DestContainer >
88void cloneAll( const SrcContainer &src, DestContainer &dest ) {
89        typename SrcContainer::const_iterator in = src.begin();
90        std::back_insert_iterator< DestContainer > out( dest );
91        while ( in != src.end() ) {
92                *out++ = (*in++)->clone();
93        } // while
94}
95
96template< typename Container >
97void assertAll( const Container &container ) {
98        int count = 0;
99        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
100                if ( !(*i) ) {
101                        std::cerr << count << " is null" << std::endl;
102                } // if
103        } // for
104}
105
106static inline std::string assign_strptr( const std::string *str ) {
107        std::string tmp( *str );
108        delete str;
109        return tmp;
110}
111
112template < typename T >
113std::list<T> tail( std::list<T> l ) {
114        if ( ! l.empty() ) {
115                std::list<T> ret(++(l.begin()), l.end());
116                return ret;
117        } // if
118}
119
120template < typename T >
121std::list<T> flatten( std::list < std::list<T> > l) {
122        typedef std::list <T> Ts;
123
124        Ts ret;
125
126        switch ( l.size() ) {
127          case 0:
128                return ret;
129          case 1:
130                return l.front();
131          default:
132                ret = flatten(tail(l));
133                ret.insert(ret.begin(), l.front().begin(), l.front().end());
134                return ret;
135        } // switch
136}
137
138template < typename T >
139void toString_single ( std::ostream & os, const T & value ) {
140        os << value;
141}
142
143template < typename T, typename... Params >
144void toString_single ( std::ostream & os, const T & value, const Params & ... params ) {
145        os << value;
146        toString_single( os, params ... );
147}
148
149template < typename ... Params >
150std::string toString ( const Params & ... params ) {
151        std::ostringstream os;
152        toString_single( os, params... );
153        return os.str();
154}
155
156template< typename T >
157void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
158        typename std::list< T >::iterator next = pos; advance( next, 1 );
159
160        //if ( next != org.end() ) {
161        org.erase( pos );
162        org.splice( next, with );
163        //}
164
165        return;
166}
167
168template< typename... Args >
169auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
170  return std::copy_if(std::forward<Args>(args)...);
171}
172
173template< typename... Args >
174auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) {
175  return zipWith(std::forward<Args>(args)..., std::make_pair);
176}
177
178template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
179void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
180        while ( b1 != e1 && b2 != e2 )
181                *out++ = func(*b1++, *b2++);
182}
183
184// it's nice to actually be able to increment iterators by an arbitrary amount
185template< class InputIt, class Distance >
186InputIt operator+( InputIt it, Distance n ) {
187        advance(it, n);
188        return it;
189}
190
191template< typename T >
192void warn_single( const T & arg ) {
193        std::cerr << arg << std::endl;
194}
195
196template< typename T, typename... Params >
197void warn_single(const T & arg, const Params & ... params ) {
198        std::cerr << arg;
199        warn_single( params... );
200}
201
202template< typename... Params >
203void warn( const Params & ... params ) {
204        std::cerr << "Warning: ";
205        warn_single( params... );
206}
207
208// -----------------------------------------------------------------------------
209// Ref Counted Singleton class
210// Objects that inherit from this class will have at most one reference to it
211// but if all references die, the object will be deleted.
212
213template< typename ThisType >
214class RefCountSingleton {
215  public:
216        static std::shared_ptr<ThisType> get() {
217                if( global_instance.expired() ) {
218                        std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>();
219                        global_instance = new_instance;
220                        return std::move(new_instance);
221                }
222                return global_instance.lock();
223        }
224  private:
225        static std::weak_ptr<ThisType> global_instance;
226};
227
228template< typename ThisType >
229std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
230
231// -----------------------------------------------------------------------------
232// RAII object to regulate "save and restore" behaviour, e.g.
233// void Foo::bar() {
234//   ValueGuard<int> guard(var); // var is a member of type Foo
235//   var = ...;
236// } // var's original value is restored
237template< typename T >
238struct ValueGuard {
239        T old;
240        T& ref;
241
242        ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
243        ~ValueGuard() { ref = old; }
244};
245
246// -----------------------------------------------------------------------------
247// Helper struct and function to support
248// for ( val : reverseIterate( container ) ) {}
249// syntax to have a for each that iterates backwards
250
251template< typename T >
252struct reverseIterate_t {
253        T& ref;
254
255        reverseIterate_t( T & ref ) : ref(ref) {}
256
257        typedef typename T::reverse_iterator iterator;
258        iterator begin() { return ref.rbegin(); }
259        iterator end() { return ref.rend(); }
260};
261
262template< typename T >
263reverseIterate_t< T > reverseIterate( T & ref ) {
264        return reverseIterate_t< T >( ref );
265}
266
267#endif // _UTILITY_H
268
269// Local Variables: //
270// tab-width: 4 //
271// mode: c++ //
272// compile-command: "make install" //
273// End: //
Note: See TracBrowser for help on using the repository browser.