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 =
';';
143 const char* envVar =
"LD_LIBRARY_PATH";
144 const char sep =
':';
147 std::regex line_format{
"^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$" };
150 std::string_view search_path;
151 if (
auto ptr =
std::getenv( envVar ) ) search_path = std::string_view{ ptr };
152 if ( !search_path.empty() ) {
155 std::string_view::size_type start_pos = 0, end_pos = 0;
156 while ( start_pos != std::string_view::npos ) {
158 if ( start_pos ) ++start_pos;
160 end_pos = search_path.find( sep, start_pos );
162 #ifdef USE_BOOST_FILESYSTEM
165 search_path.substr( start_pos, end_pos - start_pos );
169 logger().debug(
" looking into " + dirName.string() );
171 if ( is_directory( dirName ) ) {
172 for (
auto& p : fs::directory_iterator( dirName ) ) {
173 if ( p.path().extension() ==
".components" && is_regular_file( p.path() ) ) {
175 const auto& fullPath = p.path().string();
176 logger().debug(
" reading " + p.path().filename().string() );
179 int factoriesCount = 0;
185 if (
m[1] ==
"v2" ) {
188 m_factories.emplace( fact, FactoryInfo{ lib, {}, { {
"ClassName", fact } } } );
189 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
192 if ( fact != old_name ) {
194 old_name, FactoryInfo{ lib, {}, { {
"ReflexName",
"true" }, {
"ClassName", fact } } } );
214 std::call_once( m_initialized, &Registry::initialize,
const_cast<Registry*
>(
this ) );
223 Registry::FactoryInfo&
Registry::add(
const KeyType&
id, FactoryInfo info ) {
224 auto _guard = std::scoped_lock{ m_mutex };
227 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
229 const auto old_name = old_style_name(
id );
230 if (
id != old_name ) {
231 auto new_info = info;
233 new_info.properties[
"ReflexName"] =
"true";
235 add( old_name, new_info );
239 auto entry = facts.find(
id );
240 if ( entry == facts.end() ) {
242 entry = facts.emplace(
id,
std::move( info ) ).first;
245 if ( !entry->second.is_set() ) entry->second =
std::move( info );
247 return entry->second;
250 Registry::FactoryMap::size_type Registry::erase(
const KeyType&
id ) {
251 auto _guard = std::scoped_lock{ m_mutex };
253 return facts.erase(
id );
256 const Registry::FactoryInfo& Registry::getInfo(
const KeyType&
id,
const bool load )
const {
257 auto _guard = std::scoped_lock{ m_mutex };
258 static const FactoryInfo unknown = {
"unknown" };
260 auto f = facts.find(
id );
262 if ( f != facts.end() ) {
263 if ( load && !f->second.is_set() ) {
265 if ( !dlopen( library.
c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
266 logger().warning(
"cannot load " + library +
" for factory " +
id );
267 char* dlmsg = dlerror();
268 if ( dlmsg )
logger().warning( dlmsg );
271 f = facts.find(
id );
279 Registry& Registry::addProperty(
const KeyType&
id,
const KeyType& k,
const std::string&
v ) {
280 auto _guard = std::scoped_lock{ m_mutex };
282 auto f = facts.find(
id );
284 if ( f != facts.end() ) f->second.properties[k] =
v;
288 void Registry::setError(
const KeyType& warning ) { m_werror.insert( warning ); }
290 void Registry::unsetError(
const KeyType& warning ) { m_werror.erase( warning ); }
293 auto _guard = std::scoped_lock{ m_mutex };
296 if ( f.second.is_set() )
l.insert( f.first );
302 static const char* levels[] = {
"DEBUG : ",
"INFO : ",
"WARNING: ",
"ERROR : " };
306 static auto s_logger = std::make_unique<Logger>();
314 if ( dladdr( fptr, &info ) == 0 )
return "";
320 return info.dli_fname;
329 using namespace Details;
331 if ( debugLevel > 1 )
333 else if ( debugLevel > 0 )
334 l.setLevel( Logger::Info );
336 l.setLevel( Logger::Warning );
340 using namespace Details;