Dynamic class (c++ snippet)

Discussion about everything. New games, 3d math, development tips...
Post Reply
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Dynamic class (c++ snippet)

Post by JonLT »

Hi I wanted a class that I could add members to 'on the fly'. I couldn't find any documentation on it so I gave it a shot my self. It's not pretty but I don't think such a thing would ever be. (please prove me wrong :) )
The class has a few problems: adressing members from member functions, and cleaning up. Anyways.. heres the class, and how I use it:

Code: Select all

#include <map>
#include <iostream>

using namespace std;

typedef void(*fPtr)();

class DynamicClass
{
public:

    //add a new data member
    void addDataMember(char* name, void* data)
    {
        DataMembers[name] = data;
    }

    //add a new member function
    void addMemberFunction(char* name, void(*function)())
    {
        MemberFunctions[name] = function;
    }

    //get a data member from its name
    void* getDataMember(char* name)
    {
        map<char*,void*>::iterator iter = DataMembers.find(name);
        return iter->second;
    }

    //get a member function from its name
    void (*getMemberFunction(char* name))()
    {
        map<char*,fPtr>::iterator iter = MemberFunctions.find(name);
        return iter->second;
    }

    //the maps that holds the pointers
    map<char*, void*> DataMembers;
    map<char*, fPtr> MemberFunctions;

};

void function(int number)
{
    cout<<"Test Function says: "<<number<<endl;
}

int main()
{
    //create the class
    DynamicClass *dc = new DynamicClass;

    //add a data member (a int - value 23)
    dc->addDataMember("Age", new int(23));

    //get the added data member out of the class
    int Age = *(int*)dc->getDataMember("Age");

    //print it out
    cout<<Age<<endl;

    //add a member function
    dc->addMemberFunction("Function", (fPtr)function);

    //get the member function out of the class
    void (*fTest)(int) = (void(*)(int))dc->getMemberFunction("Function");

    //run the member function
    fTest(4);

    //delete a data member
    delete (int*)dc->getDataMember("Age");
    dc->DataMembers.erase("Age");

    return 0;
}
I would very much like to know if there's a better way of doing this, so please post any suggestions of improvements. Thanks!
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

For you data members you could use a template parameter instead of the void*. That way you wouldn't lose type safety. But you can do it even easier and just expose the map for it, as this is basically what a map is doing (mapping a key like a string to any data).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post by JonLT »

thanks! Up till now my opinion has been that tamplates are evil. But i tried to replace the void* with a template parameter, but i can't figure it out. This is what I've done:
the add member function:

Code: Select all

template<class T>
void addDataMember(char* name, T data)
{
    DataMembers[name] = data;
}
but i can't do this:

Code: Select all

template<class T>
map<char*, T> DataMembers;
what do you mean by this
But you can do it even easier and just expose the map for it, as this is basically what a map is doing (mapping a key like a string to any data).
3ddev
Posts: 169
Joined: Tue Sep 19, 2006 6:51 am
Contact:

Post by 3ddev »

This is an interesting piece of code. Runtime "Reflection"(Microsoft term) has always been hard to achieve in C++.

Sorry, but I haven't used templates in a while, and can't offer any solution to the above problem. :?
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Sorry, I didn't look at it hard enough and did tell something stupid. Certainly templates are not useful here, as you want to have different types at the same time as data members.

With the thing about the map I did mean that instead of offering add and get members you could give access to your DataMembers and MemberFunctions, as maps do already offer the add and remove functionality. But maybe it's easier to use your way.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Wow i did not know this thing is possible in C++! :D

Earlier I saw a java thing about "closures" and somehow they passed a function as a variable and I thot "wtf, only in java.." but I guess it can be done in C++ too :P
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post by JonLT »

I've realized that this class isn't really usefull afterall, because you have to have everything ready before you use it (the types and the functions), and if everything is ready, theres no need to use this class...
But I had a little fun writing it and fun is allways usefull. :)
CuteAlien
Admin
Posts: 9930
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

You can get a little more abstraction by using XML. For example tinyXml allows to access xml as c++ objects. At least for data types this can be useful sometimes.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
pinballwizard
Posts: 67
Joined: Wed Aug 02, 2006 1:47 am

Post by pinballwizard »

You might want to read up on "Adaptive Object Models", e.g.

http://www.adaptiveobjectmodel.com/WICS ... WICSA3.htm

Scott Bilas of Gas Powered Games also describes his dynamic object system: http://www.drizzle.com/~scottb/gdc/game ... /frame.htm

You might want to consider, though, if you really need this much flexibility.
agi_shi
Posts: 122
Joined: Mon Feb 26, 2007 12:46 am

Post by agi_shi »

boost::any...
Post Reply