Dillo v3.1.1-98-g318d1f14
Loading...
Searching...
No Matches
identity.cc
Go to the documentation of this file.
1/*
2 * Dillo Widget
3 *
4 * Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "identity.hh"
21
22#include <stdio.h>
23
24namespace lout {
25namespace identity {
26
27// ------------------------
28// IdentifiableObject
29// ------------------------
30
31using namespace object;
32using namespace container::typed;
33
35 const char *className)
36{
37 this->parent = parent;
38 this->id = id;
39 this->className = className;
40}
41
43{
44 sb->append ("<class ");
45 sb->append (className);
46 sb->append (" (");
47 sb->appendInt (id);
48 sb->append (")");
49
50 if (parent) {
51 sb->append (", parent: ");
52 parent->intoStringBuffer (sb);
53 }
54
55 sb->append (">");
56}
57
58HashTable <ConstString, IdentifiableObject::Class>
61Vector <IdentifiableObject::Class> *IdentifiableObject::classesById =
62 new Vector <IdentifiableObject::Class> (16, false);
64
69
71{
72 sb->append("<instance ");
73 sb->appendPointer(this);
74 sb->append(" of ");
75 sb->append(getClassName());
76 sb->append(">");
77}
78
83void IdentifiableObject::registerName (const char *className, int *classId)
84{
85 ConstString str (className);
86 Class *klass = classesByName->get (&str);
87 if (klass == NULL) {
88 klass = new Class (currentlyConstructedClass, classesById->size (),
89 className);
90 ConstString *key = new ConstString (className);
91 classesByName->put (key, klass);
92 classesById->put (klass);
93 *classId = klass->id;
94 }
95
96 this->classId = klass->id;
97 *classId = klass->id;
99}
100
105bool IdentifiableObject::instanceOf (int otherClassId)
106{
107 if (otherClassId == -1)
108 // Other class has not been registered yet, while it should have been,
109 // if this class is an instance of it or of a sub-class.
110 return false;
111
112 Class *otherClass = classesById->get (otherClassId);
113
114 if (otherClass == NULL) {
115 fprintf (stderr,
116 "WARNING: Something got wrong here, it seems that a "
117 "CLASS_ID was not initialized properly.\n");
118 return false;
119 }
120
121 for (Class *klass = classesById->get (classId); klass != NULL;
122 klass = klass->parent) {
123 if (klass == otherClass)
124 return true;
125 }
126
127 return false;
128}
129
130} // namespace identity
131} // namespace lout
Typed version of container::untyped::HashTable.
Definition container.hh:536
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition identity.cc:42
Class(Class *parent, int id, const char *className)
Definition identity.cc:34
static container::typed::HashTable< object::ConstString, Class > * classesByName
Definition identity.hh:114
static Class * currentlyConstructedClass
Definition identity.hh:116
bool instanceOf(int otherClassId)
Returns, whether this class is an instance of the class, given by otherClassId, or of a sub class of ...
Definition identity.cc:105
static container::typed::Vector< Class > * classesById
Definition identity.hh:115
const char * getClassName()
Return the name, under which the class of this object was registered.
Definition identity.hh:140
void registerName(const char *className, int *classId)
This method must be called in the constructor for the sub class.
Definition identity.cc:83
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition identity.cc:70
A class for fast concatenation of a large number of strings.
Definition misc.hh:570
void appendPointer(void *p)
Definition misc.hh:596
void appendInt(int n)
Definition misc.hh:594
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition misc.hh:593
An object::Object wrapper for constant strings (char*).
Definition object.hh:163