97 friend std::ostream&
operator<<(std::ostream& os,
const Clonable& cl);
100 virtual ~Clonable() {}
129 virtual std::ostream&
print(std::ostream& os)
const {
return os;}
141inline std::ostream&
operator<<(std::ostream& os,
const Clonable& cl) {
180template<
class Derived,
class Base,
185 static_assert(std::is_base_of<Clonable,Base>::value,
186 "Clonable_Impl: Base must derive from Clonable (family-base contract)");
194 enum Slot {SLOT_CLONE, SLOT_SHARING, SLOT_STATELESS};
199 assert(
typeid(*
this)==
typeid(Derived));
200 return static_cast<const Derived&
>(*this);
206 template<CloneKind K, Slot S> Base*
make()
const {
207 static_assert(std::is_base_of<Clonable_Impl,Derived>::value,
208 "Clonable_Impl: Derived must derive from its own Clonable_Impl (CRTP contract)");
210 Derived *c=
new Derived(
self()); c->set_cloned();
return c;
212 Derived *c=
new Derived(
self(),0); c->set_cloned();
return c;
214 Derived *c=
new Derived(
self(),0); c->set_sharing_cloned();
return c;
218 throw fst_error(
"Clonable_Impl: this clone kind is not supported by this class.");
222 if constexpr(S==SLOT_CLONE)
return static_cast<Base*
>(Base::clone());
223 else if constexpr(S==SLOT_SHARING)
return static_cast<Base*
>(Base::sharing_clone());
224 else return static_cast<Base*
>(Base::stateless_clone());
235 return std::unique_ptr<T>(
static_cast<T*
>(src.clone()));
240 return std::unique_ptr<T>(
static_cast<T*
>(src.sharing_clone()));
245 return std::unique_ptr<T>(
static_cast<T*
>(src.stateless_clone()));
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Base * clone() const override
dispatches to the DEEP_KIND mechanism (default CLONE_DEEP: Derived's copy-constructor)
Definition clonable.hpp:188
Base * stateless_clone() const override
dispatches to the STATELESS_KIND mechanism (default CLONE_INHERIT: defers to Base,...
Definition clonable.hpp:190
Base * make() const
Constructs the clone per CloneKind K, for the override slot S (see Slot). Only the branch selected by...
Definition clonable.hpp:206
Slot
< inherits Base's constructors, so Derived need not repeat them
Definition clonable.hpp:194
const Derived & self() const
Definition clonable.hpp:198
Base * sharing_clone() const override
dispatches to the SHARING_KIND mechanism (default CLONE_INHERIT: defers to Base, which throws by fami...
Definition clonable.hpp:189
Abstract class, defines interface for classes that may need to get cloned.
Definition clonable.hpp:95
virtual Clonable * clone() const =0
void set_cloned(const bool cloned=true)
marks/unmarks this instance as a clone; call from clone()/stateless_clone() overrides (Clonable_Impl ...
Definition clonable.hpp:131
bool is_clone() const
Definition clonable.hpp:125
virtual Clonable * stateless_clone() const =0
virtual std::ostream & print(std::ostream &os) const
writes a human-readable identification of the object; base default writes nothing
Definition clonable.hpp:129
friend std::ostream & operator<<(std::ostream &os, const Clonable &cl)
Streams a clonable object's print() to os.
Definition clonable.hpp:141
void set_sharing_cloned(const bool cloned=true, const bool sharing=true)
sets both flags at once; call from sharing_clone() overrides (Clonable_Impl does this automatically)
Definition clonable.hpp:133
bool _sharing
set by set_sharing()/set_sharing_cloned(); read by is_sharing()/is_sharing_clone()
Definition clonable.hpp:137
bool _clone
Definition clonable.hpp:136
virtual Clonable * sharing_clone() const =0
bool is_sharing() const
true if this instance is a sharing_clone() result (referencing, not owning, the source's structures)
Definition clonable.hpp:126
bool is_sharing_clone() const
true if this instance is BOTH a clone and sharing – i.e. specifically a sharing_clone() result,...
Definition clonable.hpp:127
void set_sharing(const bool sharing=true)
marks/unmarks this instance as sharing (see is_sharing())
Definition clonable.hpp:132
FST4 specific error passing class.
Definition error.hpp:61
FST4 specific error passing class.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
CloneKind
How a Clonable_Impl slot (clone/sharing_clone/stateless_clone) is implemented.
Definition clonable.hpp:146
@ CLONE_TO_STATELESS
forward to stateless_clone() (Classifier_LIBSVM contract)
Definition clonable.hpp:152
@ CLONE_WEAK
new Derived(*this,0) via the weak/referencing copy-constructor, then set_cloned()
Definition clonable.hpp:150
@ CLONE_DEEP
new Derived(*this), then set_cloned()
Definition clonable.hpp:149
@ CLONE_WEAK_SHARING
new Derived(*this,0) via the weak/referencing copy-constructor, then set_sharing_cloned()
Definition clonable.hpp:151
@ CLONE_INHERIT
do not add behavior; defer to the Base class body (typically a family-level throwing default)
Definition clonable.hpp:147
@ CLONE_THROW
throw fst_error (for classes whose Base has no default body to defer to)
Definition clonable.hpp:148
std::ostream & operator<<(std::ostream &os, const Data_Intervaller< CONTAINER, IDXTYPE > &ia)
Streams every contained interval as "si:<startidx>, c:<count>" lines to os.
Definition data_intervaller.hpp:152
std::unique_ptr< T > clone_unique(const T &src)
Definition clonable.hpp:234
std::unique_ptr< T > stateless_clone_unique(const T &src)
stateless_clone() counterpart of clone_unique() – see its comment above.
Definition clonable.hpp:244
std::unique_ptr< T > sharing_clone_unique(const T &src)
sharing_clone() counterpart of clone_unique() – see its comment above.
Definition clonable.hpp:239