source: translator/Common/utility.h@ 643a2e1

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 643a2e1 was 51b73452, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

initial commit

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * This file is part of the Cforall project
3 *
4 * Some useful template utility functions
5 *
6 * $Id: utility.h,v 1.17 2003/11/26 18:05:21 rgesteve Exp $
7 *
8 */
9
10#ifndef COMMON_UTILITY_H
11#define COMMON_UTILITY_H
12
13#include <iostream>
14#include <strstream>
15#include <iterator>
16#include <string>
17#include <cctype>
18#include <list>
19
20template< typename T >
21static inline T*
22maybeClone( const T *orig )
23{
24 if( orig ) {
25 return orig->clone();
26 } else {
27 return 0;
28 }
29}
30
31template< typename T, typename U >
32static inline T*
33maybeBuild( const U *orig )
34{
35 if( orig ) {
36 return orig->build();
37 } else {
38 return 0;
39 }
40}
41
42template< typename Input_iterator >
43void
44printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os )
45{
46 for( Input_iterator i = begin; i != end; ++i ) {
47 os << name_array[ *i ] << ' ';
48 }
49}
50
51template< typename Container >
52void
53deleteAll( Container &container )
54{
55 for( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
56 delete *i;
57 }
58}
59
60template< typename Container >
61void
62printAll( const Container &container, std::ostream &os, int indent = 0 )
63{
64 for( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
65 if( *i ) {
66 os << std::string(indent, ' ');
67 (*i)->print( os, indent + 2 );
68 os << std::endl;
69 }
70 }
71}
72
73template< typename SrcContainer, typename DestContainer >
74void
75cloneAll( const SrcContainer &src, DestContainer &dest )
76{
77 typename SrcContainer::const_iterator in = src.begin();
78 std::back_insert_iterator< DestContainer > out( dest );
79 while( in != src.end() ) {
80 *out++ = (*in++)->clone();
81 }
82}
83
84template< typename Container >
85void
86assertAll( const Container &container )
87{
88 int count = 0;
89 for( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
90 if( !(*i) ) {
91 std::cerr << count << " is null" << std::endl;
92 }
93 }
94}
95
96static inline std::string
97assign_strptr( std::string *str )
98{
99 if( str == 0 ) {
100 return "";
101 } else {
102 std::string tmp;
103 tmp = *str;
104 delete str;
105 return tmp;
106 }
107}
108
109template< class T, typename ResultType, ResultType (T::* memfunc)() >
110ResultType dispatch(T *pT){
111 return (pT->*memfunc)();
112}
113
114template < typename T >
115std::list<T> tail( std::list<T> l )
116{
117 if(! l.empty()){
118 std::list<T> ret(++(l.begin()), l.end());
119 return ret;
120 }
121}
122
123template < typename T >
124std::list<T> flatten( std::list < std::list<T> > l) {
125 typedef std::list <T> Ts;
126
127 Ts ret;
128
129 switch( l.size() ){
130 case 0:
131 return ret;
132 case 1:
133 return l.front();
134 default:
135 ret = flatten(tail(l));
136 ret.insert(ret.begin(), l.front().begin(), l.front().end());
137 return ret;
138 }
139}
140
141template < typename T >
142std::string toString ( T value ) {
143 std::ostrstream os;
144
145 os << value; // << std::ends;
146 os.freeze( false );
147
148 return std::string(os.str(), os.pcount());
149}
150
151template< class Constructed, typename Arg >
152Constructed *ctor( Arg arg ) {
153 Constructed *c = new Constructed( arg );
154 return c;
155}
156
157template< class Constructed, typename Arg >
158Constructed ctor_noptr( Arg arg ) {
159 return Constructed( arg );
160}
161
162template< typename T >
163void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
164 // TIter should secretly be a typename std::list< T >::iterator
165 // ( g++ 3.2 issues a 'is implicitly a typename' warning if I make this explicit )
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{
194 while ( begin++ != end )
195 if( pred(*begin) ) *out++ = *begin;
196
197 return;
198}
199
200template< class InputIterator1, class InputIterator2, class OutputIterator >
201void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
202 while( b1 != e1 && b2 != e2 )
203 *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++);
204}
205
206template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
207void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
208 while( b1 != e1 && b2 != e2 )
209 *out++ = func(*b1++, *b2++);
210}
211
212#endif /* #ifndef COMMON_UTILITY_H */
213
214/*
215 Local Variables:
216 mode: c++
217 End:
218*/
Note: See TracBrowser for help on using the repository browser.