Re: [osg-users] singleton instane

2013-10-18 Thread Ulrich Hertlein
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi Sajjadul,

On 13/10/2013 1:12, Sajjadul Islam wrote:
 Thanks for the suggestion of the singleton design . I tried to design a 
 template base
 class as follows :
 
 Code: ... templateclass T class Singleton { public: /** * Init the actual
 singleton. * Must be called BEFORE the class is used, like this: */ static 
 void
 init() { assert( singletonClass_.get() == NULL); singletonClass_ = new T; }
 
 
 /** * Get Pointer of the actual class * @return Pointer of the actual class 
 */ static
 T* getPtr() { assert( singletonClass_.get() != NULL); return
 singletonClass_.release(); } ... But it crashes at the assertion. I tried 
 without
 assertion , then debugger simply exits at the point where i call: ... 
 osgOpenCL::Context *cxt = osgOpenCL::SingletonosgOpenCL::Context::getPtr();

Did you call 'init' on the singleton class before this?

Also, the 'getPtr' is buggy, since it calls 'release' on the singleton object.  
This
will work the first time round, but delete the object afterwards and will crash 
on
subsequent calls.

Cheers,
/ulrich

- -- 
Fingerprint 0227 8EE1 2C64 8EF4 DA11 9864 FF16 0114 B9DA 3318
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.14 (Darwin)

iQEcBAEBAgAGBQJSYQ9qAAoJEP8WARS52jMYWY8H+wR8F/mt7t6DWYbBr7TVwTmA
/w1UO9B+VtV2ZOlX/E/Z3U6ooKZu4K/jKYU0gihqOCUVNVmops3BILr9AGRStgu6
e6Z/rSxYB7zQRjJl3D25gfg9Y+s+/oe2668Yyq8XQmZ6FABbSVTHnNEXRnwQO+XB
n2po44cQ/i+O5pIbACWr3PgDvejcIxf/AvJoIArz+R5j3jZUC/4ig6k6o6tFn8Uo
esV+DXZg80nGTx8fus+piqSEENUNmzm/2/POBFNxKuXLjX9s11lsHv0K4FtOi+ap
8Z4fshX9Mw79LElGH1hf3qbj61/weyOw9lZarRh1BFaWYTsmoNVcwYQH78PfNXY=
=2pYO
-END PGP SIGNATURE-
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] singleton instane

2013-10-12 Thread Sajjadul Islam
Hi David,

Thanks for the suggestion of the singleton design . I tried to design a 
template base class as follows :


Code:

namespace osgOpenCL
{

/**
   This class helps to build the singleton design pattern.
   Here you have full control over construction and deconstruction
   of the object.
*/
   templateclass T
   class Singleton
   {
   public:
  /**
   * Init the actual singleton.
   * Must be called BEFORE the class is used, like this:
   */
  static void init()
  {
 assert( singletonClass_.get() == NULL);
 singletonClass_ = new T;
  }


  /**
   * Get Pointer of the actual class
   * @return Pointer of the actual class
   */
  static T* getPtr()
  {
 assert( singletonClass_.get() != NULL);
 return singletonClass_.release();
  }

  /**
   * Get reference of the actual class
   * @return reference of the actual class
   */
  static T getRef()
  {
 assert( singletonClass_.get() != NULL );
 return *singletonClass_;
  }




  /**
   * Has the actual class been inited?
   */
  static bool isInited()
  {
 return (singletonClass_.valid());
  }

   private:
  static osg::ref_ptrT singletonClass_;
   };

/// init static pointers with 0
   templateclass T
   osg::ref_ptrT SingletonT::singletonClass_ = 0;

} // namespace

#endif // TGT_SINGLETON_H




But it crashes at the assertion. I tried without assertion , then debugger 
simply exits at the point where i call:


Code:

  //pull out the  singleton context
  //get the singleton context - which will be widely used in the rest of 
this function definition
  osgOpenCL::Context *cxt = 
osgOpenCL::SingletonosgOpenCL::Context::getPtr();






Any idea how to get around these issues ?

Thanks,
Sajjadul

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=56762#56762





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


[osg-users] singleton instane

2013-04-26 Thread Sajjadul Islam
Hello forum,

I have a class declaration and i want to create a singleton instance to it. 

Is there any utility class inside OSG that enables something like this?

Thank you!

Cheers,
Sajjadul

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=53820#53820





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] singleton instane

2013-04-26 Thread Robert Osfield
Hi Sajjadul,

There isn't any general signleton template or base class in the OSG, but
it's doesn't take much code to implement so when we use singleton's they
tend to get implemented locally in the code via a static access method.
Getting singleton's to work cross platform with thread safety on
initialization can be a bit tricky though so sometimes you have to jump
through hoops to get it to work in all instances.

Robert.


On 26 April 2013 09:53, Sajjadul Islam dosto.wa...@gmail.com wrote:

 Hello forum,

 I have a class declaration and i want to create a singleton instance to it.

 Is there any utility class inside OSG that enables something like this?

 Thank you!

 Cheers,
 Sajjadul

 --
 Read this topic online here:
 http://forum.openscenegraph.org/viewtopic.php?p=53820#53820





 ___
 osg-users mailing list
 osg-users@lists.openscenegraph.org
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] singleton instane

2013-04-26 Thread David Callu
Hi Sajjadul,

There are nothing in OSG to do this, but make a class a singleton is really
straightforward.

- make constructor protected or private,
- add a static getInstance function

MyClass  getInstance()
{
static MyClass * myClass = new MyClass();
return *myClass;
}

then use it in your code

MyClass::getInstance().doStuff();

alternatively, you can google 'boost+singleton' to see some examples of
implementation

HTH
David


2013/4/26 Sajjadul Islam dosto.wa...@gmail.com

 Hello forum,

 I have a class declaration and i want to create a singleton instance to it.

 Is there any utility class inside OSG that enables something like this?

 Thank you!

 Cheers,
 Sajjadul

 --
 Read this topic online here:
 http://forum.openscenegraph.org/viewtopic.php?p=53820#53820





 ___
 osg-users mailing list
 osg-users@lists.openscenegraph.org
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] singleton instane

2013-04-26 Thread David Callu
oups beter like this, or with any other smart pointer

MyClass  getInstance()
{
static osg::ref_ptrMyClass myClass( new MyClass() );
return *myClass;
}



2013/4/26 David Callu led...@gmail.com

 Hi Sajjadul,

 There are nothing in OSG to do this, but make a class a singleton is
 really straightforward.

 - make constructor protected or private,
 - add a static getInstance function

 MyClass  getInstance()
 {
 static MyClass * myClass = new MyClass();
 return *myClass;
 }

 then use it in your code

 MyClass::getInstance().doStuff();

 alternatively, you can google 'boost+singleton' to see some examples of
 implementation

 HTH
 David



 2013/4/26 Sajjadul Islam dosto.wa...@gmail.com

 Hello forum,

 I have a class declaration and i want to create a singleton instance to
 it.

 Is there any utility class inside OSG that enables something like this?

 Thank you!

 Cheers,
 Sajjadul

 --
 Read this topic online here:
 http://forum.openscenegraph.org/viewtopic.php?p=53820#53820





 ___
 osg-users mailing list
 osg-users@lists.openscenegraph.org
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org



___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] singleton instane

2013-04-26 Thread Jan Ciger
On Fri, Apr 26, 2013 at 11:18 AM, David Callu led...@gmail.com wrote:

 oups beter like this, or with any other smart pointer

 MyClass  getInstance()
 {
 static osg::ref_ptrMyClass myClass( new MyClass() );
 return *myClass;

 }


Do not forget to make all constructors private, though, otherwise you may
get a surprise when someone tries to construct your object using new
operator or define an STL vector of them ...

Regards,

Jan
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] singleton instane

2013-04-26 Thread Jan Ciger

On 04/26/2013 11:18 AM, David Callu wrote:

oups beter like this, or with any other smart pointer

MyClass  getInstance()
{
 static osg::ref_ptrMyClass myClass( new MyClass() );
 return *myClass;
}



I wonder, why would you want a reference counting smart pointer for a 
singleton? There will be only a single instance, no matter what you do, 
so the smart pointer is useless.


Regards,

Jan
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] singleton instane

2013-04-26 Thread David Callu
Hi Jan,

This is not for reference counting but to not forget to delete the instance
when program end.

Regards
David

2013/4/26 Jan Ciger jan.ci...@gmail.com

 On 04/26/2013 11:18 AM, David Callu wrote:

 oups beter like this, or with any other smart pointer

 MyClass  getInstance()
 {
  static osg::ref_ptrMyClass myClass( new MyClass() );
  return *myClass;
 }


 I wonder, why would you want a reference counting smart pointer for a
 singleton? There will be only a single instance, no matter what you do, so
 the smart pointer is useless.

 Regards,

 Jan

 __**_
 osg-users mailing list
 osg-users@lists.**openscenegraph.org osg-users@lists.openscenegraph.org
 http://lists.openscenegraph.**org/listinfo.cgi/osg-users-**
 openscenegraph.orghttp://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org