class extension for C

J

Jon Slaughter

I want to use classes in some embedded C dev to make the code more
organized. Is there any post processor that can do this? If not anyone know
of a very simple way to go about doing this? (some type of free C/C++ like
processor that I can modify without to much trouble)

Essenitally it would translate a class into compatible C code, e.g.,


class X
{

int i;
void func(int c);
};

....
X.i = 3;
X.func(3);


into


int iX;
void funcX(int c);
....

iX = 3;
funX(3);
-------

I don't need polymorphism, inheritence or encapsulation really(although it
would be nice) or even need to create objects from the class(in a sense the
classes are global static structs).

I just want to be able to write a bit more logical code in C with a little
bit of encapsultion(or really just organization).

I imagine though one could add the ability to use objects by just
specializing the class name a bit but it would require a little more work to
setup the class.

Unfortunately theres no C++ compiler for the device I'm interested in and I
don't want to write one.

Any ideas?

Thanks,
Jon
 
S

Sam

Jon said:
Essenitally it would translate a class into compatible C code, e.g.,

Ancient C++ compilers worked by preprocessing C++ code into horribly ugly C,
then compiling it.

Fortunately, those dark days are long behind us. I'm not aware of any modern
C++-to-C translators; I doubt that any exist, their value seems to be rather
dubious.
I just want to be able to write a bit more logical code in C with a little
bit of encapsultion(or really just organization).

Then just do that: write a bit more logical C code. It takes more
discipline, but it's very much doable. And you do not need any cockamamie
translator for it.

Many modern, complex, C library APIs have an internal object-oriented type
structure that works very well in presenting a logical, consistent,
structured API. A good example is the GnuTLS library. It defines many simple
and complex structures. For each one there's a well-defined initialization
function, and a well-defined de-initialization function, and other function
reference the objects using handles; this is directly analogous to C++
constructors and destructors, and a class instance pointer.
I imagine though one could add the ability to use objects by just
specializing the class name a bit but it would require a little more work to
setup the class.

Yes, indeed, writing logical, well-structured code, always requires a little
more work. C++ doesn't help you with that. You do not automatically
guarantee that your code will be clean and pristine simply by the virtue of
using C++. There's plenty of horrid C++ code out there.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBH2zLpx9p3GYHlUOIRAnAyAJ9O2YP2PJMG3i8YMytCk6cwBcrLywCfbgHi
nhVMYXq7nootmLXQt1BvAEk=
=UG5Y
-----END PGP SIGNATURE-----
 
I

Ian Collins

[You don't have to ask 3 times!]

Jon said:
I want to use classes in some embedded C dev to make the code more
organized. Is there any post processor that can do this? If not anyone know
of a very simple way to go about doing this? (some type of free C/C++ like
processor that I can modify without to much trouble)
I don't need polymorphism, inheritence or encapsulation really(although it
would be nice) or even need to create objects from the class(in a sense the
classes are global static structs).

I just want to be able to write a bit more logical code in C with a little
bit of encapsultion(or really just organization).
Why not just use "OO C", structs with function pointer as members?

Something like

typedef struct X X;

struct X
{
int i;
void (*func)( X*, int );
};

void f( X* this, int n )
{
this->i = n;
}

int main()
{
X x;

x.func = f;

x.func( &x, 42 );
}
 
K

kwikius

[You don't have to ask 3 times!]

Jon said:
I want to use classes in some embedded C dev to make the code more
organized. Is there any post processor that can do this? If not anyone know
of a very simple way to go about doing this? (some type of free C/C++ like
processor that I can modify without to much trouble)

I don't need polymorphism, inheritence or encapsulation really(although it
would be nice) or even need to create objects from the class(in a sense the
classes are global static structs).
I just want to be able to write a bit more logical code in C with a little
bit of encapsultion(or really just organization).

Why not just use "OO C", structs with function pointer as members?

Something like

typedef struct X X;

struct X
{
  int i;
  void (*func)( X*, int );

};

void f( X* this, int n )
{
  this->i = n;

}

int main()
{
  X x;

  x.func = f;

  x.func( &x, 42 );

}

Could even try some macros (only tested in VC++)

regards
Andy Litle

#include <stdio.h>

struct my{

int val;
};


#define MAKEMEMFUN(Class,fun_name) \
fun_name ## Class( struct Class * This

int MAKEMEMFUN(my,get) )
{
return This->val;
}

void MAKEMEMFUN(my,set), int v)
{
This->val = v;
}

#define MFN(Class, ob,fun_name)\
fun_name ## Class ( ob


int main()
{
struct my mm;
MFN(my,&mm,set),1000);

printf("%d\n", MFN(my,&mm,get)) );
}
 
J

Jeff Schwab

kwikius said:
[You don't have to ask 3 times!]

Jon said:
I want to use classes in some embedded C dev to make the code more
organized. Is there any post processor that can do this? If not anyone know
of a very simple way to go about doing this? (some type of free C/C++ like
processor that I can modify without to much trouble)
I don't need polymorphism, inheritence or encapsulation really(although it
would be nice) or even need to create objects from the class(in a sense the
classes are global static structs).
I just want to be able to write a bit more logical code in C with a little
bit of encapsultion(or really just organization).
Why not just use "OO C", structs with function pointer as members?

Something like

typedef struct X X;

struct X
{
int i;
void (*func)( X*, int );

};

void f( X* this, int n )
{
this->i = n;

}

int main()
{
X x;

x.func = f;

x.func( &x, 42 );

}

Could even try some macros (only tested in VC++)

regards
Andy Litle

#include <stdio.h>

struct my{

int val;
};


#define MAKEMEMFUN(Class,fun_name) \
fun_name ## Class( struct Class * This )

int MAKEMEMFUN(my,get) )
{
return This->val;
}

void MAKEMEMFUN(my,set), int v)
{
This->val = v;
}

#define MFN(Class, ob,fun_name)\
fun_name ## Class ( ob )


int main()
{
struct my mm;
MFN(my,&mm,set),1000);

printf("%d\n", MFN(my,&mm,get)) );
}

http://en.wikipedia.org/wiki/C_preprocessor#X-Macros
 
J

Jeff Schwab

Jon said:
I want to use classes in some embedded C dev to make the code more
organized. Is there any post processor that can do this? If not anyone know
of a very simple way to go about doing this? (some type of free C/C++ like
processor that I can modify without to much trouble)

Essenitally it would translate a class into compatible C code, e.g.,


class X
{

int i;
void func(int c);
};

...
X.i = 3;
X.func(3);


into


int iX;
void funcX(int c);
...

iX = 3;
funX(3);
-------

I don't need polymorphism, inheritence or encapsulation really(although it
would be nice) or even need to create objects from the class(in a sense the
classes are global static structs).

I just want to be able to write a bit more logical code in C with a little
bit of encapsultion(or really just organization).

I imagine though one could add the ability to use objects by just
specializing the class name a bit but it would require a little more work to
setup the class.

Unfortunately theres no C++ compiler for the device I'm interested in and I
don't want to write one.

As Sam said, your best bet is probably just to write decent C code. If
you are determined to go down the preprocessor route ('tis a shortcut in
name only), take a look at the KOBJ framework used in the BSD kernel.
If you just want generic types, Google turns up various macro libraries.
Here's one "inspired by" the STL:

http://sglib.sourceforge.net/
 
J

Jerry Coffin

[ ... ]
Unfortunately theres no C++ compiler for the device I'm interested in and I
don't want to write one.

I'd contact Comeau Computing, and see about getting them to port their
C++ compiler to your target. This is almost certainly a more practical
route than most of the other possibilities that involve your writing
(part of) a translator in the preprocessor, or anything on that order
(there's a reason C with Classes quickly transitioned to a real
compiler).
 
K

kwikius

kwikius said:
[You don't have to ask 3 times!]
Jon Slaughter wrote:
I want to use classes in some embedded C dev to make the code more
organized. Is there any post processor that can do this? If not anyone know
of a very simple way to go about doing this? (some type of free C/C++ like
processor that I can modify without to much trouble)
<snip>
I don't need polymorphism, inheritence or encapsulation really(although it
would be nice) or even need to create objects from the class(in a sense the
classes are global static structs).
I just want to be able to write a bit more logical code in C with a little
bit of encapsultion(or really just organization).
Why not just use "OO C", structs with function pointer as members?
Something like
typedef struct X X;
struct X
{
  int i;
  void (*func)( X*, int );
};
void f( X* this, int n )
{
  this->i = n;
}
int main()
{
  X x;
  x.func = f;
  x.func( &x, 42 );
}
Could even try some macros (only tested in VC++)
regards
Andy Litle
#include <stdio.h>
struct my{
  int val;
};
#define MAKEMEMFUN(Class,fun_name) \
  fun_name ## Class( struct Class * This )

int MAKEMEMFUN(my,get) )
{
  return This->val;
}
void MAKEMEMFUN(my,set), int v)
{
  This->val = v;
}
#define MFN(Class, ob,fun_name)\
fun_name ## Class ( ob )

int main()
{
  struct  my mm;
  MFN(my,&mm,set),1000);
  printf("%d\n",  MFN(my,&mm,get)) );
}

http://en.wikipedia.org/wiki/C_preprocessor#X-Macros- Hide quoted text -

- Show quoted text -

Or:

http://dinosaur.compilertools.net/

actually a nice one is:

http://home.earthlink.net/~slkpg/

regards
Andy Little
 
C

Chris Thomasson

Ian Collins said:
[You don't have to ask 3 times!]

Jon said:
I want to use classes in some embedded C dev to make the code more
organized. Is there any post processor that can do this? If not anyone
know
of a very simple way to go about doing this? (some type of free C/C++
like
processor that I can modify without to much trouble)
I don't need polymorphism, inheritence or encapsulation really(although
it
would be nice) or even need to create objects from the class(in a sense
the
classes are global static structs).

I just want to be able to write a bit more logical code in C with a
little
bit of encapsultion(or really just organization).
Why not just use "OO C", structs with function pointer as members?

FWIW, here is simplistic approach to OO in C:

http://groups.google.com/group/comp.lang.c/browse_frm/thread/1b106926ba5db19f

IMVHO, this is extremely straightforward and happens to work _very_ well in
practice...
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top