~c-e-pidcott/maus/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/** This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
 *
 *  MAUS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MAUS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef _SRC_LEGACY_INTERFACE_STLUTILS_HH_
#define _SRC_LEGACY_INTERFACE_STLUTILS_HH_

#include <stdlib.h>

#include <string>
#include <sstream>
#include <iomanip>

/** STLUtils namespace contains useful utility functions for the STL.
 *
 *  There are a few things one would like to do for Standard Template Library
 *  (STL) objects - for example, equality tests on STL containers, nicely
 *  formatted print function for STL containers, etc. They're defined here.
 */

namespace STLUtils {
/** Return a == b if a and b are same size and a[i] == b[i] for all i.
 * 
 *  The following operations must be defined for TEMP_ITER it:
 *    - ++it prefix increment operator
 *    - (*it) (that is unary *, i.e. dereference operator)
 *    - it1 != it2 not equals operator
 *    - (*it1) != (*it2) not equals operator of dereferenced object
 * 
 *  Call like e.g. \n
 *      std::vector<int> a,b;\n
 *      bool test_equal = IterableEquality(a.begin(), a.end(), b.begin(),
 *                        b.end());\n
 * 
 *  Can give a segmentation fault if a.begin() is not between a.begin() and
 *  a.end() (inclusive)
 */
template <class TEMP_ITER>
bool IterableEquality(TEMP_ITER a_begin, TEMP_ITER a_end,
                      TEMP_ITER b_begin, TEMP_ITER b_end);


/** Return a == b if a and b are same size and a[i] == b[i] for all i.
 * 
 *  This should work for STL containers of c-type objects. If you want to use
 *  this function for STL containers of custom objects, you will need to define
 *  != operator for your custom object. If you want to use this function for
 *  custom containers of custom objects, you will need to define all of the
 *  functions described here.
 * 
 *  Call like e.g. \n
 *      std::vector<int> a,b;\n
 *      bool test_equal = IterableEquality(a, b);\n
 * 
 *  Specifically, the following operations must be defined for TEMP_CLASS c:
 *    - c.begin() which returns an iterator object, here denoted as type
 *      TEMP_ITER
 *    - c.end() which returns an iterator object, here denoted as type
 *      TEMP_ITER
 * 
 *  The following operations must be defined for container iterator TEMP_ITER
 *  it:
 *    - ++it prefix increment operator
 *    - (*it) (that is unary *, i.e. dereference operator) that returns the
 *      contained object, here denoted as TEMP_OBJ
 *    - it1 != it2 not equals operator
 * 
 *  The following operations must be defined for contained object TEMP_OBJ obj:
 *    - obj != obj not equals operator of dereferenced object
 */
template <class TEMP_CLASS>
inline bool IterableEquality(const TEMP_CLASS& a, const TEMP_CLASS& b);

/**  Convert value to a std::string.
 *
 *  Convert from Temp type to a string type. If the Temp type is a floating
 *  point then precision sets the std::precision that will be used in the
 *  conversion.
 * 
 *  The following operations must be defined for TEMP_CLASS
 *    - std::ostream& operator<<(Temp, std::ostream&)
 */ 
template <class TEMP_CLASS>
std::string ToStringP(TEMP_CLASS value, int precision);

/** Convert value to a std::string.

 *  Convert from Temp type to a string type.
 * 
 *  The following operations must be defined for TEMP_CLASS
 *    - std::ostream& operator<<(Temp, std::ostream&)
 */ 
template <class TEMP_CLASS>
std::string ToString(TEMP_CLASS value);

/** Find and replace environment variables in a string
 *
 *  Search through a string looking for environment variables with format like
 *  "my_${ENV_VARIABLE}_string". Replace the ${ENV_VARIABLE} with the value of
 *  the environment variable. Throw a Squeal if the environment variable could
 *  not be found.
 */
std::string ReplaceVariables(std::string fileName);
}  // STLUtils namespace end

// Function definitions for inline and templates
namespace STLUtils {
template <class TEMP_CLASS>
bool IterableEquality(const TEMP_CLASS& a, const TEMP_CLASS& b) {
  return IterableEquality(a.begin(), a.end(), b.begin(), b.end());
}

template <class TEMP_ITER>
bool IterableEquality(TEMP_ITER a_begin, TEMP_ITER a_end, TEMP_ITER b_begin,
                      TEMP_ITER b_end) {
  TEMP_ITER a_it = a_begin;
  TEMP_ITER b_it = b_begin;
  while (a_it != a_end && b_it != b_end) {
    if (*a_it != *b_it) return false;
    ++a_it;
    ++b_it;
  }
  if ( a_it != a_end || b_it != b_end ) return false;
  return true;
}

template <class TEMP_CLASS> std::string ToStringP
                                             (TEMP_CLASS value, int precision) {
  std::stringstream ss;
  ss << std::setprecision(precision);
  ss << value;
  return ss.str();
}


template <class TEMP_CLASS> std::string ToString
                                             (TEMP_CLASS value) {
  std::stringstream ss;
  ss << value;
  return ss.str();
}
}

/**  A macro to disallow the copy constructor and operator= functions
 *
 *  The idea is that most classes don't want copy ctor and assignment
 *  operator - by making them private we force the compiler to try to link
 *  against the private function - which fails. This should be used in the
 *  private: declarations for all classes wher copy constructor and assignment
 *  operator are not defined.
 * 
 *  Summary - put this in private: declaration for all classes like
 *  #include "Interface/include/STLUtils.hh"
 *  class Foo {
 *   public:
 *    Foo();
 *    ~Foo();
 *  private:
 *    DISALLOW_COPY_AND_ASSIGN(Foo);
 *  };
 */
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&); \
  void operator=(const TypeName&)

#endif