source: translator/Common/utility.h @ a32b204

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since a32b204 was a32b204, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: second groups of files

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[51b7345]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{
[a32b204]24  if ( orig ) {
[51b7345]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{
[a32b204]35  if ( orig ) {
[51b7345]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{
[a32b204]46  for ( Input_iterator i = begin; i != end; ++i ) {
[51b7345]47    os << name_array[ *i ] << ' ';
48  }
49}
50
51template< typename Container >
52void
53deleteAll( Container &container )
54{
[a32b204]55  for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
[51b7345]56    delete *i;
57  }
58}
59
60template< typename Container >
61void
62printAll( const Container &container, std::ostream &os, int indent = 0 )
63{
[a32b204]64  for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
65    if ( *i ) {
[51b7345]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 );
[a32b204]79  while ( in != src.end() ) {
[51b7345]80    *out++ = (*in++)->clone();
81  }
82}
83
84template< typename Container >
85void
86assertAll( const Container &container )
87{
88  int count = 0;
[a32b204]89  for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
90    if ( !(*i) ) {
[51b7345]91      std::cerr << count << " is null" << std::endl;
92    }
93  }
94}
95
96static inline std::string
97assign_strptr( std::string *str )
98{
[a32b204]99  if ( str == 0 ) {
[51b7345]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{
[a32b204]117  if (! l.empty()){
[51b7345]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
[a32b204]129  switch ( l.size() ){
[51b7345]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 )
[a32b204]195    if ( pred(*begin) ) *out++ = *begin;
[51b7345]196
197  return;
198}
199
200template< class InputIterator1, class InputIterator2, class OutputIterator >
201void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
[a32b204]202  while ( b1 != e1 && b2 != e2 )
[51b7345]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 ) {
[a32b204]208  while ( b1 != e1 && b2 != e2 )
[51b7345]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.