87 std::string
demangle(
const std::string&
id ) {
89 auto realname = std::unique_ptr<char, decltype( free )*>(
90 abi::__cxa_demangle(
id.c_str(),
nullptr,
nullptr, &status ), free );
91 if ( !realname )
return id;
92 std::string result = realname.get();
94 result = std::regex_replace(
96 std::regex{
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?" },
99 result = std::regex_replace( result, std::regex{
"std::__1::" },
"std::" );
101 result = std::regex_replace(
102 result, std::regex{
"std::basic_string<char, std::char_traits<char>, std::allocator<char>>( (?=>))?" },
105 result = std::regex_replace( result, std::regex{
">>" },
"> >" );
106 result = std::regex_replace( result, std::regex{
">>" },
"> >" );
111 bool Registry::tryDLOpen(
const std::string_view& libName )
const {
112 const void* handle = dlopen( libName.data(), RTLD_LAZY | RTLD_GLOBAL );
114 std::cout <<
"dlopen failed for " << libName << std::endl;
115 logger().warning(
"cannot load " + std::string( libName ) );
116 if (
char* dlmsg = dlerror() ) {
logger().warning( dlmsg ); }
122 Registry& Registry::instance() {
123 auto _guard = std::scoped_lock{ ::registrySingletonMutex };
129 if (
logger().level() <= Logger::Debug ) {
130 std::stringstream msg;
131 const auto& info = Registry::instance().getInfo(
id );
132 msg <<
"bad any_cast: requested factory " <<
id <<
" of type " <<
demangle( factory_type ) <<
", got ";
134 msg <<
demangle( info.factory.type() ) <<
" from " << info.library;
137 logger().debug( msg.str() );
141 Registry::Properties::mapped_type Registry::FactoryInfo::getprop(
const Properties::key_type& name )
const {
142 auto p = properties.find( name );
143 return ( p != end( properties ) ) ? p->second : Properties::mapped_type{};
146 Registry::Registry() {}
148 void Registry::initialize() {
149 auto _guard = std::scoped_lock{ m_mutex };
150#if defined( __APPLE__ )
151 const auto envVars = {
"GAUDI_PLUGIN_PATH" };
152 const char sep =
':';
154 const auto envVars = {
"GAUDI_PLUGIN_PATH",
"LD_LIBRARY_PATH" };
155 const char sep =
':';
158 std::regex line_format{
"^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$" };
159 for (
const auto& envVar : envVars ) {
161 std::stringstream search_path;
162 if (
auto ptr = std::getenv( envVar ) ) search_path << ptr;
163 logger().debug( std::string(
"searching factories in " ) + envVar );
167 while ( std::getline( search_path, dir, sep ) ) {
169 logger().debug(
" looking into " + dir );
171 if ( !fs::is_directory( dir ) ) {
continue; }
172 for (
const auto& p : fs::directory_iterator( dir ) ) {
173 if ( p.path().extension() !=
".components" || !is_regular_file( p.path() ) ) {
continue; }
175 const auto& fullPath = p.path().string();
176 logger().debug(
" reading " + p.path().filename().string() );
179 int factoriesCount = 0;
183 std::getline( factories, line );
184 if ( regex_match( line, m, line_format ) ) {
185 if ( m[1] !=
"v2" ) {
continue; }
186 const std::string lib{
m[2] };
187 const std::string fact{
m[3] };
188 m_factories.emplace( fact, FactoryInfo{ lib, {}, { {
"ClassName", fact } } } );
189#ifdef GAUDI_REFLEX_COMPONENT_ALIASES
191 std::string old_name = old_style_name( fact );
192 if ( fact != old_name ) {
194 old_name, FactoryInfo{ lib, {}, { {
"ReflexName",
"true" }, {
"ClassName", fact } } } );
199 logger().warning(
"failed to parse line " + fullPath +
':' + std::to_string( lineCount ) );
203 logger().debug(
" found " + std::to_string( factoriesCount ) +
" factories" );
210 const Registry::FactoryMap& Registry::factories()
const {
211 std::call_once( m_initialized, &Registry::initialize,
const_cast<Registry*
>(
this ) );
215 Registry::FactoryMap& Registry::factories() {
216 std::call_once( m_initialized, &Registry::initialize,
this );
220 Registry::FactoryInfo& Registry::add(
const KeyType&
id, FactoryInfo info ) {
221 auto _guard = std::scoped_lock{ m_mutex };
224#ifdef GAUDI_REFLEX_COMPONENT_ALIASES
226 const auto old_name = old_style_name(
id );
227 if (
id != old_name ) {
228 auto new_info = info;
230 new_info.properties[
"ReflexName"] =
"true";
232 add( old_name, new_info );
236 auto entry = facts.find(
id );
237 if ( entry == facts.end() ) {
239 entry = facts.emplace(
id, std::move( info ) ).first;
242 if ( !entry->second.is_set() ) entry->second = std::move( info );
244 return entry->second;
247 Registry::FactoryMap::size_type Registry::erase(
const KeyType&
id ) {
248 auto _guard = std::scoped_lock{ m_mutex };
250 return facts.erase(
id );
253 const Registry::FactoryInfo& Registry::getInfo(
const KeyType&
id,
const bool load )
const {
254 auto _guard = std::scoped_lock{ m_mutex };
255 static const FactoryInfo unknown = {
"unknown" };
257 auto f = facts.find(
id );
259 if ( f == facts.end() ) {
return unknown; }
260 if ( !load || f->second.is_set() ) {
return f->second; }
261 const std::string_view library = f->second.library;
266 std::stringstream ss;
267 if (
auto ptr = std::getenv(
"GAUDI_PLUGIN_PATH" ) ) ss << ptr;
270 while ( std::getline( ss, dir,
':' ) && !found ) {
271 if ( !fs::exists( dir ) ) {
continue; }
272 if ( is_regular_file( dir / fs::path( library ) ) ) {
274 if ( !tryDLOpen( ( dir / fs::path( library ) ).
string() ) ) {
282 if ( !found && !tryDLOpen( library ) )
return unknown;
283 f = facts.find(
id );
287 Registry& Registry::addProperty(
const KeyType&
id,
const KeyType& k,
const std::string& v ) {
288 auto _guard = std::scoped_lock{ m_mutex };
290 auto f = facts.find(
id );
292 if ( f != facts.end() ) f->second.properties[k] =
v;
296 void Registry::setError(
const KeyType& warning ) { m_werror.insert( warning ); }
298 void Registry::unsetError(
const KeyType& warning ) { m_werror.erase( warning ); }
300 std::set<Registry::KeyType> Registry::loadedFactoryNames()
const {
301 auto _guard = std::scoped_lock{ m_mutex };
304 if ( f.second.is_set() )
l.insert( f.first );
309 void Logger::report( Level lvl,
const std::string& msg ) {
310 static const char* levels[] = {
"DEBUG : ",
"INFO : ",
"WARNING: ",
"ERROR : " };
311 if ( lvl >=
level() ) { std::cerr << levels[lvl] <<
msg << std::endl; }
314 static auto s_logger = std::make_unique<Logger>();
320#if defined _GNU_SOURCE || defined __APPLE__
322 if ( dladdr( fptr, &info ) == 0 )
return "";
324 auto pos = std::strrchr( info.dli_fname,
'/' );
328 return info.dli_fname;