Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
AnyObject.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include "AnyObjectBase.h"
27
28namespace object {
29
36template < class T >
37class AnyObject : public AnyObjectBase {
42 T m_data;
43
48 unsigned m_id;
49public:
56 explicit AnyObject ( T, unsigned id = 0);
57
61 AnyObjectBase * clone ( ) const & override;
62
66 AnyObjectBase * clone ( ) && override;
67
71 std::strong_ordering operator <=> ( const AnyObjectBase & other ) const override {
72 if ( ext::type_index ( typeid ( * this ) ) == ext::type_index ( typeid ( other ) ) ) return * this <=> static_cast < decltype ( ( * this ) ) > ( other );
73
74 return ext::type_index ( typeid ( * this ) ) <=> ext::type_index ( typeid ( other ) );
75 }
76
84 std::strong_ordering operator <=> ( const AnyObject < T > & other ) const {
85 auto res = this->getData ( ) <=> other.getData ( );
86 if ( res == 0 )
87 return m_id <=> other.m_id;
88 else if ( res < 0 )
89 return std::strong_ordering::less;
90 else
91 return std::strong_ordering::greater;
92 }
93
97 bool operator == ( const AnyObjectBase & other ) const override {
98 if ( ext::type_index ( typeid ( * this ) ) == ext::type_index ( typeid ( other ) ) ) return * this == static_cast < decltype ( ( * this ) ) > ( other );
99
100 return false;
101 }
102
110 bool operator == ( const AnyObject & other ) const {
111 bool res = this->getData ( ) == other.getData ( );
112
113 if ( res )
114 res = m_id == other.m_id;
115
116 return res;
117 }
118
122 void operator >>( ext::ostream & out ) const override;
123
127 explicit operator std::string ( ) const override;
128
132 void increment ( unsigned by ) override {
133 m_id += by;
134 }
135
141 const T & getData ( ) const;
142
146 unsigned getId ( ) const override {
147 return m_id;
148 }
149};
150
151template < class T >
152AnyObject < T >::AnyObject ( T data, unsigned id ) : m_data ( std::move ( data ) ), m_id ( id ) {
153}
154
155template < class T >
157 return new AnyObject(*this);
158}
159
160template < class T >
161AnyObjectBase * AnyObject < T >::clone() && {
162 return new AnyObject(std::move(*this));
163}
164
165template < class T >
167 out << this->getData ( );
168 for ( unsigned i = 0; i < m_id; ++ i )
169 out << "'";
170}
171
172template < class T >
173AnyObject < T > ::operator std::string () const {
174 return ext::to_string ( this->getData ( ) ) + std::string ( "'", m_id );
175}
176
177template < class T >
178const T & AnyObject < T >::getData ( ) const {
179 return m_data;
180}
181
182} /* namespace object */
183
Definition: ostream.h:14
Definition: typeindex.h:37
Definition: AnyObjectBase.h:32
Represents an adaptor of any type to a class in type hierarchy of objects in the algorithms library.
Definition: AnyObject.h:37
std::strong_ordering operator<=>(const AnyObjectBase &other) const override
<=> ( const AnyObjectBase & ) const
Definition: AnyObject.h:71
void increment(unsigned by) override
Increments the unique counter of the object. ( ) const
Definition: AnyObject.h:132
AnyObjectBase * clone() const &override
Virtual copy constructor. ( ) const &
unsigned getId() const override
( ) const
Definition: AnyObject.h:146
AnyObject(T, unsigned id=0)
Constructor of the class based on the value of the wrapped object.
Definition: AnyObject.h:152
void operator>>(ext::ostream &out) const override
>> ( ext::ostream & ) const
Definition: AnyObject.h:166
const T & getData() const
Definition: AnyObject.h:178
bool operator==(const AnyObjectBase &other) const override
== ( const AnyObjectBase & ) const
Definition: AnyObject.h:97
int i
Definition: AllEpsilonClosure.h:118
return res
Definition: MinimizeByPartitioning.h:145
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
Definition: AnyObject.h:28
Definition: FordFulkerson.hpp:16