/** * @file include/ActivMiddleware/ActivBase/SharedObject.h * * @brief Header file for the template shared object class. */ #if (!defined ACTIV_BASE_SHARED_OBJECT_H) #define ACTIV_BASE_SHARED_OBJECT_H #include "ActivMiddleware/ActivBase/PagingFileMappedMemoryFactory.h" //! Activ namespace. namespace Activ { /** * @brief Templatized shared object class. */ template class SharedObject : private Boost::noncopyable { public: /** * @brief Default constructor. */ SharedObject(); /** * @brief Destructor. */ ~SharedObject(); /** * @brief Is the shared object open? */ bool IsOpen() const; /** * @brief Create shared memory object. * * @param name the name of the object. * @param mappingFlags flags indicating how the memory segment should be mapped. * @param size the size of the object, by default the size of the class T. * * @throw OsException * @throw BaseException */ void Create(const String &name, const IMappedMemory::MappingFlags::Type mappingFlags = IMappedMemory::MappingFlags::Unique(), const size_t size = sizeof(T)); /** * @brief Open existing shared memory object. * * @param name the name of the object. * @param mappingFlags flags indicating how the memory segment should be mapped. * @param timeout object specifying how long to wait for the object to exist, in ms. * * @throw OsException. * @throw BaseException */ void Open(const String &name, const IMappedMemory::MappingFlags::Type mappingFlags = IMappedMemory::MappingFlags::None(), Timeout &timeout = Timeout::GetInfinite()); /** * @brief Get pointer to shared object. * * @return Pointer to shared object. */ T *GetPointer() const; /** * @brief Get actual size of shared object (underlying mapped memory region). * * @return Size. */ size_t GetSize() const; private: T *m_pData; ///< Pointer to mapped portion. IMappedMemory *m_pMem; ///< Pointer to shared memory object. }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- template inline SharedObject::SharedObject() : m_pData(0), m_pMem(0) { } // --------------------------------------------------------------------------------------------------------------------------------- template inline SharedObject::~SharedObject() { if (0 != m_pMem) delete m_pMem; } // --------------------------------------------------------------------------------------------------------------------------------- template inline bool SharedObject::IsOpen() const { return m_pData != 0; } // --------------------------------------------------------------------------------------------------------------------------------- template void SharedObject::Create(const String &name, const IMappedMemory::MappingFlags::Type mappingFlags, const size_t size) { m_pMem = PagingFileMappedMemoryFactory::GetInstance().Create(name, size, mappingFlags); m_pData = reinterpret_cast(m_pMem->GetPointer()); } // --------------------------------------------------------------------------------------------------------------------------------- template void SharedObject::Open(const String &name, const IMappedMemory::MappingFlags::Type mappingFlags, Timeout &timeout) { m_pMem = PagingFileMappedMemoryFactory::GetInstance().Open(name, mappingFlags, timeout); m_pData = reinterpret_cast(m_pMem->GetPointer()); } // --------------------------------------------------------------------------------------------------------------------------------- template inline T *SharedObject::GetPointer() const { return m_pData; } // --------------------------------------------------------------------------------------------------------------------------------- template size_t SharedObject::GetSize() const { return (0 != m_pMem) ? m_pMem->GetSize() : 0; } } // namespace Activ #endif // (!defined ACTIV_BASE_SHARED_OBJECT_H)