VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ugcs::vsm::Shared_memory Class Referenceabstract

Platform independent implementation of system-wide named shared memory used for interprocess communications. More...

#include <shared_memory.h>

Inheritance diagram for ugcs::vsm::Shared_memory:

Public Types

enum  Open_result { OPEN_RESULT_OK, OPEN_RESULT_CREATED, OPEN_RESULT_ERROR }
 Possible return codes from Open() call.
 
typedef std::shared_ptr
< Shared_memory
Ptr
 Pointer type.
 
typedef std::weak_ptr
< Shared_memory
Weak_ptr
 Pointer type.
 

Public Member Functions

 Shared_memory ()
 Constructor should not be called explicitly. More...
 
virtual Open_result Open (const std::string &name, const size_t size)=0
 Open/create shared memory. More...
 
virtual void Close ()=0
 Closes previously opened memory.
 
virtual void * Get ()
 Returns the pointer to shared memory. More...
 

Static Public Member Functions

template<typename... Args>
static Ptr Create (Args &&...args)
 Create an instance. More...
 
static bool Delete (const std::string &name)
 Deletes the named memory. More...
 
static Ptr Create ()
 Creates Platform specific class instance. More...
 

Protected Attributes

void * memory
 pointer to shared memory
 

Detailed Description

Platform independent implementation of system-wide named shared memory used for interprocess communications.

Workflow: 1) Call Shared_memory::Create() to create class instance. 2) Call Shared_memory::Open() to open or create shared memory. 3) Call Shared_memory::Get() to Get the pointer to shared memory.

The pointer is valid until object is closed. Object is closed:

  • explicitly via call to Close()
  • implicitly when calling Open() on the same instance.
  • in destructor.

User must be aware of the following platform specific differences in shared memory semantics: Windows: 1) When the last handle to memory is closed the OS destroys the memory. (Note: OS closes all open handles on application termination/crash) 2) By default the memory is created in Session Kernel object namespace. 3) Shared memory cannot be explicitly deleted. Implemented via CreateFileMapping() and friends.

Linux: 1) Shared memory persists in the system until explicitly deleted or system restarted. 2) It is accessible from any session of its creator user. Implemented via shm_open() and friends.

Constructor & Destructor Documentation

ugcs::vsm::Shared_memory::Shared_memory ( )
inline

Constructor should not be called explicitly.

Use Create() function to instantiate the object.

Member Function Documentation

template<typename... Args>
static Ptr ugcs::vsm::Shared_memory::Create ( Args &&...  args)
inlinestatic

Create an instance.

static Ptr ugcs::vsm::Shared_memory::Create ( )
static

Creates Platform specific class instance.

This does not create the memory itself, you should call Open() to create the named memory.

Returns
shared_ptr<Shared_memory> of newly created instance
static bool ugcs::vsm::Shared_memory::Delete ( const std::string &  name)
static

Deletes the named memory.

(Linux-only) Does not affect any opened memory with this name.

Parameters
nameShared memory name.
Returns
true Shared memory deleted. false error occurred.
virtual void* ugcs::vsm::Shared_memory::Get ( )
inlinevirtual

Returns the pointer to shared memory.

Returns
pointer to shared memory. Returns nullptr if not opened.
virtual Open_result ugcs::vsm::Shared_memory::Open ( const std::string &  name,
const size_t  size 
)
pure virtual

Open/create shared memory.

Closes previously opened memory if any.

Parameters
nameName of memory object
sizesize in bytes. Will return error if size==0
Returns
OPEN_RESULT_OK Opened existing shared memory. OPEN_RESULT_CREATED Created new shared memory. OPEN_RESULT_ERROR Error while creating shared memory. Previously opened memory is closed.

The documentation for this class was generated from the following file: