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 return std::regex_replace(
94 std::regex{
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?" },
99 bool Registry::tryDLOpen(
const std::string_view& libName )
const {
100 const void* handle = dlopen( libName.data(), RTLD_LAZY | RTLD_GLOBAL );
102 std::cout <<
"dlopen failed for " << libName << std::endl;
103 logger().warning(
"cannot load " + std::string( libName ) );
104 if (
char* dlmsg = dlerror() ) {
logger().warning( dlmsg ); }
110 Registry& Registry::instance() {
111 auto _guard = std::scoped_lock{ ::registrySingletonMutex };
117 if (
logger().level() <= Logger::Debug ) {
118 std::stringstream msg;
119 const auto& info = Registry::instance().getInfo(
id );
120 msg <<
"bad any_cast: requested factory " <<
id <<
" of type " <<
demangle( factory_type ) <<
", got ";
122 msg <<
demangle( info.factory.type() ) <<
" from " << info.library;
125 logger().debug( msg.str() );
129 Registry::Properties::mapped_type Registry::FactoryInfo::getprop(
const Properties::key_type& name )
const {
130 auto p = properties.find( name );
131 return ( p != end( properties ) ) ? p->second : Properties::mapped_type{};
134 Registry::Registry() {}
136 void Registry::initialize() {
137 auto _guard = std::scoped_lock{ m_mutex };
138#if defined( __APPLE__ )
139 const auto envVars = {
"GAUDI_PLUGIN_PATH" };
140 const char sep =
':';
142 const auto envVars = {
"GAUDI_PLUGIN_PATH",
"LD_LIBRARY_PATH" };
143 const char sep =
':';
146 std::regex line_format{
"^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$" };
147 for (
const auto& envVar : envVars ) {
149 std::stringstream search_path;
150 if (
auto ptr = std::getenv( envVar ) ) search_path << ptr;
151 logger().debug( std::string(
"searching factories in " ) + envVar );
155 while ( std::getline( search_path, dir, sep ) ) {
157 logger().debug(
" looking into " + dir );
159 if ( !fs::is_directory( dir ) ) {
continue; }
160 for (
const auto& p : fs::directory_iterator( dir ) ) {
161 if ( p.path().extension() !=
".components" || !is_regular_file( p.path() ) ) {
continue; }
163 const auto& fullPath = p.path().string();
164 logger().debug(
" reading " + p.path().filename().string() );
167 int factoriesCount = 0;
171 std::getline( factories, line );
172 if ( regex_match( line, m, line_format ) ) {
173 if ( m[1] !=
"v2" ) {
continue; }
174 const std::string lib{
m[2] };
175 const std::string fact{
m[3] };
176 m_factories.emplace( fact, FactoryInfo{ lib, {}, { {
"ClassName", fact } } } );
177#ifdef GAUDI_REFLEX_COMPONENT_ALIASES
179 std::string old_name = old_style_name( fact );
180 if ( fact != old_name ) {
182 old_name, FactoryInfo{ lib, {}, { {
"ReflexName",
"true" }, {
"ClassName", fact } } } );
187 logger().warning(
"failed to parse line " + fullPath +
':' + std::to_string( lineCount ) );
191 logger().debug(
" found " + std::to_string( factoriesCount ) +
" factories" );
198 const Registry::FactoryMap& Registry::factories()
const {
199 std::call_once( m_initialized, &Registry::initialize,
const_cast<Registry*
>(
this ) );
203 Registry::FactoryMap& Registry::factories() {
204 std::call_once( m_initialized, &Registry::initialize,
this );
208 Registry::FactoryInfo& Registry::add(
const KeyType&
id, FactoryInfo info ) {
209 auto _guard = std::scoped_lock{ m_mutex };
212#ifdef GAUDI_REFLEX_COMPONENT_ALIASES
214 const auto old_name = old_style_name(
id );
215 if (
id != old_name ) {
216 auto new_info = info;
218 new_info.properties[
"ReflexName"] =
"true";
220 add( old_name, new_info );
224 auto entry = facts.find(
id );
225 if ( entry == facts.end() ) {
227 entry = facts.emplace(
id, std::move( info ) ).first;
230 if ( !entry->second.is_set() ) entry->second = std::move( info );
232 return entry->second;
235 Registry::FactoryMap::size_type Registry::erase(
const KeyType&
id ) {
236 auto _guard = std::scoped_lock{ m_mutex };
238 return facts.erase(
id );
241 const Registry::FactoryInfo& Registry::getInfo(
const KeyType&
id,
const bool load )
const {
242 auto _guard = std::scoped_lock{ m_mutex };
243 static const FactoryInfo unknown = {
"unknown" };
245 auto f = facts.find(
id );
247 if ( f == facts.end() ) {
return unknown; }
248 if ( !load || f->second.is_set() ) {
return f->second; }
249 const std::string_view library = f->second.library;
254 std::stringstream ss;
255 if (
auto ptr = std::getenv(
"GAUDI_PLUGIN_PATH" ) ) ss << ptr;
258 while ( std::getline( ss, dir,
':' ) && !found ) {
259 if ( !fs::exists( dir ) ) {
continue; }
260 if ( is_regular_file( dir / fs::path( library ) ) ) {
262 if ( !tryDLOpen( ( dir / fs::path( library ) ).
string() ) ) {
270 if ( !found && !tryDLOpen( library ) )
return unknown;
271 f = facts.find(
id );
275 Registry& Registry::addProperty(
const KeyType&
id,
const KeyType& k,
const std::string& v ) {
276 auto _guard = std::scoped_lock{ m_mutex };
278 auto f = facts.find(
id );
280 if ( f != facts.end() ) f->second.properties[k] =
v;
284 void Registry::setError(
const KeyType& warning ) { m_werror.insert( warning ); }
286 void Registry::unsetError(
const KeyType& warning ) { m_werror.erase( warning ); }
288 std::set<Registry::KeyType> Registry::loadedFactoryNames()
const {
289 auto _guard = std::scoped_lock{ m_mutex };
292 if ( f.second.is_set() )
l.insert( f.first );
297 void Logger::report( Level lvl,
const std::string& msg ) {
298 static const char* levels[] = {
"DEBUG : ",
"INFO : ",
"WARNING: ",
"ERROR : " };
299 if ( lvl >=
level() ) { std::cerr << levels[lvl] <<
msg << std::endl; }
302 static auto s_logger = std::make_unique<Logger>();
308#if defined _GNU_SOURCE || defined __APPLE__
310 if ( dladdr( fptr, &info ) == 0 )
return "";
312 auto pos = std::strrchr( info.dli_fname,
'/' );
316 return info.dli_fname;