]> git.tdb.fi Git - libs/datafile.git/blob - source/type.h
Fix creating Symbols from arbitary types
[libs/datafile.git] / source / type.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_DATAFILE_TYPE_H_
9 #define MSP_DATAFILE_TYPE_H_
10
11 #include <msp/strings/lexicalcast.h>
12
13 namespace Msp {
14 namespace DataFile {
15
16 struct Symbol
17 {
18         std::string name;
19
20         template<typename T>
21         Symbol(const T &n): name(lexical_cast(n)) { }
22
23         template<typename T> operator T() const { return lexical_cast<T>(name); }
24 };
25
26 struct IntType
27 {
28         static const char signature = 'i';
29         typedef long long int Store;
30 };
31
32 struct FloatType
33 {
34         static const char signature = 'f';
35         typedef double Store;
36 };
37
38 struct BoolType
39 {
40         static const char signature = 'b';
41         typedef bool Store;
42 };
43
44 struct StringType
45 {
46         static const char signature = 's';
47         typedef std::string Store;
48 };
49
50 struct SymbolType
51 {
52         // For backward compatibility
53         static const char signature = 'e';
54         typedef Symbol Store;
55 };
56
57 template<typename T>
58 struct TypeInfo: SymbolType { };
59
60 template<>
61 struct TypeInfo<short int>: IntType { };
62
63 template<>
64 struct TypeInfo<unsigned short int>: IntType { };
65
66 template<>
67 struct TypeInfo<int>: IntType { };
68
69 template<>
70 struct TypeInfo<unsigned int>: IntType { };
71
72 template<>
73 struct TypeInfo<long int>: IntType { };
74
75 template<>
76 struct TypeInfo<unsigned long int>: IntType { };
77
78 template<>
79 struct TypeInfo<long long int>: IntType { };
80
81 template<>
82 struct TypeInfo<unsigned long long int>: IntType { };
83
84 template<>
85 struct TypeInfo<float>: FloatType { };
86
87 template<>
88 struct TypeInfo<double>: FloatType { };
89
90 template<>
91 struct TypeInfo<bool>: BoolType { };
92
93 template<>
94 struct TypeInfo<std::string>: StringType { };
95
96 template<typename T>
97 struct TypeInfo<const T>: TypeInfo<T> { };
98
99 template<typename T>
100 struct TypeInfo<T &>: TypeInfo<T> { };
101
102 } // namespace DataFile
103 } // namespace Msp
104
105 #endif