473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

convert a c++ program into a template metaprogram?

I need to convert C preprocessor definitions into python code. The
definitions are dumped out of gccxml (see http://www.gccxml.org) ,
running over the windows header files (for example).

This creates output like this (some excerpts):

#define DATABITS_8 ((WORD)0x0008)
#define KEY_WRITE ((STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~SYNCHRONIZE))
#define MS_NBF "MNBF"
#define PSN_FIRST (0U-200U)
#define RT_ANIICON MAKEINTRESOURCE(22)
#define STG_E_FILENOTFOUND _HRESULT_TYPEDEF_(0x80030002L)

I thought I be clever, and I wrote a couple of overloaded function to
dump out the definitions in Python syntax:

void dump(char *name, LPSTR value)
{
int a = (int)(void *)value;
if ((a & 0xFFFF0000) == 0)
cout << name << " = " << "LPSTR(" << a << ")" << endl;
else
cout << name << " = " << "r\"\"\"" << value << "\"\"\"" << endl;
}

void dump(char *name, int value)
{ cout << name << " = " << value << endl; }

void dump(char *name, unsigned int value)
{ cout << name << " = " << value << endl; }

#define DUMP(sym) dump(#sym, sym)

Now I can create a small program to dump out the definitions, compile it
and call it:

int main (int argc, char **argv)
{
DUMP(DATABITS_8);
DUMP(KEY_WRITE);
DUMP(MS_NBF);
DUMP(PSN_FIRST);
DUMP(RT_ANIICON);
DUMP(STG_E_FILENOTFOUND);
}

This works pretty well, and creates this output:

DATABITS_8 = 8
KEY_WRITE = 131078
MS_NBF = r"""MNBF"""
PSN_FIRST = 4294967096
RT_ANIICON = LPSTR(22)
STG_E_FILENOTFOUND = -2147287038
What I'm wondering now is: Is it possible to do the same with templates,
so that it isn't needed to compile and run the program? gccxml is the
parsing engine of GCC, which spits out the parsing result in xml format,
apparently after templates have been expanded (don't know if that's the
correct term).

As far as I have learned so far (I'm an experienced C programmer, but
not a c++ one) templates can only be invoked and specialized with types,
but not with the types of values. So, is this impossible?

Cheers,

Thomas
Jul 22 '05 #1
8 1934

"Thomas Heller" <th*****@python.net> wrote in message
news:u0**********@python.net...
I need to convert C preprocessor definitions into python code. The
definitions are dumped out of gccxml (see http://www.gccxml.org) ,
running over the windows header files (for example).

This creates output like this (some excerpts):

#define DATABITS_8 ((WORD)0x0008)
#define KEY_WRITE ((STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~SYNCHRONIZE)) #define MS_NBF "MNBF"
#define PSN_FIRST (0U-200U)
#define RT_ANIICON MAKEINTRESOURCE(22)
#define STG_E_FILENOTFOUND _HRESULT_TYPEDEF_(0x80030002L)

I thought I be clever, and I wrote a couple of overloaded function to
dump out the definitions in Python syntax:

void dump(char *name, LPSTR value)
{
int a = (int)(void *)value;
if ((a & 0xFFFF0000) == 0)
cout << name << " = " << "LPSTR(" << a << ")" << endl;
else
cout << name << " = " << "r\"\"\"" << value << "\"\"\"" << endl;
}

void dump(char *name, int value)
{ cout << name << " = " << value << endl; }

void dump(char *name, unsigned int value)
{ cout << name << " = " << value << endl; }

#define DUMP(sym) dump(#sym, sym)

Now I can create a small program to dump out the definitions, compile it
and call it:

int main (int argc, char **argv)
{
DUMP(DATABITS_8);
DUMP(KEY_WRITE);
DUMP(MS_NBF);
DUMP(PSN_FIRST);
DUMP(RT_ANIICON);
DUMP(STG_E_FILENOTFOUND);
}

This works pretty well, and creates this output:

DATABITS_8 = 8
KEY_WRITE = 131078
MS_NBF = r"""MNBF"""
PSN_FIRST = 4294967096
RT_ANIICON = LPSTR(22)
STG_E_FILENOTFOUND = -2147287038
What I'm wondering now is: Is it possible to do the same with templates,
so that it isn't needed to compile and run the program? gccxml is the
parsing engine of GCC, which spits out the parsing result in xml format,
apparently after templates have been expanded (don't know if that's the
correct term).

As far as I have learned so far (I'm an experienced C programmer, but
not a c++ one) templates can only be invoked and specialized with types,
but not with the types of values. So, is this impossible?

Cheers,

Thomas


I can't speak to your whole message, but I can say something about the last
paragraph. Templates may be instantiated with values as well as with other
templates. Types are not the only kinds of parameters you may declare a
template to take. If I recall properly, the values may be of types such as
integer types, pointers and pointers to members. That list may not be
complete...
Jul 22 '05 #2
"Dave" <be***********@yahoo.com> writes:
"Thomas Heller" <th*****@python.net> wrote in message
news:u0**********@python.net...

As far as I have learned so far (I'm an experienced C programmer, but
not a c++ one) templates can only be invoked and specialized with types,
but not with the types of values. So, is this impossible?

Cheers,

Thomas


I can't speak to your whole message, but I can say something about the last
paragraph. Templates may be instantiated with values as well as with other
templates. Types are not the only kinds of parameters you may declare a
template to take. If I recall properly, the values may be of types such as
integer types, pointers and pointers to members. That list may not be
complete...


Dave, what I know so far is that I can define templates that take types,
and instantiate them with the type:

template <class T> class SomeThing {
};

SomeThing<int> foo;
SomeThing<char *> bar;

and templates that take values of a certain type, and instantiate them
with a value of this type:

template <int N> class Blah {
};

Blah<42> foo;
Blah<3> bar;

but what I need is a template that is instantiated by 'calling' it with
values of different types, something like this:

MyClass<42> anIntClass;
MyClass<3U> anUnsignedClass;
MyClass<"abc"> aStringClass;

because the 'values' are #define'd symbols, of which I don't know the
type (but the compiler does).

Thomas
Jul 22 '05 #3
Thomas Heller wrote:
[...] what I need is a template that is instantiated by 'calling' it with
values of different types, something like this:

MyClass<42> anIntClass;
MyClass<3U> anUnsignedClass;
MyClass<"abc"> aStringClass;

because the 'values' are #define'd symbols, of which I don't know the
type (but the compiler does).


No, that's not really possible until 'decltype' operator comes along.
It would be better if to instantiate those objects you used a factory
template:

MyClass<int> anIntClass = InstantiateMyClass(42);
MyClass<unsigned> anUnsignedClass = InstantiateMyClass(3U);
MyClass<char const*> aStringClass = InstantiateMyClass("abc");

In this case 'InstantiateMyClass' will be a function template:

template<typename T> MyClass<T> InstantiateMyClass(T t) {
return MyClass<T>(t);
}

Of course, your generator will have to decide the type of the symbol
based on the value (form). That's not that difficult, is it? Nah...

Of course, if you don't need to declare an object and only care about
an expression, you just use 'InstantiateMyClass' function template and
you're all set.

Victor
Jul 22 '05 #4
Victor Bazarov <v.********@comAcast.net> writes:
Thomas Heller wrote:
[...] what I need is a template that is instantiated by 'calling' it with
values of different types, something like this:
MyClass<42> anIntClass;
MyClass<3U> anUnsignedClass;
MyClass<"abc"> aStringClass;
because the 'values' are #define'd symbols, of which I don't know the
type (but the compiler does).
No, that's not really possible until 'decltype' operator comes along.
It would be better if to instantiate those objects you used a factory
template:

MyClass<int> anIntClass = InstantiateMyClass(42);
MyClass<unsigned> anUnsignedClass = InstantiateMyClass(3U);
MyClass<char const*> aStringClass = InstantiateMyClass("abc");

In this case 'InstantiateMyClass' will be a function template:

template<typename T> MyClass<T> InstantiateMyClass(T t) {
return MyClass<T>(t);
}

Of course, your generator will have to decide the type of the symbol
based on the value (form). That's not that difficult, is it? Nah...


Hm, how? That's the whole problem.
Of course, if you don't need to declare an object and only care about
an expression, you just use 'InstantiateMyClass' function template and
you're all set.


See the original post - the program is never executed, and not even
compiled. It is processed with gccxml, which stops after the parsing
phase (it seems class templates have been expanded then), and the parse
tree is written to an XML file. I want to get everything from the xml.

Thomas
Jul 22 '05 #5

"Thomas Heller" <th*****@python.net> wrote in message
news:k6**********@python.net...
"Dave" <be***********@yahoo.com> writes:
"Thomas Heller" <th*****@python.net> wrote in message
news:u0**********@python.net...

As far as I have learned so far (I'm an experienced C programmer, but
not a c++ one) templates can only be invoked and specialized with types, but not with the types of values. So, is this impossible?

Cheers,

Thomas


I can't speak to your whole message, but I can say something about the last paragraph. Templates may be instantiated with values as well as with other templates. Types are not the only kinds of parameters you may declare a
template to take. If I recall properly, the values may be of types such as integer types, pointers and pointers to members. That list may not be
complete...


Dave, what I know so far is that I can define templates that take types,
and instantiate them with the type:

template <class T> class SomeThing {
};

SomeThing<int> foo;
SomeThing<char *> bar;

and templates that take values of a certain type, and instantiate them
with a value of this type:

template <int N> class Blah {
};

Blah<42> foo;
Blah<3> bar;

but what I need is a template that is instantiated by 'calling' it with
values of different types, something like this:

MyClass<42> anIntClass;
MyClass<3U> anUnsignedClass;
MyClass<"abc"> aStringClass;

because the 'values' are #define'd symbols, of which I don't know the
type (but the compiler does).

Thomas


This may not be exactly what you want, but it is a possibility. C++ does
not have a "typeof" operator. But, you can still determine a hard-coded
value's type at compile time by passing it to a function template and
letting argument deduction tell you the type. The type is then available
for you to use inside the function template. You can use that type to
instantiate your class tempalte with the type of the value, and the value
can also be a template parameter to your class template. See what I've done
below.

There might be a better way. Hmmm, I wonder if there's a typeof in boost...

#include <iostream>

using namespace std;

#define MYVAL 42

template <typename T, T VAL>
class my_class
{
public:
my_class(): m_val(VAL) {}

void foo()
{
cout << m_val << endl;
}

private:
T m_val;
};

template <typename T>
void func(T)
{
my_class<T, MYVAL> var; // Here, we really use the value.

var.foo();
}

int main()
{
func(MYVAL); // Passing MYVAL here just conveys type information,
// we're not actually using it to pass the value.

return 0;
}
Jul 22 '05 #6
"Thomas Heller" <th*****@python.net> wrote...
[...]
See the original post - the program is never executed, and not even
compiled. It is processed with gccxml, which stops after the parsing
phase (it seems class templates have been expanded then), and the parse
tree is written to an XML file. I want to get everything from the xml.


You _could_ supply the type in the same XML document, right next to the
literal, no?
Jul 22 '05 #7
"Dave" <be***********@yahoo.com> writes:
"Thomas Heller" <th*****@python.net> wrote in message
news:k6**********@python.net...

Dave, what I know so far is that I can define templates that take types,
and instantiate them with the type:

template <class T> class SomeThing {
};

SomeThing<int> foo;
SomeThing<char *> bar;

and templates that take values of a certain type, and instantiate them
with a value of this type:

template <int N> class Blah {
};

Blah<42> foo;
Blah<3> bar;

but what I need is a template that is instantiated by 'calling' it with
values of different types, something like this:

MyClass<42> anIntClass;
MyClass<3U> anUnsignedClass;
MyClass<"abc"> aStringClass;

because the 'values' are #define'd symbols, of which I don't know the
type (but the compiler does).

Thomas


This may not be exactly what you want, but it is a possibility. C++ does
not have a "typeof" operator. But, you can still determine a hard-coded
value's type at compile time by passing it to a function template and
letting argument deduction tell you the type. The type is then available
for you to use inside the function template. You can use that type to
instantiate your class tempalte with the type of the value, and the value
can also be a template parameter to your class template. See what I've done
below.

There might be a better way. Hmmm, I wonder if there's a typeof in boost...

[...]

The problem with the approach you describe above is that string literals
cannot be used to call a class template (I'm not sure about the
terminology here). I get errors like this:

a.cpp:27: error: string literal "blahblah" is not a valid template
argument because it is the address of an object with static linkage

But, I found a somewhat simpler solution. The value can also be made
available as default argument for a function template. So, this is the
final test program I have, and all I need can be found in the xml that
gccxml generates:

#define MYVAL 999999
#define MYUVAL (0U - 1U)
#define MYSTRING "blahblah"
#define MYCHAR 'c'

template <typename T> T symbol_MYSTRING(T, T val = MYSTRING) {}
template <typename T> T symbol_MYCHAR(T, T val = MYCHAR) {}
template <typename T> T symbol_MYUVAL(T, T val = MYUVAL) {}
template <typename T> T symbol_MYVAL(T, T val = MYVAL) {}

int main()
{
symbol_MYSTRING(MYSTRING, MYSTRING);
symbol_MYCHAR(MYCHAR, MYCHAR);
symbol_MYVAL(MYVAL, MYVAL);
symbol_MYUVAL(MYUVAL, MYUVAL);
}

Many, many thanks for your help,

Thomas
Jul 22 '05 #8
Dave wrote:
Hmmm, I wonder if there's a typeof in boost...


Submitted for the formal review:
http://www.boost.org/more/formal_review_schedule.html

See typeof.zip in
http://boost-sandbox.sourceforge.net/vault/

Regards,

Arkadiy

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave | last post by:
For those who might be so inclined, I was wondering if I might get honest critiques of my first real venture into template metaprogramming. This template metaprogram sorts a list of integers at...
12
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the...
5
by: Mohammad | last post by:
Hi, Is it possible to disable a method of a template class depending on the typename at compile time? thanks!
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
6
by: Protoman | last post by:
This Fibonacci program prints the wrong value for phi; it should be 1.618..., not 1. And it halts and closes as soon as I execute it. Code: #include <iostream> #include <iomanip> #include...
6
by: compboy | last post by:
Can anyone help me about this. I have been trying all the ways I knew and I could find but just didnt work. I have tried: using itoa but it says that it doesnt have that function. and...
3
by: JosephLee | last post by:
/* Below you will find the declaration and the definition of the class Format that can be used to manipulate the ostream. Although the class is only capable of setting the width and precision,...
2
by: Hel | last post by:
Hi, I'm sure you are familiar with this problem: A.h: template <unsigned Nstruct A { static const unsigned a; };
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.