147 lines
4.1 KiB
C++
147 lines
4.1 KiB
C++
/**
|
|
* @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 T>
|
|
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<class T>
|
|
inline SharedObject<T>::SharedObject() :
|
|
m_pData(0),
|
|
m_pMem(0)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<class T>
|
|
inline SharedObject<T>::~SharedObject()
|
|
{
|
|
if (0 != m_pMem)
|
|
delete m_pMem;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<class T>
|
|
inline bool SharedObject<T>::IsOpen() const
|
|
{
|
|
return m_pData != 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<class T>
|
|
void SharedObject<T>::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<T *>(m_pMem->GetPointer());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<class T>
|
|
void SharedObject<T>::Open(const String &name, const IMappedMemory::MappingFlags::Type mappingFlags, Timeout &timeout)
|
|
{
|
|
m_pMem = PagingFileMappedMemoryFactory::GetInstance().Open(name, mappingFlags, timeout);
|
|
m_pData = reinterpret_cast<T *>(m_pMem->GetPointer());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<class T>
|
|
inline T *SharedObject<T>::GetPointer() const
|
|
{
|
|
return m_pData;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<class T>
|
|
size_t SharedObject<T>::GetSize() const
|
|
{
|
|
return (0 != m_pMem) ? m_pMem->GetSize() : 0;
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_SHARED_OBJECT_H)
|