]> git.tdb.fi Git - libs/datafile.git/blob - source/argumentstore.h
Implement proper copy semantics
[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(const ArgumentStore &);
18         ArgumentStore &operator=(const ArgumentStore &);
19         ~ArgumentStore();
20 private:
21         void destroy();
22         void copy_from(const char *);
23
24 public:
25         const StatementInfo &get_info() const { return info; }
26
27         template<typename T>
28         void set(unsigned i, const T &v)
29         {
30                 *reinterpret_cast<typename TypeInfo<T>::Store *>(store+info.arg_offsets[i]) = v;
31         }
32
33         template<typename T>
34         typename TypeInfo<T>::Load get(unsigned i) const
35         {
36                 return extract<typename TypeInfo<T>::Store>(store+info.arg_offsets[i], info.key.signature[i]);
37         }
38
39 private:
40         template<typename T>
41         T extract(const char *, char) const;
42 };
43
44 template<typename T>
45 inline T ArgumentStore::extract(const char *p, char) const
46 {
47         return *reinterpret_cast<const T *>(p);
48 }
49
50 template<>
51 inline FloatType::Store ArgumentStore::extract<FloatType::Store>(const char *p, char s) const
52 {
53         if(s==IntType::signature)
54                 return *reinterpret_cast<const IntType::Store *>(p);
55         else
56                 return *reinterpret_cast<const FloatType::Store *>(p);
57 }
58
59 } // namespace DataFile
60 } // namespace Msp
61
62 #endif