gatb.core-API-0.0.0
ISmartPointer.hpp File Reference

Smart Pointer Design Pattern interface. More...

Classes

class  ISmartPointer
 Tool for managing instances life cycle. More...
 
class  SmartPointer
 Implementation of the ISmartPointer interface. More...
 
class  LocalObject
 Local usage of SmartPointer instance. More...
 

Namespaces

 gatb::core
 Core package of the GATP project.
 
 gatb::core::system
 Operating System abstraction layer.
 

Macros

#define LOCAL(object)   gatb::core::system::LocalObject __##object (object)
 
#define SP_SETATTR(a)
 

Detailed Description

Smart Pointer Design Pattern interface.

Date
01/03/2013
Author
edrezen Define tools for easing life cycle of objects. Also known as smart pointer.

Macro Definition Documentation

#define LOCAL (   object)    gatb::core::system::LocalObject __##object (object)

Macro that creates an instance of type LocalObject whose name is the prefix '__' followed by the provided argument.

Sample:

1 void foo ()
2 {
3  {
4  // we create an instance of a class that inherits from SmartPointer
5  MyClass* object = new MyClass ();
6 
7  // we want that this object lives only inside the including statements block.
8  // note that we use the LOCAL macro
9  LOCAL (object);
10  }
11 
12  // Here, the object should have been automatically deleted.
13 }
See also
LocalObject
#define SP_SETATTR (   a)
Value:
{ \
if (_##a == a) { return; } /* just to be sure that we don't reuse the same object */ \
if (_##a != 0) { _##a->forget (); } \
_##a = a; \
if (_##a != 0) { _##a->use (); } \
}

Macro that generates an instructions block that manages life cycle of a class attributes. It is dedicated to simply write clever setter methods.

As a convention, the attribute name must begin by an underscore. Then the provided argument here is the attribute name without this leading underscore.

A typical use of this macro is the following:

  • in the constructor, use the default initialization of the attribute as 0
  • in the constructor body, use the smart setter with a provided argument
  • in the destructor body, use the smart setter with null argument.
  • in the private (or protected) part, defines a smart setter for the attribute by using the SP_SETATTR macro

Sample of code:

1 class MyClass
2 {
3 public:
4  MyClass (SomeSmartPointerClass* ptr) : _ptr(0) { setPtr (ptr); }
5  ~MyClass ()  { setPtr (0); }
6 private:
7  SomeSmartPointerClass* _ptr;
8  void setPtr (SomeSmartPointerClass* ptr) { SP_SETATTR(ptr); }
9 };
See also
SmartPointer