This is a simple meta class emulation in C++. A meta class is a class of a class
objects of this class can themselves act as classes. So a user can add or remove
attributes at run time.
One of the projects that I am working on requires a functionality similar to this class.
At first, we design a class (with say x, y and z attributes), to be used by the user. But
the user who is quite enterprising would like to add some more data to this class to be used by
him and/or others. So he adds the attributes a and b to this class,
which now has x, y, z, a and b as its attributes. The new additions made by this user
will be accessible to him only, unless he or the
administrator grants the rights to other demanding users.
The administrator of the class has the right to add attributes to it,
so the static function is used.
So even though the class is the same, its visibility is different
for different users and applications. So it can be put to different uses.
In the class that I have written, just a
TCHAR is used. A list or perhaps
anything can be used instead by a user, as per his or her need.
So the class is applicable at places where users should be able
to add new attributes at runtime; -- a user defined class but at runtime.
This mechanism is not supported directly in C++ as is in Smalltalk and CLOS.
But it can be implemented in C++ using its static data and function members.
This program implements a meta class named
MetaVec. The static data and functions
MetaVec exist even before the instantiation of its object. So an attribute, in this case
a string array, can be added to it from "outside".
The attributes added to this class are accessible to all its members. The idea is to
give limited access to the objects of this class, to these attributes. This permission is
assigned by assigning specific indexes to each one of the objects. These indexes correspond
to the values (attributes) in the link list
PtrVec. So for example, if the list has 7 names
and an object
A has been assigned indexes 0, 1 and 3 only, then it has access to only those
names that are at these index locations in the link list.
Thus a number of objects of this class can all have different attributes; a kind of user
defined class, at run time.
This demonstration version of the meta class makes use of string values as attributes.
A user can use a link-list instead as the static container object.
PtrVec is a STL template
and can contain any type of objects.
typedef PtrVec::iterator itPtrVec;
typedef ListIndex::iterator itListIndex;
typedef long INDEX;
typedef long ATTRIBUTE_NO;
typedef long NO_OF_ATTRIBUTES;
typedef ListIndex LIST_INDEX;
static int ClsCount;
static PtrVec VecObject;
MetaVec(bool LockStatus = false);
static long AddAttribute(TCHAR* Name);
bool AssignIndex(ListIndex LstIndex);
bool AssignIndex(int iIndex);
INDEX RevokeIndex(int iIndex);
LIST_INDEX RevokeIndex(ListIndex IndexList);
static NO_OF_ATTRIBUTES ListAllAttributes();
bool LockObject(bool LockState = true);