source: src/Common/utility.h @ d30790f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d30790f was 4f147cc, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

fixed some more memory leaks and added safe_dynamic_cast to assert.h

  • Property mode set to 100644
File size: 5.5 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 <iostream>
20#include <sstream>
21#include <iterator>
22#include <string>
23#include <cctype>
24#include <list>
25
26template< typename T >
27static inline T * maybeClone( const T *orig ) {
28        if ( orig ) {
29                return orig->clone();
30        } else {
31                return 0;
32        } // if
33}
34
35template<typename T, typename U>
36struct maybeBuild_t {
37        static T * doit( const U *orig ) {
38                if ( orig ) {
39                        return orig->build();
40                } else {
41                        return 0;
42                } // if
43        }
44};
45
46template< typename T, typename U >
47static inline T * maybeBuild( const U *orig ) {
48        return maybeBuild_t<T,U>::doit(orig);
49}
50
51template< typename T, typename U >
52static inline T * maybeMoveBuild( const U *orig ) {
53        T* ret = maybeBuild<T>(orig);
54        delete orig;
55        return ret;
56}
57
58template< typename Input_iterator >
59void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
60        for ( Input_iterator i = begin; i != end; ++i ) {
61                os << name_array[ *i ] << ' ';
62        } // for
63}
64
65template< typename Container >
66void deleteAll( Container &container ) {
67        for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
68                delete *i;
69        } // for
70}
71
72template< typename Container >
73void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
74        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
75                if ( *i ) {
76                        os << std::string( indent,  ' ' );
77                        (*i)->print( os, indent + 2 );
78                        // need an endl after each element because it's not easy to know when each individual item should end
79                        os << std::endl;
80                } // if
81        } // for
82}
83
84template< typename SrcContainer, typename DestContainer >
85void cloneAll( const SrcContainer &src, DestContainer &dest ) {
86        typename SrcContainer::const_iterator in = src.begin();
87        std::back_insert_iterator< DestContainer > out( dest );
88        while ( in != src.end() ) {
89                *out++ = (*in++)->clone();
90        } // while
91}
92
93template< typename Container >
94void assertAll( const Container &container ) {
95        int count = 0;
96        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
97                if ( !(*i) ) {
98                        std::cerr << count << " is null" << std::endl;
99                } // if
100        } // for
101}
102
103static inline std::string assign_strptr( const std::string *str ) {
104        if ( str == 0 ) {
105                return "";
106        } else {
107                std::string tmp;
108                tmp = *str;
109                delete str;
110                return tmp;
111        } // if
112}
113
114template< class T, typename ResultType, ResultType( T::* memfunc )() >
115ResultType dispatch( T *pT ) {
116        return (pT->*memfunc)();
117}
118
119template < typename T >
120std::list<T> tail( std::list<T> l ) {
121        if ( ! l.empty() ) {
122                std::list<T> ret(++(l.begin()), l.end());
123                return ret;
124        } // if
125}
126
127template < typename T >
128std::list<T> flatten( std::list < std::list<T> > l) {
129        typedef std::list <T> Ts;
130
131        Ts ret;
132
133        switch ( l.size() ) {
134          case 0:
135                return ret;
136          case 1:
137                return l.front();
138          default:
139                ret = flatten(tail(l));
140                ret.insert(ret.begin(), l.front().begin(), l.front().end());
141                return ret;
142        } // switch
143}
144
145template < typename T >
146std::string toString ( T value ) {
147        std::ostringstream os;
148        os << value; // << std::ends;
149        return os.str();
150}
151
152template< class Constructed, typename Arg >
153Constructed *ctor( Arg arg ) {
154        Constructed *c = new Constructed( arg );
155        return c;
156}
157
158template< class Constructed, typename Arg >
159Constructed ctor_noptr( Arg arg ) {
160        return Constructed( arg );
161}
162
163template< typename T >
164void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
165        typename std::list< T >::iterator next = pos; advance( next, 1 );
166
167        //if ( next != org.end() ) {
168        org.erase( pos );
169        org.splice( next, with );
170        //}
171
172        return;
173}
174
175template< typename T1, typename T2 >
176T2 *cast_ptr( T1 *from ) {
177        return dynamic_cast< T2 * >( from );
178}
179
180template< class Exception, typename Arg >
181void inline assert_throw( bool pred, Arg arg ) {
182        if (pred) throw Exception( arg );
183}
184
185template< typename T >
186struct is_null_pointer {
187        bool operator()( const T *ptr ) { return ( ptr == 0 ); }
188};
189
190template< class InputIterator, class OutputIterator, class Predicate >
191void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) {
192        while ( begin++ != end )
193                if ( pred(*begin) ) *out++ = *begin;
194
195        return;
196}
197
198template< class InputIterator1, class InputIterator2, class OutputIterator >
199void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
200        while ( b1 != e1 && b2 != e2 )
201                *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++);
202}
203
204template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
205void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
206        while ( b1 != e1 && b2 != e2 )
207                *out++ = func(*b1++, *b2++);
208}
209
210// it's nice to actually be able to increment iterators by an arbitrary amount
211template< typename Iterator >
212Iterator operator+(Iterator i, int inc) {
213        while ( inc > 0 ) {
214                ++i;
215                --inc;
216        }
217        return i;
218}
219
220#endif // _UTILITY_H
221
222// Local Variables: //
223// tab-width: 4 //
224// mode: c++ //
225// compile-command: "make install" //
226// End: //
Note: See TracBrowser for help on using the repository browser.