bbtkBlackBox.h

Go to the documentation of this file.
00001 /*=========================================================================
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkBlackBox.h,v $
00004   Language:  C++
00005   Date:      $Date: 2011/03/03 14:33:13 $
00006   Version:   $Revision: 1.32 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and
00015 *  abiding by the rules of distribution of free software. You can  use,
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability.
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */
00030 
00031 
00032 
00043 #ifndef __bbtkBlackBox_h__
00044 #define __bbtkBlackBox_h__
00045 
00046 #include "bbtkSystem.h"
00047 #include "bbtkMessageManager.h"
00048 #include "bbtkBlackBoxDescriptor.h"
00049 #include "bbtkBlackBoxInputConnector.h"
00050 //#include "bbtkBlackBoxOutputConnector.h"
00051 #include <set>
00052 
00053 // Signal/slot mechanism for output change events
00054 #include <boost/signal.hpp>
00055 #include <boost/bind.hpp>
00056 
00057 
00058 #define bbtkBlackBoxMessage(key,level,mess) \
00059   bbtkMessage(key,level,"["<<bbGetName()<<"] "<<mess)
00060 #define bbtkBlackBoxDebugMessage(key,level,mess)        \
00061   bbtkDebugMessage(key,level,"["<<bbGetName()<<"] "<<mess)
00062 
00063 namespace bbtk
00064 {
00065 
00066   struct Void { Void(int = 0) {} };
00067 
00068   class Factory;
00069   class Connection;
00070   class BlackBoxOutputConnector;
00071 
00072   class BBTK_EXPORT BlackBox : public Object
00073   {
00074     BBTK_ABSTRACT_OBJECT_INTERFACE(BlackBox);
00075 
00076 
00077   public:
00078 
00079     //==================================================================
00080     // Types
00081     //==================================================================
00082     typedef boost::signals::trackable OutputChangeObserverType;
00083     typedef boost::signal<void (bbtk::BlackBox::Pointer,
00084                                 const std::string&,
00085                                 IOStatus)>  OutputChangeSignalType;
00086     typedef OutputChangeSignalType::slot_function_type
00087     OutputChangeCallbackType;
00088 
00090     typedef std::map<std::string, BlackBoxOutputConnector*>
00091     OutputConnectorMapType;
00093     typedef std::map<std::string, BlackBoxInputConnector*>
00094     InputConnectorMapType;
00095     //==================================================================
00096 
00097 
00098     //==================================================================
00101 
00102 
00103     virtual void bbExecute(bool force = false);
00105     //==================================================================
00106 
00107 
00108 
00109 
00110 
00111     //==================================================================
00113     virtual BlackBox::Pointer bbClone(const std::string& name) = 0;
00114     //==================================================================
00115 
00116 
00117    //==================================================================
00120 
00121 
00123     virtual BlackBoxDescriptor::Pointer bbGetDescriptor() const = 0;
00124 
00126     const std::string& bbGetTypeName() const
00127       { return bbGetDescriptor()->GetTypeName(); }
00128 
00129 
00131     const std::string& bbGetName() const { return bbmName; }
00132 
00134     virtual std::string bbGetFullName() const;
00135 
00137     virtual std::string bbGetNameWithParent() const;
00138 
00140     BlackBox::Pointer bbGetParent() const { return bbmParent.lock(); }
00141 
00143     //==================================================================
00144 
00145 
00146 
00147     //==================================================================
00150 
00151 
00153     virtual bool bbHasInput(const std::string& label) const;
00155     virtual TypeInfo bbGetInputType( const std::string &label ) const;
00157     IOStatus bbGetInputStatus( const std::string &name ) const
00158     { return mInputConnectorMap.find(name)->second->GetStatus(); }
00160     virtual Data bbGetInput( const std::string &name )  = 0;
00162     std::string bbGetInputAsString( const std::string &input);
00163 
00164 
00167     virtual void bbSetInput( const std::string &name, Data data,
00168                              bool update_time = true ) = 0;
00170     virtual void bbBruteForceSetInputPointer( const std::string &name,
00171                                               void* data,
00172                                               bool update_time = true) =0;
00173 
00174 
00176     virtual bool bbHasOutput(const std::string& label) const;
00178     virtual TypeInfo bbGetOutputType( const std::string &label ) const;
00180     virtual Data bbGetOutput( const std::string &name ) = 0;
00182     std::string bbGetOutputAsString( const std::string &output ); //,Factory *factory);
00183 
00185     virtual void bbSetOutput( const std::string &name, Data data) = 0;
00186 
00187 
00189     InputConnectorMapType&  bbGetInputConnectorMap()
00190     { return mInputConnectorMap; }
00192     const InputConnectorMapType&  bbGetInputConnectorMap() const
00193     { return mInputConnectorMap; }
00195     BlackBoxInputConnector&  bbGetInputConnector(const std::string& n)
00196     { return *(mInputConnectorMap.find(n)->second); }
00198     const BlackBoxInputConnector&  bbGetInputConnector(const std::string& n) const
00199     { return *(mInputConnectorMap.find(n)->second); }
00200 
00201 
00203     OutputConnectorMapType& bbGetOutputConnectorMap()
00204     { return mOutputConnectorMap; }
00206     const OutputConnectorMapType& bbGetOutputConnectorMap() const
00207     { return mOutputConnectorMap; }
00209     BlackBoxOutputConnector& bbGetOutputConnector(const std::string& n)
00210     { return *(mOutputConnectorMap.find(n)->second); }
00212     const BlackBoxOutputConnector& bbGetOutputConnector(const std::string& n) const
00213     { return *(mOutputConnectorMap.find(n)->second); }
00214 
00216     //==================================================================
00217 
00219     virtual void bbGetHelp(bool full=true) const;
00220 
00221 
00222 
00223     //==================================================================
00226 
00227 
00228     //==================================================================
00239     void bbAddOutputObserver(const std::string& output_name,
00240                              OutputChangeCallbackType f);
00241 
00244     void bbRemoveOutputObserver(const std::string& output_name,
00245                                 OutputChangeCallbackType f);
00246    //==================================================================
00247 
00248 
00249     //==================================================================
00257     virtual void bbSignalOutputModification(bool reaction = true);
00265     virtual void bbSignalOutputModification( const std::string& output_name,
00266                                              bool reaction = true);
00277     virtual void bbSignalOutputModification( const std::vector<std::string>&
00278                                              output_name,
00279                                              bool reaction = true);
00280    //==================================================================
00282 
00283 
00284 
00285 
00286 
00287 
00288     //==================================================================
00290 
00291 
00292     std::string bbGetInputBoxProcessMode() { return bbmBoxProcessMode; }
00294     void bbSetInputBoxProcessMode(std::string a) { bbmBoxProcessMode = a; }
00296  
00297     typedef enum 
00298      {
00299       bbPipeline,
00300       bbAlways,
00301       bbReactive
00302      }
00303     BoxProcessModeValue;
00304     
00306     BoxProcessModeValue bbGetBoxProcessModeValue() const;
00308     virtual bool bbBoxProcessModeIsReactive() const;
00310     virtual bool bbBoxProcessModeIsAlways() const;
00311 
00313     Void bbGetInputBoxExecute() { return Void(); }
00315     void bbSetInputBoxExecute(Void = 0) {}
00316 
00318     Void bbGetOutputBoxChange() { return Void(); }
00320     void bbSetOutputBoxChange(Void = 0) { }
00322     //==================================================================
00323 
00324     virtual void bbPrintHelp(BlackBox::Pointer parentblackbox,
00325                              int detail, int level
00326                              );
00327 
00329     void bbInsertHTMLGraph(  std::ofstream& s,
00330                              int detail,
00331                              int level,
00332                              bool instanceOrtype,
00333                              const std::string& output_dir,
00334                              bool relative_link )
00335     {}
00336 
00337 
00338     //==================================================================
00340 
00341     virtual void bbSetShown(bool) {}
00342     virtual bool bbIsShown() { return false; }
00344     //==================================================================
00345 
00346     //JCP changed to public 09-06-09
00347         //==================================================================
00349 
00350             static bool bbGlobalGetSomeBoxExecuting();
00351             static void bbGlobalSetSomeBoxExecuting(bool b);
00352 
00353             static void bbGlobalSetFreezeExecution(bool b);
00354             static bool bbGlobalGetFreezeExecution();
00355 
00356             static void bbGlobalAddToExecutionList( BlackBox::Pointer b );
00357             static void bbGlobalProcessExecutionList();
00358 
00360     //JCP 09-06-09
00361 
00362   protected:
00363 
00364    //==================================================================
00367 
00368 
00369     //==================================================================
00373     virtual void bbUserSetDefaultValues() {}
00374 
00381     virtual void bbUserInitializeProcessing() {}
00382 
00388     virtual void bbUserFinalizeProcessing() {}
00390     virtual void bbUserOnShow() {}
00391 
00392     //==================================================================
00393     // @}
00394     //==================================================================
00395 
00396 
00397     //==================================================================
00398 
00407     virtual void bbWriteDotFileBlackBox(FILE *ff,
00408                                         BlackBox::Pointer parentblackbox,
00409                                         int detail, int level,
00410                                         bool instanceOrtype,
00411                                         bool relative_link );
00413     virtual void bbWriteDotInputOutputName(FILE *ff,
00414                                            bool inputoutput,
00415                                            int detail, int level);
00416 
00417      virtual BlackBox::Pointer bbFindBlackBox(const std::string &blackboxname)
00418               { return BlackBox::Pointer();}
00419 
00420     virtual void Check(bool recursive = true);
00421 
00422     //==================================================================
00423     // PROTECTED PART : ACCESSIBLE TO THE BlackBox DEVELOPER
00424     // (IN INHERITED CLASSES)
00426     BlackBox(const std::string &name);
00428     BlackBox(BlackBox& from, const std::string &name);
00429     //==================================================================
00430 
00431 
00432 
00433     //==================================================================
00436 
00437     //==================================================================
00441     virtual void bbRecursiveExecute(Connection::Pointer caller);
00442     //==================================================================
00443 
00444     //==================================================================
00448     IOStatus bbUpdateInputs();
00449     //==================================================================
00450 
00451 
00452     //==================================================================
00455     virtual void bbCreateWindow()
00456     {
00457       //  bbtkError("BlackBox::bbCreateWidget called : how can this happen ?");
00458     }
00459     //==================================================================
00460 
00461     //==================================================================
00464     virtual void bbShowWindow()
00465     {
00466       //  bbtkError("BlackBox::bbShowWidget called : how can this happen ?");
00467     }
00468     //==================================================================
00469 
00470 
00471    //==================================================================
00474     virtual void bbProcess()
00475     {
00476       bbtkError("BlackBox::bbProcess called : how can this happen ?");
00477 //      this->bbUserProcess();
00478     }
00479     //==================================================================
00480 
00481     //==================================================================
00483     void bbComputePostProcessStatus();
00485     //==================================================================
00486 
00487 
00488     //==================================================================
00492     virtual void bbSetStatusAndPropagate(BlackBoxInputConnector* c,
00493                                          IOStatus s);
00494     //==================================================================
00495 
00496 
00497 
00498     //==================================================================
00500 
00501 
00502     //==================================================================
00504     virtual void bbAllocateConnectors();
00506     virtual void bbDesallocateConnectors();
00508     virtual void bbCopyIOValues(BlackBox& from);
00509     //==================================================================
00510 
00511     //==================================================================
00517     void bbInitializeProcessing();
00518 
00524     void bbFinalizeProcessing();
00525 
00532     virtual void bbRecursiveInitializeProcessing() {}
00533 
00534 
00541     virtual void bbRecursiveFinalizeProcessing() {}
00542     //==================================================================
00543 
00545     //==================================================================
00546 
00547   private:
00548     //==================================================================
00549     friend class Connection;
00550     friend class ComplexBlackBox;
00551 
00553     void bbSetParent(BlackBox::Pointer p) { bbmParent = p; }
00554 
00555     //==================================================================
00559 
00560 
00562     virtual void bbConnectInput( const std::string& name,
00563                                  Connection* c);
00565     virtual void bbConnectOutput( const std::string& name,
00566                                   Connection* c);
00568     virtual void bbDisconnectInput( const std::string& name,
00569                                     Connection* c);
00571     virtual void bbDisconnectOutput( const std::string& name,
00572                                      Connection* c);
00574     //==================================================================
00575 
00578     virtual bool bbCanReact() const;
00579 
00581     bool bbGetExecuting() const { return bbmExecuting; }
00583     void bbSetExecuting(bool b) { bbmExecuting = b; }
00584 
00585   protected:
00586 
00587     //==================================================================
00588   protected:
00589 
00590 
00591      //==================================================================
00599     struct BBTK_EXPORT Deleter : public Object::Deleter
00600     {
00601       Deleter();
00602       int Delete(Object* p);
00603     };
00604     //==================================================================
00605 
00606     //==================================================================
00608     template <class U>
00609     static boost::shared_ptr<U> MakeBlackBoxPointer(U* s, bool lock = false)
00610     {
00611       return MakePointer(s,BlackBox::Deleter(),lock);
00612     }
00613     //==================================================================
00614 
00615     //==================================================================
00620 
00621 //JCP 21-09-20 09 delete this throws and exception change due to compiler version changing and boost version
00622     virtual int bbDelete() { delete this; 
00623                              return 0; }
00624     //==================================================================
00625 
00626 
00627     //==================================================================
00628   private:
00629     //==================================================================
00630 
00631     //==================================================================
00632     // ATTRIBUTES
00634     bool bbmInitialized;
00636     bool bbmExecuting;
00638     std::string bbmName;
00640     std::string bbmPackageName;
00645     std::string bbmBoxProcessMode;
00647     BlackBox::WeakPointer bbmParent;
00648     //==================================================================
00649 
00650 
00651    //==================================================================
00652     // ATTRIBUTES
00654     OutputConnectorMapType mOutputConnectorMap;
00656     InputConnectorMapType mInputConnectorMap;
00657     //==================================================================
00658  };
00659   // Class BlackBox
00660 
00661 
00663 #define BBTK_MAKE_OUTPUT_OBSERVER(OBJECT,METHOD) \
00664     boost::bind( METHOD, OBJECT, _1, _2, _3)
00665 
00666 }
00667 // namespace bbtk
00668 #endif

Generated on Thu May 31 14:12:03 2012 for BBTK by  doxygen 1.5.7.1