OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
IPCRingBuffer.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_IPCRingBuffer_h_GUID_4F33BDA7_1BEB_4E81_B96C_1ADA1CBD1997
26 #define INCLUDED_IPCRingBuffer_h_GUID_4F33BDA7_1BEB_4E81_B96C_1ADA1CBD1997
27 
28 // Internal Includes
29 #include <osvr/Common/Export.h>
30 #include <osvr/Util/StdInt.h>
31 #include <osvr/Util/UniquePtr.h>
32 #include <osvr/Util/SharedPtr.h>
33 
34 // Library/third-party includes
35 // - none
36 
37 // Standard includes
38 #include <string>
39 
40 namespace osvr {
41 namespace common {
42  namespace detail {
43  struct IPCPutResult;
44  typedef shared_ptr<IPCPutResult> IPCPutResultPtr;
45  struct IPCGetResult;
46  typedef shared_ptr<IPCGetResult> IPCGetResultPtr;
47  }
48  // forward declaration
50 
52  typedef shared_ptr<IPCRingBuffer> IPCRingBufferPtr;
53 
65  class IPCRingBuffer : public enable_shared_from_this<IPCRingBuffer> {
66  public:
67  typedef uint8_t BackendType;
68  typedef uint16_t alignment_type;
69  typedef uint16_t entry_count_type;
70  typedef uint32_t entry_size_type;
71  typedef uint32_t abi_level_type;
72  class Options {
73  public:
74  OSVR_COMMON_EXPORT Options();
75  OSVR_COMMON_EXPORT Options(std::string const &name);
76 
77  OSVR_COMMON_EXPORT Options(std::string const &name,
78  BackendType backend);
79 
80  BackendType getBackend() const { return m_shmBackend; }
81 
84  Options &setName(std::string const &name);
85  std::string const &getName() const { return m_name; }
86 
90  Options &setAlignment(alignment_type alignment);
91  alignment_type getAlignment() const { return m_alignment; }
92 
95  Options &setEntries(entry_count_type entries);
96  entry_count_type getEntries() const { return m_entries; }
97 
100  Options &setEntrySize(entry_size_type entrySize);
101  entry_size_type getEntrySize() const { return m_entrySize; }
102 
103  private:
104  std::string m_name;
105  BackendType m_shmBackend;
106  alignment_type m_alignment = 16;
107  entry_count_type m_entries = 16;
108  entry_size_type m_entrySize = 65536;
109  };
110 
115  OSVR_COMMON_EXPORT static abi_level_type getABILevel();
116 
122  OSVR_COMMON_EXPORT static IPCRingBufferPtr create(Options const &opts);
123 
130  OSVR_COMMON_EXPORT static IPCRingBufferPtr find(Options const &opts);
131 
133  OSVR_COMMON_EXPORT BackendType getBackend() const;
134 
137  OSVR_COMMON_EXPORT std::string const &getName() const;
138 
140  OSVR_COMMON_EXPORT uint32_t getEntrySize() const;
141 
144  OSVR_COMMON_EXPORT uint16_t getEntries() const;
145 
149  typedef uint32_t sequence_type;
150 
151  typedef uint8_t value_type;
152  typedef value_type *pointer_type;
153  typedef value_type const *pointer_to_const_type;
154 
155  typedef shared_ptr<value_type> smart_pointer_type;
156 
161  public:
163  BufferWriteProxy(BufferWriteProxy const &) = delete;
164 
166  BufferWriteProxy &operator=(BufferWriteProxy const &) = delete;
167 
170  std::swap(m_data, other.m_data);
171  }
172 
175  std::swap(m_data, other.m_data);
176  return *this;
177  }
178 
179  operator pointer_type() const { return get(); }
180 
181  pointer_type get() const { return m_buf; }
182 
183  sequence_type getSequenceNumber() const { return m_seq; }
184 
185  private:
186  BufferWriteProxy(detail::IPCPutResultPtr &&data,
187  IPCRingBufferPtr &&shm);
188  friend class IPCRingBuffer;
189  pointer_type m_buf;
190  sequence_type m_seq;
191  detail::IPCPutResultPtr m_data;
192  };
193 
204  public:
207  explicit operator bool() const { return nullptr != m_buf; }
208 
210  pointer_to_const_type get() const { return m_buf; }
211 
214  smart_pointer_type getBufferSmartPointer() const;
215 
217  sequence_type getSequenceNumber() const { return m_seq; }
218 
219  pointer_to_const_type operator*() const { return m_buf; }
220  pointer_to_const_type operator->() const { return m_buf; }
221 
222  private:
223  BufferReadProxy(detail::IPCGetResultPtr &&data,
224  IPCRingBufferPtr &&shm);
225  friend class IPCRingBuffer;
226  pointer_type m_buf;
227  sequence_type m_seq;
228  detail::IPCGetResultPtr m_data;
229  };
230 
235  OSVR_COMMON_EXPORT sequence_type
236  put(pointer_to_const_type data, size_t len);
237 
242  OSVR_COMMON_EXPORT BufferWriteProxy put();
243 
246  OSVR_COMMON_EXPORT BufferReadProxy get(sequence_type num);
247 
251  OSVR_COMMON_EXPORT BufferReadProxy getLatest();
252 
254  OSVR_COMMON_EXPORT ~IPCRingBuffer();
255 
256  private:
257  static IPCRingBufferPtr m_constructorHelper(Options const &opts,
258  bool doCreate);
259  class Impl;
260  IPCRingBuffer(unique_ptr<Impl> &&impl);
261  unique_ptr<Impl> m_impl;
262  };
263 
264 } // namespace common
265 } // namespace osvr
266 
267 #endif // INCLUDED_IPCRingBuffer_h_GUID_4F33BDA7_1BEB_4E81_B96C_1ADA1CBD1997
Options & setAlignment(alignment_type alignment)
Sets the alignment for each entry, which must be a power of 2 (rounded up to the nearest if it's not)...
Header wrapping the C99 standard stdint header.
Options & setName(std::string const &name)
sets the name, after sanitizing the input string.
BufferReadProxy getLatest()
Gets access to the most recent element in the buffer: returns a proxy object that behaves mostly like...
Header to bring unique_ptr into the osvr namespace.
Header to bring shared_ptr into the osvr namespace.
shared_ptr< IPCRingBuffer > IPCRingBufferPtr
Pointer type for holding a shared memory ring buffer.
Definition: IPCRingBuffer.h:49
static IPCRingBufferPtr find(Options const &opts)
Named constructor, for use by client processes: accesses an IPC ring buffer using the options structu...
uint16_t getEntries() const
Returns the total capacity, in number of buffer entries, of this ring buffer.
sequence_type getSequenceNumber() const
Gets the sequence number associated with this entry.
static abi_level_type getABILevel()
Gets an integer representing a unique arrangement of the internal shared memory layout, such that if two processes try to communicate with different ABI levels, they will (likely) not succeed and thus should not try.
uint32_t sequence_type
The sequence number is automatically incremented with each "put" into the buffer. Note that...
BufferWriteProxy & operator=(BufferWriteProxy &&other)
move-assignable
A ring buffer for interprocess communication, with managed occupancy.
Definition: IPCRingBuffer.h:65
BufferWriteProxy(BufferWriteProxy &&other)
move-constructible
BackendType getBackend() const
Returns an integer identifying the IPC backend used.
BufferWriteProxy(BufferWriteProxy const &)=delete
not copyable
std::string const & getName() const
Returns the name string used to create or find this ring buffer.
Options & setEntries(entry_count_type entries)
Sets the number of entries in the ring buffer.
BufferWriteProxy & operator=(BufferWriteProxy const &)=delete
not copy-assignable
Options & setEntrySize(entry_size_type entrySize)
Sets the size of each entry in the ring buffer.
A class providing access to an entry in the ring buffer, holding a sharable mutex lock preventing it ...
Automatically-generated export header - do not edit!
smart_pointer_type getBufferSmartPointer() const
Gets a smart pointer to the buffer that shares ownership of the underlying resources of this object...
uint32_t getEntrySize() const
Returns the size of each individual buffer entry, in bytes.
BufferWriteProxy put()
Gets a proxy object for putting data in the next element in the buffer. You're responsible for doing ...
A class providing write access to the next available element in the ring buffer, owning the appropria...
static IPCRingBufferPtr create(Options const &opts)
Named constructor, for use by server processes: creates a shared memory ring buffer given the options...