From: Reed H. <re...@mo...> - 2007-03-26 18:31:04
|
What do people think of a new wrapper language for Swig-- a simple C wrapper around any C++ constructions? Having a C facade for a C++ library can be useful for further wrapping or integration into other stuff, or for people who want to or need to use only C when using your library. Would it be at all useful? I imagine it ought to be sort of simple to create? It would do this, I think: * Define all classes as typedefs for void * * Methods become functions prefixed with the class name, and accept a class wrapper void* as its first argument. * Whenever an object is returned from a function or passed back as an output argument, it is simply cast to one of the typedef void*'s. * Public nonmethod members need accessor functions generated. Return a pointer to the member, unless it's already a pointer then we can return the thing pointed to or a pointer to the pointer. * No idea how to do directors * A nice feature might be the ability to declare a particular class as a de-facto singleton, and set a global static instance in a setup or init function that all further functions use (either forever, or until changed by the user. There could maybe be a stack of such objects even.) e.g. Foo.hh: ---- namespace N { class Foo { public: void setX(int x); int getX(); int getX(int mask); int bar; Foo *other; }; } would become: Foo_wrap.h: ---- typedef NFoo_t void*; /* Constructor and Destructor */ NFoo_t *new_NFoo(); void delete_NFoo(NFoo_t *obj); /* Methods */ void NFoo_setX(NFoo_t *obj, int x); int NFoo_getX(NFoo_t *obj); int NFoo_getX_m(NFoo_t *obj, int mask); /* The suffix to differentiate from the other function could be taken from the argument name, or just be "2", with option in .i file to specify it. */ /* Member accessors */ int *NFoo_bar_p(NFoo_t *obj); Foo_t *NFoo_other(NFoo_t *obj); Foo_t **NFoo_other_p(NFoo_t *obj); Foo_wrap.cc: ---- #include "Foo.hh" #include "Foo_wrap.h" NFoo_t *new_NFoo() { return (NFoo_t*)new N::Foo(); } void delete_NFoo(NFoo_t *obj) { delete (N::Foo*)(obj); } void NFoo_setX(NFoo_t *obj, int x) { (N::Foo*)obj->setX(x); } int NFoo_getX(NFoo_t *obj) { return (N::Foo*)obj->getX(); } int NFoo_getX_m(NFoo_t *obj, int mask) { return (N::Foo*)obj->getX(mask); } int *NFoo_bar_p(NFoo_t *obj) { return &(N::Foo*)obj->bar; } Foo_t *NFoo_other(NFoo_t *obj) { return (N::Foo*)obj->other; } Foo_t **NFoo_other_p(NFoo_t *obj) { return &(N::Foo*)obj->other; } |
From: William S F. <ws...@fu...> - 2007-03-28 21:40:40
|
Reed Hedges wrote: > What do people think of a new wrapper language for Swig-- a simple C wrapper > around any C++ constructions? Having a C facade for a C++ library can be > useful for further wrapping or integration into other stuff, or for people who > want to or need to use only C when using your library. > > Would it be at all useful? > > I imagine it ought to be sort of simple to create? It would do this, I think: > > * Define all classes as typedefs for void * > * Methods become functions prefixed with the class name, and accept a class > wrapper void* as its first argument. > * Whenever an object is returned from a function or passed back as an output > argument, it is simply cast to one of the typedef void*'s. > * Public nonmethod members need accessor functions generated. Return a pointer > to the member, unless it's already a pointer then we can return the thing > pointed to or a pointer to the pointer. > * No idea how to do directors > * A nice feature might be the ability to declare a particular class as a > de-facto singleton, and set a global static instance in a setup or init function > that all further functions use (either forever, or until changed by the user. > There could maybe be a stack of such objects even.) > > e.g. > Yes it would be possible and useful. Likewise a C++ wrapper of C code. This suggestion comes up every few months on the mailing list, but as of yet, no-one has implemented a new SWIG language module for this. William |
From: Reed H. <re...@mo...> - 2007-03-29 15:26:40
|
William S Fulton wrote: > Yes it would be possible and useful. Likewise a C++ wrapper of C code. > This suggestion comes up every few months on the mailing list, but as of > yet, no-one has implemented a new SWIG language module for this. > Has there been past discussion on how to implement this? I might have a need for it in the future, and though I know little about swig internals on the surface I am guessing that many parts of the implementation would be simpler than other scriptnig languages? Reed |
From: William S F. <ws...@fu...> - 2007-04-02 20:19:30
|
Reed Hedges wrote: > William S Fulton wrote: > >> Yes it would be possible and useful. Likewise a C++ wrapper of C code. >> This suggestion comes up every few months on the mailing list, but as of >> yet, no-one has implemented a new SWIG language module for this. >> > > > Has there been past discussion on how to implement this? I might have a need > for it in the future, and though I know little about swig internals on the > surface I am guessing that many parts of the implementation would be simpler > than other scriptnig languages? > Not much discussion that I recall. Just it would probably be best to take the C# language module as the basis. You wouldn't need to generate a runtime library, so that is easier than a scripting module, but not unlike the strongly typed language modules. However, overall it is unlike any other language module, so there might be the odd hurdle to cross. For example, you probably don't want to throw away const like all the other language modules do. If you intend to tackle this, I suggest you post prototypes of the code being wrapped and what you intend the generated code to look like so we can discuss it. Please use the swig-devel mailing list for any discussion on the development of the module. William |