source: src/Common/utility.h@ aefcc3b

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 aefcc3b was 3c13c03, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

expand TupleExpr and TupleIndexExpr, add UniqueExpr

  • 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 <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
160// replace element of list with all elements of another list
161template< typename T >
162void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
163 typename std::list< T >::iterator next = pos; advance( next, 1 );
164
165 //if ( next != org.end() ) {
166 org.erase( pos );
167 org.splice( next, with );
168 //}
169
170 return;
171}
172
173// replace range of a list with a single element
174template< typename T >
175void replace( std::list< T > &org, typename std::list< T >::iterator begin, typename std::list< T >::iterator end, const T & with ) {
176 org.insert( begin, with );
177 org.erase( begin, end );
178}
179
180template< typename... Args >
181auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
182 return std::copy_if(std::forward<Args>(args)...);
183}
184
185template< typename... Args >
186auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) {
187 return zipWith(std::forward<Args>(args)..., std::make_pair);
188}
189
190template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
191void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
192 while ( b1 != e1 && b2 != e2 )
193 *out++ = func(*b1++, *b2++);
194}
195
196// it's nice to actually be able to increment iterators by an arbitrary amount
197template< class InputIt, class Distance >
198InputIt operator+( InputIt it, Distance n ) {
199 advance(it, n);
200 return it;
201}
202
203template< typename T >
204void warn_single( const T & arg ) {
205 std::cerr << arg << std::endl;
206}
207
208template< typename T, typename... Params >
209void warn_single(const T & arg, const Params & ... params ) {
210 std::cerr << arg;
211 warn_single( params... );
212}
213
214template< typename... Params >
215void warn( const Params & ... params ) {
216 std::cerr << "Warning: ";
217 warn_single( params... );
218}
219
220// -----------------------------------------------------------------------------
221// Ref Counted Singleton class
222// Objects that inherit from this class will have at most one reference to it
223// but if all references die, the object will be deleted.
224
225template< typename ThisType >
226class RefCountSingleton {
227 public:
228 static std::shared_ptr<ThisType> get() {
229 if( global_instance.expired() ) {
230 std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>();
231 global_instance = new_instance;
232 return std::move(new_instance);
233 }
234 return global_instance.lock();
235 }
236 private:
237 static std::weak_ptr<ThisType> global_instance;
238};
239
240template< typename ThisType >
241std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
242
243// -----------------------------------------------------------------------------
244// RAII object to regulate "save and restore" behaviour, e.g.
245// void Foo::bar() {
246// ValueGuard<int> guard(var); // var is a member of type Foo
247// var = ...;
248// } // var's original value is restored
249template< typename T >
250struct ValueGuard {
251 T old;
252 T& ref;
253
254 ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
255 ~ValueGuard() { ref = old; }
256};
257
258// -----------------------------------------------------------------------------
259// Helper struct and function to support
260// for ( val : reverseIterate( container ) ) {}
261// syntax to have a for each that iterates backwards
262
263template< typename T >
264struct reverseIterate_t {
265 T& ref;
266
267 reverseIterate_t( T & ref ) : ref(ref) {}
268
269 typedef typename T::reverse_iterator iterator;
270 iterator begin() { return ref.rbegin(); }
271 iterator end() { return ref.rend(); }
272};
273
274template< typename T >
275reverseIterate_t< T > reverseIterate( T & ref ) {
276 return reverseIterate_t< T >( ref );
277}
278
279#endif // _UTILITY_H
280
281// Local Variables: //
282// tab-width: 4 //
283// mode: c++ //
284// compile-command: "make install" //
285// End: //
Note: See TracBrowser for help on using the repository browser.