source: box/trunk/lib/common/Conversion.h @ 217

Revision 217, 2.5 KB checked in by martin, 6 years ago (diff)

Set svn:eol-style as appropriate for all files

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    Conversion.h
5//              Purpose: Convert between various types
6//              Created: 9/4/04
7//
8// --------------------------------------------------------------------------
9
10#ifndef CONVERSION__H
11#define CONVERSION__H
12
13#include <string>
14
15namespace BoxConvert
16{
17        // --------------------------------------------------------------------------
18        //
19        // Function
20        //              Name:    BoxConvert::Convert<to_type, from_type>(to_type &, from_type)
21        //              Purpose: Convert from types to types
22        //              Created: 9/4/04
23        //
24        // --------------------------------------------------------------------------
25        template<typename to_type, typename from_type>
26        inline to_type Convert(from_type From)
27        {
28                // Default conversion, simply use C++ conversion
29                return From;
30        }
31
32        // Specialise for string -> integer
33        int32_t _ConvertStringToInt(const char *pString, int Size);
34        template<>
35        inline int32_t Convert<int32_t, const std::string &>(const std::string &rFrom)
36        {
37                return BoxConvert::_ConvertStringToInt(rFrom.c_str(), 32);
38        }
39        template<>
40        inline int16_t Convert<int16_t, const std::string &>(const std::string &rFrom)
41        {
42                return BoxConvert::_ConvertStringToInt(rFrom.c_str(), 16);
43        }
44        template<>
45        inline int8_t Convert<int8_t, const std::string &>(const std::string &rFrom)
46        {
47                return BoxConvert::_ConvertStringToInt(rFrom.c_str(), 8);
48        }
49        template<>
50        inline int32_t Convert<int32_t, const char *>(const char *pFrom)
51        {
52                return BoxConvert::_ConvertStringToInt(pFrom, 32);
53        }
54        template<>
55        inline int16_t Convert<int16_t, const char *>(const char *pFrom)
56        {
57                return BoxConvert::_ConvertStringToInt(pFrom, 16);
58        }
59        template<>
60        inline int8_t Convert<int8_t, const char *>(const char *pFrom)
61        {
62                return BoxConvert::_ConvertStringToInt(pFrom, 8);
63        }
64       
65        // Specialise for integer -> string
66        void _ConvertIntToString(std::string &rTo, int32_t From);
67        template<>
68        inline std::string Convert<std::string, int32_t>(int32_t From)
69        {
70                std::string r;
71                BoxConvert::_ConvertIntToString(r, From);
72                return r;
73        }
74        template<>
75        inline std::string Convert<std::string, int16_t>(int16_t From)
76        {
77                std::string r;
78                BoxConvert::_ConvertIntToString(r, From);
79                return r;
80        }
81        template<>
82        inline std::string Convert<std::string, int8_t>(int8_t From)
83        {
84                std::string r;
85                BoxConvert::_ConvertIntToString(r, From);
86                return r;
87        }
88       
89        // Specialise for bool -> string
90        template<>
91        inline std::string Convert<std::string, bool>(bool From)
92        {
93                return std::string(From?"true":"false");
94        }
95};
96
97#endif // CONVERSION__H
98
Note: See TracBrowser for help on using the repository browser.