[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

utilities.hxx |

Go to the documentation of this file.

1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2002 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36
37 #ifndef VIGRA_BASICS_HXX
38 #define VIGRA_BASICS_HXX
39
40 #include "config.hxx"
41 #include "error.hxx"
42 #include "metaprogramming.hxx"
43 #include "tuple.hxx"
44 #include "diff2d.hxx"
45 #include "mathutil.hxx"
46 #include <string>
47 #include <sstream>
48 #include <cctype>
49
50 /*! \file */
51
52 namespace vigra {
53
54 /** Convert a value to a string. Available for integral and floating point types
55 and void *.
56 */
57 doxygen_overloaded_function(template <class T> std::string asString(T t))
58
59 #define VIGRA_AS_STRING(T) \
60 inline std::string asString(T t) \
61 { \
62 std::stringstream s; \
63 s << t; \
64 return s.str(); \
65 }
66
67 VIGRA_AS_STRING(bool)
68 VIGRA_AS_STRING(signed char)
69 VIGRA_AS_STRING(unsigned char)
70 VIGRA_AS_STRING(signed short)
71 VIGRA_AS_STRING(unsigned short)
72 VIGRA_AS_STRING(signed long)
73 VIGRA_AS_STRING(unsigned long)
74 VIGRA_AS_STRING(signed long long)
75 VIGRA_AS_STRING(unsigned long long)
76 VIGRA_AS_STRING(signed int)
77 VIGRA_AS_STRING(unsigned int)
78 VIGRA_AS_STRING(float)
79 VIGRA_AS_STRING(double)
80 VIGRA_AS_STRING(long double)
81 VIGRA_AS_STRING(void *)
82
83 #undef VIGRA_AS_STRING
84
85 template <class T>
86 std::string operator<<(std::string const & s, T const & t)
87 {
88 std::stringstream ss;
89 ss << t;
90 return s + ss.str();
91 }
92
93 /** Convert string to lower case.
94 */
95 inline std::string tolower(std::string s)
96 {
97 for(unsigned int k=0; k<s.size(); ++k)
98 s[k] = (std::string::value_type)std::tolower(s[k]);
99 return s;
100 }
101
102 inline std::string tolower(const char * s)
103 {
104 return tolower(std::string(s));
105 }
106
107 /** Convert string to lower case and remove any white space characters.
108 */
109 inline std::string normalizeString(std::string const & s)
110 {
111 std::string res;
112
113 for(unsigned int k=0; k<s.size(); ++k)
114 {
115 if(std::isspace(s[k]))
116 continue;
117 res += (std::string::value_type)std::tolower(s[k]);
118 }
119 return res;
120 }
121
122 inline std::string normalizeString(const char * s)
123 {
124 return normalizeString(std::string(s));
125 }
126
127 namespace detail {
128
129 template <class T>
130 struct FinallyImpl
131 {
132 T & destructor_;
133
134 FinallyImpl(T & destructor)
135 : destructor_(destructor)
136 {}
137
138 ~FinallyImpl()
139 {
140 destructor_();
141 }
142 };
143
144 } // namespace detail
145
146 } // namespace vigra
147
148 #define VIGRA_TOKEN_PASTE_IMPL(x, y) x##y
149 #define VIGRA_TOKEN_PASTE(x, y) VIGRA_TOKEN_PASTE_IMPL(x, y)
150
151 #define VIGRA_FINALLY_IMPL(destructor, counter) \
152 auto VIGRA_TOKEN_PASTE(_vigra_finally_impl_, counter) = [&]() { destructor; }; \
153 ::vigra::detail::FinallyImpl<decltype(VIGRA_TOKEN_PASTE(_vigra_finally_impl_, counter))> \
154 VIGRA_TOKEN_PASTE(_vigra_finally_, counter)(VIGRA_TOKEN_PASTE(_vigra_finally_impl_, counter))
155
156 /** Emulate the 'finally' keyword as known from Python and other languages.
157
158 This macro improves upon the famous
159 <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">Resource Acquisition Is Initialization</a> idiom, where a resource (e.g. heap memory or a mutex) is automatically free'ed when program execution leaves the current scope. Normally, this is implemented by calling a suitable function in the destructor of a dedicated helper class (e.g. <tt>std::unique_ptr</tt> or <tt>std::lock_guard<std::mutex></tt>).
160
161 Traditionally, a separate helper class has to be implemented for each kind of resource to be handled. In contrast, the macro <tt>VIGRA_FINALLY</tt> creates such a class on the fly by means of an embedded lambda expression.
162
163 <b>Usage:</b>
164
165 <b>\#include</b> <vigra/utilities.hxx><br/>
166
167 \code
168 std::mutex my_mutex;
169 ...
170 {
171 // the following two lines are equivalent to
172 // std::unique_ptr<std::string> my_string = new std::string("foo");
173 std::string * my_string = new std::string("foo");
174 VIGRA_FINALLY(delete my_string);
175
176 // the following two lines are equivalent to
177 // std::lock_guard<std::mutex> lock(my_mutex);
178 my_mutex.lock();
179 VIGRA_FINALLY(my_mutex.unlock());
180
181 ...
182 }
183 // the string has been deallocated and the mutex is unlocked
184 \endcode
185
186 You can pass any code to this macro. Multiple statements must be enclosed in braces as usual. Arbitrary many calls to <tt>VIGRA_FINALLY</tt> can be placed in the same scope. Their actions will be executed in the reversed order of declaration:
187
188 \code
189 int i = 0;
190 ...
191 {
192 VIGRA_FINALLY({ // execute multiple statements
193 i = i*i;
194 ++i;
195 });
196
197 VIGRA_FINALLY( i += 2 ); // this executes first
198
199 assert(i == 0); // as yet, nothing happend
200 }
201 assert(i == 5); // 'finally' code was executed in reversed order at end-of-scope
202 \endcode
203
204 This idea was popularized by Marko Tintor in "<a href="http://blog.memsql.com/c-error-handling-with-auto/">The Auto Macro: A Clean Approach to C++ Error Handling</a>".
205 */
206 #define VIGRA_FINALLY(destructor) \
207 VIGRA_FINALLY_IMPL(destructor, __COUNTER__)
208
209 namespace std {
210
211 template <class T1, class T2>
212 ostream & operator<<(ostream & s, std::pair<T1, T2> const & p)
213 {
214 s << "(" << p.first << ", " << p.second << ")";
215 return s;
216 }
217
218 }
219
220 /** \page Utilities Utilities
221 Basic helper functionality needed throughout.
222
223 <UL style="list-style-image:url(documents/bullet.gif)">
224 <LI> \ref vigra::ArrayVector
225 <BR>&nbsp;&nbsp;&nbsp;<em>replacement for std::vector (always uses consecutive memory)</em>
226 <LI> \ref vigra::Any
227 <BR>&nbsp;&nbsp;&nbsp;<em>typesafe storage of arbitrary values</em>
228 <LI> \ref vigra::BucketQueue and \ref vigra::MappedBucketQueue
229 <BR>&nbsp;&nbsp;&nbsp;<em>efficient priority queues for integer priorities</em>
230 <LI> \ref RangesAndPoints
231 <BR>&nbsp;&nbsp;&nbsp;<em>2-D and N-D positions, extents, and boxes</em>
232 <LI> \ref PixelNeighborhood
233 <BR>&nbsp;&nbsp;&nbsp;<em>4- and 8-neighborhood definitions and circulators</em>
234 <LI> \ref VoxelNeighborhood
235 <BR>&nbsp;&nbsp;&nbsp;<em>6- and 26-neighborhood definitions and circulators</em>
236 <LI> \ref vigra::IteratorAdaptor
237 <BR>&nbsp;&nbsp;&nbsp;<em>Quickly create STL-compatible 1D iterator adaptors</em>
238 <LI> \ref TupleTypes
239 <BR>&nbsp;&nbsp;&nbsp;<em>pair, triple, tuple4, tuple5</em>
240 <LI> \ref MathConstants
241 <BR>&nbsp;&nbsp;&nbsp;<em>M_PI, M_SQRT2</em>
242 <LI> \ref TimingMacros
243 <BR>&nbsp;&nbsp;&nbsp;<em>Macros for taking execution speed measurements</em>
244 <LI> \ref VIGRA_FINALLY
245 <BR>&nbsp;&nbsp;&nbsp;<em>Emulation of the 'finally' keyword from Python</em>
246 </UL>
247 */
248
249 #endif // VIGRA_BASICS_HXX
vigra::tolower
std::string tolower(std::string s)
Definition: utilities.hxx:95
std
Definition: array_vector.hxx:954
vigra
Definition: accessor.hxx:43
vigra::doxygen_overloaded_function
doxygen_overloaded_function(template<... > void separableConvolveBlockwise) template< unsigned int N
Separated convolution on ChunkedArrays.
vigra::asString
std::string asString(T t)(...)
vigra::normalizeString
std::string normalizeString(std::string const &s)
Definition: utilities.hxx:109

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.11.0