29 namespace PluginService {
41 template <
typename R,
typename... Args>
42 struct Traits<R( Args... )> {
43 using ReturnType = std::unique_ptr<std::remove_pointer_t<R>>;
44 using FactoryType = std::function<ReturnType( Args... )>;
50 std::string
demangle(
const std::type_info&
id );
59 template <
typename ID>
60 inline std::string stringify_id(
const ID&
id ) {
67 inline std::string stringify_id<std::string>(
const std::string&
id ) {
72 void reportBadAnyCast(
const std::type_info& factory_type,
const std::string&
id );
82 inline void info(
const std::string&
msg ) { report( Info,
msg ); }
83 inline void debug(
const std::string&
msg ) { report(
Debug,
msg ); }
84 inline void warning(
const std::string&
msg ) { report( Warning,
msg ); }
85 inline void error(
const std::string&
msg ) { report( Error,
msg ); }
88 virtual void report(
Level lvl,
const std::string&
msg );
101 using KeyType = std::string;
104 using Properties = std::map<KeyType, std::string>;
111 inline bool is_set()
const {
return factory.has_value(); }
112 Properties::mapped_type getprop(
const Properties::key_type&
name )
const;
116 using FactoryMap = std::map<KeyType, FactoryInfo>;
119 template <
typename F>
120 F
get(
const KeyType&
id ) {
121 const FactoryInfo& info = Registry::instance().getInfo(
id,
true );
124 const auto& prop = info.properties.find(
"deprecated" );
125 if ( prop != info.properties.end() ) {
126 std::string
msg =
"factory '" + info.getprop(
"ClassName" ) +
"' is deprecated" +
127 ( prop->second.empty() ?
"" : (
": " + prop->second ) );
128 if ( m_werror.find(
"deprecated" ) != m_werror.end() ) {
130 return std::any_cast<F>( std::any() );
136 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
137 if ( !info.getprop(
"ReflexName" ).empty() ) {
138 const std::string real_name = info.getprop(
"ClassName" );
139 logger().warning(
"requesting factory via old name '" +
id +
"' use '" +
140 ( real_name.empty() ?
"<undefined>" : real_name ) +
"' instead" );
143 return std::any_cast<F>( info.factory );
147 static Registry& instance();
150 FactoryInfo&
add(
const KeyType&
id, FactoryInfo info );
153 FactoryMap::size_type erase(
const KeyType&
id );
156 const FactoryInfo& getInfo(
const KeyType&
id,
const bool load =
false )
const;
159 Registry& addProperty(
const KeyType&
id,
const KeyType& k,
const std::string&
v );
162 void setError(
const KeyType& warning );
165 void unsetError(
const KeyType& warning );
168 std::set<KeyType> loadedFactoryNames()
const;
182 Registry(
const Registry& ) =
delete;
191 bool tryDLOpen(
const std::string_view& )
const;
194 mutable std::once_flag m_initialized;
197 FactoryMap m_factories;
200 std::set<KeyType> m_werror;
203 mutable std::recursive_mutex m_mutex;
210 template <
typename,
typename>
211 struct DefaultFactory;
212 template <
typename T,
typename R,
typename... Args>
213 struct DefaultFactory<T, Factory<R( Args... )>> {
214 inline typename Factory<R( Args... )>::ReturnType operator()( Args...
args ) {
215 return std::make_unique<T>( std::move(
args )... );
233 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial ) _register_##serial
234 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( serial ) \
235 _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial )
236 #define _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( __LINE__ )