Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-2.4.4/include/armadillo_bits/format_wrap.hpp @ 0:8b6102e2a9b0
Armadillo Library
author | maxzanoni76 <max.zanoni@eecs.qmul.ac.uk> |
---|---|
date | Wed, 11 Apr 2012 09:27:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8b6102e2a9b0 |
---|---|
1 // Copyright (C) 2008-2010 NICTA (www.nicta.com.au) | |
2 // Copyright (C) 2008-2010 Conrad Sanderson | |
3 // | |
4 // This file is part of the Armadillo C++ library. | |
5 // It is provided without any warranty of fitness | |
6 // for any purpose. You can redistribute this file | |
7 // and/or modify it under the terms of the GNU | |
8 // Lesser General Public License (LGPL) as published | |
9 // by the Free Software Foundation, either version 3 | |
10 // of the License or (at your option) any later version. | |
11 // (see http://www.opensource.org/licenses for more info) | |
12 | |
13 | |
14 //! \addtogroup format_wrap | |
15 //! @{ | |
16 | |
17 | |
18 //! \namespace arma_boost namespace for functions and classes which partially emulate Boost functionality | |
19 namespace arma_boost | |
20 { | |
21 | |
22 #if defined(ARMA_USE_BOOST_FORMAT) | |
23 | |
24 using boost::format; | |
25 using boost::basic_format; | |
26 using boost::str; | |
27 | |
28 #else | |
29 | |
30 #if defined(ARMA_HAVE_STD_SNPRINTF) | |
31 | |
32 #define arma_snprintf std::snprintf | |
33 | |
34 #else | |
35 | |
36 // better-than-nothing emulation of C99 snprintf(), | |
37 // with correct return value and null-terminated output string. | |
38 // note that _snprintf() provided by MS is not a good substitute for snprintf() | |
39 | |
40 inline | |
41 int | |
42 arma_snprintf(char* out, size_t size, const char* fmt, ...) | |
43 { | |
44 size_t i; | |
45 | |
46 for(i=0; i<size; ++i) | |
47 { | |
48 out[i] = fmt[i]; | |
49 if(fmt[i] == char(0)) | |
50 break; | |
51 } | |
52 | |
53 if(size > 0) | |
54 out[size-1] = char(0); | |
55 | |
56 return int(i); | |
57 } | |
58 | |
59 #endif | |
60 | |
61 class format | |
62 { | |
63 public: | |
64 | |
65 format(const char* in_fmt) | |
66 : A(in_fmt) | |
67 { | |
68 } | |
69 | |
70 format(const std::string& in_fmt) | |
71 : A(in_fmt) | |
72 { | |
73 } | |
74 | |
75 | |
76 const std::string A; | |
77 | |
78 private: | |
79 format(); | |
80 }; | |
81 | |
82 | |
83 | |
84 template<typename T1, typename T2> | |
85 class basic_format | |
86 { | |
87 public: | |
88 | |
89 basic_format(const T1& in_A, const T2& in_B) | |
90 : A(in_A) | |
91 , B(in_B) | |
92 { | |
93 } | |
94 | |
95 const T1& A; | |
96 const T2& B; | |
97 | |
98 private: | |
99 basic_format(); | |
100 }; | |
101 | |
102 | |
103 | |
104 template<typename T2> | |
105 inline | |
106 basic_format< format, T2 > | |
107 operator% (const format& X, const T2& arg) | |
108 { | |
109 return basic_format< format, T2 >(X, arg); | |
110 } | |
111 | |
112 | |
113 | |
114 template<typename T1, typename T2, typename T3> | |
115 inline | |
116 basic_format< basic_format<T1,T2>, T3 > | |
117 operator% (const basic_format<T1,T2>& X, const T3& arg) | |
118 { | |
119 return basic_format< basic_format<T1,T2>, T3 >(X, arg); | |
120 } | |
121 | |
122 | |
123 | |
124 template<typename T2> | |
125 inline | |
126 std::string | |
127 str(const basic_format< format, T2>& X) | |
128 { | |
129 char local_buffer[1024]; | |
130 char* buffer = local_buffer; | |
131 | |
132 int buffer_size = 1024; | |
133 int required_size = buffer_size; | |
134 | |
135 bool using_local_buffer = true; | |
136 | |
137 std::string out; | |
138 | |
139 do | |
140 { | |
141 if(using_local_buffer == false) | |
142 { | |
143 buffer = new char[buffer_size]; | |
144 } | |
145 | |
146 required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.c_str(), X.B); | |
147 | |
148 if(required_size < buffer_size) | |
149 { | |
150 if(required_size > 0) | |
151 { | |
152 out = buffer; | |
153 } | |
154 } | |
155 else | |
156 { | |
157 buffer_size *= 2; | |
158 } | |
159 | |
160 if(using_local_buffer == true) | |
161 { | |
162 using_local_buffer = false; | |
163 } | |
164 else | |
165 { | |
166 delete[] buffer; | |
167 } | |
168 | |
169 } while( (required_size >= buffer_size) ); | |
170 | |
171 return out; | |
172 } | |
173 | |
174 | |
175 | |
176 template<typename T2, typename T3> | |
177 inline | |
178 std::string | |
179 str(const basic_format< basic_format< format, T2>, T3>& X) | |
180 { | |
181 char local_buffer[1024]; | |
182 char* buffer = local_buffer; | |
183 | |
184 int buffer_size = 1024; | |
185 int required_size = buffer_size; | |
186 | |
187 bool using_local_buffer = true; | |
188 | |
189 std::string out; | |
190 | |
191 do | |
192 { | |
193 if(using_local_buffer == false) | |
194 { | |
195 buffer = new char[buffer_size]; | |
196 } | |
197 | |
198 required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.c_str(), X.A.B, X.B); | |
199 | |
200 if(required_size < buffer_size) | |
201 { | |
202 if(required_size > 0) | |
203 { | |
204 out = buffer; | |
205 } | |
206 } | |
207 else | |
208 { | |
209 buffer_size *= 2; | |
210 } | |
211 | |
212 if(using_local_buffer == true) | |
213 { | |
214 using_local_buffer = false; | |
215 } | |
216 else | |
217 { | |
218 delete[] buffer; | |
219 } | |
220 | |
221 } while( (required_size >= buffer_size) ); | |
222 | |
223 return out; | |
224 } | |
225 | |
226 | |
227 | |
228 template<typename T2, typename T3, typename T4> | |
229 inline | |
230 std::string | |
231 str(const basic_format< basic_format< basic_format< format, T2>, T3>, T4>& X) | |
232 { | |
233 char local_buffer[1024]; | |
234 char* buffer = local_buffer; | |
235 | |
236 int buffer_size = 1024; | |
237 int required_size = buffer_size; | |
238 | |
239 bool using_local_buffer = true; | |
240 | |
241 std::string out; | |
242 | |
243 do | |
244 { | |
245 if(using_local_buffer == false) | |
246 { | |
247 buffer = new char[buffer_size]; | |
248 } | |
249 | |
250 required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.A.c_str(), X.A.A.B, X.A.B, X.B); | |
251 | |
252 if(required_size < buffer_size) | |
253 { | |
254 if(required_size > 0) | |
255 { | |
256 out = buffer; | |
257 } | |
258 } | |
259 else | |
260 { | |
261 buffer_size *= 2; | |
262 } | |
263 | |
264 if(using_local_buffer == true) | |
265 { | |
266 using_local_buffer = false; | |
267 } | |
268 else | |
269 { | |
270 delete[] buffer; | |
271 } | |
272 | |
273 } while( (required_size >= buffer_size) ); | |
274 | |
275 return out; | |
276 } | |
277 | |
278 | |
279 | |
280 template<typename T2, typename T3, typename T4, typename T5> | |
281 inline | |
282 std::string | |
283 str(const basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>& X) | |
284 { | |
285 char local_buffer[1024]; | |
286 char* buffer = local_buffer; | |
287 | |
288 int buffer_size = 1024; | |
289 int required_size = buffer_size; | |
290 | |
291 bool using_local_buffer = true; | |
292 | |
293 std::string out; | |
294 | |
295 do | |
296 { | |
297 if(using_local_buffer == false) | |
298 { | |
299 buffer = new char[buffer_size]; | |
300 } | |
301 | |
302 required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.A.A.c_str(), X.A.A.A.B, X.A.A.B, X.A.B, X.B); | |
303 | |
304 if(required_size < buffer_size) | |
305 { | |
306 if(required_size > 0) | |
307 { | |
308 out = buffer; | |
309 } | |
310 } | |
311 else | |
312 { | |
313 buffer_size *= 2; | |
314 } | |
315 | |
316 if(using_local_buffer == true) | |
317 { | |
318 using_local_buffer = false; | |
319 } | |
320 else | |
321 { | |
322 delete[] buffer; | |
323 } | |
324 | |
325 } while( (required_size >= buffer_size) ); | |
326 | |
327 return out; | |
328 } | |
329 | |
330 | |
331 | |
332 template<typename T2, typename T3, typename T4, typename T5, typename T6> | |
333 inline | |
334 std::string | |
335 str(const basic_format< basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>, T6>& X) | |
336 { | |
337 char local_buffer[1024]; | |
338 char* buffer = local_buffer; | |
339 | |
340 int buffer_size = 1024; | |
341 int required_size = buffer_size; | |
342 | |
343 bool using_local_buffer = true; | |
344 | |
345 std::string out; | |
346 | |
347 do | |
348 { | |
349 if(using_local_buffer == false) | |
350 { | |
351 buffer = new char[buffer_size]; | |
352 } | |
353 | |
354 required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.A.A.A.c_str(), X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); | |
355 | |
356 if(required_size < buffer_size) | |
357 { | |
358 if(required_size > 0) | |
359 { | |
360 out = buffer; | |
361 } | |
362 } | |
363 else | |
364 { | |
365 buffer_size *= 2; | |
366 } | |
367 | |
368 if(using_local_buffer == true) | |
369 { | |
370 using_local_buffer = false; | |
371 } | |
372 else | |
373 { | |
374 delete[] buffer; | |
375 } | |
376 | |
377 } while( (required_size >= buffer_size) ); | |
378 | |
379 return out; | |
380 } | |
381 | |
382 | |
383 | |
384 template<typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> | |
385 inline | |
386 std::string | |
387 str(const basic_format< basic_format< basic_format< basic_format< basic_format< basic_format< format, T2>, T3>, T4>, T5>, T6>, T7>& X) | |
388 { | |
389 char local_buffer[1024]; | |
390 char* buffer = local_buffer; | |
391 | |
392 int buffer_size = 1024; | |
393 int required_size = buffer_size; | |
394 | |
395 bool using_local_buffer = true; | |
396 | |
397 std::string out; | |
398 | |
399 do | |
400 { | |
401 if(using_local_buffer == false) | |
402 { | |
403 buffer = new char[buffer_size]; | |
404 } | |
405 | |
406 required_size = arma_snprintf(buffer, size_t(buffer_size), X.A.A.A.A.A.A.A.c_str(), X.A.A.A.A.A.B, X.A.A.A.A.B, X.A.A.A.B, X.A.A.B, X.A.B, X.B); | |
407 | |
408 if(required_size < buffer_size) | |
409 { | |
410 if(required_size > 0) | |
411 { | |
412 out = buffer; | |
413 } | |
414 } | |
415 else | |
416 { | |
417 buffer_size *= 2; | |
418 } | |
419 | |
420 if(using_local_buffer == true) | |
421 { | |
422 using_local_buffer = false; | |
423 } | |
424 else | |
425 { | |
426 delete[] buffer; | |
427 } | |
428 | |
429 } while( (required_size >= buffer_size) ); | |
430 | |
431 return out; | |
432 } | |
433 | |
434 | |
435 | |
436 template<typename T1> | |
437 struct format_metaprog | |
438 { | |
439 static const uword depth = 0; | |
440 | |
441 inline | |
442 static | |
443 const std::string& | |
444 get_fmt(const T1& X) | |
445 { | |
446 return X.A; | |
447 } | |
448 }; | |
449 | |
450 | |
451 | |
452 //template<> | |
453 template<typename T1, typename T2> | |
454 struct format_metaprog< basic_format<T1,T2> > | |
455 { | |
456 static const uword depth = 1 + format_metaprog<T1>::depth; | |
457 | |
458 inline | |
459 static | |
460 const std::string& | |
461 get_fmt(const T1& X) | |
462 { | |
463 return format_metaprog<T1>::get_fmt(X.A); | |
464 } | |
465 | |
466 }; | |
467 | |
468 | |
469 | |
470 template<typename T1, typename T2> | |
471 inline | |
472 std::string | |
473 str(const basic_format<T1,T2>& X) | |
474 { | |
475 return format_metaprog< basic_format<T1,T2> >::get_fmt(X.A); | |
476 } | |
477 | |
478 | |
479 | |
480 template<typename T1, typename T2> | |
481 inline | |
482 std::ostream& | |
483 operator<< (std::ostream& o, const basic_format<T1,T2>& X) | |
484 { | |
485 o << str(X); | |
486 return o; | |
487 } | |
488 | |
489 | |
490 #endif | |
491 | |
492 | |
493 template<typename T> struct string_only { }; | |
494 template<> struct string_only<std::string> { typedef std::string result; }; | |
495 | |
496 template<typename T> struct char_only { }; | |
497 template<> struct char_only<char > { typedef char result; }; | |
498 | |
499 template<typename T> | |
500 struct basic_format_only { }; | |
501 | |
502 #if defined(ARMA_USE_BOOST_FORMAT) | |
503 template<typename T> | |
504 struct basic_format_only< basic_format<T> > { typedef basic_format<T> result; }; | |
505 #else | |
506 template<typename T1, typename T2> | |
507 struct basic_format_only< basic_format<T1, T2> > { typedef basic_format<T1,T2> result; }; | |
508 #endif | |
509 | |
510 | |
511 | |
512 template<typename T1> | |
513 inline | |
514 static | |
515 const T1& | |
516 str_wrapper(const T1& x, const typename string_only<T1>::result* junk = 0) | |
517 { | |
518 arma_ignore(junk); | |
519 | |
520 return x; | |
521 } | |
522 | |
523 | |
524 | |
525 template<typename T1> | |
526 inline | |
527 static | |
528 const T1* | |
529 str_wrapper(const T1* x, const typename char_only<T1>::result* junk = 0) | |
530 { | |
531 arma_ignore(junk); | |
532 | |
533 return x; | |
534 } | |
535 | |
536 | |
537 | |
538 template<typename T1> | |
539 inline | |
540 static | |
541 std::string | |
542 str_wrapper(const T1& x, const typename basic_format_only<T1>::result* junk = 0) | |
543 { | |
544 arma_ignore(junk); | |
545 | |
546 return str(x); | |
547 } | |
548 | |
549 } | |
550 | |
551 //! @} |