lichao
2021-04-26 bcd780993c176b93f7393607f8003adf66e6676a
src/shm.h
@@ -25,7 +25,6 @@
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/noncopyable.hpp>
#include <boost/uuid/uuid.hpp>
#include <chrono>
#include <thread>
@@ -76,7 +75,7 @@
   ~MutexWithTimeLimit() { static_assert(std::is_pod<Duration>::value); }
   bool try_lock();
   void lock();
   void unlock() { mutex_.unlock(); }
   void unlock();
};
// typedef boost::interprocess::interprocess_mutex Mutex;
@@ -103,7 +102,16 @@
   ~SharedMemory();
   std::string name() const { return name_; }
   bool Remove() { return Remove(name()); }
   template <class T, class... Params>
   T *FindOrCreate(const std::string &name, Params &&...params)
   {
      return find_or_construct<T>(name.c_str(), std::nothrow)(std::forward<decltype(params)>(params)...);
   }
   template <class T, class... Params>
   T *Create(const std::string &name, Params &&...params)
   {
      return construct<T>(name.c_str(), std::nothrow)(std::forward<decltype(params)>(params)...);
   }
   void *Alloc(const size_t size) { return allocate(size, std::nothrow); }
   void Dealloc(void *p)
   {
@@ -113,7 +121,7 @@
   void Dealloc(offset_ptr<T> ptr) { return Dealloc(ptr.get()); }
   template <class T, class... Params>
   T *New(Params const &...params) { return construct<T>(anonymous_instance, std::nothrow)(params...); }
   T *New(Params &&...params) { return construct<T>(anonymous_instance, std::nothrow)(std::forward<decltype(params)>(params)...); }
   template <class T>
   void Delete(T *p)
   {
@@ -138,30 +146,32 @@
{
   static std::string ObjName(const std::string &name) { return "obj" + name; }
protected:
public:
   typedef T Data;
   typedef SharedMemory ShmType;
private:
   ShmType &shm_;
   std::string name_;
   Data *pdata_ = nullptr;
   bool IsOk() const { return pdata_; }
protected:
   ShmType &shm() const { return shm_; }
public:
   template <class... Params>
   ShmObject(ShmType &segment, const std::string &name, Params &&...t) :
       shm_(segment), name_(name)
   {
      pdata_ = shm_.find_or_construct<Data>(ObjName(name_).c_str(), std::nothrow)(t...);
      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 {
         pdata_ = shm_.Find<Data>(ObjName(name_));
      }
   }
   bool IsOk() const { return pdata_; }
   static bool Remove(SharedMemory &shm, const std::string &name) { return shm.destroy<Data>(ObjName(name).c_str()); }
   static Data *Find(SharedMemory &shm, const std::string &name) { return shm.Find<Data>(ObjName(name)); }
   Data *Find(const std::string &name) { return Find(shm_, ObjName(name)); }
@@ -172,6 +182,11 @@
   Data *operator->() { return data(); }
   const Data *operator->() const { return data(); }
   bool Remove() { return Remove(shm_, name_); }
private:
   ShmType &shm_;
   std::string name_;
   Data *pdata_ = nullptr;
};
} // namespace bhome_shm