]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Object.h
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Object.h
diff --git a/lib/cpPlugins/Object.h b/lib/cpPlugins/Object.h
new file mode 100644 (file)
index 0000000..c2d50ea
--- /dev/null
@@ -0,0 +1,206 @@
+// =========================================================================
+// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
+// =========================================================================
+#ifndef __cpPlugins__Object__h__
+#define __cpPlugins__Object__h__
+
+#include <cpPlugins/Config.h>
+#include <chrono>
+
+// -------------------------------------------------------------------------
+/*! \brief Define base types and methods for any object based on
+ *         cpPlugins::Pipeline::Object.
+ *
+ * \warning Use this macro as the first thing in any object that inherits
+ *          from cpPlugins::Pipeline::Object
+ */
+#define cpPluginsTypeMacro( __c__, __s__ )                              \
+  public:                                                               \
+  typedef __s__                           Superclass;                   \
+  typedef __c__                           Self;                         \
+  typedef std::shared_ptr< Self >         SharedPtr;                    \
+  typedef std::weak_ptr< Self >           WeakPtr;                      \
+  typedef typename Superclass::TTimeStamp TTimeStamp;                   \
+  private:                                                              \
+  __c__( const Self& other );                                           \
+  Self& operator=( const Self& other )
+
+// -------------------------------------------------------------------------
+/* \brief Define all needed types and methods for shared pointers in
+ *         cpPlugins::Pipeline::Object-based classes.
+ *
+ * \warning Always use this macro in any non-abstract object that inherits
+ *          from cpPlugins::Pipeline::Object
+ */
+#define cpPluginsNewMacro( )                                            \
+  friend class _Creator;                                                \
+  friend class _Deleter;                                                \
+  private:                                                              \
+  class _Creator                                                        \
+  { public: Self* operator()( ) { return( new Self( ) ); } };           \
+  class _Deleter                                                        \
+  { public: void operator()( Self* p ) { delete p; } };                 \
+  public:                                                               \
+  static SharedPtr New( )                                               \
+  {                                                                     \
+    SharedPtr ptr = SharedPtr( _Creator( )( ), _Deleter( ) );           \
+    ptr->_Configure( );                                                 \
+    return( ptr );                                                      \
+  }
+
+namespace cpPlugins
+{
+  /*! \brief Base class for all pipeline objects.
+   *
+   * This class is intended to be the base for all pipeline objects. It
+   * contains all methods that allows an adequate synchronization between
+   * nodes (cpPlugins::ProcessObject) that connect edges
+   * (cpPlugins::DataObject).
+   */
+  class CPPLUGINS_EXPORT Object
+    : public std::enable_shared_from_this< Object >
+  {
+    //! Output streaming operator overload
+    friend std::ostream& operator<<( std::ostream& o, const Object& s )
+      {
+        s.Print( o );
+        return( o );
+      }
+
+  public:
+    typedef Object Self;
+    typedef std::enable_shared_from_this< Object > Superclass;
+
+    //! Shared pointer type which is intented to ease memory management.
+    typedef std::shared_ptr< Self > SharedPtr;
+
+    //! Weak pointer type which is intented to ease memory management.
+    typedef std::weak_ptr< Self > WeakPtr;
+
+    //! System (processor) time representation.
+    typedef std::chrono::system_clock TTimeRep;
+
+    //! Time stamp values.
+    typedef std::chrono::time_point< TTimeRep > TTimeStamp;
+
+  public:
+    /*! @defgroup Cast
+     * Cast this object to other types in the hiearchy.
+     * @{
+     */
+    template< class _TObject >
+    _TObject* Cast( )
+      {
+        return( dynamic_cast< _TObject* >( this ) );
+      }
+
+    template< class _TObject >
+    const _TObject* Cast( ) const
+      {
+        return( dynamic_cast< const _TObject* >( this ) );
+      }
+
+    template< class _TObject >
+    std::shared_ptr< _TObject > CastSharedPtr( )
+      {
+        return(
+          std::dynamic_pointer_cast< _TObject >( this->shared_from_this( ) )
+          );
+      }
+
+    template< class _TObject >
+    std::shared_ptr< const _TObject > CastSharedPtr( ) const
+      {
+        return(
+          std::dynamic_pointer_cast< const _TObject >(
+            this->shared_from_this( )
+            )
+          );
+      }
+
+    template< class _TObject >
+    std::weak_ptr< _TObject > CastWeakPtr( )
+      {
+        return( this->CastSharedPtr< _TObject >( ) );
+      }
+
+    template< class _TObject >
+    std::weak_ptr< const _TObject > CastWeakPtr( ) const
+      {
+        return( this->CastSharedPtr< _TObject >( ) );
+      }
+
+    //! @}
+
+    /*! @defgroup m_ClassName
+     * This object's class name.
+     *
+     * \warning This name is intended to be changed only at object's
+     * creation and it should remain unchaged.
+     * @{
+     */
+    const std::type_info& GetClassType( ) const;
+    std::string GetClassName( ) const;
+    virtual void Print( std::ostream& o ) const;
+    //! @}
+
+    /*! @defgroup m_LastTimeStamp
+     * Last (processor) time this object was modified.
+     * @{
+     */
+    const TTimeStamp& GetLastTimeStamp( ) const;
+
+    //! Informs if current object was recently changed.
+    virtual bool IsUpdated( const TTimeStamp& d = TTimeRep::now( ) ) const;
+
+    //! Tags the object as synchronized with current system time.
+    virtual void Modified( );
+    //! @}
+
+    /*! Informs if the current object could be dynamically casted to
+     *  another type.
+     */
+    template< class _TValue >
+    bool IsCompatible( const _TValue* other ) const
+      {
+        return( dynamic_cast< const _TValue* >( this ) != NULL );
+      }
+
+  protected:
+    /*! In order to correctly handle memory with std::shared_pointer<>,
+     *  constructors and destructor should be "invisible" to the user.
+     * @{
+     */
+    Object( );
+    virtual ~Object( );
+    //! @}
+
+    /*! Configure all values and/or objects here instead of the real
+     *  constructor
+     */
+    virtual void _Configure( );
+
+    //! @ingroup m_LastTimeStamp
+    virtual void _SynchTime( );
+
+  private:
+    /*! Purposely not implemented.
+     * @{
+     */
+    Object( const Self& );
+    Self& operator=( const Self& );
+    //! @}
+
+  public:
+    //! Zero time. This is used to identify un-synchronized objects.
+    static const TTimeStamp ZeroTimeStamp;
+
+  protected:
+    //! @ingroup m_LastTimeStamp
+    TTimeStamp m_LastTimeStamp;
+  }; // end class
+} // end namespace
+
+#endif // __cpPlugins__Object__h__
+
+// eof - $RCSfile$