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