]> git.tdb.fi Git - libs/datafile.git/blob - source/value.h
Drop copyright and license notices from source files
[libs/datafile.git] / source / value.h
1 #ifndef MSP_DATAFILE_VALUE_H_
2 #define MSP_DATAFILE_VALUE_H_
3
4 #include <vector>
5 #include <msp/core/meta.h>
6 #include <msp/core/variant.h>
7 #include "except.h"
8 #include "type.h"
9
10 namespace Msp {
11 namespace DataFile {
12
13 class Value
14 {
15 private:
16         char sig;
17         Variant data;
18
19 public:
20         template<typename T>
21         Value(T d):
22                 sig(TypeInfo<T>::signature),
23                 data(static_cast<typename TypeInfo<T>::Store>(d))
24         { }
25
26         Value(Symbol d): sig(TypeInfo<Symbol>::signature), data(d) { }
27
28         template<typename T>
29         typename RemoveReference<T>::Type get() const
30         { return get_<typename TypeInfo<T>::Store>(); }
31
32         char get_signature() const { return sig; }
33 private:
34         template<typename T>
35         T get_() const;
36 };
37
38 typedef std::vector<Value> ValueArray __attribute__((deprecated));
39
40 template<typename T>
41 inline T Value::get_() const
42 {
43         if(sig!=TypeInfo<T>::signature)
44                 throw TypeError("Type mismatch");
45
46         return data.value<typename TypeInfo<T>::Store>();
47 }
48
49 template<>
50 inline FloatType::Store Value::get_<FloatType::Store>() const
51 {
52         if(sig==IntType::signature)
53                 return data.value<IntType::Store>();
54         else if(sig!=FloatType::signature)
55                 throw TypeError("Type mismatch");
56
57         return data.value<FloatType::Store>();
58 }
59
60 } // namespace DataFile
61 } // namespace Msp
62
63 #endif