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

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 4a7d895 was 7ecbb7e, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

fixed memory leaks related to removal of rodolfo's code

  • 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
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 >
147std::string toString ( T value ) {
148        std::ostringstream os;
149        os << value; // << std::ends;
150        return os.str();
151}
152
153template< class Constructed, typename Arg >
154Constructed *ctor( Arg arg ) {
155        Constructed *c = new Constructed( arg );
156        return c;
157}
158
159template< class Constructed, typename Arg >
160Constructed ctor_noptr( Arg arg ) {
161        return Constructed( arg );
162}
163
164template< typename T >
165void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
166        typename std::list< T >::iterator next = pos; advance( next, 1 );
167
168        //if ( next != org.end() ) {
169        org.erase( pos );
170        org.splice( next, with );
171        //}
172
173        return;
174}
175
176template< typename T1, typename T2 >
177T2 *cast_ptr( T1 *from ) {
178        return dynamic_cast< T2 * >( from );
179}
180
181template< class Exception, typename Arg >
182void inline assert_throw( bool pred, Arg arg ) {
183        if (pred) throw Exception( arg );
184}
185
186template< typename T >
187struct is_null_pointer {
188        bool operator()( const T *ptr ) { return ( ptr == 0 ); }
189};
190
191template< class InputIterator, class OutputIterator, class Predicate >
192void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) {
193        while ( begin++ != end )
194                if ( pred(*begin) ) *out++ = *begin;
195
196        return;
197}
198
199template< class InputIterator1, class InputIterator2, class OutputIterator >
200void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
201        while ( b1 != e1 && b2 != e2 )
202                *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++);
203}
204
205template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
206void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
207        while ( b1 != e1 && b2 != e2 )
208                *out++ = func(*b1++, *b2++);
209}
210
211// it's nice to actually be able to increment iterators by an arbitrary amount
212template< typename Iterator >
213Iterator operator+(Iterator i, int inc) {
214        while ( inc > 0 ) {
215                ++i;
216                --inc;
217        }
218        return i;
219}
220
221#endif // _UTILITY_H
222
223// Local Variables: //
224// tab-width: 4 //
225// mode: c++ //
226// compile-command: "make install" //
227// End: //
Note: See TracBrowser for help on using the repository browser.