14 #define GAUDI_PLUGIN_SERVICE_V2
34 #ifdef USE_BOOST_FILESYSTEM
35 # include <boost/filesystem.hpp>
36 namespace fs = boost::filesystem;
38 # include <filesystem>
39 namespace fs = std::filesystem;
40 #endif // USE_BOOST_FILESYSTEM
42 #if __cplusplus >= 201703
43 # include <string_view>
45 # include <experimental/string_view>
47 using experimental::string_view;
60 void operator()(
const char c ) {
69 name.push_back(
'_' );
72 name.push_back(
'r' );
75 name.push_back(
'p' );
92 namespace PluginService {
98 abi::__cxa_demangle(
id.c_str(),
nullptr,
nullptr, &status ), free );
99 if ( !realname )
return id;
100 #if _GLIBCXX_USE_CXX11_ABI
103 std::regex{
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?" },
111 Registry& Registry::instance() {
112 auto _guard = std::scoped_lock{ ::registrySingletonMutex };
120 const auto& info = Registry::instance().getInfo(
id );
121 msg <<
"bad any_cast: requested factory " <<
id <<
" of type " <<
demangle( factory_type ) <<
", got ";
123 msg <<
demangle( info.factory.type() ) <<
" from " << info.library;
130 Registry::Properties::mapped_type Registry::FactoryInfo::getprop(
const Properties::key_type&
name )
const {
132 return ( p !=
end(
properties ) ) ? p->second : Properties::mapped_type{};
135 Registry::Registry() {}
137 void Registry::initialize() {
138 auto _guard = std::scoped_lock{ m_mutex };
139 #if defined( _WIN32 )
140 const char* envVar =
"PATH";
141 const char sep =
';';
142 #elif defined( __APPLE__ )
143 const char* envVar =
"GAUDI_PLUGIN_PATH";
144 const char sep =
':';
146 const char* envVar =
"LD_LIBRARY_PATH";
147 const char sep =
':';
150 std::regex line_format{
"^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$" };
153 std::string_view search_path;
154 if (
auto ptr =
std::getenv( envVar ) ) search_path = std::string_view{ ptr };
155 if ( !search_path.empty() ) {
158 std::string_view::size_type start_pos = 0, end_pos = 0;
159 while ( start_pos != std::string_view::npos ) {
161 if ( start_pos ) ++start_pos;
163 end_pos = search_path.find( sep, start_pos );
165 #ifdef USE_BOOST_FILESYSTEM
168 search_path.substr( start_pos, end_pos - start_pos );
171 logger().debug(
" looking into " + dirName.string() );
173 if ( is_directory( dirName ) ) {
174 for (
auto& p : fs::directory_iterator( dirName ) ) {
175 if ( p.path().extension() ==
".components" && is_regular_file( p.path() ) ) {
177 const auto& fullPath = p.path().string();
178 logger().debug(
" reading " + p.path().filename().string() );
181 int factoriesCount = 0;
187 if (
m[1] ==
"v2" ) {
190 m_factories.emplace( fact, FactoryInfo{ lib, {}, { {
"ClassName", fact } } } );
191 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
194 if ( fact != old_name ) {
196 old_name, FactoryInfo{ lib, {}, { {
"ReflexName",
"true" }, {
"ClassName", fact } } } );
216 std::call_once( m_initialized, &Registry::initialize,
const_cast<Registry*
>(
this ) );
225 Registry::FactoryInfo&
Registry::add(
const KeyType&
id, FactoryInfo info ) {
226 auto _guard = std::scoped_lock{ m_mutex };
229 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
231 const auto old_name = old_style_name(
id );
232 if (
id != old_name ) {
233 auto new_info = info;
235 new_info.properties[
"ReflexName"] =
"true";
237 add( old_name, new_info );
241 auto entry = facts.find(
id );
242 if ( entry == facts.end() ) {
244 entry = facts.emplace(
id,
std::move( info ) ).first;
247 if ( !entry->second.is_set() ) entry->second =
std::move( info );
249 return entry->second;
252 Registry::FactoryMap::size_type Registry::erase(
const KeyType&
id ) {
253 auto _guard = std::scoped_lock{ m_mutex };
255 return facts.erase(
id );
258 const Registry::FactoryInfo& Registry::getInfo(
const KeyType&
id,
const bool load )
const {
259 auto _guard = std::scoped_lock{ m_mutex };
260 static const FactoryInfo unknown = {
"unknown" };
262 auto f = facts.find(
id );
264 if ( f != facts.end() ) {
265 if ( load && !f->second.is_set() ) {
268 std::string_view search_path;
269 if (
auto ptr =
std::getenv(
"GAUDI_PLUGIN_PATH" ) ) search_path = std::string_view{ ptr };
270 if ( !search_path.empty() ) {
272 std::string_view::size_type start_pos = 0, end_pos = 0;
273 while ( start_pos != std::string_view::npos ) {
275 if ( start_pos ) ++start_pos;
277 end_pos = search_path.
find(
":", start_pos );
279 # ifdef USE_BOOST_FILESYSTEM
282 search_path.substr( start_pos, end_pos - start_pos );
286 logger().debug(
" looking into " + dirName.string() );
288 if ( is_directory( dirName ) && is_regular_file( dirName /
fs::path( library ) ) ) {
289 logger().debug(
"found " + dirName.string() +
"/" + library.
c_str() +
" for factory " +
id );
290 if ( !dlopen( ( dirName.string() +
"/" + library ).c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
291 logger().warning(
"cannot load " + library +
" for factory " +
id );
292 char* dlmsg = dlerror();
293 if ( dlmsg )
logger().warning( dlmsg );
301 if ( !dlopen( library.
c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
302 logger().warning(
"cannot load " + library +
" for factory " +
id );
303 char* dlmsg = dlerror();
304 if ( dlmsg )
logger().warning( dlmsg );
308 f = facts.find(
id );
316 Registry& Registry::addProperty(
const KeyType&
id,
const KeyType& k,
const std::string&
v ) {
317 auto _guard = std::scoped_lock{ m_mutex };
319 auto f = facts.find(
id );
321 if ( f != facts.end() ) f->second.properties[k] =
v;
325 void Registry::setError(
const KeyType& warning ) { m_werror.insert( warning ); }
327 void Registry::unsetError(
const KeyType& warning ) { m_werror.erase( warning ); }
330 auto _guard = std::scoped_lock{ m_mutex };
333 if ( f.second.is_set() )
l.insert( f.first );
339 static const char* levels[] = {
"DEBUG : ",
"INFO : ",
"WARNING: ",
"ERROR : " };
343 static auto s_logger = std::make_unique<Logger>();
349 #if defined _GNU_SOURCE || defined __APPLE__
351 if ( dladdr( fptr, &info ) == 0 )
return "";
357 return info.dli_fname;
366 using namespace Details;
368 if ( debugLevel > 1 )
370 else if ( debugLevel > 0 )
371 l.setLevel( Logger::Info );
373 l.setLevel( Logger::Warning );
377 using namespace Details;