]> Creatis software - bbtk.git/blobdiff - packages/std/src/bbstdMagicBox.h
MagicBox : support of data synchronization + various related fixes
[bbtk.git] / packages / std / src / bbstdMagicBox.h
index 3b084b35cfb6ee4794222ba7bc1a1a884b33e5a2..de0e443e99d9108a7864e5898e943ad32142de56 100644 (file)
@@ -1,37 +1,92 @@
+/*=========================================================================                                                                               
+  Program:   bbtk
+  Module:    $RCSfile: bbstdMagicBox.h,v $
+  Language:  C++
+  Date:      $Date: 2009/03/30 14:42:23 $
+  Version:   $Revision: 1.11 $
+=========================================================================*/
+
+/* ---------------------------------------------------------------------
+
+* 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.
+* ------------------------------------------------------------------------ */                                                                         
+
+
 #ifndef __bbstdMagicBox_h_INCLUDED_H__
 #define __bbstdMagicBox_h_INCLUDED_H__
 
-#include "bbtkUserBlackBox.h"
+#include "bbtkAtomicBlackBox.h"
+#include "bbstd_EXPORT.h"
 
 namespace bbstd
 {
+#define BBTK_MB_DECLARE_INPUT(NAME,TYPE)                               \
+  protected:                                                           \
+  TYPE bbmInput##NAME;                                                 \
+public:                                                                        \
+  TYPE bbGetInput##NAME ()                                             \
+  { return bbmInput##NAME; }                                           \
+    void bbSetInput##NAME (TYPE d)                                     \
+    { bbmInput##NAME = d;                                              \
+      if (mCanSet) { bbSetOutputOut(d); mCanSet = false; } }                           
+
   //==================================================================
-  class MagicBox
+  class bbstd_EXPORT MagicBox
     : 
-    public bbtk::UserBlackBox
+    public bbtk::AtomicBlackBox
   {
-    BBTK_USER_BLACK_BOX_INTERFACE(MagicBox,bbtk::UserBlackBox);
-       BBTK_DECLARE_INPUT(In,bbtk::Data);
-       BBTK_DECLARE_OUTPUT(Out,bbtk::Data);
+    BBTK_BLACK_BOX_INTERFACE(MagicBox,bbtk::AtomicBlackBox);
+    BBTK_MB_DECLARE_INPUT(In,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In1,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In2,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In3,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In4,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In5,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In6,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In7,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In8,bbtk::Data);
+    BBTK_MB_DECLARE_INPUT(In9,bbtk::Data);
+    BBTK_DECLARE_OUTPUT(Out,bbtk::Data);
     BBTK_PROCESS(DoProcess);
-    void DoProcess() { bbSetOutputOut( bbGetInputIn() ); }
-    
+    void DoProcess();
   protected:
     //    virtual void bbUserConstructor();
-    
-  };
+    virtual void bbSetStatusAndPropagate(bbtk::BlackBoxInputConnector* c,
+                                        bbtk::IOStatus s);
+    bool mCanSet;
+    virtual void bbUserConstructor() { mCanSet = true; }
+
+ };
   //==================================================================
   
-  
+#undef BBTK_MB_DECLARE_INPUT  
+
   //==================================================================
   // We have to create a particular SetFunctor for MagicBox because
   // its input is of type bbtk::Data (i.e. any) and :
   // 1) an any cannot store an any (construction with an any invokes the copy constr.)
   // 2) we cannot invoke the Set method with the content of the any because 
   //   it expects an any 
-  // hence the method Set **MUST NOT** extract the content of the Data 
-  // prior to invoking the set method
-  class MagicBoxSetFunctor : public bbtk::UserBlackBoxSetFunctor
+  // hence the Set method of the functor **MUST NOT** extract the 
+  // content of the Data prior to invoking the set method of the box
+  class MagicBoxSetFunctor : public bbtk::AtomicBlackBoxSetFunctor
   {
   public:
     /// Type of pointer on a UBB::Set method  
@@ -39,13 +94,16 @@ namespace bbstd
     
     /// Construction with the pointer on the Set method
     MagicBoxSetFunctor(SetMethodPointerType s) :
-      mSetMethodPointer(s) 
-    {}
+       mSetMethodPointer(s) 
+       {
+       }
     
     /// Concrete application of the Set method of object o
-    void Set(bbtk::UserBlackBox* o, const bbtk::Data& d)
+    void Set(bbtk::AtomicBlackBox* o, const bbtk::Data& d)
     { 
-      bbtkDebugMessage("Data",9,"MagicBoxSetfunctor::Set()"<<std::endl);
+      bbtkDebugMessage("data",9,"MagicBoxSetfunctor::Set("<<
+                      bbtk::HumanTypeName(d.type())<<
+                      ")"<<std::endl);
       (((MagicBox*)o)->*mSetMethodPointer)(d);
     }
     
@@ -54,34 +112,58 @@ namespace bbstd
     std::string GetTypeName() const { return bbtk::TypeName<bbtk::Data>(); }
     std::string GetHumanTypeName() const { return bbtk::HumanTypeName<bbtk::Data>(); }
     bool IsPointerType() const { return false; }
-    void BruteForceSetPointer(bbtk::UserBlackBox*, void*) {}
+    void BruteForceSetPointer(bbtk::AtomicBlackBox*, void*) {}
   private:
     ///  Pointer on the Set method  
     SetMethodPointerType mSetMethodPointer;
   };
   //===========================================================================
+  
+#define MAGIC_BOX_INPUT(NAME)                                          \
+  AddInputDescriptor                                                   \
+  (new bbtk::AtomicBlackBoxInputDescriptor                             \
+   (typeid(MagicBoxDescriptor),                                                \
+    #NAME,"Input data","",                                             \
+    new bbtk::AtomicBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>        \
+    (&MagicBox::bbGetInput ## NAME),                                   \
+    new MagicBoxSetFunctor (&MagicBox::bbSetInput ## NAME) ) );                
 
-
-  BBTK_BEGIN_DESCRIBE_BLACK_BOX(MagicBox,bbtk::UserBlackBox);
+  //===========================================================================
+  BBTK_BEGIN_DESCRIBE_BLACK_BOX(MagicBox,bbtk::AtomicBlackBox);
   BBTK_NAME("MagicBox");
   BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
-  BBTK_CATEGORY("adaptor");
-  BBTK_DESCRIPTION("MagicBox");
-  AddInputDescriptor
-  (new bbtk::UserBlackBoxInputDescriptor
+  BBTK_CATEGORY("misc");
+  BBTK_DESCRIPTION("Takes *any kind* of data and copies it to its output. Is a magic box as any box output can be plugged into it and its output can be plugged into any other box input (dynamic type checking, see below), hence it can be put between **any** two boxes. Type matching between its output and the input of the box(es) to which it is connected is made at *run-time*. The pipeline will be executed if the data types : i) match exactly ii) can be transformed by an adaptor iii) are related pointers, i.e. if the output pointer can be upcasted (static_cast) or downcasted (dynamic_cast) to an input type pointer (see the bbtk::any output connection rule in the guide for details). Important uses of the MagicBox are : 1) performing run-time pointer cast, either upward or backward an object hierarchy 2) perform data adaptation (adaptor creation) at run-time vs. pipeline creation time. Any has now 9 more inputs and the value of the output is the value of the lower index input which has changed. This functionality is used to implement a memory which can be written by different boxes. The current value of the memory (the output) is that of the last input which has changed, i.e. has 'written' the memory. It is used for example for synchronization issues between boxes.");
+  MAGIC_BOX_INPUT(In)  
+  MAGIC_BOX_INPUT(In1) 
+  MAGIC_BOX_INPUT(In2) 
+  MAGIC_BOX_INPUT(In3) 
+  MAGIC_BOX_INPUT(In4) 
+  MAGIC_BOX_INPUT(In5) 
+  MAGIC_BOX_INPUT(In6) 
+  MAGIC_BOX_INPUT(In7) 
+  MAGIC_BOX_INPUT(In8) 
+  MAGIC_BOX_INPUT(In9) 
+
+ /*
+ AddInputDescriptor
+  (new bbtk::AtomicBlackBoxInputDescriptor
    (typeid(MagicBoxDescriptor),
-    "In","Input data",
-    new bbtk::UserBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>
+    "In","Input data","",
+    new bbtk::AtomicBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>
     (&MagicBox::bbGetInputIn),
     new MagicBoxSetFunctor (&MagicBox::bbSetInputIn) ) );
+ */
   AddOutputDescriptor
-  (new bbtk::UserBlackBoxOutputDescriptor
+  (new bbtk::AtomicBlackBoxOutputDescriptor
    (typeid(MagicBoxDescriptor),
-    "Out","Output data",
-    new bbtk::UserBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>
+    "Out","Output data","",
+    new bbtk::AtomicBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>
     (&MagicBox::bbGetOutputOut),
     new MagicBoxSetFunctor (&MagicBox::bbSetOutputOut) ) );
   BBTK_END_DESCRIBE_BLACK_BOX(MagicBox);
+  //===========================================================================
+
 }
 // namespace bbstd