1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4 #ifndef __cpPlugins__Object__h__
5 #define __cpPlugins__Object__h__
7 #include <cpPlugins/Config.h>
10 // -------------------------------------------------------------------------
11 /*! \brief Define base types and methods for any object based on
12 * cpPlugins::Pipeline::Object.
14 * \warning Use this macro as the first thing in any object that inherits
15 * from cpPlugins::Pipeline::Object
17 #define cpPluginsTypeMacro( __c__, __s__ ) \
19 typedef __s__ Superclass; \
21 typedef std::shared_ptr< Self > SharedPtr; \
22 typedef std::weak_ptr< Self > WeakPtr; \
23 typedef typename Superclass::TTimeStamp TTimeStamp; \
25 __c__( const Self& other ); \
26 Self& operator=( const Self& other )
28 // -------------------------------------------------------------------------
29 /* \brief Define all needed types and methods for shared pointers in
30 * cpPlugins::Pipeline::Object-based classes.
32 * \warning Always use this macro in any non-abstract object that inherits
33 * from cpPlugins::Pipeline::Object
35 #define cpPluginsNewMacro( ) \
36 friend class _Creator; \
37 friend class _Deleter; \
40 { public: Self* operator()( ) { return( new Self( ) ); } }; \
42 { public: void operator()( Self* p ) { delete p; } }; \
44 static SharedPtr New( ) \
46 SharedPtr ptr = SharedPtr( _Creator( )( ), _Deleter( ) ); \
53 /*! \brief Base class for all pipeline objects.
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).
60 class CPPLUGINS_EXPORT Object
61 : public std::enable_shared_from_this< Object >
63 //! Output streaming operator overload
64 friend std::ostream& operator<<( std::ostream& o, const Object& s )
72 typedef std::enable_shared_from_this< Object > Superclass;
74 //! Shared pointer type which is intented to ease memory management.
75 typedef std::shared_ptr< Self > SharedPtr;
77 //! Weak pointer type which is intented to ease memory management.
78 typedef std::weak_ptr< Self > WeakPtr;
80 //! System (processor) time representation.
81 typedef std::chrono::system_clock TTimeRep;
83 //! Time stamp values.
84 typedef std::chrono::time_point< TTimeRep > TTimeStamp;
88 * Cast this object to other types in the hiearchy.
91 template< class _TObject >
94 return( dynamic_cast< _TObject* >( this ) );
97 template< class _TObject >
98 const _TObject* Cast( ) const
100 return( dynamic_cast< const _TObject* >( this ) );
103 template< class _TObject >
104 std::shared_ptr< _TObject > CastSharedPtr( )
107 std::dynamic_pointer_cast< _TObject >( this->shared_from_this( ) )
111 template< class _TObject >
112 std::shared_ptr< const _TObject > CastSharedPtr( ) const
115 std::dynamic_pointer_cast< const _TObject >(
116 this->shared_from_this( )
121 template< class _TObject >
122 std::weak_ptr< _TObject > CastWeakPtr( )
124 return( this->CastSharedPtr< _TObject >( ) );
127 template< class _TObject >
128 std::weak_ptr< const _TObject > CastWeakPtr( ) const
130 return( this->CastSharedPtr< _TObject >( ) );
135 /*! @defgroup m_ClassName
136 * This object's class name.
138 * \warning This name is intended to be changed only at object's
139 * creation and it should remain unchaged.
142 const std::type_info& GetClassType( ) const;
143 std::string GetClassName( ) const;
144 virtual void Print( std::ostream& o ) const;
147 /*! @defgroup m_LastTimeStamp
148 * Last (processor) time this object was modified.
151 const TTimeStamp& GetLastTimeStamp( ) const;
153 //! Informs if current object was recently changed.
154 virtual bool IsUpdated( const TTimeStamp& d = TTimeRep::now( ) ) const;
156 //! Tags the object as synchronized with current system time.
157 virtual void Modified( );
160 /*! Informs if the current object could be dynamically casted to
163 template< class _TValue >
164 bool IsCompatible( const _TValue* other ) const
166 return( dynamic_cast< const _TValue* >( this ) != NULL );
170 /*! In order to correctly handle memory with std::shared_pointer<>,
171 * constructors and destructor should be "invisible" to the user.
178 /*! Configure all values and/or objects here instead of the real
181 virtual void _Configure( );
183 //! @ingroup m_LastTimeStamp
184 virtual void _SynchTime( );
187 /*! Purposely not implemented.
190 Object( const Self& );
191 Self& operator=( const Self& );
195 //! Zero time. This is used to identify un-synchronized objects.
196 static const TTimeStamp ZeroTimeStamp;
199 //! @ingroup m_LastTimeStamp
200 TTimeStamp m_LastTimeStamp;
204 #endif // __cpPlugins__Object__h__