90 std::string
demangle(
const std::string&
id ) {
92 auto realname = std::unique_ptr<char, decltype( free )*>(
93 abi::__cxa_demangle(
id.c_str(),
nullptr,
nullptr, &status ), free );
94 if ( !realname )
return id;
95 std::string result = realname.get();
97 result = std::regex_replace(
99 std::regex{
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?" },
102 result = std::regex_replace( result, std::regex{
"std::__1::" },
"std::" );
104 result = std::regex_replace(
105 result, std::regex{
"std::basic_string<char, std::char_traits<char>, std::allocator<char>>( (?=>))?" },
108 result = std::regex_replace( result, std::regex{
">>" },
"> >" );
109 result = std::regex_replace( result, std::regex{
">>" },
"> >" );
114 Registry& Registry::instance() {
115 auto _guard = std::scoped_lock{ ::registrySingletonMutex };
121 if (
logger().level() <= Logger::Debug ) {
122 std::stringstream msg;
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;
129 logger().debug( msg.str() );
133 Registry::Properties::mapped_type Registry::FactoryInfo::getprop(
const Properties::key_type& name )
const {
134 auto p = properties.find( name );
135 return ( p != end( properties ) ) ? p->second : Properties::mapped_type{};
138 Registry::Registry() {}
140 void Registry::initialize() {
141 auto _guard = std::scoped_lock{ m_mutex };
142#if defined( __APPLE__ )
143 const auto envVars = {
"GAUDI_PLUGIN_PATH",
"DYLD_LIBRARY_PATH" };
144 const char sep =
':';
146 const auto envVars = {
"GAUDI_PLUGIN_PATH",
"LD_LIBRARY_PATH" };
147 const char sep =
':';
150 std::regex line_format{
"^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$" };
151 for (
const auto& envVar : envVars ) {
153 std::stringstream search_path;
154 if (
auto ptr = std::getenv( envVar ) ) search_path << ptr;
155 logger().debug( std::string(
"searching factories in " ) + envVar );
159 while ( std::getline( search_path, dir, sep ) ) {
161 logger().debug(
" looking into " + dir );
163 if ( !fs::is_directory( dir ) ) {
continue; }
164 for (
const auto& p : fs::directory_iterator( dir ) ) {
165 if ( p.path().extension() !=
".components" || !is_regular_file( p.path() ) ) {
continue; }
167 const auto& fullPath = p.path().string();
168 logger().debug(
" reading " + p.path().filename().string() );
171 int factoriesCount = 0;
175 std::getline( factories, line );
176 if ( regex_match( line, m, line_format ) ) {
177 if ( m[1] !=
"v2" ) {
continue; }
178 const std::string lib{
m[2] };
179 const std::string fact{
m[3] };
180 m_factories.emplace( fact, FactoryInfo{ lib, {}, { {
"ClassName", fact } } } );
181#ifdef GAUDI_REFLEX_COMPONENT_ALIASES
183 std::string old_name = old_style_name( fact );
184 if ( fact != old_name ) {
186 old_name, FactoryInfo{ lib, {}, { {
"ReflexName",
"true" }, {
"ClassName", fact } } } );
191 logger().warning(
"failed to parse line " + fullPath +
':' + std::to_string( lineCount ) );
195 logger().debug(
" found " + std::to_string( factoriesCount ) +
" factories" );
202 const Registry::FactoryMap& Registry::factories()
const {
203 std::call_once( m_initialized, &Registry::initialize,
const_cast<Registry*
>(
this ) );
207 Registry::FactoryMap& Registry::factories() {
208 std::call_once( m_initialized, &Registry::initialize,
this );
212 Registry::FactoryInfo& Registry::add(
const KeyType&
id, FactoryInfo info ) {
213 auto _guard = std::scoped_lock{ m_mutex };
216#ifdef GAUDI_REFLEX_COMPONENT_ALIASES
218 const auto old_name = old_style_name(
id );
219 if (
id != old_name ) {
220 auto new_info = info;
222 new_info.properties[
"ReflexName"] =
"true";
224 add( old_name, new_info );
228 auto entry = facts.find(
id );
229 if ( entry == facts.end() ) {
231 entry = facts.emplace(
id, std::move( info ) ).first;
234 if ( !entry->second.is_set() ) entry->second = std::move( info );
236 return entry->second;
239 Registry::FactoryMap::size_type Registry::erase(
const KeyType&
id ) {
240 auto _guard = std::scoped_lock{ m_mutex };
242 return facts.erase(
id );
245 const Registry::FactoryInfo& Registry::getInfo(
const KeyType&
id,
const bool load )
const {
246 auto _guard = std::scoped_lock{ m_mutex };
247 static const FactoryInfo unknown = {
"unknown" };
249 auto f = facts.find(
id );
251 if ( f == facts.end() ) {
return unknown; }
252 if ( !load || f->second.is_set() ) {
return f->second; }
254 if ( !loadPluginLibrary( f->second.library ) ) {
return unknown; }
256 f = facts.find(
id );
260 Registry& Registry::addProperty(
const KeyType&
id,
const KeyType& k,
const std::string& v ) {
261 auto _guard = std::scoped_lock{ m_mutex };
263 auto f = facts.find(
id );
265 if ( f != facts.end() ) f->second.properties[k] =
v;
269 void Registry::setError(
const KeyType& warning ) { m_werror.insert( warning ); }
271 void Registry::unsetError(
const KeyType& warning ) { m_werror.erase( warning ); }
273 std::set<Registry::KeyType> Registry::loadedFactoryNames()
const {
274 auto _guard = std::scoped_lock{ m_mutex };
277 if ( f.second.is_set() )
l.insert( f.first );
282 bool Registry::loadPluginLibrary( std::string_view library )
const {
286 std::stringstream ss;
288 if (
auto ptr = std::getenv(
"GAUDI_PLUGIN_PATH" ) ) ss << ptr;
290 while ( std::getline( ss, dir,
':' ) ) {
291 if ( !fs::exists( dir ) ) {
continue; }
292 const auto path = dir / fs::path( library );
293 if ( is_regular_file( path ) ) {
294 if ( dlopen(
path.c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
297 logger().warning(
"cannot load " +
path.string() );
298 if (
char* dlmsg = dlerror() ) {
logger().warning( dlmsg ); }
303 if ( dlopen( std::string{ library }.c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
307 logger().warning(
"cannot load " + std::string{ library } );
308 if (
char* dlmsg = dlerror() ) {
logger().warning( dlmsg ); }
313 void Logger::report( Level lvl,
const std::string& msg ) {
314 static const char* levels[] = {
"DEBUG : ",
"INFO : ",
"WARNING: ",
"ERROR : " };
315 if ( lvl >=
level() ) { std::cerr << levels[lvl] <<
msg << std::endl; }
318 static auto s_logger = std::make_unique<Logger>();
324#if defined _GNU_SOURCE || defined __APPLE__
326 if ( dladdr( fptr, &info ) == 0 )
return "";
328 auto pos = std::strrchr( info.dli_fname,
'/' );
332 return info.dli_fname;
340#ifdef PLUGIN_PATH_RELATIVE
341# define stringify( x ) stringify_( x )
342# define stringify_( x ) #x
343 std::string relative_path = stringify( PLUGIN_PATH_RELATIVE );
345 std::string relative_path =
".";
348 if ( dladdr( (
void*)_dso_marker, &info ) != 0 ) {
349 auto plugin_path = fs::path( info.dli_fname ).parent_path() / relative_path;
350 return plugin_path.string();
352 return relative_path;