From 6eefba812ede29549af3633c490f2e85a4805524 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 31 三月 2021 11:24:20 +0800 Subject: [PATCH] format code style. --- src/shm.h | 130 ++++++++++++++++++++++++------------------ 1 files changed, 74 insertions(+), 56 deletions(-) diff --git a/src/shm.h b/src/shm.h index 0f68754..3fce99d 100644 --- a/src/shm.h +++ b/src/shm.h @@ -19,14 +19,15 @@ #ifndef SHM_6CHO6D6C #define SHM_6CHO6D6C +#include <boost/interprocess/managed_shared_memory.hpp> +#include <boost/interprocess/sync/interprocess_condition.hpp> +#include <boost/interprocess/sync/interprocess_mutex.hpp> +#include <boost/interprocess/sync/scoped_lock.hpp> #include <boost/noncopyable.hpp> #include <boost/uuid/uuid.hpp> -#include <boost/interprocess/managed_shared_memory.hpp> -#include <boost/interprocess/sync/interprocess_mutex.hpp> -#include <boost/interprocess/sync/interprocess_condition.hpp> -#include <boost/interprocess/sync/scoped_lock.hpp> -namespace bhome_shm { +namespace bhome_shm +{ using namespace boost::interprocess; @@ -37,73 +38,90 @@ class SharedMemory : public mshm_t { - std::string name_; + std::string name_; - static permissions AllowAll() { - permissions perm; - perm.set_unrestricted(); - return perm; - } - void Swap(SharedMemory &a); + static permissions AllowAll() + { + permissions perm; + perm.set_unrestricted(); + return perm; + } + void Swap(SharedMemory &a); + public: - static bool Remove(const std::string &name) { - return shared_memory_object::remove(name.c_str()); - } - SharedMemory(const std::string &name, const uint64_t size); - ~SharedMemory(); - std::string name() const { return name_; } - bool Remove() { return Remove(name()); } + static bool Remove(const std::string &name) { return shared_memory_object::remove(name.c_str()); } - void *Alloc(const size_t size) { return allocate(size, std::nothrow); } - void Dealloc(void *p) { if(p) { deallocate(p); } } - template<class T> void Dealloc(offset_ptr<T> ptr) { return Dealloc(ptr.get()); } + SharedMemory(const std::string &name, const uint64_t size); + ~SharedMemory(); + std::string name() const { return name_; } + bool Remove() { return Remove(name()); } - template <class T, class ...Params> T * New(Params const&...params) { return construct<T>(anonymous_instance, std::nothrow)(params...); } - template <class T> void Delete(T *p) { if (p) { destroy_ptr<T>(p); }; } - template <class T> void Delete(offset_ptr<T> p) { Delete(p.get()); } - template <class T> T *Find(const std::string &name) { return find<T>(name.c_str()).first; } + void *Alloc(const size_t size) { return allocate(size, std::nothrow); } + void Dealloc(void *p) + { + if (p) { deallocate(p); } + } + template <class T> + 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...); } + template <class T> + void Delete(T *p) + { + if (p) { destroy_ptr<T>(p); }; + } + template <class T> + void Delete(offset_ptr<T> p) { Delete(p.get()); } + template <class T> + T *Find(const std::string &name) { return find<T>(name.c_str()).first; } }; // ShmObject manages an object in shared memory, but ShmObject itself is not in shared memory. // works like a smart pointer of an object in shared memory. // TODO handshake with center, and can be removed if killed. template <class T> -class ShmObject : private boost::noncopyable { - static std::string ObjName(const std::string &name) { return "obj" + name; } -protected: - typedef T Data; - typedef SharedMemory ShmType; -private: - ShmType &shm_; - std::string name_; - Data *pdata_ = nullptr; +class ShmObject : private boost::noncopyable +{ + static std::string ObjName(const std::string &name) { return "obj" + name; } - bool IsOk() const { return pdata_; } protected: - ShmType &shm() const { return shm_; } + 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...); - if (!IsOk()) { - throw("Error: Not enough memory, can not allocate \"" + name_ + "\""); - } - } - 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)); } - virtual ~ShmObject() {} - std::string name() const { return name_; } - Data* data() { return pdata_; } - const Data* data() const { return pdata_; } - Data* operator->() { return data(); } - const Data* operator->() const { return data(); } - bool Remove() { return shm_.destroy<Data>(ObjName(name_).c_str()); } + 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...); + if (!IsOk()) { + throw("Error: Not enough memory, can not allocate \"" + name_ + "\""); + } + } + 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)); } + virtual ~ShmObject() {} + std::string name() const { return name_; } + Data *data() { return pdata_; } + const Data *data() const { return pdata_; } + Data *operator->() { return data(); } + const Data *operator->() const { return data(); } + bool Remove() { return shm_.destroy<Data>(ObjName(name_).c_str()); } }; -template <class D> using Allocator = allocator<D, SharedMemory::segment_manager>; +template <class D> +using Allocator = allocator<D, SharedMemory::segment_manager>; } // namespace bhome_shm -- Gitblit v1.8.0