My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/********************************************\
*
* Sire - Molecular Simulation Framework
*
* Copyright (C) 2009 Christopher Woods
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For full details of the license please see the COPYING file
* that should have come with this distribution.
*
* You can contact the authors via the developer's mailing list
* at http://siremol.org
*
\*********************************************/

#ifndef SIREN_PRIMITIVE_H
#define SIREN_PRIMITIVE_H

#include "objref.h"
#include "stream.h"
#include "datastream.h"
#include "class.h"

SIREN_BEGIN_HEADER

namespace Siren
{
class String;
class Number;

template<class T>
class Primitive;
}

namespace Siren
{

class Logger;

namespace detail
{
void testNotImplemented(QString type_name);
void testNotImplemented(Logger &logger, QString type_name);
}

/** This is the base class of Primitive types. This just
provides the functionality to allow automatic
conversion of Object types. Primitives are simple
classes that are too small or performance/memory
intensive to be made virtual or have the costs
associated with being part of the Object hierarchy.

This class provides the functionality to allow
Primitive objects to be automatically converted
to PrimitiveObject<T> types so that they can
be used as Object types when necessary.

@author Christopher Woods
*/
template<class Derived>
class SIREN_EXPORT Primitive
{
public:
Primitive();
~Primitive();

operator ObjRef() const;

static QString typeName();

bool operator!=(const Derived &other) const;

QString what() const;

Class getClass() const;

uint hashCode() const;

void load(Stream &s);
void save(Stream &s) const;

bool test() const;
bool test(Logger &logger) const;

void stream(Stream &s);
};

/** This is a simple wrapper that provides a Siren::Object
interface to Primitive objects
*/
template<class T>
class SIREN_EXPORT PrimitiveObject : public Implements<PrimitiveObject<T>,Object>
{
public:
PrimitiveObject();
PrimitiveObject(const T &primitive);
PrimitiveObject(const PrimitiveObject<T> &other);

~PrimitiveObject();

PrimitiveObject& operator=(const PrimitiveObject<T> &other);
PrimitiveObject& operator=(const T &primitive);

bool operator==(const PrimitiveObject<T> &other) const;
bool operator!=(const PrimitiveObject<T> &other) const;

bool operator==(const T &primitive) const;
bool operator!=(const T &primitive) const;

QString toString() const;

uint hashCode() const;

void stream(Stream &s);

bool test(Logger &logger) const;

operator const T&() const;

private:
/** The actual primitive object */
T primitive_object;
};

/** This is a simple wrapper class that signifies that
the passed primitive is a String. This is used with
functions that expect a generic Object reference, and
allows the user to write;

object.func( obj0, String("Hello") );

void object::func( const Object &obj0, const Object &obj1 )
{
QString s = primitive_cast<String>(obj1);
}
*/
class SIREN_EXPORT String : public Primitive<String>
{
public:
String();
String(const QString &text);

String(const String &other);

~String();

String& operator=(const String &other);

bool operator==(const String &other) const;
bool operator!=(const String &other) const;

operator const QString&() const;

QString toString() const;

uint hashCode() const;

void stream(Stream &s);

bool test() const;
bool test(Logger &logger) const;

private:
/** The actual text in the string */
QString text;
};

/** This is a simple wrapper class that signifies that
the passed primitive is a Number. This is used with
functions that expect a generic Object reference, and
allows the user to write;

object.func( obj0, Number(42) );

void object::func( const Object &obj0, const Object &obj1 )
{
Number n = primitive_cast<Number>(obj1);

int val = primitive_cast<Number>(obj1).toInteger();
}
*/
class SIREN_EXPORT Number : public Primitive<Number>
{
public:
Number();
Number(qint64 value);
Number(double value);

Number(const Number &other);

~Number();

Number& operator=(const Number &other);

bool operator==(const Number &other) const;
bool operator!=(const Number &other) const;

QString toString() const;

uint hashCode() const;

bool test() const;
bool test(Logger &logger) const;

static Number fromInteger(qint64 value);
static Number fromFloat(double value);

qint64 toInteger() const;
double toFloat() const;

void stream(Stream &s);

operator qint64() const;
operator double() const;

private:
union
{
qint64 integer_value;
double float_value;

} number_data;

/** Whether or not this is an integer value */
bool is_integer;
};

#ifndef SIREN_SKIP_INLINE_FUNCTIONS

/////////
///////// Implementation of Primitive
/////////

/** Constructor */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
Primitive<T>::Primitive()
{}

/** Destructor */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
Primitive<T>::~Primitive()
{}

/** Automatically provide that obvious overload that
T != T is not (T == T) */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
bool Primitive<T>::operator!=(const T &other) const
{
return not static_cast<const T*>(this)->operator==(other);
}

/** Return the type name of the primitive type */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
QString Primitive<T>::typeName()
{
return QMetaType::typeName( qMetaTypeId<T>() );
}

/** Return the type name of the primitive type */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
QString Primitive<T>::what() const
{
return Primitive<T>::typeName();
}

/** Allow automatic casting to an Object reference */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
Primitive<T>::operator ObjRef() const
{
return ObjRef( PrimitiveObject<T>(static_cast<const T&>(*this)) );
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
Class Primitive<T>::getClass() const
{
const RegisterMetaType *metatype = RegisterPrimitive<T>::getRegistration();

if (metatype)
return Class( metatype->typeName() );
else
return Class( QMetaType::typeName( qMetaTypeId<T>() ) );
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
void Primitive<T>::load(Stream &s)
{
s.assertIsLoading();
static_cast<T*>(this)->stream(s);
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
void Primitive<T>::save(Stream &s) const
{
s.assertIsSaving();
const_cast<T*>( static_cast<const T*>(this) )->stream(s);
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
void Primitive<T>::stream(Stream &s)
{
static_cast<T*>(this)->stream(s);
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
uint Primitive<T>::hashCode() const
{
return static_cast<const T*>(this)->hashCode();
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
bool Primitive<T>::test(Logger &logger) const
{
#ifndef SIREN_DISABLE_TESTS
detail::testNotImplemented(logger, T::typeName());
return false;
#else
return true;
#endif
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
bool Primitive<T>::test() const
{
#ifndef SIREN_DISABLE_TESTS
detail::testNotImplemented( T::typeName() );
return false;
#else
return true;
#endif
}

/////////
///////// Implementation of PrimitiveObject
/////////

/** Default constructor */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
PrimitiveObject<T>::PrimitiveObject() : Implements<PrimitiveObject<T>,Object>()
{}

/** Construct as a copy of 'primitive' */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
PrimitiveObject<T>::PrimitiveObject(const T &primitive)
: Implements<PrimitiveObject<T>,Object>(), primitive_object(primitive)
{}

/** Copy constructor */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
PrimitiveObject<T>::PrimitiveObject(const PrimitiveObject<T> &other)
: Implements<PrimitiveObject<T>,Object>(other),
primitive_object(other.primitive_object)
{}

/** Destructor */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
PrimitiveObject<T>::~PrimitiveObject()
{}

/** Copy assignment operator */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
PrimitiveObject<T>& PrimitiveObject<T>::operator=(const PrimitiveObject<T> &other)
{
if (this != &other)
{
primitive_object = other.primitive_object;
Object::operator=(other);
}

return *this;
}

/** Copy assignment operator */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
PrimitiveObject<T>& PrimitiveObject<T>::operator=(const T &primitive)
{
primitive_object = primitive;
return *this;
}

/** Comparison operator */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
bool PrimitiveObject<T>::operator==(const PrimitiveObject<T> &other) const
{
return primitive_object == other.primitive_object;
}

/** Comparison operator */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
bool PrimitiveObject<T>::operator!=(const PrimitiveObject<T> &other) const
{
return primitive_object != other.primitive_object;
}

/** Comparison operator */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
bool PrimitiveObject<T>::operator==(const T &primitive) const
{
return primitive_object == primitive;
}

/** Comparison operator */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
bool PrimitiveObject<T>::operator!=(const T &primitive) const
{
return primitive_object != primitive;
}

/** Return a string representation of this object */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
QString PrimitiveObject<T>::toString() const
{
return primitive_object.toString();
}

/** Return a hash code for this object */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
uint PrimitiveObject<T>::hashCode() const
{
return primitive_object.hashCode();
}

/** Test this object */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
bool PrimitiveObject<T>::test(Logger &logger) const
{
return primitive_object.test(logger);
}

/** Stream this object */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
void PrimitiveObject<T>::stream(Stream &s)
{
s.assertVersion("PrimitiveObject", 1);

Schema schema = s.item<T>();

schema.data("primitive") & primitive_object;

Object::stream( schema.base() );
}

/** Allow automatic casting back to the primitive */
template<class T>
SIREN_OUTOFLINE_TEMPLATE
PrimitiveObject<T>::operator const T&() const
{
return primitive_object;
}

/** Use a 'primitive_cast' to convert an Object to the Primitive
of type 'T'

\throw Siren::invalid_cast
*/
template<class T>
const T& primitive_cast( const Object &object )
{
return object.asA< PrimitiveObject<T> >();
}

#endif // SIREN_SKIP_INLINE_FUNCTIONS

}

#ifndef SIREN_SKIP_INLINE_FUNCTIONS

template<class T>
SIREN_OUTOFLINE_TEMPLATE
uint qHash(const Siren::Primitive<T> &object)
{
return object.hashCode();
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
Siren::Stream& Siren::Stream::operator&(Siren::Primitive<T> &object)
{
object.stream(*this);
return *this;
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
QDataStream& operator<<(QDataStream &ds, const Siren::Primitive<T> &object)
{
Siren::DataStream sds(ds);
object.save(sds);
return ds;
}

template<class T>
SIREN_OUTOFLINE_TEMPLATE
QDataStream& operator>>(QDataStream &ds, Siren::Primitive<T> &object)
{
Siren::DataStream sds(ds);
object.load(sds);
return ds;
}

#endif // SIREN_SKIP_INLINE_FUNCTIONS

Q_DECLARE_METATYPE( Siren::String );
Q_DECLARE_METATYPE( Siren::Number );

SIREN_EXPOSE_PRIMITIVE( Siren::String )
SIREN_EXPOSE_PRIMITIVE( Siren::Number )

SIREN_END_HEADER

#endif

Change log

r1155 by chryswoods on Jan 9, 2010   Diff
I've fixed the memory bug in CoordGroup
and have also added guards so that the
unit test code
can be disabled (to reduce executable size
if desired). I've also fixed some problems
with
the registration of class type in Class so
that now they are all held in a central
hash rather than left within the memory of
each library.

Go to: 
Sign in to write a code review

Older revisions

r1123 by chryswoods on Dec 5, 2009   Diff
I've finished porting SireMaths to
Siren and have made a start porting
SireBase...

Code is all broken
...
r1116 by chryswoods on Nov 29, 2009   Diff
I'm making good progress porting
SireMaths to Siren...

Code is very broken

r1112 by chryswoods on Nov 27, 2009   Diff
I've ported over SireUnits - one down,
lots to go...

Code is very broken

All revisions of this file

File info

Size: 12593 bytes, 550 lines
Powered by Google Project Hosting