@@ -57,18 +57,56 @@ class FactoryBase : public Base<CRTP> {
57
57
public: // Types
58
58
using Type = T;
59
59
using MakeFunc = std::function<T(const Conf& c)>;
60
+
61
+ /* *
62
+ * TypeFactory is basically a pair with names for better readability.
63
+ */
60
64
struct TypeFactory {
61
- TypeFactory (const Box& s, MakeFunc f) : schema(s), func(f) { schema.reset_ptr (); }
65
+ TypeFactory (Box s, MakeFunc f) : schema(std::move(s)), func(std::move(f)) {
66
+ schema.reset_ptr ();
67
+ }
62
68
63
- Box schema;
64
- MakeFunc func;
69
+ Box schema; // NOLINT
70
+ MakeFunc func; // NOLINT
65
71
};
66
72
67
73
using TransformFunc = std::function<Box(Struct&&)>;
68
74
using FactoryMap = std::map<std::string, TypeFactory>;
69
75
using FactoryPairList = std::initializer_list<std::pair<std::string, TypeFactory>>;
70
76
71
77
public: // Constructors
78
+ ~FactoryBase () noexcept override = default ;
79
+
80
+ protected:
81
+ FactoryBase (const FactoryBase& other)
82
+ : Base<CRTP>(other)
83
+ , transform_func_(other.transform_func_)
84
+ , available_(other.available_)
85
+ , factory_key_(other.factory_key_)
86
+ , args_key_(other.args_key_)
87
+ , args_subset_(other.args_subset_) {
88
+ reset_schema ();
89
+ }
90
+
91
+ FactoryBase& operator =(const FactoryBase& other) {
92
+ if (&other == this ) {
93
+ return *this ;
94
+ }
95
+ Base<CRTP>::operator =(other);
96
+ transform_func_ = other.transform_func_ ;
97
+ available_ = other.available_ ;
98
+ factory_key_ = other.factory_key_ ;
99
+ args_key_ = other.args_key_ ;
100
+ args_subset_ = other.args_subset_ ;
101
+ reset_schema ();
102
+ return *this ;
103
+ }
104
+
105
+ FactoryBase (FactoryBase&&) noexcept = default ;
106
+
107
+ FactoryBase& operator =(FactoryBase&&) noexcept = default ;
108
+
109
+ public:
72
110
/* *
73
111
* Construct an empty factory.
74
112
*
@@ -137,7 +175,7 @@ class FactoryBase : public Base<CRTP> {
137
175
* Common choices could be: factory, type, binding.
138
176
*/
139
177
void set_factory_key (const std::string& keyword) {
140
- assert (keyword != " " );
178
+ assert (!keyword. empty () );
141
179
factory_key_ = keyword;
142
180
reset_schema ();
143
181
}
@@ -176,7 +214,7 @@ class FactoryBase : public Base<CRTP> {
176
214
*
177
215
* The default behavior is the identity function.
178
216
*/
179
- void set_transform_schema (TransformFunc f) { transform_func_ = f ; }
217
+ void set_transform_schema (TransformFunc f) { transform_func_ = std::move (f) ; }
180
218
181
219
/* *
182
220
* Return the schema and factory function associated with the given key.
@@ -198,7 +236,7 @@ class FactoryBase : public Base<CRTP> {
198
236
*/
199
237
bool add_factory (const std::string& key, Box&& s, MakeFunc f) {
200
238
if (!available_.count (key)) {
201
- available_.insert (std::make_pair (key, TypeFactory{std::move (s), f }));
239
+ available_.insert (std::make_pair (key, TypeFactory{std::move (s), std::move (f) }));
202
240
reset_schema ();
203
241
return true ;
204
242
}
@@ -212,7 +250,7 @@ class FactoryBase : public Base<CRTP> {
212
250
if (!available_.count (key)) {
213
251
available_.erase (key);
214
252
}
215
- available_.insert (std::make_pair (key, TypeFactory{std::move (s), f }));
253
+ available_.insert (std::make_pair (key, TypeFactory{std::move (s), std::move (f) }));
216
254
reset_schema ();
217
255
}
218
256
@@ -316,7 +354,7 @@ class FactoryBase : public Base<CRTP> {
316
354
if (available_.size () == 0 ) {
317
355
return ;
318
356
}
319
- schema_. reset ( new Variant (factory_schemas () ));
357
+ schema_ = std::make_unique< Variant> (factory_schemas ());
320
358
}
321
359
322
360
[[nodiscard]] std::vector<Box> factory_schemas () const {
@@ -353,7 +391,7 @@ class FactoryBase : public Base<CRTP> {
353
391
}
354
392
355
393
protected:
356
- std::shared_ptr <Variant> schema_;
394
+ std::unique_ptr <Variant> schema_;
357
395
TransformFunc transform_func_;
358
396
FactoryMap available_;
359
397
std::string factory_key_{" factory" };
@@ -366,7 +404,16 @@ class FactoryBase : public Base<CRTP> {
366
404
* instance but is only usable through it's make() method.
367
405
*/
368
406
template <typename T>
369
- class FactoryPointerless : public FactoryBase <T, FactoryPointerless<T>> {};
407
+ class FactoryPointerless : public FactoryBase <T, FactoryPointerless<T>> {
408
+ public:
409
+ using FactoryBase<T, FactoryPointerless<T>>::FactoryBase;
410
+ FactoryPointerless () = default ;
411
+ FactoryPointerless (const FactoryPointerless<T>& other) = default ;
412
+ FactoryPointerless (FactoryPointerless<T>&& other) noexcept = default ;
413
+ FactoryPointerless<T>& operator =(const FactoryPointerless<T>& other) = default ;
414
+ FactoryPointerless<T>& operator =(FactoryPointerless<T>&& other) noexcept = default ;
415
+ ~FactoryPointerless () override = default ;
416
+ };
370
417
371
418
/* *
372
419
* Factory is a factory schema that extends FactoryPointerless in that it
@@ -386,6 +433,11 @@ class Factory : public FactoryBase<T, Factory<T>> {
386
433
387
434
public: // Constructors
388
435
using FactoryBase<T, Factory<T>>::FactoryBase;
436
+ Factory (const Factory<T>& other) = default ;
437
+ Factory (Factory<T>&& other) noexcept = default ;
438
+ Factory<T>& operator =(const Factory<T>& other) = default ;
439
+ Factory<T>& operator =(Factory<T>&& other) noexcept = default ;
440
+ ~Factory () override = default ;
389
441
390
442
Factory (Type* ptr, std::string desc) : FactoryBase<T, Factory<T>>(std::move(desc)), ptr_(ptr) {}
391
443
0 commit comments