Convert.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_CONVERT_H_
36 #define _BLAZE_UTIL_CONVERT_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <cstdlib>
44 #include <sstream>
45 #include <string>
46 #include <typeinfo>
47 #include <blaze/util/Exception.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // CLASS TEMPLATE CASTCONVERTER
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
65 template< typename To, typename From, int IsBase >
66 struct CastConverter
67 {};
69 //*************************************************************************************************
70 
71 
72 
73 
74 
75 //=================================================================================================
76 //
77 // PARTIAL TEMPLATE SPECIALIZATION OF CASTCONVERTER
78 //
79 //=================================================================================================
80 
81 //*************************************************************************************************
86 template< typename To, typename From >
87 struct CastConverter<To*,From*,0> : private NonCreatable
88 {
89  public:
90  //**Conversion functions************************************************************************
93  static inline To* convert( From* from );
95  //**********************************************************************************************
96 };
98 //*************************************************************************************************
99 
100 
101 //*************************************************************************************************
108 template< typename To, typename From >
109 inline To* CastConverter<To*,From*,0>::convert( From* from )
110 {
111  return dynamic_cast<To*>( from );
112 }
114 //*************************************************************************************************
115 
116 
117 
118 
119 //=================================================================================================
120 //
121 // PARTIAL TEMPLATE SPECIALIZATION OF CASTCONVERTER
122 //
123 //=================================================================================================
124 
125 //*************************************************************************************************
130 template< typename To, typename From >
131 struct CastConverter<To*,From*,1> : private NonCreatable
132 {
133  public:
134  //**Conversion functions************************************************************************
137  static inline To* convert( From* from );
139  //**********************************************************************************************
140 };
142 //*************************************************************************************************
143 
144 
145 //*************************************************************************************************
152 template< typename To, typename From >
153 inline To* CastConverter<To*,From*,1>::convert( From* from )
154 {
155  return static_cast<To*>( from );
156 }
158 //*************************************************************************************************
159 
160 
161 
162 
163 //=================================================================================================
164 //
165 // CLASS TEMPLATE CONVERTER
166 //
167 //=================================================================================================
168 
169 //*************************************************************************************************
174 template< typename To, typename From >
175 struct Converter : private NonCreatable
176 {
177  public:
178  //**Conversion functions************************************************************************
181  static inline To convert( const From& from );
183  //**********************************************************************************************
184 };
186 //*************************************************************************************************
187 
188 
189 //*************************************************************************************************
196 template< typename To, typename From >
197 inline To Converter<To,From>::convert( const From& from )
198 {
199  return static_cast<To>( from );
200 }
202 //*************************************************************************************************
203 
204 
205 
206 
207 //=================================================================================================
208 //
209 // TOTAL TEMPLATE SPECIALIZATION OF CONVERTER
210 //
211 //=================================================================================================
212 
213 //*************************************************************************************************
218 template< typename To, typename From >
219 struct Converter<To*,From*> : private NonCreatable
220 {
221  public:
222  //**Conversion functions************************************************************************
225  static inline To* convert( From* from );
227  //**********************************************************************************************
228 };
230 //*************************************************************************************************
231 
232 
233 //*************************************************************************************************
240 template< typename To, typename From >
241 inline To* Converter<To*,From*>::convert( From* from )
242 {
243  return CastConverter<To*,From*,blaze::IsBaseOf<To,From>::yes>::convert( from );
244 }
246 //*************************************************************************************************
247 
248 
249 
250 
251 //=================================================================================================
252 //
253 // PARTIAL TEMPLATE SPECIALIZATION OF CONVERTER
254 //
255 //=================================================================================================
256 
257 //*************************************************************************************************
262 template< typename To >
263 struct Converter<To,std::string> : private NonCreatable
264 {
265  public:
266  //**Conversion functions************************************************************************
269  static inline To convert( const std::string& from );
271  //**********************************************************************************************
272 };
274 //*************************************************************************************************
275 
276 
277 //*************************************************************************************************
284 template< typename To >
285 inline To Converter<To,std::string>::convert( const std::string& from )
286 {
287  To to;
288  std::istringstream iss( from );
289  if( !(iss >> to) ) {
290  std::ostringstream error;
291  error << "Invalid cast from std::string to " << typeid(to).name() << "\n";
292  BLAZE_THROW_RUNTIME_ERROR( error.str() );
293  }
294  return to;
295 }
297 //*************************************************************************************************
298 
299 
300 
301 
302 //=================================================================================================
303 //
304 // PARTIAL TEMPLATE SPECIALIZATION OF CONVERTER
305 //
306 //=================================================================================================
307 
308 //*************************************************************************************************
313 template< typename From >
314 struct Converter<std::string,From> : private NonCreatable
315 {
316  public:
317  //**Conversion functions************************************************************************
320  static inline std::string convert( const From& from );
322  //**********************************************************************************************
323 };
325 //*************************************************************************************************
326 
327 
328 //*************************************************************************************************
335 template< typename From >
336 inline std::string Converter<std::string,From>::convert( const From& from )
337 {
338  std::ostringstream oss;
339  if( !(oss << from) ) {
340  std::ostringstream error;
341  error << "Invalid cast from " << typeid(from).name() << " to std::string\n";
342  BLAZE_THROW_RUNTIME_ERROR( error.str() );
343  }
344  return oss.str();
345 }
347 //*************************************************************************************************
348 
349 
350 
351 
352 //=================================================================================================
353 //
354 // TOTAL TEMPLATE SPECIALIZATION OF CONVERTER
355 //
356 //=================================================================================================
357 
358 //*************************************************************************************************
365 template<>
366 struct Converter<std::string,std::string> : private NonCreatable
367 {
368  public:
369  //**Conversion functions************************************************************************
372  static inline std::string convert( const std::string& from );
374  //**********************************************************************************************
375 };
377 //*************************************************************************************************
378 
379 
380 //*************************************************************************************************
387 inline std::string Converter<std::string,std::string>::convert( const std::string& from )
388 {
389  return from;
390 }
392 //*************************************************************************************************
393 
394 
395 
396 
397 //=================================================================================================
398 //
399 // CONVERSION FUNCTIONS
400 //
401 //=================================================================================================
402 
403 //*************************************************************************************************
417 template< typename To, typename From >
418 inline To convert( const From& from )
419 {
420  return Converter<To,From>::convert( from );
421 }
422 //*************************************************************************************************
423 
424 
425 //*************************************************************************************************
432 template<>
433 inline int convert<int,std::string>( const std::string& from )
434 {
435  return std::atoi( from.c_str() );
436 }
438 //*************************************************************************************************
439 
440 
441 //*************************************************************************************************
448 template<>
449 inline unsigned int convert<unsigned int,std::string>( const std::string& from )
450 {
451  return static_cast<unsigned int>( std::atoi( from.c_str() ) );
452 }
454 //*************************************************************************************************
455 
456 
457 //*************************************************************************************************
464 template<>
465 inline float convert<float,std::string>( const std::string& from )
466 {
467  return static_cast<float>( std::atof( from.c_str() ) );
468 }
470 //*************************************************************************************************
471 
472 
473 //*************************************************************************************************
480 template<>
481 inline double convert<double,std::string>( const std::string& from )
482 {
483  return std::atof( from.c_str() );
484 }
486 //*************************************************************************************************
487 
488 
489 
490 
491 //*************************************************************************************************
498 template< typename To >
499 inline To convert( const char* const from )
500 {
501  return Converter<To,std::string>::convert( from );
502 }
504 //*************************************************************************************************
505 
506 
507 //*************************************************************************************************
514 template<>
515 inline int convert<int>( const char* const from )
516 {
517  return std::atoi( from );
518 }
520 //*************************************************************************************************
521 
522 
523 //*************************************************************************************************
530 template<>
531 inline unsigned int convert<unsigned int>( const char* const from )
532 {
533  return static_cast<unsigned int>( std::atof( from ) );
534 }
536 //*************************************************************************************************
537 
538 
539 //*************************************************************************************************
546 template<>
547 inline float convert<float>( const char* const from )
548 {
549  return static_cast<float>( std::atof( from ) );
550 }
552 //*************************************************************************************************
553 
554 
555 //*************************************************************************************************
562 template<>
563 inline double convert<double>( const char* const from )
564 {
565  return std::atof( from );
566 }
568 //*************************************************************************************************
569 
570 
571 
572 
573 //*************************************************************************************************
580 template< typename To >
581 inline To convert( char* const from )
582 {
583  return Converter<To,std::string>::convert( from );
584 }
586 //*************************************************************************************************
587 
588 
589 //*************************************************************************************************
596 template<>
597 inline int convert<int>( char* const from )
598 {
599  return std::atoi( from );
600 }
602 //*************************************************************************************************
603 
604 
605 //*************************************************************************************************
612 template<>
613 inline unsigned int convert<unsigned int>( char* const from )
614 {
615  return static_cast<unsigned int>( std::atoi( from ) );
616 }
618 //*************************************************************************************************
619 
620 
621 //*************************************************************************************************
628 template<>
629 inline float convert<float>( char* const from )
630 {
631  return static_cast<float>( std::atof( from ) );
632 }
634 //*************************************************************************************************
635 
636 
637 //*************************************************************************************************
644 template<>
645 inline double convert<double>( char* const from )
646 {
647  return std::atof( from );
648 }
650 //*************************************************************************************************
651 
652 } // namespace blaze
653 
654 #endif
Log level for (sever) errors.
Definition: LogLevel.h:78
Header file for exception macros.
STL namespace.
#define BLAZE_THROW_RUNTIME_ERROR(MESSAGE)
Macro for the emission of a std::runtime_error exception.This macro encapsulates the default way of B...
Definition: Exception.h:379
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Header file for the IsBaseOf type trait.
Base class for non-creatable (static) classes.
To convert(const From &from)
Conversion from type From to type To.
Definition: Convert.h:418