1 #ifndef GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV2_H
2 #define GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV2_H
31 namespace PluginService {
43 template <
typename R,
typename... Args>
44 struct Traits<R( Args... )> {
45 using ReturnType = std::unique_ptr<std::remove_pointer_t<R>>;
46 using FactoryType = std::function<ReturnType( Args... )>;
52 std::string
demangle(
const std::type_info&
id );
61 template <
typename ID>
62 inline std::string stringify_id(
const ID&
id ) {
69 inline std::string stringify_id<std::string>(
const std::string&
id ) {
74 void reportBadAnyCast(
const std::type_info& factory_type,
const std::string&
id );
79 enum Level {
Debug = 0, Info = 1, Warning = 2, Error = 3 };
80 Logger( Level level = Warning ) : m_level( level ) {}
82 inline Level level()
const {
return m_level; }
83 inline void setLevel( Level level ) { m_level = level; }
84 inline void info(
const std::string& msg ) { report( Info, msg ); }
85 inline void debug(
const std::string& msg ) { report(
Debug, msg ); }
86 inline void warning(
const std::string& msg ) { report( Warning, msg ); }
87 inline void error(
const std::string& msg ) { report( Error, msg ); }
90 virtual void report( Level lvl,
const std::string& msg );
103 using KeyType = std::string;
106 using Properties = std::map<KeyType, std::string>;
111 Properties properties{};
113 inline bool is_set()
const {
return factory.has_value(); }
114 Properties::mapped_type getprop(
const Properties::key_type& name )
const;
118 using FactoryMap = std::map<KeyType, FactoryInfo>;
121 template <
typename F>
122 F get(
const KeyType&
id ) {
123 const FactoryInfo&
info = Registry::instance().getInfo(
id,
true );
124 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
125 if ( !
info.getprop(
"ReflexName" ).empty() ) {
126 const std::string real_name =
info.getprop(
"ClassName" );
127 logger().warning(
"requesting factory via old name '" +
id +
"' use '" +
128 ( real_name.empty() ?
"<undefined>" : real_name ) +
"' instead" );
131 return std::any_cast<F>(
info.factory );
135 static Registry& instance();
138 FactoryInfo& add(
const KeyType&
id, FactoryInfo
info );
141 const FactoryInfo& getInfo(
const KeyType&
id,
const bool load =
false )
const;
144 Registry& addProperty(
const KeyType&
id,
const KeyType& k,
const std::string&
v );
147 std::set<KeyType> loadedFactoryNames()
const;
154 const FactoryMap& factories()
const;
161 Registry(
const Registry& ) =
delete;
164 FactoryMap& factories();
171 mutable std::once_flag m_initialized;
174 FactoryMap m_factories;
177 mutable std::recursive_mutex m_mutex;
184 template <
typename,
typename>
185 struct DefaultFactory;
186 template <
typename T,
typename R,
typename... Args>
187 struct DefaultFactory<T, Factory<R( Args... )>> {
188 inline typename Factory<R( Args... )>::ReturnType operator()( Args... args ) {
189 return std::make_unique<T>( std::move( args )... );
207 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial ) _register_##serial
208 #define _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( serial ) \
209 _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME_TOKEN( serial )
210 #define _PS_V2_INTERNAL_FACTORY_REGISTER_CNAME _PS_V2_INTERNAL_FACTORY_MAKE_REGISTER_CNAME( __LINE__ )
212 #endif // GAUDIPLUGINSERVICE_GAUDI_DETAILS_PLUGINSERVICEDETAILSV2_H