ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> cpp-sig
cpp-sig
[C++-sig] Static, again...
by Hugo van der Merwe other posts by this author
Nov 12 2002 10:07AM messages near this date
Re: [C++-sig] Re: call_method | [C++-sig] Static, again...
I had a look at the mailing list archives about this topic, I'm still
not clear on how exactly it works:

>  #include <boost/python/module.hpp>
>  #include <boost/python/class.hpp>
> 
>  struct Num {
>     Num() {}
>     ~Num() {}
>     static int getDims() {return 3;}
>     int dims() {return 5;}
>  };
> 
>  BOOST_PYTHON_MODULE_INIT(numTest)
>  {
>     using namespace boost::python;
>     module numTestMod("numTest");
>     numTestMod
>        .add
>        ( class_<Num>("Num")
>          .def_init()
>          .def("getDims",&Num::getDims)
>          .def("dims", &Num::dims)
>          )
>        ;
>  }

>  We don't have a way to make true static methods yet.
>  getDims will work fine if you access it through the class like this:
>  
>      Nums.getDims()

I am trying to wrap a "Settings" class which does not have a public
constructor, it expects you to call "GetInstance" to get a pointer to
the always-existing instance (making the settings "global"). The above
would make me think that this will work:

	class_<Settings, boost::noncopyable> ("Settings", no_init)
	.def("GetInstance", &Settings::GetInstance,
	     return_value_policy<reference_existing_object> ());

I cannot call Settings.GetInstance(), it gives me:

TypeError: unbound method Boost.Python.function object must be called with Settings instance
 as first argument (got nothing instead)

I also read about an approach that looks something like this:

    object settings_class =
	class_<Settings, boost::noncopyable> ("Settings", no_init);
    settings_class.attr("Settings") = &Settings::GetInstance;

But I don't know how that is supposed to work...

If I wanted to expose GetInstance as Settings() instead (once I get it
working), is it as simple as just calling it "__init__" instead?
(Should I then have no_init in the class_<>  invocation or not? I
noticed the tutorial talks about no_init, but then mentions as soon as
you wish to derive classes from it, you do not want no_init. So when
is no_init useful? Only when you cannot construct a class and don't
want to derive from it, i.e. when you're using "factories"?)

I am really astonished with what can be achieved with C++ -
Boost.Python is really amazing!

Thanks a lot,
Hugo van der Merwe

_______________________________________________
C++-sig mailing list
C++-sig@[...].org
http://mail.python.org/mailman/listinfo/c++-sig
Thread:
Hugo van der Merwe
Scott A. Smith
David Abrahams
Scott A. Smith
Nicodemus
Scott A. Smith
Mike Rovner
David Abrahams
David Abrahams
Hugo van der Merwe
Hugo van der Merwe
David Abrahams
Hugo van der Merwe
David Abrahams

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved