Commit 2ecbcb31 authored by Matthieu Dorier's avatar Matthieu Dorier

interface nearly complete

parent 9d98a280
......@@ -2,14 +2,10 @@
#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>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <hepnos/Exception.hpp>
#include <hepnos/RunNumber.hpp>
#include <hepnos/DataStore.hpp>
#include <hepnos/KeyValueContainer.hpp>
namespace hepnos {
......@@ -22,7 +18,7 @@ class Run;
* another dataset. It provides functionalities to navigate nested
* datasets and to load/store data products.
*/
class DataSet {
class DataSet : public KeyValueContainer {
friend class DataStore;
friend class RunSet;
......@@ -168,67 +164,6 @@ class DataSet {
*/
bool loadRawData(const std::string& key, std::vector<char>& buffer) const;
/**
* @brief Stores a key/value pair into the DataSet.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/" or "%" characters. The
* type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to store.
* @param value Value to store.
*
* @return true if the key was found. false otherwise.
*/
template<typename K, typename V>
bool store(const K& key, const V& value) {
std::stringstream ss_value;
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 storeRawData(key, buffer);
}
/**
* @brief Loads a value associated with a key from the DataSet.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/" or "%" characters. The
* type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to load.
* @param value Value to load.
*
* @return true if the key exists and was loaded. False otherwise.
*/
template<typename K, typename V>
bool load(const K& key, V& value) const {
std::vector<char> buffer;
if(!loadRawData(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;
}
/**
* @brief Comparison operator.
*
......
......@@ -11,6 +11,7 @@ class DataSet;
class RunSet;
class Run;
class SubRun;
class Event;
/**
* The DataStore class is the main handle referencing an HEPnOS service.
......@@ -22,6 +23,7 @@ class DataStore {
friend class RunSet;
friend class Run;
friend class SubRun;
friend class Event;
public:
......@@ -367,7 +369,7 @@ class DataStore::const_iterator {
*
* @param rhs const_iterator to compare with.
*
* @return true if the two const_iterators are different, false atherwise.
* @return true if the two const_iterators are different, false otherwise.
*/
bool operator!=(const self_type& rhs) const;
};
......
#ifndef __HEPNOS_DEMANGLE_H
#define __HEPNOS_DEMANGLE_H
#include <boost/core/demangle.hpp>
#include <string>
namespace hepnos {
template<typename T>
std::string demangle() {
char const * name = typeid(T).name();
return boost::core::demangle(name);
}
template<typename T>
std::string demangle(T&&) {
return demangle<T>();
}
}
#endif
......@@ -3,18 +3,14 @@
#include <memory>
#include <string>
#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/KeyValueContainer.hpp>
#include <hepnos/DataStore.hpp>
#include <hepnos/EventNumber.hpp>
#include <hepnos/Exception.hpp>
namespace hepnos {
class Event {
class Event : public KeyValueContainer {
private:
......@@ -150,66 +146,6 @@ class Event {
*/
const std::string& container() const;
/**
* @brief Stores a key/value pair into the Event.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/" or "%" characters. The
* type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to store.
* @param value Value to store.
*
* @return true if the key was found. false otherwise.
*/
template<typename K, typename V>
bool store(const K& key, const V& value) {
std::stringstream ss_value;
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 storeRawData(key, buffer);
}
/**
* @brief Loads a value associated with a key from the Event.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/" or "%" characters. The
* type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to load.
* @param value Value to load.
*
* @return true if the key exists and was loaded. False otherwise.
*/
template<typename K, typename V>
bool load(const K& key, V& value) const {
std::vector<char> buffer;
if(!loadRawData(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;
}
};
}
......
#ifndef __HEPNOS_KEYVAL_CONTAINER_H
#define __HEPNOS_KEYVAL_CONTAINER_H
#include <memory>
#include <string>
#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/Demangle.hpp>
#include <hepnos/Exception.hpp>
namespace hepnos {
class KeyValueContainer {
public:
/**
* @brief Default constructor.
*/
KeyValueContainer() = default;
/**
* @brief Copy constructor.
*/
KeyValueContainer(const KeyValueContainer& other) = default;
/**
* @brief Move constructor.
*/
KeyValueContainer(KeyValueContainer&& other) = default;
/**
* @brief Copy-assignment operator.
*/
KeyValueContainer& operator=(const KeyValueContainer& other) = default;
/**
* @brief Move-assignment operator.
*/
KeyValueContainer& operator=(KeyValueContainer&& other) = default;
/**
* @brief Destructor.
*/
virtual ~KeyValueContainer() = default;
/**
* @brief Stores raw key/value data in this KeyValueContainer.
* This function is virtual and must be overloaded in the child class.
*
* @param key Key
* @param buffer Value
*
* @return true if the key did not already exist, false otherwise.
*/
virtual bool storeRawData(const std::string& key, const std::vector<char>& buffer) = 0;
/**
* @brief Loads raw key/value data from this KeyValueContainer.
* This function is virtual and must be overloaded in the child class.
*
* @param key Key
* @param buffer Buffer used to hold the value.
*
* @return true if the key exists, false otherwise.
*/
virtual bool loadRawData(const std::string& key, std::vector<char>& buffer) const = 0;
/**
* @brief Stores a key/value pair into the KeyValueContainer.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/", or "%" characters. The
* type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to store.
* @param value Value to store.
*
* @return true if the key was found. false otherwise.
*/
template<typename K, typename V>
bool store(const K& key, const V& value) {
std::stringstream ss_value;
std::stringstream ss_key;
ss_key << key << "#" << demangle<V>();
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 storeRawData(ss_key.str(), buffer);
}
/**
* @brief Loads a value associated with a key from the
* KeyValueContainer. The type of the key should have
* operator<< available to stream it into a std::stringstream
* for the purpose of converting it into an std::string.
* The resulting string must not have the "/" or "%" characters.
* The type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to load.
* @param value Value to load.
*
* @return true if the key exists and was loaded. False otherwise.
*/
template<typename K, typename V>
bool load(const K& key, V& value) const {
std::vector<char> buffer;
std::stringstream ss_key;
ss_key << key << "#" << demangle<V>();
if(!loadRawData(ss_key.str(), 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;
}
};
}
#endif
......@@ -3,11 +3,7 @@
#include <memory>
#include <string>
#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/KeyValueContainer.hpp>
#include <hepnos/DataStore.hpp>
#include <hepnos/RunNumber.hpp>
#include <hepnos/SubRun.hpp>
......@@ -17,7 +13,7 @@ namespace hepnos {
class RunSet;
class Run {
class Run : public KeyValueContainer {
private:
......@@ -154,67 +150,6 @@ class Run {
*/
const std::string& container() const;
/**
* @brief Stores a key/value pair into the Run.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/" or "%" characters. The
* type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to store.
* @param value Value to store.
*
* @return true if the key was found. false otherwise.
*/
template<typename K, typename V>
bool store(const K& key, const V& value) {
std::stringstream ss_value;
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 storeRawData(key, buffer);
}
/**
* @brief Loads a value associated with a key from the Run.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/" or "%" characters. The
* type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to load.
* @param value Value to load.
*
* @return true if the key exists and was loaded. False otherwise.
*/
template<typename K, typename V>
bool load(const K& key, V& value) const {
std::vector<char> buffer;
if(!loadRawData(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;
}
class const_iterator;
class iterator;
......@@ -366,6 +301,231 @@ class Run {
SubRun createSubRun(const SubRunNumber& subRunNumber);
};
class Run::const_iterator {
protected:
/**
* @brief Implementation of the class (using Pimpl idiom)
*/
class Impl;
std::unique_ptr<Impl> m_impl; /*!< Pointer to implementation */
public:
/**
* @brief Constructor. Creates a const_iterator pointing
* to an invalid SubRun.
*/
const_iterator();
/**
* @brief Constructor. Creates a const_iterator pointing
* to a given SubRun. The SubRun may or may not be valid.
*
* @param current SubRun to make the const_iterator point to.
*/
const_iterator(const SubRun& current);
/**
* @brief Constructor. Creates a const_iterator pointing
* to a given SubRun. The SubRun may or may not be valid.
*
* @param current SubRun to make the const_iterator point to.
*/
const_iterator(SubRun&& current);
typedef const_iterator self_type;
typedef SubRun value_type;
typedef SubRun& reference;
typedef SubRun* pointer;
typedef int difference_type;
typedef std::forward_iterator_tag iterator_category;
/**
* @brief Destructor. This destructor is virtual because
* the iterator class inherits from const_iterator.
*/
virtual ~const_iterator();
/**
* @brief Copy-constructor.
*
* @param other const_iterator to copy.
*/
const_iterator(const const_iterator& other);
/**
* @brief Move-constructor.
*
* @param other const_iterator to move.
*/
const_iterator(const_iterator&& other);
/**
* @brief Copy-assignment operator.
*
* @param other const_iterator to copy.
*
* @return this.
*/
const_iterator& operator=(const const_iterator&);
/**
* @brief Move-assignment operator.
*
* @param other const_iterator to move.
*
* @return this.
*/
const_iterator& operator=(const_iterator&&);
/**
* @brief Increments the const_iterator, returning
* a copy of the iterator after incrementation.
*
* @return a copy of the iterator after incrementation.
*/
self_type operator++();
/**
* @brief Increments the const_iterator, returning
* a copy of the iterator before incrementation.
*
* @return a copy of the iterator after incrementation.
*/
self_type operator++(int);
/**
* @brief Dereference operator. Returns a const reference
* to the SubRun this const_iterator points to.
*
* @return a const reference to the DataSet this
* const_iterator points to.
*/
const reference operator*();
/**
* @brief Returns a const pointer to the SubRun this
* const_iterator points to.
*
* @return a const pointer to the SubRun this
* const_iterator points to.
*/
const pointer operator->();
/**
* @brief Compares two const_iterators. The two const_iterators
* are equal if they point to the same SubRun or if both
* correspond to Run::cend().
*
* @param rhs const_iterator to compare with.
*
* @return true if the two const_iterators are equal, false otherwise.
*/
bool operator==(const self_type& rhs) const;
/**
* @brief Compares two const_iterators.
*
* @param rhs const_iterator to compare with.
*
* @return true if the two const_iterators are different, false otherwise.
*/
bool operator!=(const self_type& rhs) const;
};
class Run::iterator : public Run::const_iterator {
public:
/**
* @brief Constructor. Builds an iterator pointing to an
* invalid SubRun.
*/
iterator();
/**
* @brief Constructor. Builds an iterator pointing to
* an existing SubRun. The SubRun may or may not be
* valid.
*
* @param current SubRun to point to.
*/
iterator(const SubRun& current);
/**
* @brief Constructor. Builds an iterator pointing to
* an existing SubRun. The SubRun may or may not be
* valid.
*
* @param current SubRun to point to.
*/
iterator(SubRun&& current);
typedef iterator self_type;
typedef SubRun value_type;
typedef SubRun& reference;
typedef SubRun* pointer;
typedef int difference_type;
typedef std::forward_iterator_tag iterator_category;
/**
* @brief Destructor.
*/
~iterator();
/**
* @brief Copy constructor.
*
* @param other iterator to copy.
*/
iterator(const iterator& other);
/**
* @brief Move constructor.
*
* @param other iterator to move.
*/
iterator(iterator&& other);
/**
* @brief Copy-assignment operator.
*
* @param other iterator to copy.
*
* @return this.
*/
iterator& operator=(const iterator& other);
/**
* @brief Move-assignment operator.
*
* @param other iterator to move.
*
* @return this.
*/
iterator& operator=(iterator&& other);
/**
* @brief Dereference operator. Returns a reference
* to the SubRun this iterator points to.
*
* @return A reference to the SubRun this iterator
* points to.
*/
reference operator*();
/**
* @brief Returns a pointer to the SubRun this iterator
* points to.
*
* @return A pointer to the SubRun this iterator points to.
*/
pointer operator->();
};
}
#endif
......@@ -3,11 +3,7 @@
#include <memory>
#include <string>
#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/KeyValueContainer.hpp>
#include <hepnos/DataStore.hpp>
#include <hepnos/Event.hpp>
#include <hepnos/SubRunNumber.hpp>
......@@ -15,7 +11,7 @@
namespace hepnos {
class SubRun {
class SubRun : public KeyValueContainer {
private:
......@@ -151,67 +147,6 @@ class SubRun {
*/
const std::string& container() const;
/**
* @brief Stores a key/value pair into the SubRun.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/" or "%" characters. The
* type of the value must be serializable using Boost.
*
* @tparam K type of the key.
* @tparam V type of the value.
* @param key Key to store.
* @param value Value to store.
*
* @return true if the key was found. false otherwise.
*/
template<typename K, typename V>
bool store(const K& key, const V& value) {
std::stringstream ss_value;
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 storeRawData(key, buffer);
}
/**
* @brief Loads a value associated with a key from the SubRun.
* The type of the key should have operator<< available
* to stream it into a std::stringstream for the purpose
* of converting it into an std::string. The resulting
* string must not have the "/" or "%" characters. The
* type of the value must be serializable using Boost.
*