source: src/Common/utility.h@ 4e7f0f1

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 4e7f0f1 was 5fda7143, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Conflicts:

src/Common/utility.h

  • Property mode set to 100644
File size: 7.3 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 <iostream>
21#include <iterator>
22#include <list>
23#include <memory>
24#include <sstream>
25#include <string>
26
27template< typename T >
28static inline T * maybeClone( const T *orig ) {
29 if ( orig ) {
30 return orig->clone();
31 } else {
32 return 0;
33 } // if
34}
35
36template<typename T, typename U>
37struct maybeBuild_t {
38 static T * doit( const U *orig ) {
39 if ( orig ) {
40 return orig->build();
41 } else {
42 return 0;
43 } // if
44 }
45};
46
47template< typename T, typename U >
48static inline T * maybeBuild( const U *orig ) {
49 return maybeBuild_t<T,U>::doit(orig);
50}
51
52template< typename T, typename U >
53static inline T * maybeMoveBuild( const U *orig ) {
54 T* ret = maybeBuild<T>(orig);
55 delete orig;
56 return ret;
57}
58
59template< typename Input_iterator >
60void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
61 for ( Input_iterator i = begin; i != end; ++i ) {
62 os << name_array[ *i ] << ' ';
63 } // for
64}
65
66template< typename Container >
67void deleteAll( Container &container ) {
68 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
69 delete *i;
70 } // for
71}
72
73template< typename Container >
74void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
75 for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
76 if ( *i ) {
77 os << std::string( indent, ' ' );
78 (*i)->print( os, indent + 2 );
79 // need an endl after each element because it's not easy to know when each individual item should end
80 os << std::endl;
81 } // if
82 } // for
83}
84
85template< typename SrcContainer, typename DestContainer >
86void cloneAll( const SrcContainer &src, DestContainer &dest ) {
87 typename SrcContainer::const_iterator in = src.begin();
88 std::back_insert_iterator< DestContainer > out( dest );
89 while ( in != src.end() ) {
90 *out++ = (*in++)->clone();
91 } // while
92}
93
94template< typename Container >
95void assertAll( const Container &container ) {
96 int count = 0;
97 for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
98 if ( !(*i) ) {
99 std::cerr << count << " is null" << std::endl;
100 } // if
101 } // for
102}
103
104static inline std::string assign_strptr( const std::string *str ) {
105 if ( str == 0 ) {
106 return "";
107 } else {
108 std::string tmp;
109 tmp = *str;
110 delete str;
111 return tmp;
112 } // if
113}
114
115template< class T, typename ResultType, ResultType( T::* memfunc )() >
116ResultType dispatch( T *pT ) {
117 return (pT->*memfunc)();
118}
119
120template < typename T >
121std::list<T> tail( std::list<T> l ) {
122 if ( ! l.empty() ) {
123 std::list<T> ret(++(l.begin()), l.end());
124 return ret;
125 } // if
126}
127
128template < typename T >
129std::list<T> flatten( std::list < std::list<T> > l) {
130 typedef std::list <T> Ts;
131
132 Ts ret;
133
134 switch ( l.size() ) {
135 case 0:
136 return ret;
137 case 1:
138 return l.front();
139 default:
140 ret = flatten(tail(l));
141 ret.insert(ret.begin(), l.front().begin(), l.front().end());
142 return ret;
143 } // switch
144}
145
146template < typename T >
147void toString_single ( std::ostream & os, const T & value ) {
148 os << value;
149}
150
151template < typename T, typename... Params >
152void toString_single ( std::ostream & os, const T & value, const Params & ... params ) {
153 os << value;
154 toString_single( os, params ... );
155}
156
157template < typename ... Params >
158std::string toString ( const Params & ... params ) {
159 std::ostringstream os;
160 toString_single( os, params... );
161 return os.str();
162}
163
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
175template< typename T >
176void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
177 typename std::list< T >::iterator next = pos; advance( next, 1 );
178
179 //if ( next != org.end() ) {
180 org.erase( pos );
181 org.splice( next, with );
182 //}
183
184 return;
185}
186
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++);
214}
215
216template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
217void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
218 while ( b1 != e1 && b2 != e2 )
219 *out++ = func(*b1++, *b2++);
220}
221
222// it's nice to actually be able to increment iterators by an arbitrary amount
223template< typename Iterator >
224Iterator operator+(Iterator i, int inc) {
225 while ( inc > 0 ) {
226 ++i;
227 --inc;
228 }
229 return i;
230}
231
232template< typename T >
233void warn_single( const T & arg ) {
234 std::cerr << arg << std::endl;
235}
236
237template< typename T, typename... Params >
238void warn_single(const T & arg, const Params & ... params ) {
239 std::cerr << arg;
240 warn_single( params... );
241}
242
243template< typename... Params >
244void warn( const Params & ... params ) {
245 std::cerr << "Warning: ";
246 warn_single( params... );
247}
248
249template< typename ThisType >
250class RefCountSingleton {
251 public:
252 static std::shared_ptr<ThisType> get() {
253 if( global_instance.expired() ) {
254 std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>();
255 global_instance = new_instance;
256 return std::move(new_instance);
257 }
258 return global_instance.lock();
259 }
260 private:
261 static std::weak_ptr<ThisType> global_instance;
262};
263
264template< typename ThisType >
265std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
266
267// RAII object to regulate "save and restore" behaviour, e.g.
268// void Foo::bar() {
269// ValueGuard<int> guard(var); // var is a member of type Foo
270// var = ...;
271// } // var's original value is restored
272template< typename T >
273struct ValueGuard {
274 T old;
275 T& ref;
276
277 ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
278 ~ValueGuard() { ref = old; }
279};
280
281template< typename T >
282struct reverseIterate_t {
283 T& ref;
284
285 reverseIterate_t( T & ref ) : ref(ref) {}
286
287 typedef typename T::reverse_iterator iterator;
288 iterator begin() { return ref.rbegin(); }
289 iterator end() { return ref.rend(); }
290};
291
292template< typename T >
293reverseIterate_t< T > reverseIterate( T & ref ) {
294 return reverseIterate_t< T >( ref );
295}
296
297#endif // _UTILITY_H
298
299// Local Variables: //
300// tab-width: 4 //
301// mode: c++ //
302// compile-command: "make install" //
303// End: //
Note: See TracBrowser for help on using the repository browser.