]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/argumentstore.h
More efficient way of loading binary files
[libs/datafile.git] / source / argumentstore.h
diff --git a/source/argumentstore.h b/source/argumentstore.h
new file mode 100644 (file)
index 0000000..c2d5d76
--- /dev/null
@@ -0,0 +1,56 @@
+#ifndef MSP_DATAFILE_ARGUMENTSTORE_H_
+#define MSP_DATAFILE_ARGUMENTSTORE_H_
+
+#include "statement.h"
+
+namespace Msp {
+namespace DataFile {
+
+class ArgumentStore
+{
+private:
+       const StatementInfo &info;
+       char *store;
+
+public:
+       ArgumentStore(const StatementInfo &);
+       ~ArgumentStore();
+
+       const StatementInfo &get_info() const { return info; }
+
+       template<typename T>
+       void set(unsigned i, const T &v)
+       {
+               *reinterpret_cast<typename TypeInfo<T>::Store *>(store+info.arg_offsets[i]) = v;
+       }
+
+       template<typename T>
+       typename TypeInfo<T>::Load get(unsigned i) const
+       {
+               return extract<typename TypeInfo<T>::Store>(store+info.arg_offsets[i], info.key.signature[i]);
+       }
+
+private:
+       template<typename T>
+       T extract(const char *, char) const;
+};
+
+template<typename T>
+inline T ArgumentStore::extract(const char *p, char) const
+{
+       return *reinterpret_cast<const T *>(p);
+}
+
+template<>
+inline FloatType::Store ArgumentStore::extract<FloatType::Store>(const char *p, char s) const
+{
+       if(s==IntType::signature)
+               return *reinterpret_cast<const IntType::Store *>(p);
+       else
+               return *reinterpret_cast<const FloatType::Store *>(p);
+}
+
+} // namespace DataFile
+} // namespace Msp
+
+#endif