14 #define GAUDI_PLUGIN_SERVICE_V2 34 #if __GNUC__ >= 8 && __cplusplus >= 201703 35 # include <filesystem> 36 namespace fs = std::filesystem;
38 # include <experimental/filesystem> 39 namespace fs = std::experimental::filesystem;
42 #if __cplusplus >= 201703 43 # include <string_view> 45 # include <experimental/string_view> 47 using experimental::string_view;
51 #define REG_SCOPE_LOCK std::lock_guard<std::recursive_mutex> _guard( m_mutex ); 56 #define SINGLETON_LOCK std::lock_guard<std::mutex> _guard( ::registrySingletonMutex ); 95 namespace PluginService {
101 abi::__cxa_demangle(
id.c_str(),
nullptr,
nullptr, &status ),
free );
102 if ( !realname )
return id;
103 #if _GLIBCXX_USE_CXX11_ABI 106 std::regex{
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?"},
114 Registry& Registry::instance() {
123 const auto& info = Registry::instance().getInfo(
id );
124 msg <<
"bad any_cast: requested factory " <<
id <<
" of type " <<
demangle( factory_type ) <<
", got ";
126 msg <<
demangle( info.factory.type() ) <<
" from " << info.library;
133 Registry::Properties::mapped_type Registry::FactoryInfo::getprop(
const Properties::key_type&
name )
const {
135 return ( p !=
end(
properties ) ) ? p->second : Properties::mapped_type{};
138 Registry::Registry() {}
140 void Registry::initialize() {
142 #if defined( _WIN32 ) 143 const char* envVar =
"PATH";
144 const char sep =
';';
145 #elif defined( __APPLE__ ) 146 const char* envVar =
"DYLD_LIBRARY_PATH";
147 const char sep =
':';
149 const char* envVar =
"LD_LIBRARY_PATH";
150 const char sep =
':';
153 std::regex line_format{
"^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$"};
156 std::string_view search_path =
std::getenv( envVar );
157 if ( !search_path.empty() ) {
160 std::string_view::size_type start_pos = 0, end_pos = 0;
161 while ( start_pos != std::string_view::npos ) {
163 if ( start_pos ) ++start_pos;
165 end_pos = search_path.find( sep, start_pos );
168 search_path.substr( start_pos, end_pos - start_pos );
174 logger().debug(
" looking into " + dirName.string() );
176 if ( is_directory( dirName ) ) {
177 for (
auto& p : fs::directory_iterator( dirName ) ) {
178 if ( p.path().extension() ==
".components" && is_regular_file( p.path() ) ) {
180 const auto& fullPath = p.path().string();
181 logger().debug(
" reading " + p.path().filename().string() );
184 int factoriesCount = 0;
190 if ( m[1] ==
"v2" ) {
193 m_factories.emplace( fact, FactoryInfo{lib, {}, {{
"ClassName", fact}}} );
194 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES 197 if ( fact != old_name ) {
198 m_factories.emplace( old_name,
199 FactoryInfo{lib, {}, {{
"ReflexName",
"true"}, {
"ClassName", fact}}} );
219 std::call_once( m_initialized, &Registry::initialize, const_cast<Registry*>(
this ) );
228 Registry::FactoryInfo&
Registry::add(
const KeyType&
id, FactoryInfo info ) {
232 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES 234 const auto old_name = old_style_name(
id );
235 if (
id != old_name ) {
236 auto new_info = info;
238 new_info.properties[
"ReflexName"] =
"true";
240 add( old_name, new_info );
244 auto entry = facts.find(
id );
245 if ( entry == facts.end() ) {
247 entry = facts.emplace(
id,
std::move( info ) ).first;
250 if ( !entry->second.is_set() ) entry->second =
std::move( info );
252 return entry->second;
255 const Registry::FactoryInfo& Registry::getInfo(
const KeyType&
id,
const bool load )
const {
257 static const FactoryInfo unknown = {
"unknown"};
259 auto f = facts.find(
id );
261 if ( f != facts.end() ) {
262 if ( load && !f->second.is_set() ) {
264 if ( !dlopen( library.
c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
265 logger().warning(
"cannot load " + library +
" for factory " +
id );
266 char* dlmsg = dlerror();
267 if ( dlmsg )
logger().warning( dlmsg );
270 f = facts.find(
id );
278 Registry& Registry::addProperty(
const KeyType&
id,
const KeyType& k,
const std::string& v ) {
281 auto f = facts.find(
id );
283 if ( f != facts.end() ) f->second.properties[k] = v;
291 if ( f.second.is_set() ) l.insert( f.first );
297 static const char* levels[] = {
"DEBUG : ",
"INFO : ",
"WARNING: ",
"ERROR : "};
301 static auto s_logger = std::make_unique<Logger>();
309 if ( dladdr( fptr, &info ) == 0 )
return "";
315 return info.dli_fname;
324 using namespace Details;
326 if ( debugLevel > 1 )
328 else if ( debugLevel > 0 )
329 l.setLevel( Logger::Info );
331 l.setLevel( Logger::Warning );
335 using namespace Details;
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
void SetDebug(int debugLevel)
std::string getDSONameFor(void *fptr)
void reportBadAnyCast(const std::type_info &factory_type, const std::string &id)
T regex_replace(T...args)
std::string demangle(const std::type_info &id)
void setLogger(Logger *logger)
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
Helper functions to set/get the application return code.