> * memory management

Oops, forgot to free it in the destructor, thanks for that.  I will look into that scoped_ptr thing

> * readability

I already changed it to use a dictionary on the string formatting which makes it much more readable.

On Sun, Jul 13, 2008 at 3:14 PM, Roman Yakovenko <roman.yakovenko@gmail.com> wrote:
On Fri, Jul 11, 2008 at 6:51 AM, Gordon Wrigley
<gordon.wrigley@gmail.com> wrote:
> I have my char *variables working now, here's the current py++ code I am
> using, any comments would be appreciated
>
> for (className, varName) in stringVars:
>     wrapClass = mb.class_(className)
>     wrapClass.var(varName).exclude()
>     wrapAlias = wrapClass.wrapper_alias
>
>     wrapperCode = """
>         char *wrap_%s;
>         void set_%s(%s& inst, bp::object obj){
>             char **strPtr = &wrap_%s;
>             if(*strPtr != NULL) delete *strPtr;
>             bp::extract<std::string> x(obj);
>             if (x.check()){
>                 std::string str = x();
>                 *strPtr = new char[str.length()+1];
>                 strcpy_s(*strPtr, str.length()+1, str.c_str());
>             }else{
>                 *strPtr = NULL;
>             }
>             inst.%s = *strPtr;
>         }
>         static boost::python::object get_%s(%s& inst){
>             std::string str;
>             if(inst.%s != NULL){
>                 std::string str(inst.%s);
>                 return bp::object(str);
>             }else{
>                 return bp::object();
>             }
>         }
>         """ % (varName, varName, className, varName, varName, varName,
> className, varName, varName)
>     wrapClass.add_wrapper_code(wrapperCode)
>
>     constructorCode = "wrap_%s = NULL;" % (varName)
>     add_constructors_body(wrapClass, constructorCode)
>
>     registrationCode = 'add_property("%s", &%s::get_%s, &%s::set_%s)' %
> (varName, wrapAlias, varName, wrapAlias, varName)
>     wrapClass.add_registration_code(registrationCode)

As for me you have few problems in this code:
* memory management
 you "set..." function allocates the memory, but you don't release
it. If you have many instances created\destroyed during program
execution you will have serious memory leak
 I would use boost::scoped_ptr (
http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/scoped_ptr.htm )
or just plain std::string classes
* readability
 wrapperCode contains too many "%s" and it is really difficult to
follow what is going on and modify it letter. I suggest you to use
string.Template class( http://www.python.org/doc/2.5.2/lib/node40.html
). I am sure, you will be pleased with the result.



--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/