Skip to content

Commit

Permalink
More helpful exception text for Registerbase (plumed#1080)
Browse files Browse the repository at this point in the history
* tiny changes in RegisterBase

* set up a clearer exception for RegisterBase (in ActionRegister)

* set up a clearer exception for RegisterBase (in CLToolRegister)

* added a few lines of documentation fro the exception

---------

Co-authored-by: Daniele Rapetti <[email protected]>
  • Loading branch information
Iximiel and Iximiel authored Oct 10, 2024
1 parent db0236f commit 48d9562
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/core/ActionRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
along with plumed. If not, see <http://www.gnu.org/licenses/>.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
#include "ActionRegister.h"
#include "ModuleMap.h"
#include "Action.h"
#include <algorithm>

Expand All @@ -35,7 +36,7 @@ std::unique_ptr<Action> ActionRegister::create(const ActionOptions&ao) {
return create(images,ao);
}

std::unique_ptr<Action> ActionRegister::create(const std::vector<void*> & images,const ActionOptions&ao) {
std::unique_ptr<Action> ActionRegister::create(const std::vector<void*> & images,const ActionOptions&ao) try {
if(ao.line.size()<1)return nullptr;

auto content=get(images,ao.line[0]);
Expand All @@ -45,6 +46,17 @@ std::unique_ptr<Action> ActionRegister::create(const std::vector<void*> & images
auto fullPath=getFullPath(images,ao.line[0]);
nao.setFullPath(fullPath);
return content.create(nao);
} catch (PLMD::ExceptionRegisterError &e ) {
auto& actionName = e.getMissingKey();
e <<"Action \"" << actionName << "\" is not known.";
if (getModuleMap().count(actionName)>0) {
e << "\nAn Action named \""
<<actionName
<<"\" is available in module \""
<< getModuleMap().at(actionName)
<< "\".\nPlease consider installing PLUMED with that module enabled.";
}
throw e;
}

bool ActionRegister::printManual(const std::string& action, const bool& vimout, const bool& spellout) {
Expand All @@ -64,6 +76,7 @@ bool ActionRegister::printManual(const std::string& action, const bool& vimout,
}

bool ActionRegister::printTemplate(const std::string& action, bool include_optional) {
//no need to insert the try/catch block: check will ensure that action is known
if( check(action) ) {
Keywords keys; keys.thisactname = action;
get(action).keys(keys);
Expand All @@ -85,6 +98,7 @@ ActionRegister::ID ActionRegister::add(std::string key,creator_pointer cp,keywor
}

bool ActionRegister::getKeywords(const std::string& action, Keywords& keys) {
//no need to insert the try/catch block: check will ensure that action is known
if(check(action)) {
keys.thisactname = action;
get(action).keys(keys);
Expand Down
5 changes: 4 additions & 1 deletion src/core/CLToolRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ std::unique_ptr<CLTool> CLToolRegister::create(const CLToolOptions&ao) {
return create(images,ao);
}

std::unique_ptr<CLTool> CLToolRegister::create(const std::vector<void*> & images,const CLToolOptions&ao) {
std::unique_ptr<CLTool> CLToolRegister::create(const std::vector<void*> & images,const CLToolOptions&ao) try {
if(ao.line.size()<1)return nullptr;
auto & content=get(images,ao.line[0]);
CLToolOptions nao( ao,content.keys );
return content.create(nao);
} catch (PLMD::ExceptionRegisterError &e ) {
auto& toolName = e.getMissingKey();
throw e <<"CL tool \"" << toolName << "\" is not known.";
}

CLToolRegister::ID CLToolRegister::add(std::string key,creator_pointer cp,keywords_pointer kp) {
Expand Down
43 changes: 38 additions & 5 deletions src/core/RegisterBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "tools/Exception.h"
#include <string>
#include <string_view>
#include <map>
#include <memory>
#include <iostream>
Expand Down Expand Up @@ -95,6 +96,34 @@ class Register {
friend std::ostream & operator<<(std::ostream &log,const Register &reg);
};

/// Class representing an error in a register
class ExceptionRegisterError :
public Exception {
/// The missing key
std::string missingKey;
public:
using Exception::Exception;
/// Sets the missing key
/// \param key The missing key
/// \return This exception
///
/// ExceptionRegisterError can be used as a builder pattern:
/// `throw ExceptionRegisterError().setMissingKey(key);`
///
/// the key can be retrieved with ExceptionRegisterError::getMissingKey()
ExceptionRegisterError& setMissingKey (std::string_view key) {
missingKey=key;
return *this;
}
/// Returns the missing key
const std::string& getMissingKey() const {return missingKey;}
template<typename T>
ExceptionRegisterError& operator<<(const T & x) {
*static_cast<Exception*>(this) <<x;
return *this;
}
};

/// General register.
/// This class provide a generic implementation based on the content of the Register
template<class Content>
Expand Down Expand Up @@ -208,7 +237,9 @@ const Content & RegisterBase<Content>::get(const std::vector<void*> & images,con
auto qualified_key=imageToString(*image) + ":" + key;
if(m.count(qualified_key)>0) return m.find(qualified_key)->second->content;
}
plumed_assert(m.count(key)>0);
if (m.count(key) == 0 ) {
throw ExceptionRegisterError().setMissingKey(key);
}
return m.find(key)->second->content;
}

Expand All @@ -220,15 +251,19 @@ const std::string & RegisterBase<Content>::getFullPath(const std::vector<void*>
auto qualified_key=imageToString(*image) + ":" + key;
if(m.count(qualified_key)>0) return m.find(qualified_key)->second->fullPath;
}
plumed_assert(m.count(key)>0);
if (m.count(key) == 0 ) {
throw ExceptionRegisterError().setMissingKey(key);
}
return m.find(key)->second->fullPath;
}

template<class Content>
const Content & RegisterBase<Content>::get(const std::string & key) const {
// lock map for reading
std::shared_lock<std::shared_mutex> lock(mutex);
plumed_assert(m.count(key)>0);
if (m.count(key) == 0 ) {
throw ExceptionRegisterError().setMissingKey(key);
}
return m.find(key)->second->content;
}

Expand Down Expand Up @@ -288,5 +323,3 @@ void RegisterBase<Content>::clearStaged() noexcept {
}

#endif


0 comments on commit 48d9562

Please sign in to comment.