Donovan Rebbechi said:
Much easier to do this with interpreted languages -- you can literally
create and execute classes on the fly. I have actually done this in
python (for example). In an interpreted language, you just need to
generate the strings, and eval them.
Any object system where classes were objects (so you could instantiate
them and dynamically add state to the *classes*) would also make this
easier.
Basically, what you're trying to do is at odds with the way the C++
object model works, which is why C++ doesn't make it easy.
Depending on what you're trying to do, either dynamic loading, or
developing some sort of prototype system (classes-as-objects) might
be appropriate. The more complicated the prototype model gets, the
closer you are to building a different object model on top of C++.
Cheers,
well, I don't understand why it would be at "odds" with c++ since I am just
trying to create C++ code. Maybe I was unclear in what I wanted to do... I
just want a way to create C++ at compile time so I can make it easier on
myself... sorta like macro's and templates. The problem is that these are
somewhat limited in what they can do and for my application I need a little
bit more control. basicaly I have a class that has properties in it. A
property is simply a data type that as a range and acceptable values.
Though, the "range" and "acceptable" values are only used for a user
interface to change those variables.
lets say I have this code
class test
{
properties:
int Note = {'A'=0, 'B'=1, ..., 'G' = 6, default=0};
char Note_Name="Note";
public:
....
}
now, C++ doesn't understand the properties clause and declaration of Note..
if I couldn only make it understand then it would same me a lot of trouble.
Now, the problem is that I might have 10k instanstiations of test... there
is no reason to store the "range" and Name for each object because they will
all be the same. (so if I just made a class called note that kept all that
info then I would be wasting a lot of space.
I could declare the "range" as static(probably keep it in a list or
something) but then I would still have to make a new class for every type of
new property I had.
now if I could "translate" the properties clause into "valid" C++ it might
go something like this:
extern const char *Note_ID = "{'A'=0, 'B'=1, ..., 'G' = 6, default=0}; "
class test
{
private:
int Note;
public:
test()
{
Properties.Add_Property(&Note);
}
~test()
{
Properties.Remove_Property(&Note, Note_ID);
}
....
};
static class properties
{
private:
list<list<void *> > props;
public:
Add_Property(void *prop, char *str)
{
... // figures out where to stick the prop with the property str in the
list of lists
};
...
};
-----------
basicaly the properties class is a static class that keeps a list of the
properties as the first element in each of the sublists and the rest of the
elements are the addresses of the elements that have that property. This way
I can write a usr interface where one can change the value of a property and
see its name and its range(so I might have a list box above that contains
'A'-'G' with the name "Note" above them and they can select which ever one
they want and it will update that objects properties value.
Now the above code isn't at all valid or good but just to give an idea about
what I am talking about. (Maybe there are much better ways but this is just
off the top of my head)
The whole point is this: In my application I will have n objects(not all of
the same class) each of which have several different "properties". The range
and type of many of these properties for many different objects will be the
same and many will be different. I need the ability to allow the user to
modify the values of those properties in an "intelligent" way(i.e. by only
being allowed to select the "real" range of the object and not the
"internal" representation of the object. (i.e. in above note is an int.. but
the user should only see 'A' though 'G' (so I have to keep track of that but
it would be a terrible waste of memory to keep track by keeping that
tracking information inside of each object)).
I doubt that as made anything clearer ;/ I can't seem to express well what I
am trying to do(probably because I haven't programmed much in a long time).
(Maybe what I am trying to do is possible and easy in C++ but I can't see
how to do it). I have looked over templates and "properties" in C++ by
using managed extensions and things like libpropc++ but nothing really seems
to have what I need exactly.
I am still looking at how I can do this efficiently with templates though,
as I think that is my only choice. I feel that I will end up having to
modify my code in several places for every new property I add and every time
I add one to a class.. which will end up being a lot of excess work.
Thats the reason I brought this up.. because I Cannot do something like
#define add_prop(type, name, range)
@global
if (name_ID DNE)
extern const char *name_ID = range;
@current
if (type_name DNE)
private: type name;
@constructor
properties.add_prop(&name, name_ID)
@global
if (properties DNE)
class properties
{
blah...... (// code for the properties class)
};
------------
so I might use it like this
class test
{
properties: // a does nothing, just for looks... by define it as "#define
properties "
add_prop(type, note, "{'A'=0, 'B'=1, ..., 'G' = 6, default=0}; ")
}
---------
and it would produce
extern const char *note_ID = "{'A'=0, 'B'=1, ..., 'G' = 6, default=0}; ";
class test
{
private:
int note;
public:
test()
{
properties.add_prop(¬e, note_ID);
};
}
class properties
{
blah...... (// code for the properties class)
};
-------------------
heh, I hope that makes some sense. Its not suppose to be valid code, just to
give an idea about what I would need to make my life simple. i.e. I have one
macro that does all the work for me and I can add as many "properties" as I
want with very little work.
Thanks
Jon
(p.s. The whole reason I kinda used "objects" in the first post was because
one needed a way to insert code at specific points... so I was thinking for
each "object" in the C++ code there would be a "corresponding" object in the
"meta code". (so a class declaration/prototype would be considered as one
object... a function declaration/prototype would be considered another)..
though I could understand how that could lead to basicaly "redefining" C++
and could lead to many problems if abused... I'm really just trying to
"expand" the macro language of C++ and not create something on top. )