]> git.tdb.fi Git - libs/datafile.git/blob - source/argumentstore.h
c2d5d76f3dbb473c613840c7f92dd9320fa07d5b
[libs/datafile.git] / source / argumentstore.h
1 #ifndef MSP_DATAFILE_ARGUMENTSTORE_H_
2 #define MSP_DATAFILE_ARGUMENTSTORE_H_
3
4 #include "statement.h"
5
6 namespace Msp {
7 namespace DataFile {
8
9 class ArgumentStore
10 {
11 private:
12         const StatementInfo &info;
13         char *store;
14
15 public:
16         ArgumentStore(const StatementInfo &);
17         ~ArgumentStore();
18
19         const StatementInfo &get_info() const { return info; }
20
21         template<typename T>
22         void set(unsigned i, const T &v)
23         {
24                 *reinterpret_cast<typename TypeInfo<T>::Store *>(store+info.arg_offsets[i]) = v;
25         }
26
27         template<typename T>
28         typename TypeInfo<T>::Load get(unsigned i) const
29         {
30                 return extract<typename TypeInfo<T>::Store>(store+info.arg_offsets[i], info.key.signature[i]);
31         }
32
33 private:
34         template<typename T>
35         T extract(const char *, char) const;
36 };
37
38 template<typename T>
39 inline T ArgumentStore::extract(const char *p, char) const
40 {
41         return *reinterpret_cast<const T *>(p);
42 }
43
44 template<>
45 inline FloatType::Store ArgumentStore::extract<FloatType::Store>(const char *p, char s) const
46 {
47         if(s==IntType::signature)
48                 return *reinterpret_cast<const IntType::Store *>(p);
49         else
50                 return *reinterpret_cast<const FloatType::Store *>(p);
51 }
52
53 } // namespace DataFile
54 } // namespace Msp
55
56 #endif