Skip to content

Commit 532462c

Browse files
committed
Added xplugin_registry constructor from list of paths
1 parent 227aa50 commit 532462c

File tree

4 files changed

+277
-150
lines changed

4 files changed

+277
-150
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ set(XPLUGIN_HEADERS
5252
${XPLUGIN_INCLUDE_DIR}/xplugin/xfactory.hpp
5353
${XPLUGIN_INCLUDE_DIR}/xplugin/xlazy_shared_library_plugin_factory.hpp
5454
${XPLUGIN_INCLUDE_DIR}/xplugin/xplugin_config.hpp
55+
${XPLUGIN_INCLUDE_DIR}/xplugin/xplugin_util.hpp
5556
${XPLUGIN_INCLUDE_DIR}/xplugin/xplugin_registry.hpp
5657
${XPLUGIN_INCLUDE_DIR}/xplugin/xshared_library.hpp
5758
)

include/xplugin/xplugin_registry.hpp

+68-46
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* *
66
* The full license is in the file LICENSE, distributed with this software. *
77
****************************************************************************/
8+
89
#ifndef XPLUGIN_REGISTRY_HPP
910
#define XPLUGIN_REGISTRY_HPP
1011

@@ -17,10 +18,10 @@
1718

1819
#include <xplugin/xlazy_shared_library_plugin_factory.hpp>
1920
#include <xplugin/xplugin_config.hpp>
21+
#include <xplugin/xplugin_util.hpp>
2022

2123
namespace xp::detail
2224
{
23-
2425
template <class FACTORY_BASE, bool THREAD_SAFE>
2526
class xplugin_registry_impl;
2627

@@ -95,11 +96,15 @@ namespace xp::detail
9596
using const_iterator = xplugin_registry_iterator<self_type, typename storage_map_type::const_iterator>;
9697
using iterator = xplugin_registry_iterator<self_type, typename storage_map_type::iterator>;
9798

98-
xplugin_registry_impl(const std::filesystem::path &path,
99-
const std::string &prefix = get_default_library_prefix(),
100-
const std::string &extension = get_default_library_extension());
99+
explicit xplugin_registry_impl(const std::filesystem::path& path,
100+
const std::string& prefix = get_default_library_prefix(),
101+
const std::string& extension = get_default_library_extension());
101102

102-
factory_base_type *operator[](const std::string &name);
103+
template <class R, std::enable_if_t<xp::util::is_range_of_v<R, std::filesystem::path>, int> = 3>
104+
explicit xplugin_registry_impl(R r,
105+
const std::string& prefix = get_default_library_prefix());
106+
107+
factory_base_type* operator[](const std::string& name);
103108

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

117122
private:
118123

124+
void add_entry(const std::filesystem::path& path, const std::string& prefix);
125+
119126
static std::string get_default_library_extension();
120127
static std::string get_default_library_prefix();
121128

@@ -199,51 +206,51 @@ namespace xp::detail
199206
}
200207

201208
// registry implementation
202-
template <class FACTORY_BASE, bool THREAD_SAVE>
203-
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::iterator xplugin_registry_impl<FACTORY_BASE,
204-
THREAD_SAVE>::begin()
209+
template <class FACTORY_BASE, bool THREAD_SAFE>
210+
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::iterator xplugin_registry_impl<FACTORY_BASE,
211+
THREAD_SAFE>::begin()
205212
{
206213
return iterator(m_lazy_shared_lib_factories.begin());
207214
}
208215

209-
template <class FACTORY_BASE, bool THREAD_SAVE>
210-
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::iterator xplugin_registry_impl<FACTORY_BASE,
211-
THREAD_SAVE>::end()
216+
template <class FACTORY_BASE, bool THREAD_SAFE>
217+
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::iterator xplugin_registry_impl<FACTORY_BASE,
218+
THREAD_SAFE>::end()
212219
{
213220
return iterator(m_lazy_shared_lib_factories.end());
214221
}
215222

216223
// const iterator
217-
template <class FACTORY_BASE, bool THREAD_SAVE>
218-
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::const_iterator xplugin_registry_impl<
219-
FACTORY_BASE, THREAD_SAVE>::begin() const
224+
template <class FACTORY_BASE, bool THREAD_SAFE>
225+
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::const_iterator xplugin_registry_impl<
226+
FACTORY_BASE, THREAD_SAFE>::begin() const
220227
{
221228
return const_iterator(m_lazy_shared_lib_factories.cbegin());
222229
}
223230

224-
template <class FACTORY_BASE, bool THREAD_SAVE>
225-
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::const_iterator xplugin_registry_impl<
226-
FACTORY_BASE, THREAD_SAVE>::end() const
231+
template <class FACTORY_BASE, bool THREAD_SAFE>
232+
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::const_iterator xplugin_registry_impl<
233+
FACTORY_BASE, THREAD_SAFE>::end() const
227234
{
228235
return const_iterator(m_lazy_shared_lib_factories.cend());
229236
}
230237

231-
template <class FACTORY_BASE, bool THREAD_SAVE>
232-
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::const_iterator xplugin_registry_impl<
233-
FACTORY_BASE, THREAD_SAVE>::cbegin() const
238+
template <class FACTORY_BASE, bool THREAD_SAFE>
239+
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::const_iterator xplugin_registry_impl<
240+
FACTORY_BASE, THREAD_SAFE>::cbegin() const
234241
{
235242
return const_iterator(m_lazy_shared_lib_factories.cbegin());
236243
}
237244

238-
template <class FACTORY_BASE, bool THREAD_SAVE>
239-
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::const_iterator xplugin_registry_impl<
240-
FACTORY_BASE, THREAD_SAVE>::cend() const
245+
template <class FACTORY_BASE, bool THREAD_SAFE>
246+
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::const_iterator xplugin_registry_impl<
247+
FACTORY_BASE, THREAD_SAFE>::cend() const
241248
{
242249
return const_iterator(m_lazy_shared_lib_factories.cend());
243250
}
244251

245-
template <class FACTORY_BASE, bool THREAD_SAVE>
246-
xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::xplugin_registry_impl(const std::filesystem::path &path,
252+
template <class FACTORY_BASE, bool THREAD_SAFE>
253+
xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::xplugin_registry_impl(const std::filesystem::path &path,
247254
const std::string &prefix,
248255
const std::string &extension)
249256
{
@@ -252,27 +259,30 @@ namespace xp::detail
252259
{
253260
if (entry.path().extension() == extension)
254261
{
255-
std::string name = entry.path().stem().string();
256-
257-
// remove prefix
258-
if (name.substr(0, prefix.size()) == prefix)
259-
{
260-
name = name.substr(prefix.size());
261-
m_lazy_shared_lib_factories.emplace(name, entry.path());
262-
}
262+
add_entry(entry.path(), prefix);
263263
}
264264
}
265265
}
266266

267-
template <class FACTORY_BASE, bool THREAD_SAVE>
268-
bool xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::contains(const std::string &name) const
267+
template <class FACTORY_BASE, bool THREAD_SAFE>
268+
template <class R, std::enable_if_t<xp::util::is_range_of_v<R, std::filesystem::path>, int>>
269+
xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::xplugin_registry_impl(R r, const std::string& prefix)
270+
{
271+
for (const auto& entry: r)
272+
{
273+
add_entry(entry, prefix);
274+
}
275+
}
276+
277+
template <class FACTORY_BASE, bool THREAD_SAFE>
278+
bool xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::contains(const std::string &name) const
269279
{
270280
return m_lazy_shared_lib_factories.find(name) != m_lazy_shared_lib_factories.end();
271281
}
272282

273-
template <class FACTORY_BASE, bool THREAD_SAVE>
274-
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::factory_base_type *xplugin_registry_impl<
275-
FACTORY_BASE, THREAD_SAVE>::operator[](const std::string &name)
283+
template <class FACTORY_BASE, bool THREAD_SAFE>
284+
typename xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::factory_base_type *xplugin_registry_impl<
285+
FACTORY_BASE, THREAD_SAFE>::operator[](const std::string &name)
276286
{
277287
auto find_res = m_lazy_shared_lib_factories.find(name);
278288
if (find_res == m_lazy_shared_lib_factories.end())
@@ -282,20 +292,32 @@ namespace xp::detail
282292
return find_res->second.factory();
283293
}
284294

285-
template <class FACTORY_BASE, bool THREAD_SAVE>
286-
std::size_t xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::size() const
295+
template <class FACTORY_BASE, bool THREAD_SAFE>
296+
std::size_t xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::size() const
287297
{
288298
return m_lazy_shared_lib_factories.size();
289299
}
290300

291-
template <class FACTORY_BASE, bool THREAD_SAVE>
292-
bool xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::empty() const
301+
template <class FACTORY_BASE, bool THREAD_SAFE>
302+
bool xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::empty() const
293303
{
294304
return m_lazy_shared_lib_factories.empty();
295305
}
296306

297-
template <class FACTORY_BASE, bool THREAD_SAVE>
298-
std::string xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::get_default_library_extension()
307+
template <class FACTORY_BASE, bool THREAD_SAFE>
308+
void xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::add_entry(const std::filesystem::path& p, const std::string& prefix)
309+
{
310+
std::string name = p.stem().string();
311+
// remove prefix
312+
if (name.substr(0, prefix.size()) == prefix)
313+
{
314+
name = name.substr(prefix.size());
315+
m_lazy_shared_lib_factories.emplace(name, p);
316+
}
317+
}
318+
319+
template <class FACTORY_BASE, bool THREAD_SAFE>
320+
std::string xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::get_default_library_extension()
299321
{
300322
#ifdef _WIN32
301323
return ".dll";
@@ -306,8 +328,8 @@ namespace xp::detail
306328
#endif
307329
}
308330

309-
template <class FACTORY_BASE, bool THREAD_SAVE>
310-
std::string xplugin_registry_impl<FACTORY_BASE, THREAD_SAVE>::get_default_library_prefix()
331+
template <class FACTORY_BASE, bool THREAD_SAFE>
332+
std::string xplugin_registry_impl<FACTORY_BASE, THREAD_SAFE>::get_default_library_prefix()
311333
{
312334
#ifdef _WIN32
313335
return "";

include/xplugin/xplugin_util.hpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/***************************************************************************
2+
* Copyright (c) 2018, Johan Mabille, Sylvain Corlay, Wolf Vollprecht, *
3+
* Martin Renou and Dr. Thorsten Beier *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#ifndef XPLUGIN_UTIL_HPP
11+
#define XPLUGIN_UTIL_HPP
12+
13+
#include <type_traits>
14+
15+
namespace xp::util
16+
{
17+
template <class T, class = void>
18+
struct has_begin : std::false_type
19+
{
20+
};
21+
22+
template <class T>
23+
struct has_begin<T, std::void_t<decltype(std::declval<T>().begin())>> : std::true_type
24+
{
25+
};
26+
27+
template <class T, class = void>
28+
struct has_end : std::false_type
29+
{
30+
};
31+
32+
template <class T>
33+
struct has_end<T, std::void_t<decltype(std::declval<T>().end())>> : std::true_type
34+
{
35+
};
36+
37+
template <class T>
38+
using is_range = std::conjunction<has_begin<T>, has_end<T>>;
39+
40+
template <class T>
41+
constexpr bool is_range_v = is_range<T>::value;
42+
43+
namespace detail
44+
{
45+
struct invalid_type {};
46+
}
47+
48+
template <class R, class = void>
49+
struct range_value
50+
{
51+
using type = detail::invalid_type;
52+
};
53+
54+
template <class R>
55+
struct range_value<R, std::void_t<std::decay_t<decltype(*(std::declval<R>().begin()))>>>
56+
{
57+
using type = typename R::value_type;
58+
};
59+
60+
template <class R>
61+
using range_value_t = typename range_value<R>::type;
62+
63+
template <class R, class T>
64+
struct is_range_of
65+
: std::conjunction<
66+
is_range<R>,
67+
std::is_convertible<range_value_t<R>, T>
68+
>
69+
{
70+
};
71+
72+
template <class R, class T>
73+
constexpr bool is_range_of_v = is_range_of<R, T>::value;
74+
}
75+
76+
#endif

0 commit comments

Comments
 (0)