]> Creatis software - bbtk.git/blob - packages/std/src/bbstdMagicBox.h
4f0bebe06f61353be8b87c4856afb0b3fbc49b72
[bbtk.git] / packages / std / src / bbstdMagicBox.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbstdMagicBox.h,v $
4   Language:  C++
5   Date:      $Date: 2009/06/08 14:50:06 $
6   Version:   $Revision: 1.13 $
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 #include "bbstd_EXPORT.h"
37
38 namespace bbstd
39 {
40
41   //==================================================================
42   class bbstd_EXPORT MagicBox
43     : 
44     public bbtk::AtomicBlackBox
45   {
46     BBTK_BLACK_BOX_INTERFACE(MagicBox,bbtk::AtomicBlackBox);
47     BBTK_DECLARE_INPUT(In,bbtk::Data);
48     BBTK_DECLARE_OUTPUT(Out,bbtk::Data);
49     BBTK_PROCESS(DoProcess);
50     void DoProcess();
51  };
52   //==================================================================
53   
54
55   //==================================================================
56   // We have to create a particular SetFunctor for MagicBox because
57   // its input is of type bbtk::Data (i.e. any) and :
58   // 1) an any cannot store an any (construction with an any invokes the copy constr.)
59   // 2) we cannot invoke the Set method with the content of the any because 
60   //   it expects an any 
61   // hence the Set method of the functor **MUST NOT** extract the 
62   // content of the Data prior to invoking the set method of the box
63   class MagicBoxSetFunctor : public bbtk::AtomicBlackBoxSetFunctor
64   {
65   public:
66     /// Type of pointer on a UBB::Set method  
67     typedef void (MagicBox::*SetMethodPointerType)(bbtk::Data);
68     
69     /// Construction with the pointer on the Set method
70     MagicBoxSetFunctor(SetMethodPointerType s) :
71        mSetMethodPointer(s) 
72        {
73        }
74     
75     /// Concrete application of the Set method of object o
76     void Set(bbtk::AtomicBlackBox* o, const bbtk::Data& d)
77     { 
78       bbtkDebugMessage("data",9,"MagicBoxSetfunctor::Set("<<
79                        bbtk::HumanTypeName(d.type())<<
80                        ")"<<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  
104  AddInputDescriptor
105   (new bbtk::AtomicBlackBoxInputDescriptor
106    (typeid(MagicBoxDescriptor),
107     "In","Input data","",
108     new bbtk::AtomicBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>
109     (&MagicBox::bbGetInputIn),
110     new MagicBoxSetFunctor (&MagicBox::bbSetInputIn) ) );
111
112   AddOutputDescriptor
113   (new bbtk::AtomicBlackBoxOutputDescriptor
114    (typeid(MagicBoxDescriptor),
115     "Out","Output data","",
116     new bbtk::AtomicBlackBoxTGetFunctor<MagicBox,bbtk::Data,bbtk::Data>
117     (&MagicBox::bbGetOutputOut),
118     new MagicBoxSetFunctor (&MagicBox::bbSetOutputOut) ) );
119   BBTK_END_DESCRIBE_BLACK_BOX(MagicBox);
120   //===========================================================================
121
122 }
123 // namespace bbstd
124
125 #endif // __bbstdMagicBox_h_INCLUDED_H__