CMS 3D CMS Logo

ReadBuffer.h
Go to the documentation of this file.
1 #ifndef FWCore_SharedMemory_ReadBuffer_h
2 #define FWCore_SharedMemory_ReadBuffer_h
3 // -*- C++ -*-
4 //
5 // Package: FWCore/SharedMemory
6 // Class : ReadBuffer
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: 21/01/2020
19 //
20 
21 // system include files
22 #include <array>
23 #include <memory>
24 #include <string>
25 #include "boost/interprocess/managed_shared_memory.hpp"
26 
27 // user include files
30 
31 // forward declarations
32 
33 namespace edm::shared_memory {
34  class ReadBuffer {
35  public:
39  ReadBuffer(std::string const& iUniqueName, BufferInfo* iBufferInfo)
40  : buffer_{nullptr, 0}, bufferInfo_{iBufferInfo}, bufferOldIndex_{3} {
41  *bufferInfo_ = {0, 0};
42  bufferNames_[0] = iUniqueName + buffer_names::kBuffer0;
43  bufferNames_[1] = iUniqueName + buffer_names::kBuffer1;
44  }
45  ReadBuffer(const ReadBuffer&) = delete;
46  const ReadBuffer& operator=(const ReadBuffer&) = delete;
47  ReadBuffer(ReadBuffer&&) = delete;
48  const ReadBuffer& operator=(ReadBuffer&&) = delete;
49 
50  // ---------- const member functions ---------------------
51  int bufferIdentifier() const { return bufferInfo_->identifier_; }
52 
53  // ---------- member functions ---------------------------
54  std::pair<char*, std::size_t> buffer() {
55  if (mustGetBufferAgain()) {
56  using namespace boost::interprocess;
57  sm_ = std::make_unique<managed_shared_memory>(open_only, bufferNames_[bufferInfo_->index_].c_str());
58  buffer_ = sm_->find<char>(buffer_names::kBuffer);
60  }
61  return buffer_;
62  }
63 
64  private:
66  // ---------- member data --------------------------------
67  std::pair<char*, std::size_t> buffer_;
70  std::array<std::string, 2> bufferNames_;
71  std::unique_ptr<boost::interprocess::managed_shared_memory> sm_;
72  };
73 } // namespace edm::shared_memory
74 
75 #endif
std::pair< char *, std::size_t > buffer()
Definition: ReadBuffer.h:54
constexpr char const *const kBuffer0
Definition: buffer_names.h:30
constexpr char const *const kBuffer
Definition: buffer_names.h:29
std::unique_ptr< boost::interprocess::managed_shared_memory > sm_
Definition: ReadBuffer.h:71
ReadBuffer(std::string const &iUniqueName, BufferInfo *iBufferInfo)
Definition: ReadBuffer.h:39
constexpr char const *const kBuffer1
Definition: buffer_names.h:31
std::array< std::string, 2 > bufferNames_
Definition: ReadBuffer.h:70
const ReadBuffer & operator=(const ReadBuffer &)=delete
std::pair< char *, std::size_t > buffer_
Definition: ReadBuffer.h:67