Commit 74c003fd authored by Matthieu Dorier's avatar Matthieu Dorier

DataSet now using PIMPL idiom

parent ec1a1d40
#ifndef __HEPNOS_DATA_SET_H
#define __HEPNOS_DATA_SET_H
#include <memory>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
......@@ -17,8 +18,6 @@ class DataSet {
private:
DataSet();
DataSet(DataStore& ds, uint8_t level, const std::string& fullname);
DataSet(DataStore& ds, uint8_t level, const std::string& container, const std::string& name);
......@@ -27,28 +26,25 @@ class DataSet {
bool loadBuffer(const std::string& key, std::vector<char>& buffer) const;
DataStore* m_datastore;
uint8_t m_level;
std::string m_container;
std::string m_name;
class Impl;
std::unique_ptr<Impl> m_impl;
public:
const std::string& name() const {
return m_name;
}
DataSet();
const std::string container() const {
return m_container;
}
DataSet(const DataSet&);
DataSet(DataSet&&);
DataSet& operator=(const DataSet&);
DataSet& operator=(DataSet&&);
~DataSet();
std::string fullname() const {
std::stringstream ss;
if(m_container.size() != 0)
ss << m_container << "/";
ss << m_name;
return ss.str();
}
const std::string& name() const;
const std::string& container() const;
std::string fullname() const;
DataSet next() const;
......
......@@ -2,41 +2,66 @@
namespace hepnos {
class DataSet::Impl {
public:
DataStore* m_datastore;
uint8_t m_level;
std::string m_container;
std::string m_name;
Impl(DataStore* ds, uint8_t level, const std::string& container, const std::string& name)
: m_datastore(ds)
, m_level(level)
, m_container(container)
, m_name(name) {}
};
DataSet::DataSet()
: m_datastore(nullptr)
, m_level(0)
, m_container("")
, m_name("") {}
: m_impl(std::make_unique<DataSet::Impl>(nullptr, 0, "", "")) {}
DataSet::DataSet(DataStore& ds, uint8_t level, const std::string& fullname)
: m_datastore(&ds)
, m_level(level) {
: m_impl(std::make_unique<DataSet::Impl>(&ds, level, "", "")) {
size_t p = fullname.find_last_of('/');
if(p == std::string::npos) {
m_name = fullname;
m_impl->m_name = fullname;
} else {
m_name = fullname.substr(p+1);
m_container = fullname.substr(0, p);
m_impl->m_name = fullname.substr(p+1);
m_impl->m_container = fullname.substr(0, p);
}
}
DataSet::DataSet(DataStore& ds, uint8_t level, const std::string& container, const std::string& name)
: m_datastore(&ds)
, m_level(level)
, m_container(container)
, m_name(name) {}
: m_impl(std::make_unique<DataSet::Impl>(&ds, level, container, name)) {}
DataSet::DataSet(const DataSet& other)
: m_impl(std::make_unique<DataSet::Impl>(*other.m_impl)) {}
DataSet::DataSet(DataSet&&) = default;
DataSet& DataSet::operator=(const DataSet& other) {
if(this == &other) return *this;
m_impl = std::make_unique<DataSet::Impl>(*other.m_impl);
return *this;
}
DataSet& DataSet::operator=(DataSet&&) = default;
DataSet::~DataSet() {}
DataSet DataSet::next() const {
if(!valid()) return DataSet();
std::vector<std::string> keys;
size_t s = m_datastore->nextKeys(m_level, m_container, m_name, keys, 1);
size_t s = m_impl->m_datastore->nextKeys(
m_impl->m_level, m_impl->m_container, m_impl->m_name, keys, 1);
if(s == 0) return DataSet();
return DataSet(*m_datastore, m_level, m_container, keys[0]);
return DataSet(*(m_impl->m_datastore), m_impl->m_level, m_impl->m_container, keys[0]);
}
bool DataSet::valid() const {
return m_datastore != nullptr;
return m_impl && m_impl->m_datastore;
}
......@@ -45,7 +70,7 @@ bool DataSet::storeBuffer(const std::string& key, const std::vector<char>& buffe
throw Exception("Calling store() on invalid DataSet");
}
// forward the call to the datastore's store function
return m_datastore->store(0, fullname(), key, buffer);
return m_impl->m_datastore->store(0, fullname(), key, buffer);
}
bool DataSet::loadBuffer(const std::string& key, std::vector<char>& buffer) const {
......@@ -54,17 +79,35 @@ bool DataSet::loadBuffer(const std::string& key, std::vector<char>& buffer) cons
}
// forward the call to the datastore's load function
std::stringstream ss;
if(m_container.size() != 0)
ss << m_container << "/";
ss << m_name;
return m_datastore->load(0, fullname(), key, buffer);
if(m_impl->m_container.size() != 0)
ss << m_impl->m_container << "/";
ss << m_impl->m_name;
return m_impl->m_datastore->load(0, fullname(), key, buffer);
}
bool DataSet::operator==(const DataSet& other) const {
return m_datastore == other.m_datastore
&& m_level == other.m_level
&& m_container == other.m_container
&& m_name == other.m_name;
return m_impl->m_datastore == other.m_impl->m_datastore
&& m_impl->m_level == other.m_impl->m_level
&& m_impl->m_container == other.m_impl->m_container
&& m_impl->m_name == other.m_impl->m_name;
}
const std::string& DataSet::name() const {
return m_impl->m_name;
}
const std::string& DataSet::container() const {
return m_impl->m_container;
}
std::string DataSet::fullname() const {
std::stringstream ss;
if(container().size() != 0)
ss << container() << "/";
ss << name();
return ss.str();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment