]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkAtomicBlackBoxMacros.h
=== MAJOR RELEASE ====
[bbtk.git] / kernel / src / bbtkAtomicBlackBoxMacros.h
index ed05e79981dca978b3b07c2991ef93f8f92ebb3c..7ff6640249c0f4c5baddb15655bf704a77581c37 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkAtomicBlackBoxMacros.h,v $
   Language:  C++
-  Date:      $Date: 2008/04/09 11:16:56 $
-  Version:   $Revision: 1.4 $
+  Date:      $Date: 2008/04/18 12:59:15 $
+  Version:   $Revision: 1.5 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 //============================================================================
 /// Declares the standard interface of a AtomicBlackBox 
 /// (ctor, New, descriptor related methods)
-#define BBTK_USER_BLACK_BOX_INTERFACE(CLASS,PARENT)                    \
+#define BBTK_BLACK_BOX_INTERFACE_INTERNAL(CLASS,PARENT)                \
+  BBTK_OBJECT_MINIMAL_INTERFACE;                               \
   private:                                                             \
-  inline static void bbCreateDescriptorIfNeeded();                     \
   protected:                                                           \
   CLASS(const std::string& name, bool allocate_connectors = true);     \
-  CLASS(CLASS& from, const std::string& name,                          \
+  CLASS(Self& from, const std::string& name,                           \
        bool allocate_connectors = true);                               \
   ~CLASS();                                                            \
-  public: \
-  inline static CLASS* bbNew(const std::string& name)                  \
-  {                                                                    \
-    bbtkDebugMessageInc("Kernel",9,#CLASS<<"::bbNew(\""<<name<<"\")"<<std::endl); \
-    bbCreateDescriptorIfNeeded();                                      \
-    CLASS* c = new CLASS(name);                                                \
-    c->bbGetDescriptor()->Reference();                                 \
-    bbtkDebugDecTab("Kernel",9);                                       \
-    return c;                                                          \
-  }                                                                    \
-  inline bbtk::BlackBox* bbClone(const std::string& name)              \
-  {                                                                    \
-    bbtkDebugMessageInc("Kernel",9,#CLASS<<"::bbClone(\""<<name<<"\")"<<std::endl); \
-    bbCreateDescriptorIfNeeded();                                      \
-    CLASS* c = new CLASS(*this,name);                                  \
-    bbGetDescriptor()->Reference();                                    \
-    bbtkDebugDecTab("Kernel",9);                                       \
-    return c;                                                          \
-  }                                                                    \
-  bbtk::BlackBoxDescriptor* bbGetDescriptor() const                    \
+  public:                                                              \
+  std::string GetObjectName() const                                    \
+  { return std::string(#CLASS)+std::string(" '")                       \
+      +bbGetNameWithParent()+std::string("'"); }                       \
+  inline static Pointer New(const std::string& name)                   \
   {                                                                    \
-    return (bbtk::BlackBoxDescriptor*)bbDescriptor();                  \
+    bbtkDebugMessage("object",1,"##> "<<#CLASS                         \
+                    <<"::New(\""<<name<<"\")"<<std::endl);             \
+    Pointer p = MakeBlackBoxPointer(new Self(name));                   \
+    bbtkDebugMessage("object",1,"<## "<<#CLASS                         \
+                    <<"::New(\""<<name<<"\")"<<std::endl);             \
+    return p;                                                          \
   }                                                                    \
-  static bbtk::BlackBoxDescriptor* bbDescriptor()                      \
+  inline bbtk::BlackBox::Pointer bbClone(const std::string& name)      \
   {                                                                    \
-    bbCreateDescriptorIfNeeded();                                      \
-    return bbDescriptorPointer();                                      \
+    bbtkDebugMessage("object",1,"##> "<<#CLASS                         \
+                    <<"::bbClone(\""<<name<<"\")"<<std::endl);         \
+    Pointer p = MakeBlackBoxPointer(new Self(*this,name));             \
+    bbtkDebugMessage("object",1,"<## "<<#CLASS                         \
+                    <<"::bbClone(\""<<name<<"\")"<<std::endl);         \
+    return p;                                                          \
   }                                                                    \
+  virtual void bbLockDescriptor();                                     \
   private:                                                             \
   CLASS() : PARENT("") {}                                              \
-  static bbtk::BlackBoxDescriptor*& bbDescriptorPointer()              \
-  {                                                                    \
-    static bbtk::BlackBoxDescriptor* d = 0;                            \
-    return d;                                                          \
-  }                                                                    
+  CLASS(const CLASS&) : PARENT("") {}                                  
+
 //============================================================================
 
+//============================================================================
+#define BBTK_BLACK_BOX_INTERFACE(CLASS,PARENT) \
+  public : typedef CLASS Self;                         \
+  BBTK_BLACK_BOX_INTERFACE_INTERNAL(CLASS,PARENT);
+//============================================================================
 
 //============================================================================
 /// Defines the bbUserProcess method
 
 //============================================================================
 #define BBTK_BEGIN_BLACK_BOX_CONSTRUCTOR(CLASS,ALLOC)                  \
-  bbtkDebugMessageInc("Kernel",7,#CLASS<<"::"<<#CLASS                  \
-                     <<"(\""<<bbGetName()<<"\")"<<std::endl);          \
-  if (ALLOC) bbAllocateConnectors();                                   
+  bbtkDebugMessage("object",2,"==> "<<#CLASS<<"::"<<#CLASS             \
+                  <<"(\""<<bbGetName()<<"\")"<<std::endl);             \
+  if (ALLOC)                                                           \
+    {                                                                  \
+      bbLockDescriptor();                                              \
+      bbAllocateConnectors();                                          \
+    }
+//============================================================================
+
+//============================================================================
+#define BBTK_END_BLACK_BOX_CONSTRUCTOR(CLASS)                          \
+  bbtkDebugMessage("object",2,"<== "<<#CLASS<<"::"<<#CLASS             \
+                  <<"(\""<<bbGetName()<<"\")"<<std::endl);             
 //============================================================================
 
 //============================================================================
 #define BBTK_BEGIN_BLACK_BOX_COPY_CONSTRUCTOR(CLASS,FROM,ALLOC)                \
-  bbtkDebugMessageInc("Kernel",7,#CLASS<<"::"<<#CLASS                  \
+  bbtkDebugMessageInc("object",2,"==> "<<#CLASS<<"::"<<#CLASS          \
                      <<"("<<FROM.bbGetFullName()<<",\""                \
                      <<bbGetName()<<"\")"<<std::endl);                 \
   if (ALLOC)                                                           \
     {                                                                  \
+      bbLockDescriptor();                                              \
       bbAllocateConnectors();                                          \
       bbCopyIOValues(FROM);                                            \
    }
 //============================================================================
 
-
 //============================================================================
-#define BBTK_END_BLACK_BOX_CONSTRUCTOR         \
-  bbtkDebugDecTab("Kernel",7)
+#define BBTK_END_BLACK_BOX_COPY_CONSTRUCTOR(CLASS,FROM)                        \
+  bbtkDebugMessage("object",2,"<== "<<#CLASS<<"::"<<#CLASS             \
+                  <<"("<<FROM.bbGetFullName()<<",\""                   \
+                  <<bbGetName()<<"\")"<<std::endl);            
 //============================================================================
 
 //============================================================================
 #define BBTK_BEGIN_BLACK_BOX_DESTRUCTOR(CLASS)                         \
-  bbtkDebugMessageInc("Kernel",7,#CLASS <<"::~"<< #CLASS                       \
-                     <<"() ["<<this->bbGetFullName()<<"]"<<std::endl);
+  bbtkDebugMessage("object",2,"==> "<<#CLASS <<"::~"<< #CLASS          \
+                  <<"() ["<<this->bbGetFullName()<<"]"<<std::endl);
 //============================================================================
 
-
-
-
 //============================================================================
-#define BBTK_END_BLACK_BOX_DESTRUCTOR          \
-  bbtkDebugDecTab("Kernel",7)
+#define BBTK_END_BLACK_BOX_DESTRUCTOR(CLASS)                           \
+  bbtkDebugMessage("object",2,"<== "<<#CLASS <<"::~"<< #CLASS          \
+                  <<"() ["<<this->bbGetFullName()<<"]"<<std::endl);
+
 //============================================================================
 
 
 //============================================================================
 /// AtomicBlackBox std implementation of ctor and dtor
-#define BBTK_USER_BLACK_BOX_IMPLEMENTATION(CLASS,PARENT)               \
+#define BBTK_BLACK_BOX_IMPLEMENTATION(CLASS,PARENT)            \
   CLASS::CLASS(const std::string& name, bool allocate_connectors)      \
     : PARENT(name,false)                                               \
   {                                                                    \
     BBTK_BEGIN_BLACK_BOX_CONSTRUCTOR(CLASS,allocate_connectors);       \
     CLASS::bbUserConstructor();                                                \
-    BBTK_END_BLACK_BOX_CONSTRUCTOR;                                    \
+    BBTK_END_BLACK_BOX_CONSTRUCTOR(CLASS);                             \
   }                                                                    \
   CLASS::CLASS(CLASS& from,                                            \
               const std::string& name, bool allocate_connectors)       \
   {                                                                    \
     BBTK_BEGIN_BLACK_BOX_COPY_CONSTRUCTOR(CLASS,from,allocate_connectors); \
     CLASS::bbUserCopyConstructor();                                    \
-    BBTK_END_BLACK_BOX_CONSTRUCTOR;                                    \
+    BBTK_END_BLACK_BOX_COPY_CONSTRUCTOR(CLASS,from);                   \
   }                                                                    \
   CLASS::~CLASS()                                                      \
   {                                                                    \
     BBTK_BEGIN_BLACK_BOX_DESTRUCTOR(CLASS);                            \
     CLASS::bbUserDestructor();                                         \
-    BBTK_END_BLACK_BOX_DESTRUCTOR;                                     \
-  }                                                                    
+    BBTK_END_BLACK_BOX_DESTRUCTOR(CLASS);                              \
+  }                                                                    \
+  void CLASS::bbLockDescriptor()                                       \
+  {                                                                    \
+    bbmDescriptorPointer = CLASS ## Descriptor::Instance();            \
+  }
 //============================================================================
 
 
 #define BBTK_BEGIN_DESCRIBE_BLACK_BOX(CLASS,PARENT)                    \
   class /*BBTK_EXPORT*/ CLASS ## Descriptor : public PARENT ## Descriptor \
   {                                                                    \
+  public: typedef CLASS ## Descriptor Self;                            \
+    BBTK_OBJECT_MINIMAL_INTERFACE;                                     \
   public:                                                              \
-    bbtk::BlackBox::Pointer CreateInstance(const std::string& name)    \
+    std::string GetObjectName() const                                  \
+    {                                                                  \
+      return std::string(BBTK_STRINGIFY(CLASS))                                \
+       +std::string("Descriptor '")+GetFullTypeName()                  \
+       +std::string("'");                                              \
+    }                                                                  \
+    size_t GetObjectSize() const { return sizeof(*this); }             \
+    bbtk::BlackBox::Pointer NewBlackBox(const std::string& name)       \
     {                                                                  \
-      return CLASS::bbNew(name);                                       \
+      return CLASS::New(name);                                         \
     }                                                                  \
+    virtual bbtk::AtomicBlackBoxDescriptor::Pointer GetInstance()      const \
+      {                                                                        \
+       return Instance();                                              \
+      }                                                                        \
+    static bbtk::AtomicBlackBoxDescriptor::Pointer Instance()                  \
+      {                                                                        \
+       static bbtk::AtomicBlackBoxDescriptor::WeakPointer i;                   \
+       bbtk::AtomicBlackBoxDescriptor::Pointer j;                              \
+       if (!i.lock()) { j = Self::New(); i = j; }                      \
+       return i.lock();                                                \
+      }                                                                        \
+    static CLASS ## Descriptor::Pointer New()                          \
+      {                                                                        \
+       bbtkDebugMessage("object",1,"##> "<<BBTK_STRINGIFY(CLASS)       \
+                        <<"Descriptor::New" <<std::endl);              \
+       CLASS ## Descriptor::Pointer p =                                \
+         MakePointer(new CLASS ## Descriptor());                       \
+       bbtkDebugMessage("object",1,"<## "<<BBTK_STRINGIFY(CLASS)       \
+                        <<"Descriptor::New" <<std::endl);              \
+       return p;                                                       \
+      }                                                                        \
+  protected:                                                           \
     CLASS ## Descriptor()                                              \
       {                                                                        \
-       bbtkDebugMessageInc("Kernel",9,#CLASS<<"Descriptor::"<<#CLASS   \
-                           <<"Descriptor()"<<std::endl)
+       bbtkDebugMessage("object",2,"==> "<<#CLASS<<"Descriptor::"      \
+                        <<#CLASS<<"Descriptor()"<<std::endl);          
 //============================================================================
 
 //============================================================================
 /// Ends the AtomicBlackBox description block
 #define BBTK_END_DESCRIBE_BLACK_BOX(CLASS)                             \
-  bbtkDecTab("Kernel",9);                                                      \
+  bbtkDebugMessage("object",2,"<== "<<#CLASS<<"Descriptor::"           \
+                  <<#CLASS<<"Descriptor()"<<std::endl);                \
   }                                                                    \
   };                                                                   \
-  void CLASS::bbCreateDescriptorIfNeeded()                             \
-  {                                                                    \
-    if ( !bbDescriptorPointer() )                                      \
-      bbDescriptorPointer() = new CLASS ## Descriptor;                 \
-  }
+  
 //============================================================================
 
 
 
 //============================================================================
 /// Describes a AtomicBlackBox input (to be put inside the UBB description block)
-#define BBTK_INPUT(CLASS,NAME,DESCR,TYPE,NATURE)                               \
+#define BBTK_INPUT(CLASS,NAME,DESCR,TYPE,NATURE)                       \
   AddInputDescriptor(new bbtk::AtomicBlackBoxInputDescriptor           \
                     (typeid(CLASS ## Descriptor),                      \
                      #NAME,DESCR,NATURE,                               \
 //============================================================================
 //============================================================================
 
+//============================================================================
+#define BBTK_TEMPLATE_BLACK_BOX_INTERFACE(CLASS,PARENT,T)      \
+  public : typedef CLASS<T> Self;                              \
+  BBTK_BLACK_BOX_INTERFACE_INTERNAL(CLASS,PARENT);
+//============================================================================
+
 //============================================================================
 /// Begins a template AtomicBlackBox of template param T description block
-#define BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(CLASS)  \
-  template <class T>                                   \
-  class /*BBTK_EXPORT*/ CLASS ## Descriptor : public bbtk::BlackBoxDescriptor \
+#define BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX(CLASS,PARENT)           \
+  template <class T>                                                   \
+  class /*BBTK_EXPORT*/ CLASS ## Descriptor : public PARENT ## Descriptor \
   {                                                                    \
+  public: typedef CLASS ## Descriptor<T> Self;                         \
+    BBTK_OBJECT_MINIMAL_INTERFACE;                                     \
   public:                                                              \
-    bbtk::BlackBox::Pointer CreateInstance(const std::string& name)    \
+    std::string GetObjectName() const                                  \
+    {                                                                  \
+      return std::string(BBTK_STRINGIFY(CLASS))                                \
+       +std::string("Descriptor<")+bbtk::TypeName<T>()                 \
+       +std::string("> '")+GetFullTypeName()                           \
+       +std::string("'");                                              \
+    }                                                                  \
+    static Pointer New()                                               \
+      {                                                                        \
+       bbtkDebugMessage("object",1,"##> "<<BBTK_STRINGIFY(CLASS)       \
+                        <<"Descriptor<"<<bbtk::TypeName<T>()<<">::New" \
+                        <<std::endl);                                  \
+       Pointer p = MakePointer(new Self());                            \
+       bbtkDebugMessage("object",1,"<## "<<BBTK_STRINGIFY(CLASS)       \
+                        <<"Descriptor<"<<bbtk::TypeName<T>()<<">::New" \
+                        <<std::endl);                                  \
+       return p;                                                       \
+      }                                                                        \
+    virtual bbtk::AtomicBlackBoxDescriptor::Pointer GetInstance()      const \
+      {                                                                        \
+       return Instance();                                              \
+      }                                                                        \
+    static bbtk::AtomicBlackBoxDescriptor::Pointer Instance()          \
+      {                                                                        \
+       static bbtk::AtomicBlackBoxDescriptor::WeakPointer i;           \
+       bbtk::AtomicBlackBoxDescriptor::Pointer j;                      \
+       if (!i.lock()) { j = Self::New(); i = j; }                      \
+       return i.lock();                                                \
+      }                                                                        \
+    bbtk::BlackBox::Pointer NewBlackBox(const std::string& name)       \
     {                                                                  \
-      return CLASS<T>::bbNew(name);                                    \
+      return CLASS<T>::New(name);                                      \
     }                                                                  \
     CLASS ## Descriptor()                                              \
       {                                                                        \
-       bbtkDebugMessageInc("Kernel",9,#CLASS<<"Descriptor::"<<#CLASS   \
-                           <<"Descriptor()"<<std::endl)
+       bbtkDebugMessage("object",2,"==> "<<#CLASS<<"Descriptor<"       \
+                        <<bbtk::TypeName<T>()<<">::"                   \
+                        <<#CLASS<<"Descriptor()"<<std::endl);          
+
 //============================================================================
 
 //============================================================================
 /// Ends a template AtomicBlackBox of template param T description block
-#define BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX(CLASS)    \
-  bbtkDecTab("Kernel",9);                                                      \
+#define BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX(CLASS)                    \
+  bbtkDebugMessage("object",2,"<== "<<#CLASS<<"Descriptor<"            \
+                  <<bbtk::TypeName<T>()<<">::"                         \
+                  <<#CLASS<<"Descriptor()"<<std::endl);                \
   }                                                                    \
-  };                                                                   \
-  template <class T>                                   \
-  void CLASS<T>::bbCreateDescriptorIfNeeded()          \
-  {                                                                    \
-    if ( !bbDescriptorPointer() )                                      \
-      bbDescriptorPointer() = new CLASS ## Descriptor<T>;      \
-  }
+  };                                                                   
+
 //============================================================================
 
 //============================================================================
 
 //============================================================================
 /// Template AtomicBlackBox std implementation of ctor and dtor
-#define BBTK_USER_BLACK_BOX_TEMPLATE_IMPLEMENTATION(CLASS,PARENT)      \
+#define BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION(CLASS,PARENT)           \
   template <class T>                                                   \
   CLASS<T>::CLASS(const std::string& name, bool alloc)                 \
     : PARENT(name,false)                                               \
   {                                                                    \
-    BBTK_BEGIN_BLACK_BOX_CONSTRUCTOR(CLASS<T>,alloc);                  \
-    CLASS<T>::bbUserConstructor();                             \
-    BBTK_END_BLACK_BOX_CONSTRUCTOR;                                    \
-  }                            \
+    BBTK_BEGIN_BLACK_BOX_CONSTRUCTOR(CLASS,alloc);                     \
+    CLASS<T>::bbUserConstructor();                                     \
+    BBTK_END_BLACK_BOX_CONSTRUCTOR(CLASS);                             \
+  }                                                                    \
   template <class T>                                                   \
-  CLASS<T>::CLASS(CLASS<T>& from,                                              \
-              const std::string& name, bool allocate_connectors)       \
+  CLASS<T>::CLASS(CLASS<T>& from,                                      \
+                 const std::string& name, bool allocate_connectors)    \
     : PARENT(from,name,false)                                          \
   {                                                                    \
-    BBTK_BEGIN_BLACK_BOX_COPY_CONSTRUCTOR(CLASS<T>,from,allocate_connectors); \
+    BBTK_BEGIN_BLACK_BOX_COPY_CONSTRUCTOR(CLASS,from,allocate_connectors); \
     CLASS<T>::bbUserCopyConstructor();                                 \
-    BBTK_END_BLACK_BOX_CONSTRUCTOR;                                    \
-  }            \
+    BBTK_END_BLACK_BOX_COPY_CONSTRUCTOR(CLASS,from);                   \
+  }                                                                    \
   template <class T>                                                   \
   CLASS<T>::~CLASS()                                                   \
   {                                                                    \
-    BBTK_BEGIN_BLACK_BOX_DESTRUCTOR(CLASS<T>);                         \
+    BBTK_BEGIN_BLACK_BOX_DESTRUCTOR(CLASS);                            \
     CLASS<T>::bbUserDestructor();                                      \
-    BBTK_END_BLACK_BOX_DESTRUCTOR;                                     \
-  }                                                                    
+    BBTK_END_BLACK_BOX_DESTRUCTOR(CLASS);                              \
+  }                                                                    \
+  template <class T>                                                   \
+  void CLASS<T>::bbLockDescriptor()                                    \
+  {                                                                    \
+    bbmDescriptorPointer = CLASS ## Descriptor<T>::Instance();         \
+  }
 //============================================================================
 
+
+
 //============================================================================
 // Two template params user black boxes macros
+//============================================================================
+
+//============================================================================
+#define BBTK_TEMPLATE2_BLACK_BOX_INTERFACE(CLASS,PARENT,T1,T2) \
+  public : typedef CLASS<T1,T2> Self;                                  \
+  BBTK_BLACK_BOX_INTERFACE_INTERNAL(CLASS,PARENT);
+//===========================================================================
 
+//============================================================================
 /// Begins a template AtomicBlackBox description block of template param T1 and T2 
-#define BBTK_BEGIN_DESCRIBE_TEMPLATE2_BLACK_BOX(CLASS)         \
+#define BBTK_BEGIN_DESCRIBE_TEMPLATE2_BLACK_BOX(CLASS,PARENT)          \
   template <class T1, class T2>                                                \
-  class /*BBTK_EXPORT*/ CLASS ## Descriptor : public bbtk::BlackBoxDescriptor \
+  class /*BBTK_EXPORT*/ CLASS ## Descriptor : public PARENT ## Descriptor \
   {                                                                    \
+  public: typedef CLASS ## Descriptor<T1,T2> Self;                     \
+    BBTK_OBJECT_MINIMAL_INTERFACE;                                     \
   public:                                                              \
-    bbtk::BlackBox::Pointer CreateInstance(const std::string& name)    \
+    std::string GetObjectName() const                                  \
+    {                                                                  \
+      return std::string(BBTK_STRINGIFY(CLASS))                                \
+       +std::string("Descriptor<")+bbtk::TypeName<T1>()                \
+       +std::string(",")+bbtk::TypeName<T2>()                          \
+       +std::string("> '")+GetFullTypeName()                           \
+       +std::string("'");                                              \
+    }                                                                  \
+    static Pointer New()                                               \
+      {                                                                        \
+       bbtkDebugMessage("object",1,"##> "<<BBTK_STRINGIFY(CLASS)       \
+                        <<"Descriptor<"<<bbtk::TypeName<T1>()<<","     \
+                        <<bbtk::TypeName<T2>()<<">::New"<<std::endl);  \
+       Pointer p = MakePointer(new Self());                            \
+       bbtkDebugMessage("object",1,"<## "<<BBTK_STRINGIFY(CLASS)       \
+                        <<"Descriptor<"<<bbtk::TypeName<T1>()<<","     \
+                        <<bbtk::TypeName<T2>()<<">::New"<<std::endl);  \
+       return p;                                                       \
+      }                                                                        \
+    virtual bbtk::AtomicBlackBoxDescriptor::Pointer GetInstance() const \
+      {                                                                        \
+       return Instance();                                              \
+      }                                                                        \
+    static bbtk::AtomicBlackBoxDescriptor::Pointer Instance()          \
+      {                                                                        \
+       static bbtk::AtomicBlackBoxDescriptor::WeakPointer i;           \
+       bbtk::AtomicBlackBoxDescriptor::Pointer j;                      \
+       if (!i.lock()) { j = Self::New(); i = j; }                      \
+       return i.lock();                                                \
+      }                                                                        \
+    bbtk::BlackBox::Pointer NewBlackBox(const std::string& name)       \
     {                                                                  \
-      return CLASS<T1,T2>::bbNew(name);                                        \
+      return CLASS<T1,T2>::New(name);                                  \
     }                                                                  \
     CLASS ## Descriptor()                                              \
       {                                                                        \
-      bbtkDebugMessageInc("Kernel",9,#CLASS<<"Descriptor::"<<#CLASS    \
-                         <<"Descriptor()"<<std::endl)
+       bbtkDebugMessage("object",2,"==> "<<#CLASS<<"Descriptor<"       \
+                        <<bbtk::TypeName<T1>()<<","                    \
+                        <<bbtk::TypeName<T2>()<<">::"                  \
+                        <<#CLASS<<"Descriptor()"<<std::endl);          
 //============================================================================
 
 //============================================================================
 /// Ends a template AtomicBlackBox description block of template param T1 and T2
-#define BBTK_END_DESCRIBE_TEMPLATE2_BLACK_BOX(CLASS)           \
-  bbtkDecTab("Kernel",9);                                                      \
+#define BBTK_END_DESCRIBE_TEMPLATE2_BLACK_BOX(CLASS)                   \
+  bbtkDebugMessage("object",2,"<== "<<#CLASS<<"Descriptor<"            \
+                  <<bbtk::TypeName<T1>()<<","                          \
+                  <<bbtk::TypeName<T2>()<<">::"                        \
+                  <<#CLASS<<"Descriptor()"<<std::endl);                \
   }                                                                    \
-  };                                                                   \
-  template <class T1, class T2>                                                \
-  void CLASS<T1,T2>::bbCreateDescriptorIfNeeded()                      \
-  {                                                                    \
-    if ( !bbDescriptorPointer() )                                      \
-      bbDescriptorPointer() = new CLASS ## Descriptor<T1,T2>;          \
-  }
+  };                                                                   
+
 //============================================================================
 
+
+
+/*
 //============================================================================
 // Two template params user black boxes macros
 
 /// Begins a template AtomicBlackBox description block of template param T1 and T2 
 #define BBTK_BEGIN_DESCRIBE_TEMPLATE2_WITH_TYPES_BLACK_BOX(CLASS,TYPE1,TYPE2)          \
   template <TYPE1 T1, TYPE2 T2>                                                \
-  class /*BBTK_EXPORT*/ CLASS ## Descriptor : public bbtk::BlackBoxDescriptor \
+  class  CLASS ## Descriptor : public bbtk::BlackBoxDescriptor         \
   {                                                                    \
   public:                                                              \
-    bbtk::BlackBox::Pointer CreateInstance(const std::string& name)    \
+    virtual bbtk:AtomicBlackBoxDescriptor::Pointer GetInstance()       const   \
+      {                                                                        \
+       return Instance();                                              \
+      }                                                                        \
+    static bbtk:AtomicBlackBoxDescriptor::Pointer Instance()                   \
+      {                                                                        \
+       static bbtk:AtomicBlackBoxDescriptor::WeakPointer i;                    \
+       bbtk:AtomicBlackBoxDescriptor::Pointer j;                               \
+       if (!i.lock()) { j = Self::New(); i = j; }                      \
+       return i.lock();                                                \
+      }                                                                        \
+    bbtk::BlackBox::Pointer NewBlackBox(const std::string& name)       \
     {                                                                  \
       return new CLASS<T1,T2>(name);                                   \
     }                                                                  \
 #define BBTK_END_DESCRIBE_TEMPLATE2_WITH_TYPES_BLACK_BOX(CLASS,TYPE1,TYPE2)    \
   bbtkDecTab("Kernel",9);                                                      \
   }                                                                    \
-  };                                                                   \
-  template <TYPE1 T1, TYPE2 T2>                                                \
-  void CLASS<T1,T2>::bbCreateDescriptorIfNeeded()                      \
-  {                                                                    \
-    if ( !bbDescriptorPointer() )                                      \
-      bbDescriptorPointer() = new CLASS ## Descriptor<T1,T2>;          \
-  }
-//============================================================================
+  };                                                                   
 
+//============================================================================
 
+*/
 
 //============================================================================
 /// Describes a 2 template params AtomicBlackBox input (to be put inside the UBB description block)
 
 //============================================================================
 /// Template AtomicBlackBox std implementation of ctor and dtor
-#define BBTK_USER_BLACK_BOX_TEMPLATE2_IMPLEMENTATION(CLASS,PARENT)     \
+#define BBTK_BLACK_BOX_TEMPLATE2_IMPLEMENTATION(CLASS,PARENT)          \
   template <class T1, class T2>                                                \
   CLASS<T1,T2>::CLASS(const std::string& name, bool alloc)             \
     : PARENT(name,false)                                               \
   {                                                                    \
     BBTK_BEGIN_BLACK_BOX_CONSTRUCTOR(CLASS,alloc);                     \
     CLASS<T1,T2>::bbUserConstructor();                                 \
-    BBTK_END_BLACK_BOX_CONSTRUCTOR;                                    \
+    BBTK_END_BLACK_BOX_CONSTRUCTOR(CLASS);                             \
   }                                                                    \
-  template <class T1, class T2>                                                        \
-  CLASS<T1,T2>::CLASS(CLASS<T1,T2>& from,                                              \
-              const std::string& name, bool allocate_connectors)       \
+  template <class T1, class T2>                                                \
+  CLASS<T1,T2>::CLASS(CLASS<T1,T2>& from,                              \
+                     const std::string& name, bool allocate_connectors) \
     : PARENT(from,name,false)                                          \
   {                                                                    \
     BBTK_BEGIN_BLACK_BOX_COPY_CONSTRUCTOR(CLASS,from,allocate_connectors); \
     CLASS<T1,T2>::bbUserCopyConstructor();                             \
-    BBTK_END_BLACK_BOX_CONSTRUCTOR;                                    \
+    BBTK_END_BLACK_BOX_COPY_CONSTRUCTOR(CLASS,from);                   \
   }                                                                    \
   template <class T1, class T2>                                                \
   CLASS<T1,T2>::~CLASS()                                               \
   {                                                                    \
     BBTK_BEGIN_BLACK_BOX_DESTRUCTOR(CLASS);                            \
     CLASS<T1,T2>::bbUserDestructor();                                  \
-    BBTK_END_BLACK_BOX_DESTRUCTOR;                                     \
-  }                                                                    
+    BBTK_END_BLACK_BOX_DESTRUCTOR(CLASS);                              \
+  }                                                                    \
+  template <class T1, class T2>                                                \
+  void CLASS<T1,T2>::bbLockDescriptor()                                        \
+  {                                                                    \
+    bbmDescriptorPointer = CLASS ## Descriptor<T1,T2>::Instance();     \
+  }
 //============================================================================
 
 
+/*
 //============================================================================
 /// Template AtomicBlackBox std implementation of ctor and dtor
-#define BBTK_USER_BLACK_BOX_TEMPLATE2_WITH_TYPES_IMPLEMENTATION(CLASS,PARENT,TYPE1,TYPE2) \
+#define BBTK_BLACK_BOX_TEMPLATE2_WITH_TYPES_IMPLEMENTATION(CLASS,PARENT,TYPE1,TYPE2) \
   template <TYPE1 T1, TYPE2 T2>                                                \
   CLASS<T1,T2>::CLASS(const std::string& name, bool alloc)             \
     : PARENT(name,false)                                               \
     BBTK_BEGIN_BLACK_BOX_DESTRUCTOR(CLASS);                            \
     this->bbUserDestructor();                                          \
     BBTK_END_BLACK_BOX_DESTRUCTOR;                                     \
-  }                                                                    
+  }                                                                    \
+  template <class T1, class T2>                                                \
+  void CLASS<T1,T2>::bbLockDescriptor()                                        \
+  {                                                                    \
+    bbmDescriptorPointer = CLASS ## Descriptor<T1,T2>::Instance();     \
+  }
 //============================================================================
 
-
+*/