diff options
author | Jan Huwald <jh@sotun.de> | 2012-05-07 20:01:51 (GMT) |
---|---|---|
committer | Jan Huwald <jh@sotun.de> | 2012-05-07 20:01:51 (GMT) |
commit | 420d2ef464d4a741028e132e662d5626806a41f5 (patch) | |
tree | 1aca6eb512e4ed0fb5f3c10c528cb998b6ffd695 /core/scalar.hpp |
Diffstat (limited to 'core/scalar.hpp')
-rw-r--r-- | core/scalar.hpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/core/scalar.hpp b/core/scalar.hpp new file mode 100644 index 0000000..cbab5e6 --- /dev/null +++ b/core/scalar.hpp @@ -0,0 +1,58 @@ +#ifndef LOp9uF4R8c3Nq3YZQrpOlN8rCLE +#define LOp9uF4R8c3Nq3YZQrpOlN8rCLE + +#include <inttypes.h> +#include <boost/static_assert.hpp> +#include <string> + +#include "mempool.hpp" + +using std::string; + +template<typename T, long long width = 8 * sizeof(T)> +struct Scalar { + explicit Scalar(const string name); + + inline T & operator() () const; + inline T get() const; + inline void set(const T value) const; + void sync() const; + + // associated memory container + MemPool mem; + + BOOST_STATIC_ASSERT((width > 0)); + +private: + // Scalar(); +}; + +// Implementation +template<typename T, long long width> +Scalar<T, width>::Scalar(const string name) : mem((width + 7) / 8, name) { +} + +template<typename T, long long width> +inline T & Scalar<T, width>::operator() () const { + return *((T*) mem()); +} + + +template<typename T, long long width> +inline T Scalar<T, width>::get() const { + return (*this)(); +} + +template<typename T, long long width> +inline void Scalar<T, width>::set(const T value) const { + // only a single value is in a scalar so we do not require bit-level + // access + (*this)() = value; +} + +template<typename T, long long width> +void Scalar<T, width>::sync() const { + mem.sync(); +} + +#endif // LOp9uF4R8c3Nq3YZQrpOlN8rCLE |