14 #define GAUDI_PLUGIN_SERVICE_V2
17 #include <boost/algorithm/string.hpp>
38 namespace fs = std::filesystem;
40 #include <string_view>
42 #define REG_SCOPE_LOCK std::lock_guard<std::recursive_mutex> _guard( m_mutex );
45 std::mutex registrySingletonMutex;
47 #define SINGLETON_LOCK std::lock_guard<std::mutex> _guard( ::registrySingletonMutex );
54 void operator()(
const char c ) {
63 name.push_back(
'_' );
66 name.push_back(
'r' );
69 name.push_back(
'p' );
80 std::string old_style_name(
const std::string& name ) {
81 return std::for_each( name.begin(), name.end(), OldStyleCnv() ).name;
86 namespace PluginService {
89 std::string
demangle(
const std::string&
id ) {
91 auto realname = std::unique_ptr<char, decltype( free )*>(
92 abi::__cxa_demangle(
id.c_str(),
nullptr,
nullptr, &status ), free );
93 if ( !realname )
return id;
94 #if _GLIBCXX_USE_CXX11_ABI
95 return std::regex_replace(
97 std::regex{
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?"},
100 return std::string{realname.get()};
105 Registry& Registry::instance() {
113 std::stringstream msg;
114 const auto&
info = Registry::instance().getInfo(
id );
115 msg <<
"bad any_cast: requested factory " <<
id <<
" of type " <<
demangle( factory_type ) <<
", got ";
120 logger().debug( msg.str() );
124 Registry::Properties::mapped_type Registry::FactoryInfo::getprop(
const Properties::key_type& name )
const {
125 auto p = properties.find( name );
126 return ( p != end( properties ) ) ? p->second : Properties::mapped_type{};
129 Registry::Registry() {}
131 void Registry::initialize() {
133 #if defined( _WIN32 )
134 const std::string envVar =
"PATH";
135 const std::string sep =
";";
136 #elif defined( __APPLE__ )
137 const std::string envVar =
"DYLD_LIBRARY_PATH";
138 const std::string sep =
":";
140 const std::string envVar =
"LD_LIBRARY_PATH";
141 const std::string sep =
":";
144 std::regex line_format{
"^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$"};
147 std::string search_path;
148 const char* envPtr = std::getenv( envVar.c_str() );
149 search_path = envPtr ? envPtr :
"/usr/lib64:/usr/lib:/usr/local/lib";
150 if ( search_path.empty() ) {
154 logger().debug(
"searching factories in " + envVar);
155 logger().debug(
"searching factories in " + search_path);
157 std::vector<std::string> directories;
158 boost::split(directories, search_path, boost::is_any_of(sep));
160 for(fs::path dirName: directories) {
161 if ( not is_directory( dirName ) ) {
164 logger().debug(
" looking into " + dirName.string() );
165 for (
auto& p : fs::directory_iterator( dirName ) ) {
167 if ( p.path().extension() ==
".components" && is_regular_file( p.path() ) ) {
169 const auto& fullPath = p.path().string();
170 logger().debug(
" reading " + p.path().filename().string() );
171 std::ifstream factories{fullPath};
173 int factoriesCount = 0;
175 while ( !factories.eof() ) {
177 std::getline( factories, line );
178 if ( regex_match( line, matches, line_format ) ) {
179 if ( matches[1] ==
"v2" ) {
180 const std::string lib{matches[2]};
181 const std::string fact{matches[3]};
182 m_factories.emplace( fact, FactoryInfo{lib, {}, {{
"ClassName", fact}}} );
183 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
185 std::string old_name = old_style_name( fact );
186 if ( fact != old_name ) {
187 m_factories.emplace( old_name,
188 FactoryInfo{lib, {}, {{
"ReflexName",
"true"}, {
"ClassName", fact}}} );
194 logger().debug(
"failed to parse line " + fullPath +
':' + std::to_string( lineCount ) );
198 logger().debug(
" found " + std::to_string( factoriesCount ) +
" factories" );
205 const Registry::FactoryMap& Registry::factories()
const {
206 std::call_once( m_initialized, &Registry::initialize,
const_cast<Registry*
>(
this ) );
210 Registry::FactoryMap& Registry::factories() {
211 std::call_once( m_initialized, &Registry::initialize,
this );
215 Registry::FactoryInfo& Registry::add(
const KeyType&
id, FactoryInfo
info ) {
217 FactoryMap& facts = factories();
219 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
221 const auto old_name = old_style_name(
id );
222 if (
id != old_name ) {
223 auto new_info =
info;
225 new_info.properties[
"ReflexName"] =
"true";
227 add( old_name, new_info );
231 auto entry = facts.find(
id );
232 if ( entry == facts.end() ) {
234 entry = facts.emplace(
id, std::move(
info ) ).first;
237 if ( !entry->second.is_set() ) entry->second = std::move(
info );
239 return entry->second;
242 const Registry::FactoryInfo& Registry::getInfo(
const KeyType&
id,
const bool load )
const {
244 static const FactoryInfo unknown = {
"unknown"};
245 const FactoryMap& facts = factories();
246 auto f = facts.find(
id );
248 if ( f != facts.end() ) {
249 if ( load && !f->second.is_set() ) {
250 const std::string library = f->second.library;
251 if ( !dlopen( library.c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
252 logger().warning(
"cannot load " + library +
" for factory " +
id );
253 char* dlmsg = dlerror();
254 if ( dlmsg )
logger().warning( dlmsg );
257 f = facts.find(
id );
265 Registry& Registry::addProperty(
const KeyType&
id,
const KeyType& k,
const std::string&
v ) {
267 FactoryMap& facts = factories();
268 auto f = facts.find(
id );
270 if ( f != facts.end() ) f->second.properties[k] =
v;
274 std::set<Registry::KeyType> Registry::loadedFactoryNames()
const {
277 for (
const auto& f : factories() ) {
278 if ( f.second.is_set() ) l.insert( f.first );
283 void Logger::report( Level lvl,
const std::string& msg ) {
284 static const char* levels[] = {
"DEBUG : ",
"INFO : ",
"WARNING: ",
"ERROR : "};
285 if ( lvl >= level() ) { std::cerr << levels[lvl] << msg << std::endl; }
288 static auto s_logger = std::make_unique<Logger>();
296 if ( dladdr( fptr, &
info ) == 0 )
return "";
298 auto pos = std::strrchr(
info.dli_fname,
'/' );
302 return info.dli_fname;
311 using namespace Details;
313 if ( debugLevel > 1 )
315 else if ( debugLevel > 0 )
316 l.setLevel( Logger::Info );
318 l.setLevel( Logger::Warning );
322 using namespace Details;
323 switch (
logger().level() ) {