BitSetBuffer.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_DEVICES_MULTIAXIS_BITSETBUFFER_H
#define SURGSIM_DEVICES_MULTIAXIS_BITSETBUFFER_H
#include <string>
#include "SurgSim/Framework/Assert.h"
namespace SurgSim
{
namespace Devices
{
template <size_t N>
class BitSetBuffer
{
public:
BitSetBuffer()
{
reset();
}
BitSetBuffer(const BitSetBuffer& other) :
m_bytes(other.m_bytes)
{
}
BitSetBuffer& operator=(const BitSetBuffer& other)
{
m_bytes = other.m_bytes;
return *this;
}
void set()
{
m_bytes.fill(~static_cast<value_type>(0));
}
void set(size_t pos)
{
SURGSIM_ASSERT(pos < NUM_BITS);
m_bytes[pos / ELEMENT_BITS] |= (1U << (pos % ELEMENT_BITS));
}
void reset()
{
m_bytes.fill(0);
}
void reset(size_t pos)
{
SURGSIM_ASSERT(pos < NUM_BITS);
m_bytes[pos / ELEMENT_BITS] &= ~(1U << (pos % ELEMENT_BITS));
}
bool test(size_t pos) const
{
SURGSIM_ASSERT(pos < NUM_BITS);
return ((m_bytes[pos / ELEMENT_BITS] & (1U << (pos % ELEMENT_BITS))) != 0);
}
void* getPointer()
{
return &(m_bytes[0]);
}
const void* getPointer() const
{
return &(m_bytes[0]);
}
static size_t size()
{
return NUM_BITS;
}
static size_t sizeBytes()
{
return NUM_BYTES;
}
private:
typedef unsigned char value_type;
static const size_t ELEMENT_BYTES = sizeof(value_type);
static const size_t ELEMENT_BITS = ELEMENT_BYTES * 8;
static_assert(ELEMENT_BITS == 8, "An unsigned char is not 8 bits?!");
static const size_t NUM_BITS = N;
static const size_t NUM_BYTES = (NUM_BITS + ELEMENT_BITS - 1) / ELEMENT_BITS;
std::array<value_type, NUM_BYTES> m_bytes;
};
}; // namespace Devices
}; // namespace SurgSim
#endif // SURGSIM_DEVICES_MULTIAXIS_BITSETBUFFER_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Devices::BitSetBuffer::value_type
unsigned char value_type
Definition: BitSetBuffer.h:119
SurgSim::Devices::BitSetBuffer::getPointer
void * getPointer()
Get a pointer to the buffer&#39;s storage.
Definition: BitSetBuffer.h:95
SurgSim::Devices::BitSetBuffer::BitSetBuffer
BitSetBuffer(const BitSetBuffer &other)
Create a bit buffer by copying another buffer.
Definition: BitSetBuffer.h:46
SurgSim::Devices::BitSetBuffer::getPointer
const void * getPointer() const
Get a pointer to the buffer&#39;s storage.
Definition: BitSetBuffer.h:101
SurgSim::Devices::BitSetBuffer::ELEMENT_BITS
static const size_t ELEMENT_BITS
Definition: BitSetBuffer.h:121
SurgSim::Devices::BitSetBuffer::NUM_BITS
static const size_t NUM_BITS
Definition: BitSetBuffer.h:124
SurgSim::Devices::BitSetBuffer::test
bool test(size_t pos) const
Get the specified bit in the buffer.
Definition: BitSetBuffer.h:88
SurgSim::Devices::BitSetBuffer::reset
void reset()
Reset all bits in the buffer to off.
Definition: BitSetBuffer.h:73
SurgSim::Devices::BitSetBuffer::size
static size_t size()
Get the number of bits in the bit set.
Definition: BitSetBuffer.h:107
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
SurgSim::Devices::BitSetBuffer::BitSetBuffer
BitSetBuffer()
Create a bit buffer with all bits set to zero.
Definition: BitSetBuffer.h:40
SurgSim::Devices::BitSetBuffer::reset
void reset(size_t pos)
Reset the specified bit in the buffer to off.
Definition: BitSetBuffer.h:80
SurgSim::Devices::BitSetBuffer
A bit set corresponding to a contiguous memory buffer.
Definition: BitSetBuffer.h:36
SurgSim::Devices::BitSetBuffer::ELEMENT_BYTES
static const size_t ELEMENT_BYTES
Definition: BitSetBuffer.h:120
SurgSim::Devices::BitSetBuffer::m_bytes
std::array< value_type, NUM_BYTES > m_bytes
Definition: BitSetBuffer.h:127
Assert.h
The header that provides the assertion API.
SurgSim::Devices::BitSetBuffer::operator=
BitSetBuffer & operator=(const BitSetBuffer &other)
Copy bit buffer contents from another buffer.
Definition: BitSetBuffer.h:52
SurgSim::Devices::BitSetBuffer::NUM_BYTES
static const size_t NUM_BYTES
Definition: BitSetBuffer.h:125
SurgSim::Devices::BitSetBuffer::sizeBytes
static size_t sizeBytes()
Get the number of bytes in the bit set.
Definition: BitSetBuffer.h:113