]> git.tdb.fi Git - libs/datafile.git/blob - source/value.h
Move the definition of Input's operator bool to the header
[libs/datafile.git] / source / value.h
1 #ifndef MSP_DATAFILE_VALUE_H_
2 #define MSP_DATAFILE_VALUE_H_
3
4 #include <vector>
5 #include <type_traits>
6 #include <msp/core/attributes.h>
7 #include <msp/core/meta.h>
8 #include <msp/core/variant.h>
9 #include "type.h"
10
11 namespace Msp {
12 namespace DataFile {
13
14 class Value
15 {
16 private:
17         char sig;
18         Variant data;
19
20 public:
21         template<typename T>
22         Value(T &&d, typename std::enable_if<!std::is_same<typename std::remove_reference<T>::type, Value>::value, int>::type = 0):
23                 sig(TypeInfo<T>::signature),
24                 data(static_cast<typename TypeInfo<T>::Store>(std::forward<T>(d)))
25         { }
26
27         Value(const Symbol &d): sig(TypeInfo<Symbol>::signature), data(d) { }
28         Value(Symbol &&d): sig(TypeInfo<Symbol>::signature), data(std::move(d)) { }
29
30         template<typename T>
31         typename TypeInfo<T>::Load get() const
32         { return get_<typename TypeInfo<T>::Store>(); }
33
34         char get_signature() const { return sig; }
35 private:
36         template<typename T>
37         T get_() const;
38 };
39
40 template<typename T>
41 inline T Value::get_() const
42 {
43         return data.value<typename TypeInfo<T>::Store>();
44 }
45
46 template<>
47 inline FloatType::Store Value::get_<FloatType::Store>() const
48 {
49         if(sig==IntType::signature)
50                 return data.value<IntType::Store>();
51         else
52                 return data.value<FloatType::Store>();
53 }
54
55 } // namespace DataFile
56 } // namespace Msp
57
58 #endif