source: src/Common/utility.h@ b6fe7e6

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 b6fe7e6 was 940bba3, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

made some minor cleanup in utility.h

  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[51587aa]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//
[60089f4]7// utility.h --
[51587aa]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[e04ef3a]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Jun 8 17:33:59 2016
13// Update Count : 22
[51587aa]14//
[01aeade]15
16#ifndef _UTILITY_H
17#define _UTILITY_H
[51b73452]18
[e491159]19#include <cctype>
[940bba3]20#include <algorithm>
[51b73452]21#include <iostream>
22#include <iterator>
23#include <list>
[e491159]24#include <memory>
25#include <sstream>
26#include <string>
[51b73452]27
28template< typename T >
[01aeade]29static inline T * maybeClone( const T *orig ) {
30 if ( orig ) {
31 return orig->clone();
32 } else {
33 return 0;
34 } // if
[51b73452]35}
36
[e04ef3a]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
[51b73452]48template< typename T, typename U >
[01aeade]49static inline T * maybeBuild( const U *orig ) {
[e04ef3a]50 return maybeBuild_t<T,U>::doit(orig);
[51b73452]51}
52
[7ecbb7e]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
[51b73452]60template< typename Input_iterator >
[01aeade]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
[51b73452]65}
66
67template< typename Container >
[01aeade]68void deleteAll( Container &container ) {
69 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
70 delete *i;
71 } // for
[51b73452]72}
73
74template< typename Container >
[01aeade]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 ) {
[cf0941d]78 os << std::string( indent, ' ' );
[01aeade]79 (*i)->print( os, indent + 2 );
[60089f4]80 // need an endl after each element because it's not easy to know when each individual item should end
[01aeade]81 os << std::endl;
82 } // if
83 } // for
[51b73452]84}
85
86template< typename SrcContainer, typename DestContainer >
[01aeade]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
[51b73452]93}
94
95template< typename Container >
[01aeade]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
[2871210]105static inline std::string assign_strptr( const std::string *str ) {
[01aeade]106 if ( str == 0 ) {
107 return "";
108 } else {
109 std::string tmp;
110 tmp = *str;
111 delete str;
112 return tmp;
113 } // if
[51b73452]114}
115
116template < typename T >
[01aeade]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
[51b73452]122}
123
124template < typename T >
125std::list<T> flatten( std::list < std::list<T> > l) {
[01aeade]126 typedef std::list <T> Ts;
[51b73452]127
[01aeade]128 Ts ret;
[51b73452]129
[a08ba92]130 switch ( l.size() ) {
[01aeade]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
[51b73452]140}
141
[60089f4]142template < typename T >
[79970ed]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 ) {
[5f2f2d7]155 std::ostringstream os;
[79970ed]156 toString_single( os, params... );
[5f2f2d7]157 return os.str();
[51b73452]158}
159
160template< typename T >
161void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
[01aeade]162 typename std::list< T >::iterator next = pos; advance( next, 1 );
[51b73452]163
[01aeade]164 //if ( next != org.end() ) {
[a08ba92]165 org.erase( pos );
166 org.splice( next, with );
167 //}
[51b73452]168
[01aeade]169 return;
[51b73452]170}
171
[940bba3]172template< typename... Args >
173auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
174 return std::copy_if(std::forward<Args>(args)...);
[51b73452]175}
176
[940bba3]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);
[51b73452]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 ) {
[01aeade]184 while ( b1 != e1 && b2 != e2 )
185 *out++ = func(*b1++, *b2++);
[51b73452]186}
187
[d939274]188// it's nice to actually be able to increment iterators by an arbitrary amount
[940bba3]189template< class InputIt, class Distance >
190InputIt operator+( InputIt it, Distance n ) {
191 advance(it, n);
192 return it;
[d939274]193}
194
[79970ed]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
[940bba3]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
[e491159]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
[1f75e2d]232template< typename ThisType >
233std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
234
[940bba3]235// -----------------------------------------------------------------------------
[c8dfcd3]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
[940bba3]250// -----------------------------------------------------------------------------
251// Helper struct and function to support
252// for ( val : reverseIterate( container ) ) {}
253// syntax to have a for each that iterates backwards
254
[44f6341]255template< typename T >
[1ba88a0]256struct reverseIterate_t {
[44f6341]257 T& ref;
258
[1ba88a0]259 reverseIterate_t( T & ref ) : ref(ref) {}
[44f6341]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 >
[1ba88a0]267reverseIterate_t< T > reverseIterate( T & ref ) {
268 return reverseIterate_t< T >( ref );
[44f6341]269}
270
[01aeade]271#endif // _UTILITY_H
[51b73452]272
[51587aa]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.