]> Creatis software - bbtk.git/blob - packages/std/src/bbstdSharedMemory.h
a981f28b3ea20d53cf5e45b56194cb2317ce5d8e
[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     BBTK_MB_DECLARE_INPUT(In,bbtk::Data);
61     BBTK_MB_DECLARE_INPUT(In1,bbtk::Data);
62     BBTK_MB_DECLARE_INPUT(In2,bbtk::Data);
63     BBTK_MB_DECLARE_INPUT(In3,bbtk::Data);
64     BBTK_MB_DECLARE_INPUT(In4,bbtk::Data);
65     BBTK_MB_DECLARE_INPUT(In5,bbtk::Data);
66     BBTK_MB_DECLARE_INPUT(In6,bbtk::Data);
67     BBTK_MB_DECLARE_INPUT(In7,bbtk::Data);
68     BBTK_MB_DECLARE_INPUT(In8,bbtk::Data);
69     BBTK_MB_DECLARE_INPUT(In9,bbtk::Data);
70     BBTK_DECLARE_OUTPUT(Out,bbtk::Data);
71     BBTK_PROCESS(DoProcess);
72     void DoProcess();
73   protected:
74     //    virtual void bbUserConstructor();
75     virtual void bbSetStatusAndPropagate(bbtk::BlackBoxInputConnector* c,
76                                          bbtk::IOStatus s);
77     bool mCanSet;
78  };
79   //==================================================================
80   
81 #undef BBTK_MB_DECLARE_INPUT  
82
83   //==================================================================
84   // We have to create a particular SetFunctor for SharedMemory because
85   // its input is of type bbtk::Data (i.e. any) and :
86   // 1) an any cannot store an any (construction with an any invokes the copy constr.)
87   // 2) we cannot invoke the Set method with the content of the any because 
88   //   it expects an any 
89   // hence the Set method of the functor **MUST NOT** extract the 
90   // content of the Data prior to invoking the set method of the box
91   class SharedMemorySetFunctor : public bbtk::AtomicBlackBoxSetFunctor
92   {
93   public:
94     /// Type of pointer on a UBB::Set method  
95     typedef void (SharedMemory::*SetMethodPointerType)(bbtk::Data);
96     
97     /// Construction with the pointer on the Set method
98     SharedMemorySetFunctor(SetMethodPointerType s) :
99        mSetMethodPointer(s) 
100        {
101        }
102     
103     /// Concrete application of the Set method of object o
104     void Set(bbtk::AtomicBlackBox* o, const bbtk::Data& d)
105     { 
106       bbtkDebugMessage("data",9,"SharedMemorySetfunctor::Set("<<
107                        bbtk::HumanTypeName(d.type())<<
108                        ")"<<std::endl);
109       (((SharedMemory*)o)->*mSetMethodPointer)(d);
110     }
111     
112     /// 
113     bbtk::TypeInfo GetTypeInfo() const { return typeid(bbtk::Data); }
114     std::string GetTypeName() const { return bbtk::TypeName<bbtk::Data>(); }
115     std::string GetHumanTypeName() const { return bbtk::HumanTypeName<bbtk::Data>(); }
116     bool IsPointerType() const { return false; }
117     void BruteForceSetPointer(bbtk::AtomicBlackBox*, void*) {}
118   private:
119     ///  Pointer on the Set method  
120     SetMethodPointerType mSetMethodPointer;
121   };
122   //===========================================================================
123   
124 #define MAGIC_BOX_INPUT(NAME)                                           \
125   AddInputDescriptor                                                    \
126   (new bbtk::AtomicBlackBoxInputDescriptor                              \
127    (typeid(SharedMemoryDescriptor),                                             \
128     #NAME,"Input data","",                                              \
129     new bbtk::AtomicBlackBoxTGetFunctor<SharedMemory,bbtk::Data,bbtk::Data>     \
130     (&SharedMemory::bbGetInput ## NAME),                                        \
131     new SharedMemorySetFunctor (&SharedMemory::bbSetInput ## NAME) ) );         
132
133   //===========================================================================
134   BBTK_BEGIN_DESCRIBE_BLACK_BOX(SharedMemory,bbtk::AtomicBlackBox);
135   BBTK_NAME("SharedMemory");
136   BBTK_AUTHOR("laurent.guigues@creatis.insa-lyon.fr");
137   BBTK_CATEGORY("misc");
138   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.");
139   MAGIC_BOX_INPUT(In)   
140   MAGIC_BOX_INPUT(In1)  
141   MAGIC_BOX_INPUT(In2)  
142   MAGIC_BOX_INPUT(In3)  
143   MAGIC_BOX_INPUT(In4)  
144   MAGIC_BOX_INPUT(In5)  
145   MAGIC_BOX_INPUT(In6)  
146   MAGIC_BOX_INPUT(In7)  
147   MAGIC_BOX_INPUT(In8)  
148   MAGIC_BOX_INPUT(In9)  
149
150  /*
151  AddInputDescriptor
152   (new bbtk::AtomicBlackBoxInputDescriptor
153    (typeid(SharedMemoryDescriptor),
154     "In","Input data","",
155     new bbtk::AtomicBlackBoxTGetFunctor<SharedMemory,bbtk::Data,bbtk::Data>
156     (&SharedMemory::bbGetInputIn),
157     new SharedMemorySetFunctor (&SharedMemory::bbSetInputIn) ) );
158  */
159   AddOutputDescriptor
160   (new bbtk::AtomicBlackBoxOutputDescriptor
161    (typeid(SharedMemoryDescriptor),
162     "Out","Output data","",
163     new bbtk::AtomicBlackBoxTGetFunctor<SharedMemory,bbtk::Data,bbtk::Data>
164     (&SharedMemory::bbGetOutputOut),
165     new SharedMemorySetFunctor (&SharedMemory::bbSetOutputOut) ) );
166   BBTK_END_DESCRIBE_BLACK_BOX(SharedMemory);
167   //===========================================================================
168
169 }
170 // namespace bbstd
171
172 #endif // __bbstdSharedMemory_h_INCLUDED_H__