Dillo
object.hh
Go to the documentation of this file.
1 #ifndef __LOUT_OBJECT_HH__
2 #define __LOUT_OBJECT_HH__
3 
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #include "misc.hh"
8 
9 namespace lout {
10 
15 namespace object {
16 
24 class Object
25 {
26 public:
27  virtual ~Object();
28  virtual bool equals(Object *other);
29  virtual int hashValue();
30  virtual Object *clone();
31  virtual void intoStringBuffer(misc::StringBuffer *sb);
32  const char *toString();
33  virtual size_t sizeOf();
34 };
35 
41 class Comparable: public Object
42 {
43 public:
58  virtual int compareTo(Comparable *other) = 0;
59 };
60 
66 class Comparator: public Object
67 {
68 public:
82  virtual int compare(Object *o1, Object *o2) = 0;
83 
85  static int compareFun(const void *p1, const void *p2);
86 };
87 
89 {
90 public:
91  int compare(Object *o1, Object *o2);
92 };
93 
95 
99 class Pointer: public Object
100 {
101 private:
102  void *value;
103 
104 public:
105  Pointer(void *value) { this->value = value; }
106  bool equals(Object *other);
107  int hashValue();
109  inline void *getValue() { return value; }
110 };
111 
115 template <class T> class TypedPointer: public Pointer
116 {
117 public:
118  inline TypedPointer(T *value) : Pointer ((void*)value) { }
119  inline T *getTypedValue() { return (T*)getValue(); }
120 };
121 
122 
126 class Integer: public Comparable
127 {
128  int value;
129 
130 public:
131  Integer(int value) { this->value = value; }
132  bool equals(Object *other);
133  int hashValue();
135  int compareTo(Comparable *other);
136  inline int getValue() { return value; }
137 };
138 
139 
143 class Boolean: public Comparable
144 {
145  bool value;
146 
147 public:
148  Boolean(bool value) { this->value = value; }
149  bool equals(Object *other);
150  int hashValue();
152  int compareTo(Comparable *other);
153  inline bool getValue() { return value; }
154 };
155 
156 
162 class ConstString: public Comparable
163 {
164 protected:
165  const char *str;
166 
167 public:
168  ConstString(const char *str) { this->str = str; }
169  bool equals(Object *other);
170  int hashValue();
171  int compareTo(Comparable *other);
173 
174  inline const char *chars() { return str; }
175 
176  static int hashValue(const char *str);
177 };
178 
179 
185 class String: public ConstString
186 {
187 public:
188  String(const char *str);
189  ~String();
190 };
191 
195 class PairBase: public Object
196 {
197 protected:
199 
200 public:
201  PairBase(Object *first, Object *second);
202  ~PairBase();
203 
204  bool equals(Object *other);
205  int hashValue();
207  size_t sizeOf();
208 };
209 
213 class Pair: public PairBase
214 {
215 public:
216  Pair(Object *first, Object *second): PairBase (first, second) { }
217 
218  inline Object *getFirst () { return first; }
219  inline Object *getSecond () { return second; }
220 };
221 
225 template <class F, class S> class TypedPair: public PairBase
226 {
227 public:
228  TypedPair(F *first, S *second): PairBase (first, second) { }
229 
230  inline F *getFirst () { return first; }
231  inline S *getSecond () { return second; }
232 };
233 
234 } // namespace object
235 
236 } // namespace lout
237 
238 #endif // __LOUT_OBJECT_HH__
virtual int compareTo(Comparable *other)=0
Compare two objects, this and other.
const char * toString()
Use object::Object::intoStringBuffer to return a textual representation of the object.
Definition: object.cc:81
virtual bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:50
An object::Object wrapper for strings (char*).
Definition: object.hh:185
A class for fast concatenation of a large number of strings.
Definition: misc.hh:565
~PairBase()
Definition: object.cc:314
static Comparator * compareFunComparator
Definition: object.hh:84
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition: object.cc:260
PairBase(Object *first, Object *second)
Definition: object.cc:308
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:181
Pair(Object *first, Object *second)
Definition: object.hh:216
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition: object.cc:235
size_t sizeOf()
Return the number of bytes, this object totally uses.
Definition: object.cc:371
Definition: object.hh:213
S * getSecond()
Definition: object.hh:231
~String()
Definition: object.cc:298
This is the base class for many other classes, which defines very common virtual methods.
Definition: object.hh:24
int hashValue()
Return a hash value for the object.
Definition: object.cc:159
int hashValue()
Return a hash value for the object.
Definition: object.cc:254
Used for other orders as the one defined by Comparable.
Definition: object.hh:66
F * getFirst()
Definition: object.hh:230
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:192
Instances of a sub class of may be compared (less, greater).
Definition: object.hh:41
T * getTypedValue()
Definition: object.hh:119
Integer(int value)
Definition: object.hh:131
TypedPointer(T *value)
Definition: object.hh:118
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:230
static int compareFun(const void *p1, const void *p2)
This static method may be used as compare function for qsort(3) and bsearch(3), for an array of Objec...
Definition: object.cc:127
ConstString(const char *str)
Definition: object.hh:168
Object * second
Definition: object.hh:198
virtual size_t sizeOf()
Return the number of bytes, this object totally uses.
Definition: object.cc:105
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:202
Definition: object.hh:88
const char * str
Definition: object.hh:165
Definition: container.cc:27
Definition: object.hh:225
An object::Object wrapper for void pointers.
Definition: object.hh:99
virtual int hashValue()
Return a hash value for the object.
Definition: object.cc:59
StandardComparator standardComparator
Definition: object.cc:148
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:218
TypedPair(F *first, S *second)
Definition: object.hh:228
virtual int compare(Object *o1, Object *o2)=0
Compare two objects o1 and o2.
int hashValue()
Return a hash value for the object.
Definition: object.cc:197
virtual ~Object()
The destructor is defined as virtual (but not abstract), so that destruction of Object's works proper...
Definition: object.cc:39
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:285
bool value
Definition: object.hh:145
An object::Object wrapper for constant strings (char*).
Definition: object.hh:162
Object * first
Definition: object.hh:198
void * value
Definition: object.hh:102
void * getValue()
Definition: object.hh:109
An object::Object wrapper for int's.
Definition: object.hh:126
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:322
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:154
int hashValue()
Return a hash value for the object.
Definition: object.cc:225
const char * chars()
Definition: object.hh:174
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:244
Object * getFirst()
Definition: object.hh:218
int value
Definition: object.hh:128
int compare(Object *o1, Object *o2)
Compare two objects o1 and o2.
Definition: object.cc:136
Definition: object.hh:195
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:352
Pointer(void *value)
Definition: object.hh:105
virtual void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: object.cc:95
bool getValue()
Definition: object.hh:153
virtual Object * clone()
Return an exact copy of the object.
Definition: object.cc:68
int getValue()
Definition: object.hh:136
A typed version of object::Pointer.
Definition: object.hh:115
int compareTo(Comparable *other)
Compare two objects, this and other.
Definition: object.cc:209
Object * getSecond()
Definition: object.hh:219
int hashValue()
Return a hash value for the object.
Definition: object.cc:340
Boolean(bool value)
Definition: object.hh:148
An object::Object wrapper for bool's.
Definition: object.hh:143
String(const char *str)
Definition: object.cc:294