OSVR-Core
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
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
Header wrapping the C99 standard stdint header.
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: IPCRingBuffer.h:72
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
Definition: IPCRingBuffer.cpp:289
uint32_t sequence_type
The sequence number is automatically incremented with each "put" into the buffer. ...
Definition: IPCRingBuffer.h:149
Definition: newuoa.h:1888
BufferWriteProxy & operator=(BufferWriteProxy &&other)
move-assignable
Definition: IPCRingBuffer.h:174
A ring buffer for interprocess communication, with managed occupancy.
Definition: IPCRingBuffer.h:65
BufferWriteProxy(BufferWriteProxy &&other)
move-constructible
Definition: IPCRingBuffer.h:169
sequence_type getSequenceNumber() const
Gets the sequence number associated with this entry.
Definition: IPCRingBuffer.h:217
A class providing access to an entry in the ring buffer, holding a sharable mutex lock preventing it ...
Definition: IPCRingBuffer.h:203
Definition: ProjectionMatrix.h:77
A class providing write access to the next available element in the ring buffer, owning the appropria...
Definition: IPCRingBuffer.h:160