Skip to content

Added xplugin_registry constructor from list of paths #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ set(XPLUGIN_HEADERS
${XPLUGIN_INCLUDE_DIR}/xplugin/xfactory.hpp
${XPLUGIN_INCLUDE_DIR}/xplugin/xlazy_shared_library_plugin_factory.hpp
${XPLUGIN_INCLUDE_DIR}/xplugin/xplugin_config.hpp
${XPLUGIN_INCLUDE_DIR}/xplugin/xplugin_util.hpp
${XPLUGIN_INCLUDE_DIR}/xplugin/xplugin_registry.hpp
${XPLUGIN_INCLUDE_DIR}/xplugin/xshared_library.hpp
)
Expand Down
114 changes: 68 additions & 46 deletions include/xplugin/xplugin_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XPLUGIN_REGISTRY_HPP
#define XPLUGIN_REGISTRY_HPP

Expand All @@ -17,10 +18,10 @@

#include <xplugin/xlazy_shared_library_plugin_factory.hpp>
#include <xplugin/xplugin_config.hpp>
#include <xplugin/xplugin_util.hpp>

namespace xp::detail
{

template <class FACTORY_BASE, bool THREAD_SAFE>
class xplugin_registry_impl;

Expand Down Expand Up @@ -95,11 +96,15 @@ namespace xp::detail
using const_iterator = xplugin_registry_iterator<self_type, typename storage_map_type::const_iterator>;
using iterator = xplugin_registry_iterator<self_type, typename storage_map_type::iterator>;

xplugin_registry_impl(const std::filesystem::path &path,
const std::string &prefix = get_default_library_prefix(),
const std::string &extension = get_default_library_extension());
explicit xplugin_registry_impl(const std::filesystem::path& path,
const std::string& prefix = get_default_library_prefix(),
const std::string& extension = get_default_library_extension());

factory_base_type *operator[](const std::string &name);
template <class R, std::enable_if_t<xp::util::is_range_of_v<R, std::filesystem::path>, int> = 3>
explicit xplugin_registry_impl(R r,
const std::string& prefix = get_default_library_prefix());

factory_base_type* operator[](const std::string& name);

std::size_t size() const;
bool empty() const;
Expand All @@ -116,6 +121,8 @@ namespace xp::detail

private:

void add_entry(const std::filesystem::path& path, const std::string& prefix);

static std::string get_default_library_extension();
static std::string get_default_library_prefix();

Expand Down Expand Up @@ -199,51 +206,51 @@ namespace xp::detail
}

// registry implementation
template <class FACTORY_BASE, bool THREAD_SAVE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::iterator xplugin_registry_impl<FACTORY_BASE,
THREAD_SAVE>::begin()
template <class FACTORY_BASE, bool THREAD_SAFE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::iterator xplugin_registry_impl<FACTORY_BASE,
THREAD_SAFE>::begin()
{
return iterator(m_lazy_shared_lib_factories.begin());
}

template <class FACTORY_BASE, bool THREAD_SAVE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::iterator xplugin_registry_impl<FACTORY_BASE,
THREAD_SAVE>::end()
template <class FACTORY_BASE, bool THREAD_SAFE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::iterator xplugin_registry_impl<FACTORY_BASE,
THREAD_SAFE>::end()
{
return iterator(m_lazy_shared_lib_factories.end());
}

// const iterator
template <class FACTORY_BASE, bool THREAD_SAVE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::const_iterator xplugin_registry_impl<
FACTORY_BASE, THREAD_SAVE>::begin() const
template <class FACTORY_BASE, bool THREAD_SAFE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::const_iterator xplugin_registry_impl<
FACTORY_BASE, THREAD_SAFE>::begin() const
{
return const_iterator(m_lazy_shared_lib_factories.cbegin());
}

template <class FACTORY_BASE, bool THREAD_SAVE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::const_iterator xplugin_registry_impl<
FACTORY_BASE, THREAD_SAVE>::end() const
template <class FACTORY_BASE, bool THREAD_SAFE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::const_iterator xplugin_registry_impl<
FACTORY_BASE, THREAD_SAFE>::end() const
{
return const_iterator(m_lazy_shared_lib_factories.cend());
}

template <class FACTORY_BASE, bool THREAD_SAVE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::const_iterator xplugin_registry_impl<
FACTORY_BASE, THREAD_SAVE>::cbegin() const
template <class FACTORY_BASE, bool THREAD_SAFE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::const_iterator xplugin_registry_impl<
FACTORY_BASE, THREAD_SAFE>::cbegin() const
{
return const_iterator(m_lazy_shared_lib_factories.cbegin());
}

template <class FACTORY_BASE, bool THREAD_SAVE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::const_iterator xplugin_registry_impl<
FACTORY_BASE, THREAD_SAVE>::cend() const
template <class FACTORY_BASE, bool THREAD_SAFE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::const_iterator xplugin_registry_impl<
FACTORY_BASE, THREAD_SAFE>::cend() const
{
return const_iterator(m_lazy_shared_lib_factories.cend());
}

template <class FACTORY_BASE, bool THREAD_SAVE>
xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::xplugin_registry_impl(const std::filesystem::path &path,
template <class FACTORY_BASE, bool THREAD_SAFE>
xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::xplugin_registry_impl(const std::filesystem::path &path,
const std::string &prefix,
const std::string &extension)
{
Expand All @@ -252,27 +259,30 @@ namespace xp::detail
{
if (entry.path().extension() == extension)
{
std::string name = entry.path().stem().string();

// remove prefix
if (name.substr(0, prefix.size()) == prefix)
{
name = name.substr(prefix.size());
m_lazy_shared_lib_factories.emplace(name, entry.path());
}
add_entry(entry.path(), prefix);
}
}
}

template <class FACTORY_BASE, bool THREAD_SAVE>
bool xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::contains(const std::string &name) const
template <class FACTORY_BASE, bool THREAD_SAFE>
template <class R, std::enable_if_t<xp::util::is_range_of_v<R, std::filesystem::path>, int>>
xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::xplugin_registry_impl(R r, const std::string& prefix)
{
for (const auto& entry: r)
{
add_entry(entry, prefix);
}
}

template <class FACTORY_BASE, bool THREAD_SAFE>
bool xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::contains(const std::string &name) const
{
return m_lazy_shared_lib_factories.find(name) != m_lazy_shared_lib_factories.end();
}

template <class FACTORY_BASE, bool THREAD_SAVE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::factory_base_type *xplugin_registry_impl<
FACTORY_BASE, THREAD_SAVE>::operator[](const std::string &name)
template <class FACTORY_BASE, bool THREAD_SAFE>
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::factory_base_type *xplugin_registry_impl<
FACTORY_BASE, THREAD_SAFE>::operator[](const std::string &name)
{
auto find_res = m_lazy_shared_lib_factories.find(name);
if (find_res == m_lazy_shared_lib_factories.end())
Expand All @@ -282,20 +292,32 @@ namespace xp::detail
return find_res->second.factory();
}

template <class FACTORY_BASE, bool THREAD_SAVE>
std::size_t xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::size() const
template <class FACTORY_BASE, bool THREAD_SAFE>
std::size_t xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::size() const
{
return m_lazy_shared_lib_factories.size();
}

template <class FACTORY_BASE, bool THREAD_SAVE>
bool xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::empty() const
template <class FACTORY_BASE, bool THREAD_SAFE>
bool xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::empty() const
{
return m_lazy_shared_lib_factories.empty();
}

template <class FACTORY_BASE, bool THREAD_SAVE>
std::string xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::get_default_library_extension()
template <class FACTORY_BASE, bool THREAD_SAFE>
void xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::add_entry(const std::filesystem::path& p, const std::string& prefix)
{
std::string name = p.stem().string();
// remove prefix
if (name.substr(0, prefix.size()) == prefix)
{
name = name.substr(prefix.size());
m_lazy_shared_lib_factories.emplace(name, p);
}
}

template <class FACTORY_BASE, bool THREAD_SAFE>
std::string xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::get_default_library_extension()
{
#ifdef _WIN32
return ".dll";
Expand All @@ -306,8 +328,8 @@ namespace xp::detail
#endif
}

template <class FACTORY_BASE, bool THREAD_SAVE>
std::string xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::get_default_library_prefix()
template <class FACTORY_BASE, bool THREAD_SAFE>
std::string xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::get_default_library_prefix()
{
#ifdef _WIN32
return "";
Expand Down
76 changes: 76 additions & 0 deletions include/xplugin/xplugin_util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/***************************************************************************
* Copyright (c) 2018, Johan Mabille, Sylvain Corlay, Wolf Vollprecht, *
* Martin Renou and Dr. Thorsten Beier *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XPLUGIN_UTIL_HPP
#define XPLUGIN_UTIL_HPP

#include <type_traits>

namespace xp::util
{
template <class T, class = void>
struct has_begin : std::false_type
{
};

template <class T>
struct has_begin<T, std::void_t<decltype(std::declval<T>().begin())>> : std::true_type
{
};

template <class T, class = void>
struct has_end : std::false_type
{
};

template <class T>
struct has_end<T, std::void_t<decltype(std::declval<T>().end())>> : std::true_type
{
};

template <class T>
using is_range = std::conjunction<has_begin<T>, has_end<T>>;

template <class T>
constexpr bool is_range_v = is_range<T>::value;

namespace detail
{
struct invalid_type {};
}

template <class R, class = void>
struct range_value
{
using type = detail::invalid_type;
};

template <class R>
struct range_value<R, std::void_t<std::decay_t<decltype(*(std::declval<R>().begin()))>>>
{
using type = typename R::value_type;
};

template <class R>
using range_value_t = typename range_value<R>::type;

template <class R, class T>
struct is_range_of
: std::conjunction<
is_range<R>,
std::is_convertible<range_value_t<R>, T>
>
{
};

template <class R, class T>
constexpr bool is_range_of_v = is_range_of<R, T>::value;
}

#endif
Loading