SharedMemory.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // SharedMemory.h
  3. //
  4. // $Id: //poco/Main/Foundation/include/Poco/SharedMemory.h#4 $
  5. //
  6. // Library: Poco
  7. // Package: Processes
  8. // Module: SharedMemory
  9. //
  10. // Definition of the SharedMemory class.
  11. //
  12. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Poco_SharedMemory_INCLUDED
  38. #define Poco_SharedMemory_INCLUDED
  39. #include "Poco/Poco.h"
  40. #include <algorithm>
  41. #include <cstddef>
  42. namespace Poco {
  43. class SharedMemoryImpl;
  44. class File;
  45. class Foundation_API SharedMemory
  46. /// Create and manage a shared memory object.
  47. ///
  48. /// A SharedMemory object has value semantics, but
  49. /// is implemented using a handle/implementation idiom.
  50. /// Therefore, multiple SharedMemory objects can share
  51. /// a single, reference counted SharedMemoryImpl object.
  52. {
  53. public:
  54. enum AccessMode
  55. {
  56. AM_READ = 0,
  57. AM_WRITE
  58. };
  59. SharedMemory();
  60. /// Default constructor creates an unmapped SharedMemory object.
  61. /// No clients can connect to an unmapped SharedMemory object.
  62. SharedMemory(const std::string& name, std::size_t size, AccessMode mode, const void* addrHint = 0);
  63. /// Creates or connects to a shared memory object with the given name.
  64. ///
  65. /// For maximum portability, name should be a valid Unix filename and not
  66. /// contain any slashes or backslashes.
  67. ///
  68. /// An address hint can be passed to the system, specifying the desired
  69. /// start address of the shared memory area. Whether the hint
  70. /// is actually honored is, however, up to the system. Windows platform
  71. /// will generally ignore the hint.
  72. SharedMemory(const File& file, AccessMode mode, const void* addrHint = 0);
  73. /// Maps the entire contents of file into a shared memory segment.
  74. ///
  75. /// An address hint can be passed to the system, specifying the desired
  76. /// start address of the shared memory area. Whether the hint
  77. /// is actually honored is, however, up to the system. Windows platform
  78. /// will generally ignore the hint.
  79. SharedMemory(const SharedMemory& other);
  80. /// Creates a SharedMemory object by copying another one.
  81. ~SharedMemory();
  82. /// Destroys the SharedMemory.
  83. SharedMemory& operator = (const SharedMemory& other);
  84. /// Assigns another SharedMemory object.
  85. void swap(SharedMemory& other);
  86. /// Swaps the SharedMemory object with another one.
  87. char* begin() const;
  88. /// Returns the start address of the shared memory segment.
  89. /// Will be NULL for illegal segments.
  90. char* end() const;
  91. /// Returns the one-past-end end address of the shared memory segment.
  92. /// Will be NULL for illegal segments.
  93. private:
  94. SharedMemoryImpl* _pImpl;
  95. };
  96. //
  97. // inlines
  98. //
  99. inline void SharedMemory::swap(SharedMemory& other)
  100. {
  101. using std::swap;
  102. swap(_pImpl, other._pImpl);
  103. }
  104. } // namespace Poco::Poco
  105. #endif // Poco_SharedMemory_INCLUDED