Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
Object.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/variant>
9#include <object/AnyObject.h>
10
11namespace object {
12
16class Object {
21
26 void unify ( Object & other ) {
27 if ( this->m_data.use_count ( ) > other.m_data.use_count ( ) )
28 other.m_data = this->m_data;
29 else
30 this->m_data = other.m_data;
31 }
32
37 template < class Variant >
38 static Object processVariant ( Variant && data ) {
39 auto visitor = [] ( auto && element ) {
40 return Object ( std::forward < decltype ( element ) > ( element ) );
41 };
42
43 return ext::visit ( visitor, std::forward < Variant > ( data ) );
44 }
45
49 explicit Object ( AnyObjectBase * data ) : m_data ( data ) {
50 }
51
57 void setData ( AnyObjectBase * data ) {
58 if ( & getData ( ) == data )
59 return;
60
61 this->m_data = ext::cow_shared_ptr < AnyObjectBase > ( data );
62 }
63
64public:
69 explicit Object ( const char * string ) : Object ( std::string ( string ) ) {
70 }
71
76 template < class Type >
77 requires ( ! std::is_same_v < std::decay_t < Type >, Object > )
78 // NOLINTNEXTLINE ( bugprone-forwarding-reference-overload )
79 explicit Object ( Type && data ) : Object ( object::AnyObject < typename std::decay < Type >::type > ( std::forward < Type > ( data ) ) ) {
80 }
81
89 template < class ... Types >
90 explicit Object ( ext::variant < Types ... > && data ) : Object ( processVariant ( std::move ( data ) ) ) {
91 }
92
100 template < class ... Types >
101 explicit Object ( const ext::variant < Types ... > & data ) : Object ( processVariant ( data ) ) {
102 }
103
107 template < class Type >
108 explicit Object ( const AnyObject < Type > & data ) : m_data ( data.clone ( ) ) {
109 }
110
114 template < class Type >
115 explicit Object ( AnyObject < Type > & data ) : m_data ( data.clone ( ) ) {
116 }
117
121 template < class Type >
122 explicit Object ( AnyObject < Type > && data ) : m_data ( std::move ( data ).clone ( ) ) {
123 }
124
128 explicit Object ( const AnyObject < object::Object > & data ) : Object ( data.getData ( ) ) {
129 }
130
134 explicit Object ( AnyObject < object::Object > && data ) : Object ( std::move ( data ).getData ( ) ) {
135 }
136
142 const AnyObjectBase & getData ( ) const {
143 return * m_data;
144 }
145
152 return * m_data;
153 }
154
158 void setData ( const AnyObjectBase & data ) {
159 setData ( data.clone ( ) );
160 }
161
165 void setData ( AnyObjectBase && data ) {
166 setData ( std::move ( data ).clone ( ) );
167 }
168
176 std::strong_ordering operator <=> ( const Object & other ) const {
177 if ( this->m_data.get ( ) == other.m_data.get ( ) )
178 return std::strong_ordering::equal;
179
180 std::strong_ordering res = ( * this->m_data ) <=> ( * other.m_data );
181 if ( res == 0 )
182 const_cast < Object * > ( this )->unify ( const_cast < Object & > ( other ) );
183
184 return res;
185 }
186
194 bool operator ==( const Object & other ) const {
195 if ( this->m_data.get ( ) == other.m_data.get ( ) )
196 return true;
197
198 bool res = ( * this->m_data ) == ( * other.m_data );
199 if ( res )
200 const_cast < Object * > ( this )->unify ( const_cast < Object & > ( other ) );
201
202 return res;
203 }
204
213 friend ext::ostream & operator <<( ext::ostream & os, const Object & instance ) {
214 instance.getData ( ) >> os;
215 return os;
216 }
217
223 explicit operator std::string ( ) const {
224 return m_data->operator std::string ( );
225 }
226
234 this->getData ( ).increment ( 1 );
235 return *this;
236 }
237
245 Object res = * this;
246 ++ * this;
247 return res;
248 }
249
256 Object operator += ( unsigned by ) {
257 this->getData ( ).increment ( by );
258 return *this;
259 }
260
266 unsigned getId ( ) const {
267 return this->getData ( ).getId ( );
268 }
269};
270
271} /* namespace object */
272
Specialisation of copy on write pointer for classes based with copy on write pointer base.
Definition: memory.hpp:46
T * get()
Definition: memory.hpp:156
unsigned use_count() const
Getter of the number how many times the managed pointer is referenced.
Definition: memory.hpp:186
Definition: ostream.h:14
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
Definition: AnyObjectBase.h:32
virtual AnyObjectBase * clone() const &=0
Virtual copy constructor.
virtual void increment(unsigned by)=0
Increments the unique counter of the object.
virtual unsigned getId() const =0
Represents an adaptor of any type to a class in type hierarchy of objects in the algorithms library.
Definition: AnyObject.h:37
Definition: Object.h:16
Object(const AnyObject< object::Object > &data)
Definition: Object.h:128
std::strong_ordering operator<=>(const Object &other) const
Definition: Object.h:176
Object(ext::variant< Types ... > &&data)
Specialisation of the make method for variants.
Definition: Object.h:90
Object(AnyObject< Type > &data)
Definition: Object.h:115
void setData(const AnyObjectBase &data)
Definition: Object.h:158
Object(const ext::variant< Types ... > &data)
Specialisation of the make method for variants.
Definition: Object.h:101
Object(const AnyObject< Type > &data)
Definition: Object.h:108
Object(const char *string)
Specialisation of the make method for c-strings.
Definition: Object.h:69
friend ext::ostream & operator<<(ext::ostream &os, const Object &instance)
Definition: Object.h:213
Object operator+=(unsigned by)
Increments the unique counter of the object.
Definition: Object.h:256
AnyObjectBase & getData()
Definition: Object.h:151
Object(Type &&data)
Specialisation of the make method for objects that are not from the object hierarchy of Algorithms li...
Definition: Object.h:79
unsigned getId() const
Definition: Object.h:266
void setData(AnyObjectBase &&data)
Definition: Object.h:165
Object(AnyObject< object::Object > &&data)
Definition: Object.h:134
Object(AnyObject< Type > &&data)
Definition: Object.h:122
const AnyObjectBase & getData() const
Definition: Object.h:142
bool operator==(const Object &other) const
Definition: Object.h:194
Object & operator++()
Increments the unique counter of the object by one. Prefix version.
Definition: Object.h:233
return res
Definition: MinimizeByPartitioning.h:145
auto clone(T &&tmp)
Wrapper around clone by means of using copy constructor or clone method if available.
Definition: clone.hpp:41
constexpr auto visit(Visitor &&vis, Variants &&... vars)
Definition: variant.hpp:42
Type
Definition: MeasurementTypes.hpp:20
Definition: AnyObject.h:28
Definition: FordFulkerson.hpp:16
Definition: RandomStringFactory.cpp:12