]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Object.h
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Object.h
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4 #ifndef __cpPlugins__Object__h__
5 #define __cpPlugins__Object__h__
6
7 #include <cpPlugins/Config.h>
8 #include <chrono>
9
10 // -------------------------------------------------------------------------
11 /*! \brief Define base types and methods for any object based on
12  *         cpPlugins::Pipeline::Object.
13  *
14  * \warning Use this macro as the first thing in any object that inherits
15  *          from cpPlugins::Pipeline::Object
16  */
17 #define cpPluginsTypeMacro( __c__, __s__ )                              \
18   public:                                                               \
19   typedef __s__                           Superclass;                   \
20   typedef __c__                           Self;                         \
21   typedef std::shared_ptr< Self >         SharedPtr;                    \
22   typedef std::weak_ptr< Self >           WeakPtr;                      \
23   typedef typename Superclass::TTimeStamp TTimeStamp;                   \
24   private:                                                              \
25   __c__( const Self& other );                                           \
26   Self& operator=( const Self& other )
27
28 // -------------------------------------------------------------------------
29 /* \brief Define all needed types and methods for shared pointers in
30  *         cpPlugins::Pipeline::Object-based classes.
31  *
32  * \warning Always use this macro in any non-abstract object that inherits
33  *          from cpPlugins::Pipeline::Object
34  */
35 #define cpPluginsNewMacro( )                                            \
36   friend class _Creator;                                                \
37   friend class _Deleter;                                                \
38   private:                                                              \
39   class _Creator                                                        \
40   { public: Self* operator()( ) { return( new Self( ) ); } };           \
41   class _Deleter                                                        \
42   { public: void operator()( Self* p ) { delete p; } };                 \
43   public:                                                               \
44   static SharedPtr New( )                                               \
45   {                                                                     \
46     SharedPtr ptr = SharedPtr( _Creator( )( ), _Deleter( ) );           \
47     ptr->_Configure( );                                                 \
48     return( ptr );                                                      \
49   }
50
51 namespace cpPlugins
52 {
53   /*! \brief Base class for all pipeline objects.
54    *
55    * This class is intended to be the base for all pipeline objects. It
56    * contains all methods that allows an adequate synchronization between
57    * nodes (cpPlugins::ProcessObject) that connect edges
58    * (cpPlugins::DataObject).
59    */
60   class CPPLUGINS_EXPORT Object
61     : public std::enable_shared_from_this< Object >
62   {
63     //! Output streaming operator overload
64     friend std::ostream& operator<<( std::ostream& o, const Object& s )
65       {
66         s.Print( o );
67         return( o );
68       }
69
70   public:
71     typedef Object Self;
72     typedef std::enable_shared_from_this< Object > Superclass;
73
74     //! Shared pointer type which is intented to ease memory management.
75     typedef std::shared_ptr< Self > SharedPtr;
76
77     //! Weak pointer type which is intented to ease memory management.
78     typedef std::weak_ptr< Self > WeakPtr;
79
80     //! System (processor) time representation.
81     typedef std::chrono::system_clock TTimeRep;
82
83     //! Time stamp values.
84     typedef std::chrono::time_point< TTimeRep > TTimeStamp;
85
86   public:
87     /*! @defgroup Cast
88      * Cast this object to other types in the hiearchy.
89      * @{
90      */
91     template< class _TObject >
92     _TObject* Cast( )
93       {
94         return( dynamic_cast< _TObject* >( this ) );
95       }
96
97     template< class _TObject >
98     const _TObject* Cast( ) const
99       {
100         return( dynamic_cast< const _TObject* >( this ) );
101       }
102
103     template< class _TObject >
104     std::shared_ptr< _TObject > CastSharedPtr( )
105       {
106         return(
107           std::dynamic_pointer_cast< _TObject >( this->shared_from_this( ) )
108           );
109       }
110
111     template< class _TObject >
112     std::shared_ptr< const _TObject > CastSharedPtr( ) const
113       {
114         return(
115           std::dynamic_pointer_cast< const _TObject >(
116             this->shared_from_this( )
117             )
118           );
119       }
120
121     template< class _TObject >
122     std::weak_ptr< _TObject > CastWeakPtr( )
123       {
124         return( this->CastSharedPtr< _TObject >( ) );
125       }
126
127     template< class _TObject >
128     std::weak_ptr< const _TObject > CastWeakPtr( ) const
129       {
130         return( this->CastSharedPtr< _TObject >( ) );
131       }
132
133     //! @}
134
135     /*! @defgroup m_ClassName
136      * This object's class name.
137      *
138      * \warning This name is intended to be changed only at object's
139      * creation and it should remain unchaged.
140      * @{
141      */
142     const std::type_info& GetClassType( ) const;
143     std::string GetClassName( ) const;
144     virtual void Print( std::ostream& o ) const;
145     //! @}
146
147     /*! @defgroup m_LastTimeStamp
148      * Last (processor) time this object was modified.
149      * @{
150      */
151     const TTimeStamp& GetLastTimeStamp( ) const;
152
153     //! Informs if current object was recently changed.
154     virtual bool IsUpdated( const TTimeStamp& d = TTimeRep::now( ) ) const;
155
156     //! Tags the object as synchronized with current system time.
157     virtual void Modified( );
158     //! @}
159
160     /*! Informs if the current object could be dynamically casted to
161      *  another type.
162      */
163     template< class _TValue >
164     bool IsCompatible( const _TValue* other ) const
165       {
166         return( dynamic_cast< const _TValue* >( this ) != NULL );
167       }
168
169   protected:
170     /*! In order to correctly handle memory with std::shared_pointer<>,
171      *  constructors and destructor should be "invisible" to the user.
172      * @{
173      */
174     Object( );
175     virtual ~Object( );
176     //! @}
177
178     /*! Configure all values and/or objects here instead of the real
179      *  constructor
180      */
181     virtual void _Configure( );
182
183     //! @ingroup m_LastTimeStamp
184     virtual void _SynchTime( );
185
186   private:
187     /*! Purposely not implemented.
188      * @{
189      */
190     Object( const Self& );
191     Self& operator=( const Self& );
192     //! @}
193
194   public:
195     //! Zero time. This is used to identify un-synchronized objects.
196     static const TTimeStamp ZeroTimeStamp;
197
198   protected:
199     //! @ingroup m_LastTimeStamp
200     TTimeStamp m_LastTimeStamp;
201   }; // end class
202 } // end namespace
203
204 #endif // __cpPlugins__Object__h__
205
206 // eof - $RCSfile$