]> Creatis software - bbtk.git/blob - packages/std/src/bbstdMagicBox.h
99d27e872937bd25399a12388379f56eb1ead0e5
[bbtk.git] / packages / std / src / bbstdMagicBox.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbstdMagicBox.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:51:32 $
33   Version:   $Revision: 1.14 $
34 =========================================================================*/
35
36
37 #ifndef __bbstdMagicBox_h_INCLUDED_H__
38 #define __bbstdMagicBox_h_INCLUDED_H__
39
40 #include "bbtkAtomicBlackBox.h"
41 #include "bbstd_EXPORT.h"
42
43 namespace bbstd
44 {
45
46   //==================================================================
47   class bbstd_EXPORT MagicBox
48     : 
49     public bbtk::AtomicBlackBox
50   {
51     BBTK_BLACK_BOX_INTERFACE(MagicBox,bbtk::AtomicBlackBox);
52     BBTK_DECLARE_INPUT(In,bbtk::Data);
53     BBTK_DECLARE_INPUT(Active,bool);
54     BBTK_DECLARE_OUTPUT(Out,bbtk::Data);
55     BBTK_PROCESS(DoProcess);
56     void DoProcess();
57
58     virtual void bbComputePostProcessStatus();
59  };
60   //==================================================================
61   
62
63   //==================================================================
64   // We have to create a particular SetFunctor for MagicBox because
65   // its input is of type bbtk::Data (i.e. any) and :
66   // 1) an any cannot store an any (construction with an any invokes the copy constr.)
67   // 2) we cannot invoke the Set method with the content of the any because 
68   //   it expects an any 
69   // hence the Set method of the functor **MUST NOT** extract the 
70   // content of the Data prior to invoking the set method of the box
71   class MagicBoxSetFunctor : public bbtk::AtomicBlackBoxSetFunctor
72   {
73   public:
74     /// Type of pointer on a UBB::Set method  
75     typedef void (MagicBox::*SetMethodPointerType)(bbtk::Data);
76     
77     /// Construction with the pointer on the Set method
78     MagicBoxSetFunctor(SetMethodPointerType s) :
79        mSetMethodPointer(s) 
80        {
81        }
82     
83     /// Concrete application of the Set method of object o
84     void Set(bbtk::AtomicBlackBox* o, const bbtk::Data& d)
85     { 
86       bbtkDebugMessage("data",9,"MagicBoxSetfunctor::Set("<<
87                        bbtk::HumanTypeName(d.type())<<
88                        ")"<<std::endl);
89       (((MagicBox*)o)->*mSetMethodPointer)(d);
90     }
91     
92     /// 
93     bbtk::TypeInfo GetTypeInfo() const { return typeid(bbtk::Data); }
94     std::string GetTypeName() const { return bbtk::TypeName<bbtk::Data>(); }
95     std::string GetHumanTypeName() const { return bbtk::HumanTypeName<bbtk::Data>(); }
96     bool IsPointerType() const { return false; }
97     void BruteForceSetPointer(bbtk::AtomicBlackBox*, void*) {}
98   private:
99     ///  Pointer on the Set method  
100     SetMethodPointerType mSetMethodPointer;
101   };
102   //===========================================================================
103   
104
105   //===========================================================================
106   BBTK_BEGIN_DESCRIBE_BLACK_BOX(MagicBox,bbtk::AtomicBlackBox);
107   BBTK_NAME("MagicBox");
108   BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
109   BBTK_CATEGORY("misc");
110   BBTK_DESCRIPTION("(C++,Python) 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.");
111  
112  AddInputDescriptor
113   (new bbtk::AtomicBlackBoxInputDescriptor
114    (typeid(MagicBoxDescriptor),
115     "In","Input data","",
116     new bbtk::AtomicBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>
117     (&MagicBox::bbGetInputIn),
118     new MagicBoxSetFunctor (&MagicBox::bbSetInputIn) ) );
119
120   BBTK_INPUT(MagicBox, Active, "Active True/False (default True)",bool,"");  
121
122
123   AddOutputDescriptor
124   (new bbtk::AtomicBlackBoxOutputDescriptor
125    (typeid(MagicBoxDescriptor),
126     "Out","Output data","",
127     new bbtk::AtomicBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>
128     (&MagicBox::bbGetOutputOut),
129     new MagicBoxSetFunctor (&MagicBox::bbSetOutputOut) ) );
130   BBTK_END_DESCRIBE_BLACK_BOX(MagicBox);
131   //===========================================================================
132
133 }
134 // namespace bbstd
135
136 #endif // __bbstdMagicBox_h_INCLUDED_H__