BitSetBuffer.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DEVICES_MULTIAXIS_BITSETBUFFER_H
17 #define SURGSIM_DEVICES_MULTIAXIS_BITSETBUFFER_H
18 
19 #include <string>
21 
22 
23 namespace SurgSim
24 {
25 namespace Device
26 {
27 
35 template <size_t N>
37 {
38 public:
41  {
42  reset();
43  }
44 
46  BitSetBuffer(const BitSetBuffer& other) :
47  m_bytes(other.m_bytes)
48  {
49  }
50 
53  {
54  m_bytes = other.m_bytes;
55  return *this;
56  }
57 
59  void set()
60  {
61  m_bytes.fill(~static_cast<value_type>(0));
62  }
63 
66  void set(size_t pos)
67  {
68  SURGSIM_ASSERT(pos < NUM_BITS);
69  m_bytes[pos / ELEMENT_BITS] |= (1U << (pos % ELEMENT_BITS));
70  }
71 
73  void reset()
74  {
75  m_bytes.fill(0);
76  }
77 
80  void reset(size_t pos)
81  {
82  SURGSIM_ASSERT(pos < NUM_BITS);
83  m_bytes[pos / ELEMENT_BITS] &= ~(1U << (pos % ELEMENT_BITS));
84  }
85 
88  bool test(size_t pos) const
89  {
90  SURGSIM_ASSERT(pos < NUM_BITS);
91  return ((m_bytes[pos / ELEMENT_BITS] & (1U << (pos % ELEMENT_BITS))) != 0);
92  }
93 
95  void* getPointer()
96  {
97  return &(m_bytes[0]);
98  }
99 
101  const void* getPointer() const
102  {
103  return &(m_bytes[0]);
104  }
105 
107  static size_t size()
108  {
109  return NUM_BITS;
110  }
111 
113  static size_t sizeBytes()
114  {
115  return NUM_BYTES;
116  }
117 
118 private:
119  typedef unsigned char value_type;
120  static const size_t ELEMENT_BYTES = sizeof(value_type);
121  static const size_t ELEMENT_BITS = ELEMENT_BYTES * 8;
122  static_assert(ELEMENT_BITS == 8, "An unsigned char is not 8 bits?!");
123 
124  static const size_t NUM_BITS = N;
125  static const size_t NUM_BYTES = (NUM_BITS + ELEMENT_BITS - 1) / ELEMENT_BITS;
126 
127  std::array<value_type, NUM_BYTES> m_bytes;
128 };
129 
130 }; // namespace Device
131 }; // namespace SurgSim
132 
133 #endif // SURGSIM_DEVICES_MULTIAXIS_BITSETBUFFER_H
Definition: DriveElementFromInputBehavior.cpp:27
unsigned char value_type
Definition: BitSetBuffer.h:119
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
BitSetBuffer(const BitSetBuffer &other)
Create a bit buffer by copying another buffer.
Definition: BitSetBuffer.h:46
static const size_t ELEMENT_BITS
Definition: BitSetBuffer.h:121
static const size_t NUM_BITS
Definition: BitSetBuffer.h:124
static const size_t ELEMENT_BYTES
Definition: BitSetBuffer.h:120
void reset(size_t pos)
Reset the specified bit in the buffer to off.
Definition: BitSetBuffer.h:80
BitSetBuffer & operator=(const BitSetBuffer &other)
Copy bit buffer contents from another buffer.
Definition: BitSetBuffer.h:52
void reset()
Reset all bits in the buffer to off.
Definition: BitSetBuffer.h:73
const void * getPointer() const
Get a pointer to the buffer&#39;s storage.
Definition: BitSetBuffer.h:101
The header that provides the assertion API.
static const size_t NUM_BYTES
Definition: BitSetBuffer.h:125
BitSetBuffer()
Create a bit buffer with all bits set to zero.
Definition: BitSetBuffer.h:40
std::array< value_type, NUM_BYTES > m_bytes
Definition: BitSetBuffer.h:127
void * getPointer()
Get a pointer to the buffer&#39;s storage.
Definition: BitSetBuffer.h:95
A bit set corresponding to a contiguous memory buffer.
Definition: BitSetBuffer.h:36
bool test(size_t pos) const
Get the specified bit in the buffer.
Definition: BitSetBuffer.h:88
static size_t size()
Get the number of bits in the bit set.
Definition: BitSetBuffer.h:107
static size_t sizeBytes()
Get the number of bytes in the bit set.
Definition: BitSetBuffer.h:113