Commit 336691b5 authored by Matthieu Dorier's avatar Matthieu Dorier

done with adding exceptions everywhere. It will throw like fireworks now.

parent 699ac222
...@@ -169,11 +169,15 @@ Run DataSet::createRun(const RunNumber& runNumber) { ...@@ -169,11 +169,15 @@ Run DataSet::createRun(const RunNumber& runNumber) {
DataSet DataSet::operator[](const std::string& datasetName) const { DataSet DataSet::operator[](const std::string& datasetName) const {
auto it = find(datasetName); auto it = find(datasetName);
if(!it->valid())
throw Exception("Requested DataSet does not exist");
return std::move(*it); return std::move(*it);
} }
Run DataSet::operator[](const RunNumber& runNumber) const { Run DataSet::operator[](const RunNumber& runNumber) const {
auto it = runs().find(runNumber); auto it = runs().find(runNumber);
if(!it->valid())
throw Exception("Requested Run does not exist");
return std::move(*it); return std::move(*it);
} }
......
...@@ -87,6 +87,8 @@ DataStore::iterator DataStore::find(const std::string& datasetPath) { ...@@ -87,6 +87,8 @@ DataStore::iterator DataStore::find(const std::string& datasetPath) {
DataSet DataStore::operator[](const std::string& datasetName) const { DataSet DataStore::operator[](const std::string& datasetName) const {
auto it = find(datasetName); auto it = find(datasetName);
if(!it->valid())
throw Exception("Requested DataSet does not exist");
return std::move(*it); return std::move(*it);
} }
......
...@@ -15,14 +15,17 @@ Event::Event() ...@@ -15,14 +15,17 @@ Event::Event()
Event::Event(DataStore* ds, uint8_t level, const std::string& container, const EventNumber& rn) Event::Event(DataStore* ds, uint8_t level, const std::string& container, const EventNumber& rn)
: m_impl(std::make_unique<Impl>(ds, level, container, rn)) { } : m_impl(std::make_unique<Impl>(ds, level, container, rn)) { }
Event::Event(const Event& other) Event::Event(const Event& other) {
: m_impl(std::make_unique<Impl>(*other.m_impl)) {} if(other.m_impl)
m_impl = std::make_unique<Impl>(*other.m_impl);
}
Event::Event(Event&&) = default; Event::Event(Event&&) = default;
Event& Event::operator=(const Event& other) { Event& Event::operator=(const Event& other) {
if(this == &other) return *this; if(this == &other) return *this;
m_impl = std::make_unique<Impl>(*other.m_impl); if(other.m_impl)
m_impl = std::make_unique<Impl>(*other.m_impl);
return *this; return *this;
} }
...@@ -31,7 +34,9 @@ Event& Event::operator=(Event&&) = default; ...@@ -31,7 +34,9 @@ Event& Event::operator=(Event&&) = default;
Event::~Event() = default; Event::~Event() = default;
DataStore* Event::getDataStore() const { DataStore* Event::getDataStore() const {
if(!m_impl) return nullptr; if(!valid()) {
throw Exception("Calling Event member function on an invalid Event object");
}
return m_impl->m_datastore; return m_impl->m_datastore;
} }
...@@ -61,7 +66,7 @@ bool Event::valid() const { ...@@ -61,7 +66,7 @@ bool Event::valid() const {
ProductID Event::storeRawData(const std::string& key, const std::vector<char>& buffer) { ProductID Event::storeRawData(const std::string& key, const std::vector<char>& buffer) {
if(!valid()) { if(!valid()) {
throw Exception("Calling store() on invalid Event"); throw Exception("Calling Event member function on an invalid Event object");
} }
// forward the call to the datastore's store function // forward the call to the datastore's store function
return m_impl->m_datastore->m_impl->store(0, m_impl->fullpath(), key, buffer); return m_impl->m_datastore->m_impl->store(0, m_impl->fullpath(), key, buffer);
...@@ -69,17 +74,22 @@ ProductID Event::storeRawData(const std::string& key, const std::vector<char>& b ...@@ -69,17 +74,22 @@ ProductID Event::storeRawData(const std::string& key, const std::vector<char>& b
bool Event::loadRawData(const std::string& key, std::vector<char>& buffer) const { bool Event::loadRawData(const std::string& key, std::vector<char>& buffer) const {
if(!valid()) { if(!valid()) {
throw Exception("Calling load() on invalid Event"); throw Exception("Calling Event member function on an invalid Event object");
} }
// forward the call to the datastore's load function // forward the call to the datastore's load function
return m_impl->m_datastore->m_impl->load(0, m_impl->fullpath(), key, buffer); return m_impl->m_datastore->m_impl->load(0, m_impl->fullpath(), key, buffer);
} }
bool Event::operator==(const Event& other) const { bool Event::operator==(const Event& other) const {
bool v1 = valid();
bool v2 = other.valid();
if(!v1 && !v2) return true;
if(!v1 && v2) return false;
if(v1 && !v2) return false;
return m_impl->m_datastore == other.m_impl->m_datastore return m_impl->m_datastore == other.m_impl->m_datastore
&& m_impl->m_level == other.m_impl->m_level && m_impl->m_level == other.m_impl->m_level
&& m_impl->m_container == other.m_impl->m_container && m_impl->m_container == other.m_impl->m_container
&& m_impl->m_event_nr == other.m_impl->m_event_nr; && m_impl->m_event_nr == other.m_impl->m_event_nr;
} }
bool Event::operator!=(const Event& other) const { bool Event::operator!=(const Event& other) const {
...@@ -87,6 +97,9 @@ bool Event::operator!=(const Event& other) const { ...@@ -87,6 +97,9 @@ bool Event::operator!=(const Event& other) const {
} }
const EventNumber& Event::number() const { const EventNumber& Event::number() const {
if(!valid()) {
throw Exception("Calling Event member function on an invalid Event object");
}
return m_impl->m_event_nr; return m_impl->m_event_nr;
} }
......
...@@ -124,6 +124,8 @@ SubRun Run::createSubRun(const SubRunNumber& subRunNumber) { ...@@ -124,6 +124,8 @@ SubRun Run::createSubRun(const SubRunNumber& subRunNumber) {
SubRun Run::operator[](const SubRunNumber& subRunNumber) const { SubRun Run::operator[](const SubRunNumber& subRunNumber) const {
auto it = find(subRunNumber); auto it = find(subRunNumber);
if(!it->valid())
throw Exception("Requested SubRun does not exist");
return std::move(*it); return std::move(*it);
} }
......
...@@ -27,6 +27,8 @@ RunSet::~RunSet() {} ...@@ -27,6 +27,8 @@ RunSet::~RunSet() {}
Run RunSet::operator[](const RunNumber& runNumber) { Run RunSet::operator[](const RunNumber& runNumber) {
auto it = find(runNumber); auto it = find(runNumber);
if(!it->valid())
throw Exception("Requested Run does not exist");
return std::move(*it); return std::move(*it);
} }
......
...@@ -17,14 +17,19 @@ SubRun::SubRun() ...@@ -17,14 +17,19 @@ SubRun::SubRun()
SubRun::SubRun(DataStore* ds, uint8_t level, const std::string& container, const SubRunNumber& rn) SubRun::SubRun(DataStore* ds, uint8_t level, const std::string& container, const SubRunNumber& rn)
: m_impl(std::make_unique<Impl>(ds, level, container, rn)) { } : m_impl(std::make_unique<Impl>(ds, level, container, rn)) { }
SubRun::SubRun(const SubRun& other) SubRun::SubRun(const SubRun& other) {
: m_impl(std::make_unique<Impl>(*other.m_impl)) {} if(other.m_impl)
m_impl = std::make_unique<Impl>(*other.m_impl);
}
SubRun::SubRun(SubRun&&) = default; SubRun::SubRun(SubRun&&) = default;
SubRun& SubRun::operator=(const SubRun& other) { SubRun& SubRun::operator=(const SubRun& other) {
if(this == &other) return *this; if(this == &other) return *this;
m_impl = std::make_unique<Impl>(*other.m_impl); if(other.m_impl)
m_impl = std::make_unique<Impl>(*other.m_impl);
else
m_impl.reset();
return *this; return *this;
} }
...@@ -33,7 +38,9 @@ SubRun& SubRun::operator=(SubRun&&) = default; ...@@ -33,7 +38,9 @@ SubRun& SubRun::operator=(SubRun&&) = default;
SubRun::~SubRun() = default; SubRun::~SubRun() = default;
DataStore* SubRun::getDataStore() const { DataStore* SubRun::getDataStore() const {
if(!m_impl) return nullptr; if(!valid()) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
return m_impl->m_datastore; return m_impl->m_datastore;
} }
...@@ -58,12 +65,11 @@ SubRun SubRun::next() const { ...@@ -58,12 +65,11 @@ SubRun SubRun::next() const {
bool SubRun::valid() const { bool SubRun::valid() const {
return m_impl && m_impl->m_datastore; return m_impl && m_impl->m_datastore;
} }
ProductID SubRun::storeRawData(const std::string& key, const std::vector<char>& buffer) { ProductID SubRun::storeRawData(const std::string& key, const std::vector<char>& buffer) {
if(!valid()) { if(!valid()) {
throw Exception("Calling store() on invalid SubRun"); throw Exception("Calling SubRun member function on invalid SubRun object");
} }
// forward the call to the datastore's store function // forward the call to the datastore's store function
return m_impl->m_datastore->m_impl->store(0, m_impl->fullpath(), key, buffer); return m_impl->m_datastore->m_impl->store(0, m_impl->fullpath(), key, buffer);
...@@ -71,13 +77,18 @@ ProductID SubRun::storeRawData(const std::string& key, const std::vector<char>& ...@@ -71,13 +77,18 @@ ProductID SubRun::storeRawData(const std::string& key, const std::vector<char>&
bool SubRun::loadRawData(const std::string& key, std::vector<char>& buffer) const { bool SubRun::loadRawData(const std::string& key, std::vector<char>& buffer) const {
if(!valid()) { if(!valid()) {
throw Exception("Calling load() on invalid SubRun"); throw Exception("Calling SubRun member function on invalid SubRun object");
} }
// forward the call to the datastore's load function // forward the call to the datastore's load function
return m_impl->m_datastore->m_impl->load(0, m_impl->fullpath(), key, buffer); return m_impl->m_datastore->m_impl->load(0, m_impl->fullpath(), key, buffer);
} }
bool SubRun::operator==(const SubRun& other) const { bool SubRun::operator==(const SubRun& other) const {
bool v1 = valid();
bool v2 = other.valid();
if(!v1 && !v2) return true;
if(!v1 && v2) return false;
if(v1 && !v2) return false;
return m_impl->m_datastore == other.m_impl->m_datastore return m_impl->m_datastore == other.m_impl->m_datastore
&& m_impl->m_level == other.m_impl->m_level && m_impl->m_level == other.m_impl->m_level
&& m_impl->m_container == other.m_impl->m_container && m_impl->m_container == other.m_impl->m_container
...@@ -93,6 +104,9 @@ const SubRunNumber& SubRun::number() const { ...@@ -93,6 +104,9 @@ const SubRunNumber& SubRun::number() const {
} }
Event SubRun::createEvent(const EventNumber& eventNumber) { Event SubRun::createEvent(const EventNumber& eventNumber) {
if(!valid()) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
std::string parent = m_impl->fullpath(); std::string parent = m_impl->fullpath();
std::string eventStr = Event::Impl::makeKeyStringFromEventNumber(eventNumber); std::string eventStr = Event::Impl::makeKeyStringFromEventNumber(eventNumber);
m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, eventStr, std::vector<char>()); m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, eventStr, std::vector<char>());
...@@ -101,10 +115,15 @@ Event SubRun::createEvent(const EventNumber& eventNumber) { ...@@ -101,10 +115,15 @@ Event SubRun::createEvent(const EventNumber& eventNumber) {
Event SubRun::operator[](const EventNumber& eventNumber) const { Event SubRun::operator[](const EventNumber& eventNumber) const {
auto it = find(eventNumber); auto it = find(eventNumber);
if(!it->valid())
throw Exception("Requested Event does not exist");
return std::move(*it); return std::move(*it);
} }
SubRun::iterator SubRun::find(const EventNumber& eventNumber) { SubRun::iterator SubRun::find(const EventNumber& eventNumber) {
if(!valid()) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
int ret; int ret;
std::vector<char> data; std::vector<char> data;
std::string parent = m_impl->fullpath(); std::string parent = m_impl->fullpath();
...@@ -136,6 +155,9 @@ SubRun::iterator SubRun::begin() { ...@@ -136,6 +155,9 @@ SubRun::iterator SubRun::begin() {
} }
SubRun::iterator SubRun::end() { SubRun::iterator SubRun::end() {
if(!valid()) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
return m_impl->m_end; return m_impl->m_end;
} }
...@@ -144,6 +166,9 @@ SubRun::const_iterator SubRun::begin() const { ...@@ -144,6 +166,9 @@ SubRun::const_iterator SubRun::begin() const {
} }
SubRun::const_iterator SubRun::end() const { SubRun::const_iterator SubRun::end() const {
if(!valid()) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
return m_impl->m_end; return m_impl->m_end;
} }
...@@ -152,6 +177,9 @@ SubRun::const_iterator SubRun::cbegin() const { ...@@ -152,6 +177,9 @@ SubRun::const_iterator SubRun::cbegin() const {
} }
SubRun::const_iterator SubRun::cend() const { SubRun::const_iterator SubRun::cend() const {
if(!valid()) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
return m_impl->m_end; return m_impl->m_end;
} }
...@@ -189,6 +217,9 @@ SubRun::const_iterator SubRun::lower_bound(const EventNumber& lb) const { ...@@ -189,6 +217,9 @@ SubRun::const_iterator SubRun::lower_bound(const EventNumber& lb) const {
} }
SubRun::iterator SubRun::upper_bound(const EventNumber& ub) { SubRun::iterator SubRun::upper_bound(const EventNumber& ub) {
if(!valid()) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
Event event(m_impl->m_datastore, Event event(m_impl->m_datastore,
m_impl->m_level+1, m_impl->m_level+1,
m_impl->fullpath(), ub); m_impl->fullpath(), ub);
...@@ -246,15 +277,20 @@ SubRun::const_iterator::const_iterator(Event&& event) ...@@ -246,15 +277,20 @@ SubRun::const_iterator::const_iterator(Event&& event)
SubRun::const_iterator::~const_iterator() {} SubRun::const_iterator::~const_iterator() {}
SubRun::const_iterator::const_iterator(const SubRun::const_iterator& other) SubRun::const_iterator::const_iterator(const SubRun::const_iterator& other) {
: m_impl(std::make_unique<Impl>(*other.m_impl)) {} if(other.m_impl)
m_impl = std::make_unique<Impl>(*other.m_impl);
}
SubRun::const_iterator::const_iterator(SubRun::const_iterator&& other) SubRun::const_iterator::const_iterator(SubRun::const_iterator&& other)
: m_impl(std::move(other.m_impl)) {} : m_impl(std::move(other.m_impl)) {}
SubRun::const_iterator& SubRun::const_iterator::operator=(const SubRun::const_iterator& other) { SubRun::const_iterator& SubRun::const_iterator::operator=(const SubRun::const_iterator& other) {
if(&other == this) return *this; if(&other == this) return *this;
m_impl = std::make_unique<Impl>(*other.m_impl); if(other.m_impl)
m_impl = std::make_unique<Impl>(*other.m_impl);
else
m_impl.reset();
return *this; return *this;
} }
...@@ -286,7 +322,9 @@ const SubRun::const_iterator::reference SubRun::const_iterator::operator*() { ...@@ -286,7 +322,9 @@ const SubRun::const_iterator::reference SubRun::const_iterator::operator*() {
} }
const SubRun::const_iterator::pointer SubRun::const_iterator::operator->() { const SubRun::const_iterator::pointer SubRun::const_iterator::operator->() {
if(!m_impl) return nullptr; if(!m_impl) {
throw Exception("Trying to dereference an invalid iterator");
}
return &(m_impl->m_current_event); return &(m_impl->m_current_event);
} }
...@@ -324,7 +362,10 @@ SubRun::iterator::iterator(SubRun::iterator&& other) ...@@ -324,7 +362,10 @@ SubRun::iterator::iterator(SubRun::iterator&& other)
SubRun::iterator& SubRun::iterator::operator=(const SubRun::iterator& other) { SubRun::iterator& SubRun::iterator::operator=(const SubRun::iterator& other) {
if(this == &other) return *this; if(this == &other) return *this;
m_impl = std::make_unique<Impl>(*other.m_impl); if(other.m_impl)
m_impl = std::make_unique<Impl>(*other.m_impl);
else
m_impl.reset();
return *this; return *this;
} }
......
...@@ -71,8 +71,7 @@ void DataSetTest::testBraketOperator() { ...@@ -71,8 +71,7 @@ void DataSetTest::testBraketOperator() {
CPPUNIT_ASSERT(mds.valid()); CPPUNIT_ASSERT(mds.valid());
// check that accessing a dataset that does not exist // check that accessing a dataset that does not exist
// yields a non-valid DataSet // yields a non-valid DataSet
DataSet ds6 = mds["ds6"]; CPPUNIT_ASSERT_THROW(mds["ds6"], hepnos::Exception);
CPPUNIT_ASSERT(!ds6.valid());
// check that accessing a dataset that exists yields // check that accessing a dataset that exists yields
// a valid DataSet instance with correct information // a valid DataSet instance with correct information
...@@ -169,9 +168,7 @@ void DataSetTest::testCreateRuns() { ...@@ -169,9 +168,7 @@ void DataSetTest::testCreateRuns() {
CPPUNIT_ASSERT(mds.valid()); CPPUNIT_ASSERT(mds.valid());
{ {
Run r = mds[45]; CPPUNIT_ASSERT_THROW(mds[45], hepnos::Exception);
CPPUNIT_ASSERT(!r.valid());
CPPUNIT_ASSERT_THROW(r.number(), hepnos::Exception);
} }
{ {
......
...@@ -27,10 +27,7 @@ void DataStoreTest::testFillDataStore() { ...@@ -27,10 +27,7 @@ void DataStoreTest::testFillDataStore() {
CPPUNIT_ASSERT_EQUAL_STR("", ds1.container()); CPPUNIT_ASSERT_EQUAL_STR("", ds1.container());
CPPUNIT_ASSERT_EQUAL_STR("ds1", ds1.fullname()); CPPUNIT_ASSERT_EQUAL_STR("ds1", ds1.fullname());
// assert invalid dataset when it does not exist // assert invalid dataset when it does not exist
DataSet ds_invalid = (*datastore)["invalid"]; CPPUNIT_ASSERT_THROW((*datastore)["invalid"], hepnos::Exception);
DataSet ds_invalid2 = (*datastore)["invalid2"];
CPPUNIT_ASSERT(!ds_invalid.valid());
CPPUNIT_ASSERT(!ds_invalid2.valid());
// assert comparison with a default-constructed dataset // assert comparison with a default-constructed dataset
DataSet ds0; DataSet ds0;
CPPUNIT_ASSERT(ds0 != ds1); CPPUNIT_ASSERT(ds0 != ds1);
...@@ -56,9 +53,8 @@ void DataStoreTest::testFillDataStore() { ...@@ -56,9 +53,8 @@ void DataStoreTest::testFillDataStore() {
void DataStoreTest::testBraketOperator() { void DataStoreTest::testBraketOperator() {
// check that accessing a dataset that does not exist // check that accessing a dataset that does not exist
// yields a non-valid DataSet // throws an exception
DataSet ds6 = (*datastore)["ds6"]; CPPUNIT_ASSERT_THROW((*datastore)["ds6"], hepnos::Exception);
CPPUNIT_ASSERT(!ds6.valid());
// check that accessing a dataset that exists yields // check that accessing a dataset that exists yields
// a valid DataSet instance with correct information // a valid DataSet instance with correct information
......
...@@ -54,8 +54,7 @@ void RunSetTest::testBraketOperator() { ...@@ -54,8 +54,7 @@ void RunSetTest::testBraketOperator() {
CPPUNIT_ASSERT(mds.valid()); CPPUNIT_ASSERT(mds.valid());
// check that accessing a Run that does not exist // check that accessing a Run that does not exist
// yields a non-valid Run instance // yields a non-valid Run instance
Run r6 = mds[43]; CPPUNIT_ASSERT_THROW(mds[43], hepnos::Exception);
CPPUNIT_ASSERT(!r6.valid());
// check that accessing a run that exists yields // check that accessing a run that exists yields
// a valid Run instance with correct information // a valid Run instance with correct information
...@@ -156,9 +155,7 @@ void RunSetTest::testCreateSubRuns() { ...@@ -156,9 +155,7 @@ void RunSetTest::testCreateSubRuns() {
Run r2 = mds[45]; Run r2 = mds[45];
{ {
SubRun sr = r2[73]; CPPUNIT_ASSERT_THROW(r2[73], hepnos::Exception);
CPPUNIT_ASSERT(!sr.valid());
CPPUNIT_ASSERT(sr.number() == InvalidSubRunNumber);
} }
{ {
......
...@@ -47,9 +47,7 @@ void RunTest::testBraketOperator() { ...@@ -47,9 +47,7 @@ void RunTest::testBraketOperator() {
CPPUNIT_ASSERT(r1.valid()); CPPUNIT_ASSERT(r1.valid());
// check access to non-existing SubRun // check access to non-existing SubRun
SubRun sr0 = r1[12]; CPPUNIT_ASSERT_THROW(r1[12], hepnos::Exception);
CPPUNIT_ASSERT(!sr0.valid());
CPPUNIT_ASSERT(sr0.number() == InvalidSubRunNumber);
// check access to existing SubRun // check access to existing SubRun
SubRun sr13 = r1[13]; SubRun sr13 = r1[13];
......
...@@ -52,9 +52,7 @@ void SubRunTest::testBraketOperator() { ...@@ -52,9 +52,7 @@ void SubRunTest::testBraketOperator() {
CPPUNIT_ASSERT(sr1.valid()); CPPUNIT_ASSERT(sr1.valid());
// check access to non-existing SubRun // check access to non-existing SubRun
Event e0 = sr1[12]; CPPUNIT_ASSERT_THROW(sr1[12], hepnos::Exception);
CPPUNIT_ASSERT(!e0.valid());
CPPUNIT_ASSERT(e0.number() == InvalidEventNumber);
// check access to existing SubRun // check access to existing SubRun
Event e13 = sr1[13]; Event e13 = sr1[13];
......
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