| | |
| | | template <class D> |
| | | using SharedPtr = shared_ptr<D, Allocator<void>, Deleter<D>>; |
| | | |
| | | enum Mode { |
| | | eOpen = 0, |
| | | eCreate = 1, |
| | | eOpenOrCreate = 2 |
| | | }; |
| | | |
| | | // ShmObject manages an object in shared memory, but ShmObject itself is not in shared memory. |
| | | template <class T> |
| | | class ShmObject : private boost::noncopyable |
| | |
| | | ShmType &shm() const { return shm_; } |
| | | |
| | | template <class... Params> |
| | | ShmObject(ShmType &segment, const std::string &name, Params &&...t) : |
| | | ShmObject(ShmType &segment, const std::string &name, Mode mode, Params &&...t) : |
| | | shm_(segment), name_(name) |
| | | { |
| | | pdata_ = shm_.FindOrCreate<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | if (!IsOk()) { |
| | | throw("Error: Not enough memory, can not allocate \"" + name_ + "\""); |
| | | } |
| | | } |
| | | template <class... Params> |
| | | ShmObject(ShmType &segment, const bool create_or_else_find, const std::string &name, Params &&...t) : |
| | | shm_(segment), name_(name) |
| | | { |
| | | if (create_or_else_find) { |
| | | pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | } else { |
| | | switch (mode) { |
| | | case eOpen: |
| | | pdata_ = shm_.Find<Data>(ObjName(name_)); |
| | | break; |
| | | case eCreate: |
| | | pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | break; |
| | | case eOpenOrCreate: |
| | | pdata_ = shm_.FindOrCreate<Data>(ObjName(name_), std::forward<decltype(t)>(t)...); |
| | | break; |
| | | default: break; |
| | | } |
| | | if (!IsOk()) { |
| | | throw("Error: shm can not create/open \"" + name_ + "\""); |
| | | } |
| | | } |
| | | |
| | | ShmObject(const int64_t offset, ShmType &segment, const std::string &name) : |
| | | shm_(segment), name_(name) |
| | | { |