DataSet.hpp 2.48 KB
Newer Older
1 2 3
#ifndef __HEPNOS_DATA_SET_H
#define __HEPNOS_DATA_SET_H

4
#include <memory>
5 6 7 8 9 10
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <hepnos/Exception.hpp>
11 12 13 14 15 16 17 18 19 20
#include <hepnos/DataStore.hpp>

namespace hepnos {

class DataSet {

    friend class DataStore;

    private:

21
        DataSet(DataStore& ds, uint8_t level, const std::string& fullname);
22

23 24 25 26 27 28
        DataSet(DataStore& ds, uint8_t level, const std::string& container, const std::string& name);

        bool storeBuffer(const std::string& key, const std::vector<char>& buffer);

        bool loadBuffer(const std::string& key, std::vector<char>& buffer) const;

29 30 31
        class Impl;

        std::unique_ptr<Impl> m_impl;
32 33 34

    public:

35 36 37 38 39 40 41 42 43 44 45 46 47
        DataSet();

        DataSet(const DataSet&);
        DataSet(DataSet&&);
        DataSet& operator=(const DataSet&);
        DataSet& operator=(DataSet&&);
        ~DataSet();

        const std::string& name() const;

        const std::string& container() const;

        std::string fullname() const;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

        DataSet next() const;

        bool valid() const;

        template<typename K, typename V>
        bool store(const K& key, const V& value) {
            std::stringstream ss_key, ss_value;
            ss_key << key;
            boost::archive::binary_oarchive oa(ss_value);
            try {
                oa << value;
            } catch(...) {
                throw Exception("Exception occured during serialization");
            }
            std::string serialized = ss_value.str();
            std::vector<char> buffer(serialized.begin(), serialized.end());
            return storeBuffer(ss_key.str(), buffer);
        }

        template<typename K, typename V>
        bool load(const K& key, V& value) const {
            std::stringstream ss_key;
            ss_key << key;
            std::vector<char> buffer;
            if(!loadBuffer(key, buffer)) {
                return false;
            }
            try {
                std::string serialized(buffer.begin(), buffer.end());
                std::stringstream ss(serialized);
                boost::archive::binary_iarchive ia(ss);
                ia >> value;
            } catch(...) {
                throw Exception("Exception occured during serialization");
            }
            return true;
        }

        bool operator==(const DataSet& other) const;
88 89 90 91 92
};

}

#endif