summaryrefslogtreecommitdiff
path: root/lib/NativeSharedMemory.hpp
blob: 7c8d25b5b43fda1b0c9383a7871bf9f530f36236 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef WSJTX_NATIVE_SHARED_MEMORY_HPP
#define WSJTX_NATIVE_SHARED_MEMORY_HPP

#include <cstddef>
#include <string>

class NativeSharedMemory final
{
public:
  NativeSharedMemory () = default;
  ~NativeSharedMemory ();
  NativeSharedMemory (NativeSharedMemory const&) = delete;
  NativeSharedMemory& operator= (NativeSharedMemory const&) = delete;

  bool create (std::string const& name, std::size_t bytes);
  bool attach (std::string const& name);
  bool detach () noexcept;
  static bool remove (std::string const& name, std::string * error = nullptr);

  void * data () const noexcept { return data_; }
  std::size_t size () const noexcept { return size_; }
  bool isAttached () const noexcept { return data_ != nullptr; }
  bool alreadyExists () const noexcept;
  bool notFound () const noexcept;
  std::string errorString () const;

private:
  bool begin (std::string const& name);
  bool fail (char const * operation, unsigned long code) noexcept;

  std::string name_;
  void * data_ {nullptr};
  std::size_t size_ {0};
  bool owner_ {false};
  char const * operation_ {nullptr};
  unsigned long error_ {0};
#ifdef _WIN32
  void * handle_ {nullptr};
#else
  int descriptor_ {-1};
#endif
};

#endif