C++ code generator

  • Thread starter christopher diggins
  • Start date
C

christopher diggins

I am considering writing a code generator which takes interfaces
specifications in the following style:

interface IStack<class Elem_T>
invariant: // number of items pushed >= number of items popped
{
Bool IsEmpty();
precondition: result = number of items pushed == number of items popped
void Push(const Elem_T& x);
void Pop();
precondition: IsEmpty() == false
postcondition: // result = most recent pushed item which hasn't been
popped
const Elem_T& Top();
precondition: IsEmpty() == false
}

The code generator would output a C++ class to represent the interface (
using the BIL, see http://www.codeproject.com/cpp/retrofitpolymorphism.asp )
It would also output a contract class which verifies the preconditions /
postconditions / etc. It would also ouput a stub implementation class. That
is a class which implements the interface but does nothing.

This is actually only scratching the surface of what I would do with it: I
would also support pseudo-code class definitions, which implement Heron
style classes.

The feature list for the classes would be considerably more far reaching:
- aspect oriented programming support (
http://www.heron-language.com/aspect-oriented-programming.html )
- self / inherited keywords
- delegation
- implicit result variables
- properties
- _ctor and _dtor named constructors and destructors

I am trying to gauge the level of interest in such a tool. Thanks in
advance, and feel free to share your two cents.
 
P

Puppet_Sock

christopher said:
I am considering writing a code generator which takes interfaces
specifications in the following style:

interface IStack<class Elem_T>
invariant: // number of items pushed >= number of items popped
{
Bool IsEmpty();
precondition: result = number of items pushed == number of items popped
void Push(const Elem_T& x);
void Pop();
precondition: IsEmpty() == false
postcondition: // result = most recent pushed item which hasn't been
popped
const Elem_T& Top();
precondition: IsEmpty() == false
}

Code generation tools are good things in most cases. However,
there's something, um, skewed about requiring psuedo code as
input to such a tool. The level of effort required to create
the input you've typed here is too large compared to the result.

Instead, how about a menu driven something-or-other that lets
you select certain items from a list, then blasts the appropriate
text into a file for you. Put a nice GUI on there and you've got
something.

Of course, with the example you've got, what you've got is a
fairly straightforward template class. So, the question that
arises fairly quickly is, what's wrong with just providing that
fairly straightforward template class? Template classes are
pretty cool.
Socks
 
C

christopher diggins

Puppet_Sock said:
Code generation tools are good things in most cases. However,
there's something, um, skewed about requiring psuedo code as
input to such a tool. The level of effort required to create
the input you've typed here is too large compared to the result.

I don't see how I could reduce it down further.

Take a look at the code needed to replicate the interface:

template<typename Elem_T>
BOOST_IDL_BEGIN1(IStack)
BOOST_IDL_FN1(Push, void, (const Elem_T&, x))
BOOST_IDL_FN0(Pop, const Elem_T&)
BOOST_IDL_FN0(Peek, const Elem_T&)
BOOST_IDL_FN0(IsEmpty, Bool)
BOOST_IDL_END1(IStack)

There is the contract:

template<typename Stack_T, typename Elem_T>
struct Stack_contract : public Stack_T
{
// type identities
typedef Stack_T inherited;
// constructor
Stack_contract() { }
template<typename Arg_T> Stack_contract(const Arg_T& x) : inherited(X)
{ };
// contract verification
const Elem_T& Pop() { PRE(!IsEmpty()); return inherited::pop(); }
const Elem_T& Peek() { PRE(!IsEmpty()); return inherited::peek(); }
};

The wrapper :

template<typename Stack_T, typename Elem_T>
struct Stack_wrapper
{
#ifdef APPLY_CONTRACTS
typedef Stack_ext<Stack_contract<Stack_T, Elem_T>, Elem_T> type;
#else
typedef Stack_ext<Stack_T, Elem_T> type;
#endif
};

And a stub:

template<typename Elem_T>
struct Stack_strub
{
Stack_stub() { }
// IStack implementation
const Elem_T& Peek() { }
const Elem_T& Pop() { }
void Push(const Elem_T& x) { }
Bool IsEmpty() { }
};
Instead, how about a menu driven something-or-other that lets
you select certain items from a list, then blasts the appropriate
text into a file for you. Put a nice GUI on there and you've got
something.

This is an interesting idea.
Of course, with the example you've got, what you've got is a
fairly straightforward template class. So, the question that
arises fairly quickly is, what's wrong with just providing that
fairly straightforward template class? Template classes are
pretty cool.

All of this is not as easy to implement as it might seem on first glance
check out http://www.ootl.org/src/collections.hpp

Thanks for your feedback
CD
 
F

fuzzylollipop

what you have to ask yourself is why create yet another templating
language.

C++ has one built in already.
There is already a standard IDL, for doing just this.
There is XML/UML formats for this also ->
http://www.yy.ics.keio.ac.jp/~suzuki/project/uxf/
Tools like SWIG do this already for other languages to C/C++ mappings.
There are tonnes of template based parsers and frameworks, some like
EMF that are already integrated into an IDE ( Eclipse )

second you have to answer, is your idea and implementation so much
better than everyone elses that people are going to drop what they are
doing, learn your NEW language/syntax/api or whatever and adopt it
because it saves them so much time and effort ( including the learning
curve ).
First pass at your suggestion my answer to the second question is a NO.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top