-
Notifications
You must be signed in to change notification settings - Fork 1
/
metaapi.cpp
1435 lines (1216 loc) · 32.9 KB
/
metaapi.cpp
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2009 James Haley
//
// 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
//
//--------------------------------------------------------------------------
//
// DESCRIPTION:
//
// Metatables for storage of multiple types of objects in an associative
// array.
//
//-----------------------------------------------------------------------------
#include "z_zone.h"
#include "i_system.h"
#include "doomtype.h"
#include "m_collection.h"
#include "m_dllist.h"
#include "e_hash.h"
#include "m_qstr.h"
#include "m_misc.h"
#include "metaapi.h"
// Macros
// Tunables
#define METANUMCHAINS 53
#define METANUMTYPECHAINS 31
#define METALOADFACTOR 0.667f
// These primes roughly double in size.
static const unsigned int metaPrimes[] =
{
53, 97, 193, 389, 769, 1543,
3079, 6151, 12289, 24593, 49157, 98317
};
#define METANUMPRIMES earrlen(metaPrimes)
// Globals
// metaerrno represents the last error to occur. All routines that can cause
// an error will reset this to 0 if no error occurs.
int metaerrno = 0;
//=============================================================================
//
// Hash Table Tuning
//
//
// MetaHashRebuild
//
// Static method for rebuilding an EHashTable when its load factor has exceeded
// the metatable load factor. Expansion is limited; once the table is too large,
// load factor will increase indefinitely. Hash chain table size is currently
// limited to approximately 384 KB, which would require almost 400K objects to
// be in the table to hit 100% load factor... I'm not really concerned :P
//
template<typename HashType>
static void MetaHashRebuild(HashType &hash)
{
auto curNumChains = hash.getNumChains();
// check for key table overload
if(hash.getLoadFactor() > METALOADFACTOR &&
curNumChains < metaPrimes[METANUMPRIMES - 1])
{
int i;
// find the next larger prime
for(i = 0; curNumChains < metaPrimes[i]; i++);
hash.rebuild(metaPrimes[i]);
}
}
//=============================================================================
//
// Key Interning
//
// MetaObject key strings are interned here, for space efficiency.
//
struct metakey_t
{
DLListItem<metakey_t> links; // hash links
char *key; // key name
size_t index; // numeric index
unsigned int unmodHC; // unmodulated hash key
};
// Hash table of keys by their name
static EHashTable<metakey_t, ENCStringHashKey, &metakey_t::key, &metakey_t::links>
metaKeyHash;
// Collection of all key objects
static PODCollection<metakey_t *> metaKeys;
//
// MetaKey
//
// If the given key string is already interned, the metakey_t structure that
// stores it will be returned. If not, it will be added to the collection of
// keys and hashed by name.
//
static metakey_t &MetaKey(const char *key)
{
metakey_t *keyObj;
unsigned int unmodHC = ENCStringHashKey::HashCode(key);
// Do we already have this key?
if(!(keyObj = metaKeyHash.objectForKey(key, unmodHC)))
{
keyObj = estructalloc(metakey_t, 1);
// add it to the list
metaKeys.add(keyObj);
keyObj->key = estrdup(key);
keyObj->index = metaKeys.getLength() - 1;
keyObj->unmodHC = unmodHC;
// check for table overload, and hash it
MetaHashRebuild<>(metaKeyHash);
metaKeyHash.addObject(keyObj, keyObj->unmodHC);
}
return *keyObj;
}
//
// MetaKeyForIndex
//
// Given a key index, get the key.
//
static metakey_t &MetaKeyForIndex(size_t index)
{
if(index >= metaKeys.getLength())
I_Error("MetaKeyForIndex: illegal key index requested\n");
return *metaKeys[index];
}
//=============================================================================
//
// MetaObject Methods
//
IMPLEMENT_RTTI_TYPE(MetaObject)
//
// MetaObject Default Constructor
//
// Not recommended for use. This exists because use of DECLARE_RTTI_OBJECT
// requires it.
//
MetaObject::MetaObject()
: Super(), links(), typelinks(), type()
{
metakey_t &keyObj = MetaKey("default"); // TODO: GUID?
key = keyObj.key;
keyIdx = keyObj.index;
}
//
// MetaObject(size_t keyIndex)
//
// Constructor for MetaObject using an interned key index
//
MetaObject::MetaObject(size_t keyIndex)
: Super(), links(), typelinks(), type()
{
metakey_t &keyObj = MetaKeyForIndex(keyIndex);
key = keyObj.key;
keyIdx = keyObj.index;
}
//
// MetaObject(const char *pKey)
//
// Constructor for MetaObject when key is known.
//
MetaObject::MetaObject(const char *pKey)
: Super(), links(), typelinks(), type()
{
metakey_t &keyObj = MetaKey(pKey);
key = keyObj.key;
keyIdx = keyObj.index;
}
//
// MetaObject::toString
//
// Virtual method for conversion of metaobjects into strings. As with the prior
// C implementation, the returned string pointer is a static buffer and should
// not be cached. The default toString method creates a hex dump representation
// of the object. This should be pretty interesting in C++...
// 04/03/11: Altered to use new ZoneObject functionality so that the entire
// object, including all subclasses, are dumped properly. Really cool ;)
//
const char *MetaObject::toString() const
{
static qstring qstr;
size_t bytestoprint = getZoneSize();
const byte *data = reinterpret_cast<const byte *>(getBlockPtr());
qstr.clearOrCreate(128);
if(!bytestoprint) // Not a zone object? Can only dump the base class.
{
bytestoprint = sizeof(*this);
data = reinterpret_cast<const byte *>(this); // Not evil, I swear :P
}
while(bytestoprint)
{
int i;
// print up to 12 bytes on each line
for(i = 0; i < 12 && bytestoprint; ++i, --bytestoprint)
{
byte val = *data++;
char bytes[4] = { 0 };
sprintf(bytes, "%02x ", val);
qstr += bytes;
}
qstr += '\n';
}
return qstr.constPtr();
}
//=============================================================================
//
// Metaobject Specializations
//
// We provide specialized metaobjects for basic types here.
// Ownership of metaobjects for basic types *is* assumed by the routines here.
// Adding or removing a metaobject of these types via their specific interfaces
// below will allocate or free the objects holding them.
//
//
// Integer
//
IMPLEMENT_RTTI_TYPE(MetaInteger)
//
// MetaInteger::toString
//
// String conversion method for MetaInteger objects.
//
const char *MetaInteger::toString() const
{
static char str[33];
memset(str, 0, sizeof(str));
M_Itoa(value, str, 10);
return str;
}
//
// Double
//
IMPLEMENT_RTTI_TYPE(MetaDouble)
//
// MetaDouble::toString
//
// toString method for metadouble objects.
//
const char *MetaDouble::toString() const
{
static char str[64];
memset(str, 0, sizeof(str));
psnprintf(str, sizeof(str), "%+.5f", this->value);
return str;
}
//
// Strings
//
// metastrings created with these APIs assume ownership of the string.
//
IMPLEMENT_RTTI_TYPE(MetaString)
//
// MetaString::setValue
//
// Non-trivial, unlike the other MetaObjects' setValue methods.
// This one can return the current value of the MetaString in *ret if the
// pointer-to-pointer is non-NULL. If it IS NULL, the current value will
// be freed instead.
//
void MetaString::setValue(const char *s, char **ret)
{
if(value)
{
if(ret)
*ret = value;
else
efree(value);
}
value = estrdup(s);
}
//
// Const Strings
//
// Const strings are not owned by their metaobject; they are simply referenced.
// These are for efficient storage of string constants/literals as meta fields.
//
// All we need here is the RTTIObject proxy type instance
IMPLEMENT_RTTI_TYPE(MetaConstString)
//
// End MetaObject Specializations
//
//=============================================================================
//=============================================================================
//
// MetaTable Methods - General Utilities
//
//
// MetaTablePimpl
//
// Private implementation structure for the MetaTable class. Because I am not
// about to expose the entire engine to the EHashTable template if I can help
// it.
//
// A MetaTable is just a pair of hash tables, one on keys and one on types.
//
class MetaTablePimpl : public ZoneObject
{
public:
// the key hash is growable; keys are case-insensitive.
EHashTable<MetaObject, ENCStringHashKey,
&MetaObject::key, &MetaObject::links> keyhash;
// the type hash is fixed size since there are a limited number of types
// defined in the source code; types are case sensitive, because they are
// based on C++ types.
EHashTable<MetaObject, EStringHashKey,
&MetaObject::type, &MetaObject::typelinks> typehash;
MetaTablePimpl() : ZoneObject(), keyhash(METANUMCHAINS), typehash(METANUMCHAINS) {}
virtual ~MetaTablePimpl()
{
keyhash.destroy();
typehash.destroy();
}
};
IMPLEMENT_RTTI_TYPE(MetaTable)
//
// MetaTable Default Constructor
//
MetaTable::MetaTable() : Super()
{
// Construct the private implementation object that holds our dual hashes
pImpl = new MetaTablePimpl();
}
//
// MetaTable(name)
//
MetaTable::MetaTable(const char *name) : Super(name)
{
// Construct the private implementation object that holds our dual hashes
pImpl = new MetaTablePimpl();
}
//
// MetaTable(const MetaTable &)
//
// Copy constructor
//
MetaTable::MetaTable(const MetaTable &other) : Super(other)
{
pImpl = new MetaTablePimpl();
copyTableFrom(&other);
}
//
// MetaTable Destructor
//
MetaTable::~MetaTable()
{
clearTable();
delete pImpl;
pImpl = NULL;
}
//
// MetaTable::clone
//
// Virtual method inherited from MetaObject. Create a copy of this table.
//
MetaObject *MetaTable::clone() const
{
return new MetaTable(*this);
}
//
// MetaTable::getLoadFactor
//
// Returns load factor of the key hash table.
//
float MetaTable::getLoadFactor() const
{
return pImpl->keyhash.getLoadFactor();
}
//
// MetaTable::getNumItems
//
// Returns the number of items in the table.
//
unsigned int MetaTable::getNumItems() const
{
return pImpl->keyhash.getNumItems();
}
//
// MetaTable::toString
//
// Virtual method inherited from MetaObject.
//
const char *MetaTable::toString() const
{
return key;
}
//
// MetaTable::hasKey
//
// Returns true or false if an object of the same key is in the metatable.
// No type checking is done, so it will match any object with that key.
//
bool MetaTable::hasKey(const char *key)
{
return (pImpl->keyhash.objectForKey(key) != NULL);
}
//
// MetaTable::hasType
//
// Returns true or false if an object of the same type is in the metatable.
//
bool MetaTable::hasType(const char *type)
{
return (pImpl->typehash.objectForKey(type) != NULL);
}
//
// MetaTable::hasKeyAndType
//
// Returns true if an object exists in the table of both the specified key
// and type, and it is the same object. This is naturally slower as it must
// search down the key hash chain for a type match.
//
bool MetaTable::hasKeyAndType(const char *key, const char *type)
{
MetaObject *obj = NULL;
bool found = false;
while((obj = pImpl->keyhash.keyIterator(obj, key)))
{
// for each object that matches the key, test the type
if(obj->isInstanceOf(type))
{
found = true;
break;
}
}
return found;
}
//
// MetaTable::countOfKey
//
// Returns the count of objects in the metatable with the given key.
//
int MetaTable::countOfKey(const char *key)
{
MetaObject *obj = NULL;
int count = 0;
while((obj = pImpl->keyhash.keyIterator(obj, key)))
++count;
return count;
}
//
// MetaTable::countOfType
//
// Returns the count of objects in the metatable with the given type.
//
int MetaTable::countOfType(const char *type)
{
MetaObject *obj = NULL;
int count = 0;
while((obj = pImpl->typehash.keyIterator(obj, type)))
++count;
return count;
}
//
// MetaTable::countOfKeyAndType
//
// As above, but satisfying both conditions at once.
//
int MetaTable::countOfKeyAndType(const char *key, const char *type)
{
MetaObject *obj = NULL;
int count = 0;
while((obj = pImpl->keyhash.keyIterator(obj, key)))
{
if(obj->isInstanceOf(type))
++count;
}
return count;
}
//=============================================================================
//
// MetaTable Methods - General Metaobjects
//
//
// MetaTable::addObject
//
// Adds a generic metaobject to the table. The metatable does not assume
// responsibility for the memory management of metaobjects or type strings.
// Key strings are managed, however, to avoid serious problems with mutual
// references between metaobjects.
//
void MetaTable::addObject(MetaObject *object)
{
// Check for rehash
MetaHashRebuild<>(pImpl->keyhash);
// Initialize type name
object->setType();
// Add the object to the key table.
// haleyjd 09/17/2012: use the precomputed unmodulated hash code for the
// MetaObject's interned key.
pImpl->keyhash.addObject(object, MetaKeyForIndex(object->getKeyIdx()).unmodHC);
// Add the object to the type table, which is static in size
pImpl->typehash.addObject(object);
}
//
// Convenience overload for references
//
void MetaTable::addObject(MetaObject &object)
{
addObject(&object);
}
//
// MetaTable::removeObject
//
// Removes the provided object from the given metatable.
//
void MetaTable::removeObject(MetaObject *object)
{
pImpl->keyhash.removeObject(object);
pImpl->typehash.removeObject(object);
}
//
// Convenience overload for references.
//
void MetaTable::removeObject(MetaObject &object)
{
removeObject(&object);
}
//
// MetaTable::getObject
//
// Returns the first object found in the metatable with the given key,
// regardless of its type. Returns NULL if no such object exists.
//
MetaObject *MetaTable::getObject(const char *key)
{
return pImpl->keyhash.objectForKey(key);
}
//
// MetaTable::getObject
//
// Overload taking a MetaObject interned key index.
//
MetaObject *MetaTable::getObject(size_t keyIndex)
{
metakey_t &keyObj = MetaKeyForIndex(keyIndex);
return pImpl->keyhash.objectForKey(keyObj.key, keyObj.unmodHC);
}
//
// MetaTable::getObjectType
//
// Returns the first object found in the metatable which matches the type.
// Returns NULL if no such object exists.
//
MetaObject *MetaTable::getObjectType(const char *type)
{
return pImpl->typehash.objectForKey(type);
}
//
// MetaTable::getObjectType
//
// Overload taking a MetaObject::Type instance.
//
MetaObject *MetaTable::getObjectType(const MetaObject::Type &type)
{
return pImpl->typehash.objectForKey(type.getName());
}
//
// MetaTable::getObjectKeyAndType
//
// As above, but satisfying both conditions at once.
//
MetaObject *MetaTable::getObjectKeyAndType(const char *key,
const MetaObject::Type *type)
{
MetaObject *obj = NULL;
while((obj = pImpl->keyhash.keyIterator(obj, key)))
{
if(obj->isInstanceOf(type))
break;
}
return obj;
}
//
// MetaTable::getObjectKeyAndType
//
// As above, but satisfying both conditions at once.
// Overload for type names.
//
MetaObject *MetaTable::getObjectKeyAndType(const char *key, const char *type)
{
MetaObject::Type *rttiType = FindTypeCls<MetaObject>(type);
return rttiType ? getObjectKeyAndType(key, rttiType) : NULL;
}
//
// MetaTable::getObjectKeyAndType
//
// Overload taking a MetaObject interned key index and RTTIObject::Type
// instance.
//
MetaObject *MetaTable::getObjectKeyAndType(size_t keyIndex,
const MetaObject::Type *type)
{
metakey_t &keyObj = MetaKeyForIndex(keyIndex);
MetaObject *obj = NULL;
while((obj = pImpl->keyhash.keyIterator(obj, keyObj.key, keyObj.unmodHC)))
{
if(obj->isInstanceOf(type))
break;
}
return obj;
}
//
// MetaTable::getObjectKeyAndType
//
// Overload taking a MetaObject interned key index and type name.
//
MetaObject *MetaTable::getObjectKeyAndType(size_t keyIndex, const char *type)
{
MetaObject::Type *rttiType = FindTypeCls<MetaObject>(type);
return rttiType ? getObjectKeyAndType(keyIndex, rttiType) : NULL;
}
//
// MetaTable::getNextObject
//
// Returns the next object with the same key, or the first such object
// in the table if NULL is passed in the object pointer. Returns NULL
// when no further objects of the same key are available.
//
MetaObject *MetaTable::getNextObject(MetaObject *object, const char *key)
{
// If no key is provided but object is valid, get the next object with the
// same key as the current one.
if(object && !key)
{
unsigned int hc = MetaKeyForIndex(object->getKeyIdx()).unmodHC;
key = object->getKey();
return pImpl->keyhash.keyIterator(object, key, hc);
}
else
return pImpl->keyhash.keyIterator(object, key);
}
//
// MetaTable::getNextObject
//
// Overload taking a MetaObject interned key index.
//
MetaObject *MetaTable::getNextObject(MetaObject *object, size_t keyIndex)
{
metakey_t &keyObj = MetaKeyForIndex(keyIndex);
return pImpl->keyhash.keyIterator(object, keyObj.key, keyObj.unmodHC);
}
//
// MetaTable::getNextType
//
// Similar to above, but this returns the next object which also matches
// the specified type.
//
MetaObject *MetaTable::getNextType(MetaObject *object, const char *type)
{
// As above, allow using the same type as the current object
if(object && !type)
type = object->getClassName();
return pImpl->typehash.keyIterator(object, type);
}
//
// MetaTable::getNextType
//
// Overload accepting a pointer to a MetaObject RTTI proxy. If object is valid,
// type is optional, but otherwise it must be valid.
//
MetaObject *MetaTable::getNextType(MetaObject *object, const MetaObject::Type *type)
{
// Same as above
if(object && !type)
type = object->getDynamicType();
// Must have a type
if(!type)
return NULL;
return pImpl->typehash.keyIterator(object, type->getName());
}
//
// MetaTable::getNextKeyAndType
//
// As above, but satisfying both conditions at once.
//
MetaObject *MetaTable::getNextKeyAndType(MetaObject *object, const char *key, const char *type)
{
MetaObject *obj = object;
if(object)
{
// As above, allow NULL in either key or type to mean "same as current"
if(!key)
key = object->getKey();
if(!type)
type = object->getClassName();
}
while((obj = pImpl->keyhash.keyIterator(obj, key)))
{
if(obj->isInstanceOf(type))
break;
}
return obj;
}
//
// MetaTable::getNextKeyAndType
//
// Overload taking a MetaObject interned key index.
//
MetaObject *MetaTable::getNextKeyAndType(MetaObject *object, size_t keyIdx, const char *type)
{
MetaObject *obj = object;
metakey_t &keyObj = MetaKeyForIndex(keyIdx);
if(object)
{
// As above, allow NULL in type to mean "same as current"
if(!type)
type = object->getClassName();
}
while((obj = pImpl->keyhash.keyIterator(obj, keyObj.key, keyObj.unmodHC)))
{
if(obj->isInstanceOf(type))
break;
}
return obj;
}
//
// MetaTable::getNextKeyAndType
//
// Overload taking a key string and a MetaObject RTTI proxy object.
//
MetaObject *MetaTable::getNextKeyAndType(MetaObject *object, const char *key,
const MetaObject::Type *type)
{
MetaObject *obj = object;
if(object)
{
// As above, allow NULL in either key or type to mean "same as current"
if(!key)
key = object->getKey();
if(!type)
type = object->getDynamicType();
}
while((obj = pImpl->keyhash.keyIterator(obj, key)))
{
if(obj->isInstanceOf(type))
break;
}
return obj;
}
//
// MetaTable::getNextKeyAndType
//
// Overload taking a MetaKey index and a MetaObject RTTI proxy object.
//
MetaObject *MetaTable::getNextKeyAndType(MetaObject *object, size_t keyIdx,
const MetaObject::Type *type)
{
MetaObject *obj = object;
metakey_t &keyObj = MetaKeyForIndex(keyIdx);
if(object)
{
// As above, allow NULL in type to mean "same as current"
if(!type)
type = object->getDynamicType();
}
while((obj = pImpl->keyhash.keyIterator(obj, keyObj.key, keyObj.unmodHC)))
{
if(obj->isInstanceOf(type))
break;
}
return obj;
}
//
// MetaTable::tableIterator
//
// Iterates on all objects in the metatable, regardless of key or type.
// Const version.
//
const MetaObject *MetaTable::tableIterator(const MetaObject *object) const
{
return pImpl->keyhash.tableIterator(object);
}
//
// MetaTable::tableIterator
//
// Iterates on all objects in the metatable, regardless of key or type.
// Mutable version.
//
MetaObject *MetaTable::tableIterator(MetaObject *object) const
{
return pImpl->keyhash.tableIterator(object);
}
//
// MetaTable::addInt
//
// Add an integer to the metatable using an interned key index.
//
void MetaTable::addInt(size_t keyIndex, int value)
{
addObject(new MetaInteger(keyIndex, value));
}
//
// MetaTable::addInt
//
// Add an integer to the metatable using a raw string key.
//
void MetaTable::addInt(const char *key, int value)
{
addObject(new MetaInteger(key, value));
}
//
// MetaTable::getInt
//
// Get an integer from the metatable. This routine returns the value
// rather than a pointer to a metaint_t. If an object of the requested
// name doesn't exist in the table, defvalue is returned and metaerrno
// is set to indicate the problem.
//
// Use of this routine only returns the first such value in the table.
// This routine is meant for singleton fields.
//
int MetaTable::getInt(size_t keyIndex, int defValue)
{
int retval;
MetaObject *obj;
metaerrno = META_ERR_NOERR;
if(!(obj = getObjectKeyAndType(keyIndex, RTTI(MetaInteger))))
{
metaerrno = META_ERR_NOSUCHOBJECT;
retval = defValue;
}
else
retval = static_cast<MetaInteger *>(obj)->value;
return retval;
}
//
// MetaTable::getInt
//
// Overload for raw key strings.
//
int MetaTable::getInt(const char *key, int defValue)
{
return getInt(MetaKey(key).index, defValue);
}
//
// MetaTable::setInt
//
// If the metatable already contains a metaint of the given name, it will
// be edited to have the provided value. Otherwise, a new metaint will be
// added to the table with that value.
//
void MetaTable::setInt(size_t keyIndex, int newValue)
{
MetaObject *obj;