Mantid
Loading...
Searching...
No Matches
TypeRegistry.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//-----------------------------------------------------------------------------
8// Includes
9//-----------------------------------------------------------------------------
13#include <boost/python/type_id.hpp>
14#include <map>
15
17namespace // <anonymous>
18{
22using TypeIDMap = std::map<const boost::python::type_info, std::shared_ptr<PropertyValueHandler>>;
23
28TypeIDMap &typeRegistry() {
29 static TypeIDMap typeHandlers;
30 return typeHandlers;
31}
32} // namespace
33
34//-------------------------------------------------------------------------------------------
35// Public methods
36//-------------------------------------------------------------------------------------------
37
39// -- Register a handler for each basic type and vector of each basic type +
40// std::string --
41// macro helps with keeping information in one place
42#define SUBSCRIBE_HANDLER(Type) \
43 subscribe<TypedPropertyValueHandler<Type>>(); \
44 subscribe<SequenceTypeHandler<std::vector<Type>>>();
45
46 // unsigned ints
49 SUBSCRIBE_HANDLER(long long);
50 // signed ints
51 SUBSCRIBE_HANDLER(unsigned int);
52 SUBSCRIBE_HANDLER(unsigned long);
53 SUBSCRIBE_HANDLER(unsigned long long);
54 // boolean
56 // double
57 SUBSCRIBE_HANDLER(double);
58 // string
59 SUBSCRIBE_HANDLER(std::string);
60
61#undef SUBSCRIBE_HANDLER
62}
63
71void TypeRegistry::subscribe(const std::type_info &typeObject, PropertyValueHandler *handler) {
72 TypeIDMap &typeHandlers = typeRegistry();
73 boost::python::type_info typeInfo(typeObject);
74 if (typeHandlers.find(typeInfo) == typeHandlers.end()) {
75 typeHandlers.emplace(typeInfo, std::shared_ptr<PropertyValueHandler>(handler));
76 } else {
77 throw std::invalid_argument(std::string("TypeRegistry::subscribe() - A handler has already "
78 "registered for type '") +
79 typeInfo.name() + "'");
80 }
81}
82
89const PropertyValueHandler &TypeRegistry::retrieve(const std::type_info &typeObject) {
90 TypeIDMap &typeHandlers = typeRegistry();
91 TypeIDMap::const_iterator itr = typeHandlers.find(boost::python::type_info(typeObject));
92 if (itr != typeHandlers.end()) {
93 return *(itr->second);
94 } else {
95 throw std::invalid_argument(std::string("TypeRegistry::retrieve(): No PropertyValueHandler "
96 "registered for type '") +
97 boost::python::type_info(typeObject).name() + "'");
98 }
99}
100} // namespace Mantid::PythonInterface::Registry
#define SUBSCRIBE_HANDLER(Type)
static void subscribe()
Subscribe a handler object for given template type.
Definition: TypeRegistry.h:36
static void registerBuiltins()
Register handlers for basic C++ types into the registry.
static const PropertyValueHandler & retrieve(const std::type_info &typeInfo)
Lookup a handler base on a given type_info object.
This class provides a base-class objects that are able to take a python object and set it on an algor...