473,569 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_RIGH TS_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_FILENOTFO UND _HRESULT_TYPEDE F_(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_FILE NOTFOUND);
}

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_FILENOTFO UND = -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 1947

"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_RIGH TS_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_FILENOTFO UND _HRESULT_TYPEDE F_(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_FILE NOTFOUND);
}

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_FILENOTFO UND = -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 = InstantiateMyCl ass(42);
MyClass<unsigne d> anUnsignedClass = InstantiateMyCl ass(3U);
MyClass<char const*> aStringClass = InstantiateMyCl ass("abc");

In this case 'InstantiateMyC lass' will be a function template:

template<typena me T> MyClass<T> InstantiateMyCl ass(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 'InstantiateMyC lass' function template and
you're all set.

Victor
Jul 22 '05 #4
Victor Bazarov <v.********@com Acast.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 = InstantiateMyCl ass(42);
MyClass<unsigne d> anUnsignedClass = InstantiateMyCl ass(3U);
MyClass<char const*> aStringClass = InstantiateMyCl ass("abc");

In this case 'InstantiateMyC lass' will be a function template:

template<typena me T> MyClass<T> InstantiateMyCl ass(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 'InstantiateMyC lass' 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(M YCHAR, MYCHAR);
symbol_MYVAL(MY VAL, MYVAL);
symbol_MYUVAL(M YUVAL, 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
1240
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 compile time. It works, but if my approach is more awkward or "bad" than need be, I'd love to hear suggestions for improvement! #include...
12
2142
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 code)? It's not a perfect analogy, but it seems to be a reasonable statement...
5
1716
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
3612
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 of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and...
6
4652
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 <cstdlib> #define setprec setprecision using namespace std;
6
34137
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 also
3
1796
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, further functionality could be added. The class is implemented as a template class. Your task is to
2
4560
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
17513
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() == "3"; Convert<HelloWorld>::Get() == "HelloWorld"; The reason I need this is that in our design every class of a certain
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.