source: translator/Common/utility.h @ 51587aa

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 51587aa was 51587aa, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: fourth groups of files

  • Property mode set to 100644
File size: 5.3 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// XXX.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By :
12// Last Modified On :
13// Update Count     : 0
14//
15/*
16 * This file is part of the Cforall project
17 *
18 * Some useful template utility functions
19 *
20 * $Id: utility.h,v 1.17 2003/11/26 18:05:21 rgesteve Exp $
21 *
22 */
23
24#ifndef COMMON_UTILITY_H
25#define COMMON_UTILITY_H
26
27#include <iostream>
28#include <strstream>
29#include <iterator>
30#include <string>
31#include <cctype>
32#include <list>
33
34template< typename T >
35static inline T*
36maybeClone( const T *orig )
37{
38  if ( orig ) {
39    return orig->clone();
40  } else {
41    return 0;
42  }
43}
44
45template< typename T, typename U >
46static inline T*
47maybeBuild( const U *orig )
48{
49  if ( orig ) {
50    return orig->build();
51  } else {
52    return 0;
53  }
54}
55
56template< typename Input_iterator >
57void
58printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os )
59{
60  for ( Input_iterator i = begin; i != end; ++i ) {
61    os << name_array[ *i ] << ' ';
62  }
63}
64
65template< typename Container >
66void
67deleteAll( Container &container )
68{
69  for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
70    delete *i;
71  }
72}
73
74template< typename Container >
75void
76printAll( const Container &container, std::ostream &os, int indent = 0 )
77{
78  for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
79    if ( *i ) {
80      os << std::string(indent,  ' ');
81      (*i)->print( os, indent + 2 );
82      os << std::endl;
83    }
84  }
85}
86
87template< typename SrcContainer, typename DestContainer >
88void
89cloneAll( const SrcContainer &src, DestContainer &dest )
90{
91  typename SrcContainer::const_iterator in = src.begin();
92  std::back_insert_iterator< DestContainer > out( dest );
93  while ( in != src.end() ) {
94    *out++ = (*in++)->clone();
95  }
96}
97
98template< typename Container >
99void
100assertAll( const Container &container )
101{
102  int count = 0;
103  for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
104    if ( !(*i) ) {
105      std::cerr << count << " is null" << std::endl;
106    }
107  }
108}
109
110static inline std::string
111assign_strptr( std::string *str )
112{
113  if ( str == 0 ) {
114    return "";
115  } else {
116    std::string tmp;
117    tmp = *str;
118    delete str;
119    return tmp;
120  }
121}
122
123template< class T, typename ResultType, ResultType (T::* memfunc)() >
124ResultType dispatch(T *pT){
125  return (pT->*memfunc)();
126}
127
128template < typename T >
129std::list<T> tail( std::list<T> l )
130{
131  if (! l.empty()){
132    std::list<T> ret(++(l.begin()), l.end());
133    return ret;
134  }
135}
136
137template < typename T >
138std::list<T> flatten( std::list < std::list<T> > l) {
139  typedef std::list <T> Ts;
140
141  Ts ret;
142
143  switch ( l.size() ){
144  case 0:
145    return ret;
146  case 1:
147    return l.front();
148  default:
149    ret = flatten(tail(l));
150    ret.insert(ret.begin(), l.front().begin(), l.front().end());
151    return ret;
152  }
153}
154
155template < typename T > 
156std::string toString ( T value ) {
157  std::ostrstream os;
158 
159  os << value; // << std::ends;
160  os.freeze( false );
161
162  return std::string(os.str(), os.pcount());
163}
164
165template< class Constructed, typename Arg >
166Constructed *ctor( Arg arg ) {
167  Constructed *c = new Constructed( arg );
168  return c;
169}
170
171template< class Constructed, typename Arg >
172Constructed ctor_noptr( Arg arg ) {
173  return Constructed( arg );
174}
175
176template< typename T >
177void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
178  // TIter should secretly be a typename std::list< T >::iterator
179  //   ( g++ 3.2 issues a 'is implicitly a typename' warning if I make this explicit )
180   typename std::list< T >::iterator next = pos; advance( next, 1 );
181
182   //if ( next != org.end() ) {
183    org.erase( pos );
184    org.splice( next, with );
185    //}
186
187  return;
188}
189
190template< typename T1, typename T2 >
191T2 *cast_ptr( T1 *from ) {
192  return dynamic_cast< T2 * >( from );
193}
194
195template< class Exception, typename Arg >
196void inline assert_throw( bool pred, Arg arg ) {
197  if (pred) throw Exception( arg );
198}
199
200template< typename T >
201struct is_null_pointer {
202  bool operator()( const T *ptr ){ return ( ptr == 0 ); }
203};
204
205template< class InputIterator, class OutputIterator, class Predicate >
206void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred)
207{
208  while ( begin++ != end )
209    if ( pred(*begin) ) *out++ = *begin;
210
211  return;
212}
213
214template< class InputIterator1, class InputIterator2, class OutputIterator >
215void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
216  while ( b1 != e1 && b2 != e2 )
217    *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++);
218}
219
220template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
221void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
222  while ( b1 != e1 && b2 != e2 )
223    *out++ = func(*b1++, *b2++);
224}
225
226#endif /* #ifndef COMMON_UTILITY_H */
227
228/*
229  Local Variables:
230  mode: c++
231  End:
232*/
233// Local Variables: //
234// tab-width: 4 //
235// mode: c++ //
236// compile-command: "make install" //
237// End: //
Note: See TracBrowser for help on using the repository browser.