C++ Meta Programming language

J

Jon Slaughter

is there any way to create c++ code using some type of objected oriented
framework that is designed to be used to create C++ applications?

something like maybe

new class1;
class1.add_class("first_class", "base_class");
class1.add_private_data("char *", "name");
class1.add_public_member("void", "Name", "const char *n", "{ name = n; }");

might create the following C++ code


"
class first_class : base_class
{
private:
char *name;
public:
void Name(const char *n)
{
name = n;
}
};

"

or whatever...


or even a modification of C++ that has a keyword that tells the
pre-processor to insert C++ code somewhere

like in a normal c++ program:


#include <iostream>

using namespace std;

#code
new class1;
class1.add_class("first_class", "base_class");
class1.add_private_data("char *", "name");
class1.add_public_member("void", "Name", "const char *n", "{ name = n; }");
#endcode

blah...



void main(void)
{

#code
code("class1 c");
#endcode

c.Name("Hello");

//or if you wanted you could just put that in the #code block as
code("c.Name("Hello");");
}



or whatever... Basicaly I have some specific things I need to do and it
seems that the c++ language does not make them easy to do(and they are not
really worth while to do by "hand" in C++)... but if I could tell "program"
the pre-processor a little to do some simple things(like insert some code
somewhere(though at multiple places at once) then it would be a hell of a
lot easier.


Anything like that out there?

Thanks

Jon
 
R

REH

Jon Slaughter said:
or whatever... Basicaly I have some specific things I need to do and it
seems that the c++ language does not make them easy to do(and they are not
really worth while to do by "hand" in C++)... but if I could tell
"program" the pre-processor a little to do some simple things(like insert
some code somewhere(though at multiple places at once) then it would be a
hell of a lot easier.

Why do you think it would be easier to dynamically define your classes or
tell the propressor to do it, then to just define them yourself?

REH
 
D

Donovan Rebbechi

is there any way to create c++ code using some type of objected
oriented framework that is designed to be used to create C++

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,
 
J

Jon Slaughter

REH said:
Why do you think it would be easier to dynamically define your classes or
tell the propressor to do it, then to just define them yourself?

REH

because there are some things I have to do that is much more work to do by
hand... the classes example is just an example and not necessarily what I
need to do. Ofcourse you should know why its easier... this is one of the
reasons why C++ has templates and many people use them(because it is a
"dynamic" way of defining classes and is much easier to do).
 
R

REH

Jon Slaughter said:
because there are some things I have to do that is much more work to do by
hand... the classes example is just an example and not necessarily what I
need to do. Ofcourse you should know why its easier... this is one of the
reasons why C++ has templates and many people use them(because it is a
"dynamic" way of defining classes and is much easier to do).

What is your definition of "dynamic." Templates are cetainly not dynamic,
by any stretch of meaning. Nor would I consider defining a class template
easier than defining a class.

Maybe you should look into languages like Simula, Phyton, et. al. IF
languages like Inform also do what you want.

REH
 
J

Jon Slaughter

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(&note, 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. )
 
D

Donovan Rebbechi

well, I don't understand why it would be at "odds" with c++ since I
am just trying to create C++ code.

It's because the C++ language C++ has no runtime support for creating
C++ code (the closest thing to compile-time support it has for that
are templates/macros)
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 use static member data.
(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.

Maybe "property" could be a template class, then you'd only need a
new parametrization for every new property.

Getting it to work would be tricky though ...
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);

Not sure why you're trying to do this dynamically -- seems to me this
potentially makes it harder.
static class properties
{
private:
list<list<void *> > props;

This isn't typesafe. Take a look at the boost::Any class for an
example on how you can get flexibility without throwing away type
safety.
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. )

It may be possibly to do this using templates/macros, but may I
suggest another approach -- have you thought of using lex or a
similar parsing tool, as a sort of preprocessor ? If you don't need
to do this at runtime, then a "code generator" or "precompiler" might
be the right approach.

Cheers,
 
D

Donovan Rebbechi

because there are some things I have to do that is much more work to
do by hand... the classes example is just an example and not
necessarily what I need to do. Ofcourse you should know why its
easier... this is one of the reasons why C++ has templates and many
people use them(because it is a "dynamic" way of defining classes
and is much easier to do).

You can't define classes at runtime using templates. In fact you
can't really define classes dynamically at all in C++ (the closest
you can come to "dynamically defining classes" is adding code to a
running program using dynamic loading) Templates are fully expanded at
compile time.

Cheers,
 
J

Jon Slaughter

It may be possibly to do this using templates/macros, but may I
suggest another approach -- have you thought of using lex or a
similar parsing tool, as a sort of preprocessor ? If you don't need
to do this at runtime, then a "code generator" or "precompiler" might
be the right approach.

Cheers,

Right now I am looking over templates and such and seeing how far I can get
and how much work it would be in the long run.

Your idea about using lex is very similar to what I am thinking but I am not
familiar with it much. AFAIK it would probably be easer just to modify GC++
and add some directives to do what I want? (since essentially that is what I
would be doing with lex or some other preprocessing method)

All this stuff I've been talking about is simply to make programming my
application easier. It can be completely done without any "pre-processing"
but would be much more work and error prone and less modular. I think by
setting up a pre-processing system to do exactly what I need will make a
huge difference.

I will look into some of your ideas and see what is the best.

Thanks
Jon
 
S

Steven T. Hatton

Jon Slaughter wrote:

[...]
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.

If you are talking about invoking some kind of code generation during
preprocessing, yes, you can do it, and it is frequently done. AAMOF, in
TC++PL(SE) Stroustrup recommends using code generators for repetitive
things. Take a look at http://doc.trolltech.com/4.0/moc.html#moc for one
example of where this is done.
 
J

Jon Slaughter

Steven T. Hatton said:
Jon Slaughter wrote:

[...]
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.

If you are talking about invoking some kind of code generation during
preprocessing, yes, you can do it, and it is frequently done. AAMOF, in
TC++PL(SE) Stroustrup recommends using code generators for repetitive
things. Take a look at http://doc.trolltech.com/4.0/moc.html#moc for one
example of where this is done.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand
Russell

Ok, I will look into that... its exactly what I need except maybe the
"packaging" is different(i.e. I might end up putting a lot of work in
dealing with the pre-processing side)... though its good to know that its
available.

Thanks
Jon
 
S

Steven T. Hatton

REH said:
What is your definition of "dynamic." Templates are cetainly not dynamic,
by any stretch of meaning. Nor would I consider defining a class template
easier than defining a class.

I would say the term "compile-time polymorphism" is describing a form of
dynamic behavior.
 
J

Jon Slaughter

Steven T. Hatton said:
I would say the term "compile-time polymorphism" is describing a form of
dynamic behavior.



--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand
Russell

yeah, but I assumed that what he ment by dynamic was some type of
pre-runtime dynamic behavior since I thought I was clear on my OP about the
subject(but now that I look but I know I wasn't very clear(a problem with
terminology and other things since I've forgotten a lot of stuff).

But to be clear I mean complete compile time "morphology"... This is
strictly to increase my coding productivity and has nothing to do with any
run-time behaviors.

Thanks
Jon
 
J

Jon Slaughter

Steven T. Hatton said:
http://www.boost.org/libs/spirit/index.html
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand
Russell

lol, I just found that too and I'm looking at how to use it ;) Not sure if
its more trouble to learn that or just write some C++ code myself(ofcourse
my code wouldn't be as general).

Basicaly what I plan on doing now, it seems, is just extend C++ to handle a
"properties" keyword defined in a class similar to what I showed before.

so I can do code like this:

class test
{
properties:
typename My_Prop<range>;
....
}

and then have a preprocessor(probably done using the spirit library) go
ahead and convert that directive into normal C++ code then compile that code
using my favorite compiler. What is nice about it, possibly, is that
basicaly I can extend C++ to whatever I want(like add static classes and
property setters and getters and such). All the time the code will be
converted to normal C++.


Thanks again.

Jon
 
I

Ian

Jon said:
is there any way to create c++ code using some type of objected oriented
framework that is designed to be used to create C++ applications?
You could, as my group did for a while, define many of the class aspects
in XML and then use XSLT to morph the XML into C++ code.

Ian
 
J

Jon Slaughter

Ian said:
You could, as my group did for a while, define many of the class aspects
in XML and then use XSLT to morph the XML into C++ code.

Ian

I thought about that. I could just keep the property info in xml and load it
up at runtime when I need too. While this is one way I feel its not the
optimal(because one still have to modify the xml for every change). (though
I think you are talking bout doing pre-compile time code generation and it
might be a viable alternative too)

I just feel that the compiler/pre-processor can handle the properties much
more efficiently than I can and basicaly it could automate much of the
process that I would end up having to do by hand(in any way that I had to do
it).

Right now it seems that there are several approaches and I'm not sure which
one is the best. The one I like the most is the writing a pre-processor that
takes a simple "one line" piece of code and translates it into all the grunt
work behind the scenes. I like this idea because one I learn how to do it
then I can use it to "extend" C++ to do whatever I need it to do.. though
this might not be the best programming pratice in general, it maybe prove
very useful in the long run.

Thanks
Jon
 
J

Jon Slaughter

Donovan Rebbechi said:
Also, did you see libpropc++ ?

http://www.programurl.com/libpropc-for-c-.htm

Just ran into it doing a quick google.

Cheers,

Yes, I saw that when I first started looking into the idea. It seems that,
AFAIK, that its mainly just compile-time reinterpretation of data types into
"properties" but there is no run-time aspects of this.

i.e. there are two things I have to do:

1. redefine simple data types into complete data types that act as simple
data types(such as strings and integers that act as a subset of the strings
and integers)

2. Associate a name with those data types.


both steps are very simple by just creating a class for each data type(or a
template) and storing the range informaiton and name of the data type in the
class. The point is that I would pretty much have to have a class for each
data type(unless I can work them into templates easily) and ofcourse have
getter and setter methods that restrict the data type appropriately.

the problem is that the code I have create to do that is pretty extreme
compared to if I just have to do some "one line" piece of code that the
compiler could understand. By "making" it understand I can make things a
lot easier(though it might be more work just to get the compiler to
"understand")


We will have to see, I'm still looking over the many ways of doing this.


Thanks
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top