All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Convert.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_CONVERT_H_
23 #define _BLAZE_UTIL_CONVERT_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <cstdlib>
31 #include <sstream>
32 #include <stdexcept>
33 #include <string>
34 #include <typeinfo>
37 
38 
39 namespace blaze {
40 
41 //=================================================================================================
42 //
43 // CLASS TEMPLATE CASTCONVERTER
44 //
45 //=================================================================================================
46 
47 //*************************************************************************************************
52 template< typename To, typename From, int IsBase >
53 struct CastConverter
54 {};
56 //*************************************************************************************************
57 
58 
59 
60 
61 
62 //=================================================================================================
63 //
64 // PARTIAL TEMPLATE SPECIALIZATION OF CASTCONVERTER
65 //
66 //=================================================================================================
67 
68 //*************************************************************************************************
73 template< typename To, typename From >
74 struct CastConverter<To*,From*,0> : private NonCreatable
75 {
76  public:
77  //**Conversion functions************************************************************************
80  static inline To* convert( From* from );
82  //**********************************************************************************************
83 };
85 //*************************************************************************************************
86 
87 
88 //*************************************************************************************************
95 template< typename To, typename From >
96 inline To* CastConverter<To*,From*,0>::convert( From* from )
97 {
98  return dynamic_cast<To*>( from );
99 }
101 //*************************************************************************************************
102 
103 
104 
105 
106 //=================================================================================================
107 //
108 // PARTIAL TEMPLATE SPECIALIZATION OF CASTCONVERTER
109 //
110 //=================================================================================================
111 
112 //*************************************************************************************************
117 template< typename To, typename From >
118 struct CastConverter<To*,From*,1> : private NonCreatable
119 {
120  public:
121  //**Conversion functions************************************************************************
124  static inline To* convert( From* from );
126  //**********************************************************************************************
127 };
129 //*************************************************************************************************
130 
131 
132 //*************************************************************************************************
139 template< typename To, typename From >
140 inline To* CastConverter<To*,From*,1>::convert( From* from )
141 {
142  return static_cast<To*>( from );
143 }
145 //*************************************************************************************************
146 
147 
148 
149 
150 //=================================================================================================
151 //
152 // CLASS TEMPLATE CONVERTER
153 //
154 //=================================================================================================
155 
156 //*************************************************************************************************
161 template< typename To, typename From >
162 struct Converter : private NonCreatable
163 {
164  public:
165  //**Conversion functions************************************************************************
168  static inline To convert( const From& from );
170  //**********************************************************************************************
171 };
173 //*************************************************************************************************
174 
175 
176 //*************************************************************************************************
183 template< typename To, typename From >
184 inline To Converter<To,From>::convert( const From& from )
185 {
186  return static_cast<To>( from );
187 }
189 //*************************************************************************************************
190 
191 
192 
193 
194 //=================================================================================================
195 //
196 // TOTAL TEMPLATE SPECIALIZATION OF CONVERTER
197 //
198 //=================================================================================================
199 
200 //*************************************************************************************************
205 template< typename To, typename From >
206 struct Converter<To*,From*> : private NonCreatable
207 {
208  public:
209  //**Conversion functions************************************************************************
212  static inline To* convert( From* from );
214  //**********************************************************************************************
215 };
217 //*************************************************************************************************
218 
219 
220 //*************************************************************************************************
227 template< typename To, typename From >
228 inline To* Converter<To*,From*>::convert( From* from )
229 {
230  return CastConverter<To*,From*,blaze::IsBaseOf<To,From>::yes>::convert( from );
231 }
233 //*************************************************************************************************
234 
235 
236 
237 
238 //=================================================================================================
239 //
240 // PARTIAL TEMPLATE SPECIALIZATION OF CONVERTER
241 //
242 //=================================================================================================
243 
244 //*************************************************************************************************
249 template< typename To >
250 struct Converter<To,std::string> : private NonCreatable
251 {
252  public:
253  //**Conversion functions************************************************************************
256  static inline To convert( const std::string& from );
258  //**********************************************************************************************
259 };
261 //*************************************************************************************************
262 
263 
264 //*************************************************************************************************
271 template< typename To >
272 inline To Converter<To,std::string>::convert( const std::string& from )
273 {
274  To to;
275  std::istringstream iss( from );
276  if( !(iss >> to) ) {
277  std::ostringstream error;
278  error << "Invalid cast from std::string to " << typeid(to).name() << "\n";
279  throw std::runtime_error( error.str() );
280  }
281  return to;
282 }
284 //*************************************************************************************************
285 
286 
287 
288 
289 //=================================================================================================
290 //
291 // PARTIAL TEMPLATE SPECIALIZATION OF CONVERTER
292 //
293 //=================================================================================================
294 
295 //*************************************************************************************************
300 template< typename From >
301 struct Converter<std::string,From> : private NonCreatable
302 {
303  public:
304  //**Conversion functions************************************************************************
307  static inline std::string convert( const From& from );
309  //**********************************************************************************************
310 };
312 //*************************************************************************************************
313 
314 
315 //*************************************************************************************************
322 template< typename From >
323 inline std::string Converter<std::string,From>::convert( const From& from )
324 {
325  std::ostringstream oss;
326  if( !(oss << from) ) {
327  std::ostringstream error;
328  error << "Invalid cast from " << typeid(from).name() << " to std::string\n";
329  throw std::runtime_error( error.str() );
330  }
331  return oss.str();
332 }
334 //*************************************************************************************************
335 
336 
337 
338 
339 //=================================================================================================
340 //
341 // TOTAL TEMPLATE SPECIALIZATION OF CONVERTER
342 //
343 //=================================================================================================
344 
345 //*************************************************************************************************
352 template<>
353 struct Converter<std::string,std::string> : private NonCreatable
354 {
355  public:
356  //**Conversion functions************************************************************************
359  static inline std::string convert( const std::string& from );
361  //**********************************************************************************************
362 };
364 //*************************************************************************************************
365 
366 
367 //*************************************************************************************************
374 inline std::string Converter<std::string,std::string>::convert( const std::string& from )
375 {
376  return from;
377 }
379 //*************************************************************************************************
380 
381 
382 
383 
384 //=================================================================================================
385 //
386 // CONVERSION FUNCTIONS
387 //
388 //=================================================================================================
389 
390 //*************************************************************************************************
404 template< typename To, typename From >
405 inline To convert( const From& from )
406 {
407  return Converter<To,From>::convert( from );
408 }
409 //*************************************************************************************************
410 
411 
412 //*************************************************************************************************
419 template<>
420 inline int convert<int,std::string>( const std::string& from )
421 {
422  return std::atoi( from.c_str() );
423 }
425 //*************************************************************************************************
426 
427 
428 //*************************************************************************************************
435 template<>
436 inline unsigned int convert<unsigned int,std::string>( const std::string& from )
437 {
438  return static_cast<unsigned int>( std::atoi( from.c_str() ) );
439 }
441 //*************************************************************************************************
442 
443 
444 //*************************************************************************************************
451 template<>
452 inline float convert<float,std::string>( const std::string& from )
453 {
454  return static_cast<float>( std::atof( from.c_str() ) );
455 }
457 //*************************************************************************************************
458 
459 
460 //*************************************************************************************************
467 template<>
468 inline double convert<double,std::string>( const std::string& from )
469 {
470  return std::atof( from.c_str() );
471 }
473 //*************************************************************************************************
474 
475 
476 
477 
478 //*************************************************************************************************
485 template< typename To >
486 inline To convert( const char* const from )
487 {
488  return Converter<To,std::string>::convert( from );
489 }
491 //*************************************************************************************************
492 
493 
494 //*************************************************************************************************
501 template<>
502 inline int convert<int>( const char* const from )
503 {
504  return std::atoi( from );
505 }
507 //*************************************************************************************************
508 
509 
510 //*************************************************************************************************
517 template<>
518 inline unsigned int convert<unsigned int>( const char* const from )
519 {
520  return static_cast<unsigned int>( std::atof( from ) );
521 }
523 //*************************************************************************************************
524 
525 
526 //*************************************************************************************************
533 template<>
534 inline float convert<float>( const char* const from )
535 {
536  return static_cast<float>( std::atof( from ) );
537 }
539 //*************************************************************************************************
540 
541 
542 //*************************************************************************************************
549 template<>
550 inline double convert<double>( const char* const from )
551 {
552  return std::atof( from );
553 }
555 //*************************************************************************************************
556 
557 
558 
559 
560 //*************************************************************************************************
567 template< typename To >
568 inline To convert( char* const from )
569 {
570  return Converter<To,std::string>::convert( from );
571 }
573 //*************************************************************************************************
574 
575 
576 //*************************************************************************************************
583 template<>
584 inline int convert<int>( char* const from )
585 {
586  return std::atoi( from );
587 }
589 //*************************************************************************************************
590 
591 
592 //*************************************************************************************************
599 template<>
600 inline unsigned int convert<unsigned int>( char* const from )
601 {
602  return static_cast<unsigned int>( std::atoi( from ) );
603 }
605 //*************************************************************************************************
606 
607 
608 //*************************************************************************************************
615 template<>
616 inline float convert<float>( char* const from )
617 {
618  return static_cast<float>( std::atof( from ) );
619 }
621 //*************************************************************************************************
622 
623 
624 //*************************************************************************************************
631 template<>
632 inline double convert<double>( char* const from )
633 {
634  return std::atof( from );
635 }
637 //*************************************************************************************************
638 
639 } // namespace blaze
640 
641 #endif