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