sharedmem.h 842 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. #include "base/define.h"
  3. #include "base/error.h"
  4. namespace ylib
  5. {
  6. /// <summary>
  7. /// 共享内存
  8. /// </summary>
  9. class sharedmem:public ylib::error_base
  10. {
  11. public:
  12. /// <summary>
  13. /// 自动销毁
  14. /// </summary>
  15. /// <param name="auto_free"></param>
  16. sharedmem(bool auto_free = false);
  17. ~sharedmem();
  18. /// <summary>
  19. /// 创建
  20. /// </summary>
  21. /// <param name="name"></param>
  22. /// <param name="size"></param>
  23. /// <returns></returns>
  24. bool create(const std::string& name,size_t size);
  25. /// <summary>
  26. /// 销毁
  27. /// </summary>
  28. void destory();
  29. void* mem() const { return m_memory; }
  30. uchar operator[](size_t );
  31. private:
  32. std::string m_name;
  33. size_t m_size = 0;
  34. void* m_memory = nullptr;
  35. bool m_auto_free = false;
  36. #ifdef _WIN32
  37. void* m_handle = 0;
  38. #else
  39. int m_handle = -1;
  40. #endif
  41. };
  42. }