Click here to Skip to main content
Click here to Skip to main content

C++ MetaClass

, 30 Nov 1999
Rate this:
Please Sign up or sign in to vote.
A class that can be modified at run-time

Introduction

This is a simple meta class emulation in C++. A meta class is a class of a class i.e. the 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 of 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.

// MetaVec class definition

typedef vectorPtrVec; // container object
typedef PtrVec::iterator itPtrVec;

typedef vectorListIndex;
typedef ListIndex::iterator itListIndex;


typedef long INDEX;
typedef long ATTRIBUTE_NO;
typedef long NO_OF_ATTRIBUTES;
typedef ListIndex LIST_INDEX;


class MetaVec
{
private:
    // identifer of this object
    int iClsID;
    // total count of classes for assigining ClsIDs automatically
    static int ClsCount;
    // list of indices for attributes
    // that are permitted fot this object
    ListIndex Index;
    // for locking the object. New attributes
    // can be added but cannot be used
    bool ObjectLock;

    // list of attributes for the metaclass ie all objects
    static PtrVec VecObject;

public:
    MetaVec(bool LockStatus = false);
    ~MetaVec();

    // Adds a new attributes to the metaclass - in the list of names
    static long AddAttribute(TCHAR* Name);

    // This assignes an index to an object
    // to decide the permissions that object has on
    // the attributes of the metaclass. Any object can
    // access only those attributes that 
    // are assigned to it
    bool AssignIndex(ListIndex LstIndex);

    // overloaded to receive a single value
    bool AssignIndex(int iIndex);

    // removes the assigned index and
    // returns the removed index -- overloaded
    INDEX RevokeIndex(int iIndex);

    // removes a list of assigned indexes and returns the removed list
    LIST_INDEX RevokeIndex(ListIndex IndexList);

    // this lists all the available attributes and their IDs
    static NO_OF_ATTRIBUTES ListAllAttributes();

    // Queries an object for its properties - Class ID, Index and Name
    void QueryObject();

    // locks the object so that no new indices can be assigned to it
    // toggles between locked and un-locked
    bool LockObject(bool LockState = true);

    // checking the lock status of an object
    bool CheckLockStatus();
};

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Niraj Kedar

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberSharjith11-Nov-10 9:31 
GeneralNo semantic info PinsussPhartPhace17-Sep-00 14:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 1 Dec 1999
Article Copyright 1999 by Niraj Kedar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid