]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkBlackBox.h
Try to display planes (from Image Planes)
[bbtk.git] / kernel / src / bbtkBlackBox.h
index f160dd574031f98133aa0cca4b583028f0b02b2b..2082d77676c6e57a072f13e5f717d59ced7da307 100644 (file)
@@ -1,21 +1,34 @@
-/*=========================================================================
-                                                                                
+/*=========================================================================                                                                               
   Program:   bbtk
   Module:    $RCSfile: bbtkBlackBox.h,v $
   Language:  C++
-  Date:      $Date: 2008/04/09 11:16:57 $
-  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
-  http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
-                                                                                
-     This software is distributed WITHOUT ANY WARRANTY; without even
-     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-     PURPOSE.  See the above copyright notices for more information.
-                                                                                
+  Date:      $Date: 2008/12/03 09:34:37 $
+  Version:   $Revision: 1.15 $
 =========================================================================*/
 
+/* ---------------------------------------------------------------------
+
+* Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
+* Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
+*
+*  This software is governed by the CeCILL-B license under French law and 
+*  abiding by the rules of distribution of free software. You can  use, 
+*  modify and/ or redistribute the software under the terms of the CeCILL-B 
+*  license as circulated by CEA, CNRS and INRIA at the following URL 
+*  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
+*  or in the file LICENSE.txt.
+*
+*  As a counterpart to the access to the source code and  rights to copy,
+*  modify and redistribute granted by the license, users are provided only
+*  with a limited warranty  and the software's author,  the holder of the
+*  economic rights,  and the successive licensors  have only  limited
+*  liability. 
+*
+*  The fact that you are presently reading this means that you have had
+*  knowledge of the CeCILL-B license and that you accept its terms.
+* ------------------------------------------------------------------------ */                                                                         
+
+
 
 /**
  *  \file 
@@ -32,7 +45,6 @@
 
 #include "bbtkSystem.h"
 #include "bbtkBlackBoxDescriptor.h"
-//#include "bbtkConnection.h"
 #include "bbtkBlackBoxInputConnector.h"
 #include "bbtkBlackBoxOutputConnector.h"
 #include <set>
@@ -44,18 +56,16 @@ namespace bbtk
   struct Void { Void(int = 0) {} };
   
   class Factory;
+  class Connection;
 
-  class BBTK_EXPORT BlackBox 
+  class BBTK_EXPORT BlackBox : public Object
   {
-
-
+    BBTK_ABSTRACT_OBJECT_INTERFACE(BlackBox);
   public: 
     //==================================================================
     // INTERFACE
     //==================================================================
-    /// The type of pointer on a BlackBox
-    typedef BlackBox* Pointer;
-
     /// The type of map of output connector pointers
     typedef std::map<std::string, BlackBoxOutputConnector*> 
     OutputConnectorMapType;
@@ -64,13 +74,11 @@ namespace bbtk
     InputConnectorMapType;
 
     /// Returns the pointer on the descriptor of the box
-    virtual BlackBoxDescriptor* bbGetDescriptor() const = 0;
+    virtual BlackBoxDescriptor::Pointer bbGetDescriptor() const = 0;
 
     /// Returns a pointer on a clone of the box with name <name>
-    virtual BlackBox* bbClone(const std::string& name) = 0;
+    virtual BlackBox::Pointer bbClone(const std::string& name) = 0;
 
-    /// Destruction method of a black box
-    virtual void bbDelete();
     /// User overloadable destruction method of a black box
     virtual void bbUserDelete();
 
@@ -86,12 +94,10 @@ namespace bbtk
     virtual std::string bbGetFullName() const;
 
     /// Returns the name with the name of the parent prepended if any
-    std::string bbGetNameWithParent() const;
+    virtual std::string bbGetNameWithParent() const;
     
-    /// Sets the parent of the BlackBox
-    void bbSetParent(BlackBox::Pointer p) { bbmParent = p; }
     /// Returns the parent of the BlackBox, i.e the BlackBox that contains it (0 if none)
-    BlackBox::Pointer bbGetParent() const { return bbmParent; }
+    BlackBox::Pointer bbGetParent() const { return bbmParent.lock(); }
 
 
     /// Main processing method of the box.
@@ -151,15 +157,6 @@ bool reaction = true);
     ///  Sets the data of the output called <name>
     virtual void bbSetOutput( const std::string &name, Data data) = 0;
 
-    /// Connects the input <name> to the connection c
-    virtual void bbConnectInput( const std::string& name, Connection* c);
-    /// Connects the output <name> to the connection c
-    virtual void bbConnectOutput( const std::string& name, Connection* c);
-
-    /// Disconnects the input <name> from the connection c
-    virtual void bbDisconnectInput( const std::string& name, Connection* c);
-    /// Disconnects the output <name> from the connection c
-    virtual void bbDisconnectOutput( const std::string& name, Connection* c);
 
     ///  Returns the input connectors map
     InputConnectorMapType&  bbGetInputConnectorMap() 
@@ -232,7 +229,7 @@ bool reaction = true);
     ///  (uses bbGetInputAsString / bbGetOutputAsString which use adaptors)
     /// If relative_link is true then creates relative hrefs
     virtual void bbWriteDotFileBlackBox(FILE *ff,
-                                       BlackBox *parentblackbox, 
+                                       BlackBox::Pointer parentblackbox, 
                                        int detail, int level, 
                                        bool instanceOrtype,
                                        bool relative_link );
@@ -242,16 +239,20 @@ bool reaction = true);
                                           int detail, int level);
     
      
-    virtual void bbShowRelations(BlackBox *parentblackbox, 
+    virtual void bbShowRelations(BlackBox::Pointer parentblackbox, 
                                 int detail, int level
                                 );
     
     std::string bbGetOutputAsString( const std::string &output ); //,Factory *factory);
     std::string bbGetInputAsString( const std::string &input); //,Factory *factory);
-    virtual BlackBox *bbFindBlackBox(const std::string &blackboxname) { return NULL;}
+    virtual BlackBox::Pointer bbFindBlackBox(const std::string &blackboxname) 
+    { return BlackBox::Pointer();}
 
     virtual void Check(bool recursive = true);
 
+       virtual void bbUserOnShow() { }
+       void bbUserOnShowWidget(std::string nameInput);
+
   protected:
     //==================================================================
     // PROTECTED PART : ACCESSIBLE TO THE BlackBox DEVELOPER 
@@ -260,8 +261,6 @@ bool reaction = true);
     BlackBox(const std::string &name);
     /// Constructor from an existing box (copy) with a new name 
     BlackBox(BlackBox& from, const std::string &name);
-    ///  Destructor
-    virtual ~BlackBox();
     //==================================================================
 
 
@@ -270,6 +269,27 @@ bool reaction = true);
     void bbSetStatus( IOStatus t) { bbmStatus = t; } 
     //==================================================================
     
+  private:
+    friend class Connection;
+    friend class ComplexBlackBox;
+
+    /// Sets the parent of the BlackBox
+    void bbSetParent(BlackBox::Pointer p) { bbmParent = p; }
+    
+
+    /// Connects the input <name> to the connection c
+    virtual void bbConnectInput( const std::string& name, 
+                                Connection* c);
+    /// Connects the output <name> to the connection c
+    virtual void bbConnectOutput( const std::string& name, 
+                                 Connection* c);
+    /// Disconnects the input <name> from the connection c
+    virtual void bbDisconnectInput( const std::string& name, 
+                                   Connection* c);
+    /// Disconnects the output <name> from the connection c
+    virtual void bbDisconnectOutput( const std::string& name, 
+                                    Connection* c);
+
 
  
     /// @name Pipeline processing methods
@@ -294,8 +314,8 @@ bool reaction = true);
     /// - calls bbProcess which is the user callback which does the actual processing
     /// - calls bbUpdateChildren
     /// - calls bbShowWidget which shows the widget associated to the box (if any)
-  public:
-    virtual IOStatus bbBackwardUpdate(Connection* caller) = 0;
+  protected:
+    virtual IOStatus bbBackwardUpdate(Connection::Pointer caller) = 0;
     //==================================================================
 
     //==================================================================
@@ -309,7 +329,7 @@ bool reaction = true);
     /// - calls bbCreateWidget
     /// - calls bbProcess which is the user callback which does the actual processing
     /// - calls bbUpdateChildren which recursively calls bbForwardUpdate on connections attached the "Child" output
-    // virtual void bbForwardUpdate(Connection* caller) = 0;
+    // virtual void bbForwardUpdate(Connection::Pointer caller) = 0;
     //==================================================================
   protected:
     //==================================================================
@@ -322,7 +342,7 @@ bool reaction = true);
     //==================================================================
     /// Updates the pipeline in upstream-downstream direction along the "Child"-"Parent" connections only.
     /// Does nothing here. Overloaded in WxContainerBlackbox
-    //virtual void bbUpdateChildren( Connection* caller ) { }
+    //virtual void bbUpdateChildren( Connection::Pointer caller ) { }
     //==================================================================
 
     //==================================================================
@@ -333,9 +353,10 @@ bool reaction = true);
     /// Shows the window associated to the box 
     /// (called after bbProcess during bbExecute)
     /// Does nothing here but overloaded in WxBlackBox and WxContainerBlackBox
-    virtual void bbShowWindow(Connection* caller) { }
+    virtual void bbShowWindow(Connection::Pointer caller) { }
+
     virtual void bbHideWindow() {}
+    virtual void bbCloseWindow() { }
    //==================================================================
 
     //@}
@@ -351,9 +372,14 @@ bool reaction = true);
     /// which means execute in response to an input change 
     virtual bool bbCanReact() const;
     
-    
-    static void bbGlobalAddToExecutionList( BlackBox* b );
+    /// Returns true iff the box is executing
+    bool bbGetExecuting() const { return bbmExecuting; }
+
+  protected:  
+    static void bbGlobalAddToExecutionList( BlackBox::Pointer b );
     static void bbGlobalProcessExecutionList();
+    /// Sets the bbmExecuting bool returned by bbGetExecuting
+    void bbSetExecuting(bool b) { bbmExecuting = b; }
 
     //==================================================================
   protected:
@@ -366,37 +392,49 @@ bool reaction = true);
     virtual void bbCopyIOValues(BlackBox& from);
     //==================================================================
 
-    bool bbGetExecuting() const { return bbmExecuting; }
-    void bbSetExecuting(bool b) { bbmExecuting = b; }
-
+    // Black box objects have a special deleter 
+    // which must take care of releasing the descriptor 
+    // **AFTER** the box is deleted 
+    // (Releasing it in the destructor may cause dl close and crash)
+    /// Black box deleter 
+    /// 1) Calls the user overloadable bbDelete method
+    /// 2) Releases the box descriptor
+    struct BBTK_EXPORT Deleter : public Object::Deleter
+    { 
+      Deleter();
+      void Delete(Object* p);
+    };
+
+    template <class U>
+    static boost::shared_ptr<U> MakeBlackBoxPointer(U* s, bool lock = false)
+    {
+      return MakePointer(s,BlackBox::Deleter(),lock);
+    }
+
+    virtual void bbDelete() { delete this; }
   private:
     
  
     //==================================================================
     // PRIVATE PART 
-    /// Default constructor is private : derived classes must use the constructor with the BlackBox's name
-    BlackBox() {}
-    BlackBox(const BlackBox&) {}
     /// The status of the box
     IOStatus bbmStatus;
+    /// Is the box executing ?
+    bool bbmExecuting;
     /// The name of the black-box
     std::string bbmName;
     /// The name of the package to which it belongs
     std::string bbmPackageName;
        
-    /// 0 : "Pipeline" mode : bbUpdate() only calls Process if Status == MODIFIED (normal pipeline processing)
+    /// 0 : "Pipeline" mode : bbBackwardUpdate() only calls Process if Status == MODIFIED (normal pipeline processing)
     /// 1 : "Always" mode : bbUpdate() always calls Process  
     /// 2 : "Reactive" mode : bbSetModifiedStatus() calls bbUpdate()
     std::string bbmBoxProcessMode;
 
     /// The parent of the black box in the ComplexBlackBox hierarchy
-    BlackBox::Pointer bbmParent;
+    BlackBox::WeakPointer bbmParent;
     //==================================================================
 
-    //==================================================================
-    // Am I executing (to test pipeline cycles) ?
-    bool bbmExecuting;
-    //==================================================================
 
    //==================================================================
     // ATTRIBUTES