]> git.tdb.fi Git - libs/datafile.git/blob - source/value.h
Rewrite the type system
[libs/datafile.git] / source / value.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2006-2008, 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_DATAFILE_VALUE_H_
9 #define MSP_DATAFILE_VALUE_H_
10
11 #include <vector>
12 #include <msp/core/meta.h>
13 #include <msp/core/variant.h>
14 #include "except.h"
15 #include "type.h"
16
17 namespace Msp {
18 namespace DataFile {
19
20 class Value
21 {
22 private:
23         char sig;
24         Variant data;
25
26 public:
27         template<typename T>
28         Value(T d):
29                 sig(TypeInfo<T>::signature),
30                 data(static_cast<typename TypeInfo<T>::Store>(d))
31         { }
32
33         Value(Symbol d): sig(TypeInfo<Symbol>::signature), data(d) { }
34
35         template<typename T>
36         typename RemoveReference<T>::Type get() const
37         { return get_<typename TypeInfo<T>::Store>(); }
38
39         char get_signature() const { return sig; }
40 private:
41         template<typename T>
42         T get_() const;
43 };
44
45 typedef std::vector<Value> ValueArray __attribute__((deprecated));
46
47 template<typename T>
48 inline T Value::get_() const
49 {
50         if(sig!=TypeInfo<T>::signature)
51                 throw TypeError("Type mismatch");
52
53         return data.value<typename TypeInfo<T>::Store>();
54 }
55
56 template<>
57 inline FloatType::Store Value::get_<FloatType::Store>() const
58 {
59         if(sig==IntType::signature)
60                 return data.value<IntType::Store>();
61         else if(sig!=FloatType::signature)
62                 throw TypeError("Type mismatch");
63
64         return data.value<FloatType::Store>();
65 }
66
67 } // namespace DataFile
68 } // namespace Msp
69
70 #endif