Česky
Kamil Dudka

Share Library (C++)

File detail

Name:DownloadSharedSegment.h [Download]
Location: sharelib > src > sharelib
Size:2.7 KB
Last modification:2007-08-27 01:16

Source code

#ifndef SHARED_SEGMENT_H
#define SHARED_SEGMENT_H
 
/**
 * @file SharedSegment.h
 * @brief Platform-indepent shared segment representation
 * @author Kamil Dudka, xdudka00@gmail.com
 * @date 2007-05-15
 * @ingroup backend
 */
 
#if !defined _LINUX && !defined _WIN32
#  error "Platform was not specified. Add macro _LINUX or _WIN32 to compiler options."
#endif
 
#include "sharelib.h"
 
#ifdef _WIN32
#include <windows.h>
#endif  // _WIN32
 
namespace Share {
#ifdef _WIN32
  typedef HANDLE SegmentId;   ///< Return type of CreateFileMappingA and OpenFileMappingA funcitons
#endif  // _WIN32
 
#ifdef _LINUX
  typedef int SegmentId;      ///< Return type of shmget function
#endif // _LINUX
 
  /**
  * @brief Platform independent shared segment representation
  * @ingroup backend
  * @note Implementation of this class was taken from @b MSDTk.
  */
  class SharedSegment {
    public:
      /**
      * If segment with given name already exists, ShareException is thrown.
      * @brief @b Create @b and @b attach new shared segment with given name and size.
      * @param name Name of segment created.
      * @param size Size of segment created. This size is fixed in this version of library.
      * @throw ShareException Library-specific exception derived from std::bad_alloc
      * 
      */
      SHARE_EXPORT SharedSegment (const char *name, size_t size) throw (ShareException);
 
      /**
      * If segment with given name does not exists, ShareException is thrown.
      * @brief Attach to already existing shared segment.
      * @param name Name of shared segment to attach to.
      * @throw ShareException Library-specific exception derived from std::bad_alloc
      */
      SHARE_EXPORT SharedSegment (const char *name) throw (ShareException);
 
      /**
      * @brief Detach from shared segment.
      */
      SHARE_EXPORT ~SharedSegment () throw ();
 
      /**
      * @brief @return Return name of shared segment.
      */
      SHARE_EXPORT const char* name() const { return szName_; }
 
      /**
      * @brief @return Return attached shared segment address.
      */
      SHARE_EXPORT void* atAddress () const { return atAddr_; }
 
      /**
      * @brief Destroy shared segment.
      * @attention On Win32 platform is segment destoroyed automatically by OS, when there are no more references to segment.
      */
      SHARE_EXPORT void destroy ();
 
    private:
      SharedSegment (const SharedSegment&);                 ///< avoid object copying
      void operator= (const SharedSegment&);                ///< avoid object copying
 
    private:
      SegmentId id_;
      char *szName_;
      void *atAddr_;
 
    private:
      void setName (const char *name);
      void attach ();
  };  // (class SharedSegment)
 
} // (namespace Share)
 
#endif // SHARED_SEGMENT_H