Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Meta Programming language

Reply
Thread Tools

C++ Meta Programming language

 
 
Jon Slaughter
Guest
Posts: n/a
 
      07-02-2005
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


 
Reply With Quote
 
 
 
 
REH
Guest
Posts: n/a
 
      07-02-2005

"Jon Slaughter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Donovan Rebbechi
Guest
Posts: n/a
 
      07-02-2005
On 2005-07-02, Jon Slaughter <(E-Mail Removed)> wrote:
> 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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
 
Reply With Quote
 
Jon Slaughter
Guest
Posts: n/a
 
      07-02-2005

"REH" <(E-Mail Removed)> wrote in message
news:6nCxe.316$(E-Mail Removed)...
>
> "Jon Slaughter" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>
>


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).



 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      07-02-2005

"Jon Slaughter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "REH" <(E-Mail Removed)> wrote in message
> news:6nCxe.316$(E-Mail Removed)...
>>
>> "Jon Slaughter" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>>
>>

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


 
Reply With Quote
 
Jon Slaughter
Guest
Posts: n/a
 
      07-02-2005

"Donovan Rebbechi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2005-07-02, Jon Slaughter <(E-Mail Removed)> wrote:
>> 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,
> --
> Donovan Rebbechi
> http://pegasus.rutgers.edu/~elflord/


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. )








 
Reply With Quote
 
Donovan Rebbechi
Guest
Posts: n/a
 
      07-02-2005
On 2005-07-02, Jon Slaughter <(E-Mail Removed)> wrote:
>
> 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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
 
Reply With Quote
 
Donovan Rebbechi
Guest
Posts: n/a
 
      07-02-2005
On 2005-07-02, Jon Slaughter <(E-Mail Removed)> wrote:
>


> 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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
 
Reply With Quote
 
Jon Slaughter
Guest
Posts: n/a
 
      07-02-2005

"Donovan Rebbechi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2005-07-02, Jon Slaughter <(E-Mail Removed)> wrote:
>>


<snip>

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

> --
> Donovan Rebbechi
> http://pegasus.rutgers.edu/~elflord/


Thanks
Jon


 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      07-02-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorry for interrupt,because my mother language is not English,it maybe a little confused to read the c programming language. Zhang Yuan C Programming 6 06-14-2012 04:35 AM
Meta-Meta-Programming, revisited Erik Veenstra Ruby 21 07-25-2006 10:26 PM
Meta-Meta-Programming Erik Veenstra Ruby 29 02-08-2006 08:22 PM
Meta methods to govern meta data? Duane Johnson Ruby 6 10-28-2005 03:57 AM
META NAME and META HTTP-EQUIV Nym Pseudo HTML 1 09-26-2003 09:13 AM



Advertisments