]> Creatis software - bbtk.git/blob - packages/std/src/bbstdSharedMemory.h
3152 BBTK BugN ew Normal - ShareMemory, ComboBox->list
[bbtk.git] / packages / std / src / bbstdSharedMemory.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: bbstdSharedMemory.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:51:32 $
33   Version:   $Revision: 1.2 $
34 =========================================================================*/
35
36 #ifndef __bbstdSharedMemory_h_INCLUDED_H__
37 #define __bbstdSharedMemory_h_INCLUDED_H__
38
39 #include "bbtkAtomicBlackBox.h"
40 #include "bbstd_EXPORT.h"
41
42 namespace bbstd
43 {
44 #define BBTK_MB_DECLARE_INPUT(NAME,TYPE)                                \
45   protected:                                                            \
46   TYPE bbmInput##NAME;                                                  \
47 public:                                                                 \
48   TYPE bbGetInput##NAME ()                                              \
49   { return bbmInput##NAME; }                                            \
50     void bbSetInput##NAME (TYPE d)                                      \
51     { bbmInput##NAME = d;                                               \
52       if (mCanSet) { bbSetOutputOut(d); mCanSet = false; } }                            
53
54   //==================================================================
55   class bbstd_EXPORT SharedMemory
56     : 
57     public bbtk::AtomicBlackBox
58   {
59     BBTK_BLACK_BOX_INTERFACE(SharedMemory,bbtk::AtomicBlackBox);
60
61 //    BBTK_DECLARE_INPUT(In,bbtk::Data);
62     BBTK_MB_DECLARE_INPUT(In,bbtk::Data);
63
64     BBTK_MB_DECLARE_INPUT(In1,bbtk::Data);
65     BBTK_MB_DECLARE_INPUT(In2,bbtk::Data);
66     BBTK_MB_DECLARE_INPUT(In3,bbtk::Data);
67     BBTK_MB_DECLARE_INPUT(In4,bbtk::Data);
68     BBTK_MB_DECLARE_INPUT(In5,bbtk::Data);
69     BBTK_MB_DECLARE_INPUT(In6,bbtk::Data);
70     BBTK_MB_DECLARE_INPUT(In7,bbtk::Data);
71     BBTK_MB_DECLARE_INPUT(In8,bbtk::Data);
72     BBTK_MB_DECLARE_INPUT(In9,bbtk::Data);
73     BBTK_DECLARE_OUTPUT(Out,bbtk::Data);
74     BBTK_PROCESS(DoProcess);
75     void DoProcess();
76   protected:
77     //    virtual void bbUserConstructor();
78     virtual void bbSetStatusAndPropagate(bbtk::BlackBoxInputConnector* c,
79                                          bbtk::IOStatus s);
80     bool mCanSet;
81     bool firsttime;
82  };
83   //==================================================================
84   
85 #undef BBTK_MB_DECLARE_INPUT  
86
87   //==================================================================
88   // We have to create a particular SetFunctor for SharedMemory because
89   // its input is of type bbtk::Data (i.e. any) and :
90   // 1) an any cannot store an any (construction with an any invokes the copy constr.)
91   // 2) we cannot invoke the Set method with the content of the any because 
92   //   it expects an any 
93   // hence the Set method of the functor **MUST NOT** extract the 
94   // content of the Data prior to invoking the set method of the box
95   class SharedMemorySetFunctor : public bbtk::AtomicBlackBoxSetFunctor
96   {
97   public:
98     /// Type of pointer on a UBB::Set method  
99     typedef void (SharedMemory::*SetMethodPointerType)(bbtk::Data);
100     
101     /// Construction with the pointer on the Set method
102     SharedMemorySetFunctor(SetMethodPointerType s) :
103        mSetMethodPointer(s) 
104        {
105        }
106     
107     /// Concrete application of the Set method of object o
108     void Set(bbtk::AtomicBlackBox* o, const bbtk::Data& d)
109     { 
110       bbtkDebugMessage("data",9,"SharedMemorySetfunctor::Set("<<
111                        bbtk::HumanTypeName(d.type())<<
112                        ")"<<std::endl);
113       (((SharedMemory*)o)->*mSetMethodPointer)(d);
114     }
115     
116     /// 
117     bbtk::TypeInfo GetTypeInfo() const { return typeid(bbtk::Data); }
118     std::string GetTypeName() const { return bbtk::TypeName<bbtk::Data>(); }
119     std::string GetHumanTypeName() const { return bbtk::HumanTypeName<bbtk::Data>(); }
120     bool IsPointerType() const { return false; }
121     void BruteForceSetPointer(bbtk::AtomicBlackBox*, void*) {}
122   private:
123     ///  Pointer on the Set method  
124     SetMethodPointerType mSetMethodPointer;
125   };
126   //===========================================================================
127   
128 #define MAGIC_BOX_INPUT(NAME)                                           \
129   AddInputDescriptor                                                    \
130   (new bbtk::AtomicBlackBoxInputDescriptor                              \
131    (typeid(SharedMemoryDescriptor),                                             \
132     #NAME,"Input data","",                                              \
133     new bbtk::AtomicBlackBoxTGetFunctor<SharedMemory,bbtk::Data,bbtk::Data>     \
134     (&SharedMemory::bbGetInput ## NAME),                                        \
135     new SharedMemorySetFunctor (&SharedMemory::bbSetInput ## NAME) ) );         
136
137   //===========================================================================
138   BBTK_BEGIN_DESCRIBE_BLACK_BOX(SharedMemory,bbtk::AtomicBlackBox);
139   BBTK_NAME("SharedMemory");
140   BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
141   BBTK_CATEGORY("misc");
142   BBTK_DESCRIPTION("Implements a memory which can be shared by different boxes. The value of the output is the value of the lower index input which has changed. It is used for example for synchronization issues between boxes.");
143   MAGIC_BOX_INPUT(In)   
144   MAGIC_BOX_INPUT(In1)  
145   MAGIC_BOX_INPUT(In2)  
146   MAGIC_BOX_INPUT(In3)  
147   MAGIC_BOX_INPUT(In4)  
148   MAGIC_BOX_INPUT(In5)  
149   MAGIC_BOX_INPUT(In6)  
150   MAGIC_BOX_INPUT(In7)  
151   MAGIC_BOX_INPUT(In8)  
152   MAGIC_BOX_INPUT(In9)  
153
154  /*
155  AddInputDescriptor
156   (new bbtk::AtomicBlackBoxInputDescriptor
157    (typeid(SharedMemoryDescriptor),
158     "In","Input data","",
159     new bbtk::AtomicBlackBoxTGetFunctor<SharedMemory,bbtk::Data,bbtk::Data>
160     (&SharedMemory::bbGetInputIn),
161     new SharedMemorySetFunctor (&SharedMemory::bbSetInputIn) ) );
162  */
163   AddOutputDescriptor
164   (new bbtk::AtomicBlackBoxOutputDescriptor
165    (typeid(SharedMemoryDescriptor),
166     "Out","Output data","",
167     new bbtk::AtomicBlackBoxTGetFunctor<SharedMemory,bbtk::Data,bbtk::Data>
168     (&SharedMemory::bbGetOutputOut),
169     new SharedMemorySetFunctor (&SharedMemory::bbSetOutputOut) ) );
170   BBTK_END_DESCRIBE_BLACK_BOX(SharedMemory);
171   //===========================================================================
172
173 }
174 // namespace bbstd
175
176 #endif // __bbstdSharedMemory_h_INCLUDED_H__