std::bitset<> replaced with BitSet<>; stdexcept is not included unless exceptions are enabled

This commit is contained in:
Pavel Kirienko
2014-05-04 20:49:58 +04:00
parent 9b465a0959
commit d0c2898def
5 changed files with 203 additions and 11 deletions
+186
View File
@@ -0,0 +1,186 @@
/*
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
*/
#pragma once
#include <cassert>
#include <cstddef>
#include <cstring>
#include <uavcan/impl_constants.hpp>
namespace uavcan
{
/**
* STL-like bitset
*/
template <std::size_t NumBits>
class BitSet
{
enum { NumBytes = (NumBits + 7) / 8 };
static std::size_t getByteNum(std::size_t bit_num) { return bit_num / 8; }
static std::size_t getBitNum(const std::size_t bit_num) { return bit_num % 8; }
static void validatePos(std::size_t& inout_pos)
{
if (inout_pos >= NumBits)
{
assert(0);
inout_pos = NumBits - 1;
}
}
char data_[NumBytes];
public:
class Reference
{
friend class BitSet;
BitSet* const parent_;
const std::size_t bitpos_;
Reference(BitSet* arg_parent, std::size_t arg_bitpos)
: parent_(arg_parent)
, bitpos_(arg_bitpos)
{ }
public:
Reference& operator=(bool x)
{
parent_->set(bitpos_, x);
return *this;
}
Reference& operator=(const Reference& x)
{
parent_->set(bitpos_, x);
return *this;
}
bool operator~() const
{
return !parent_->test(bitpos_);
}
operator bool() const
{
return parent_->test(bitpos_);
}
};
BitSet()
: data_()
{
reset();
}
BitSet<NumBits>& reset()
{
std::memset(data_, 0, NumBytes);
return *this;
}
BitSet<NumBits>& set()
{
std::memset(data_, 0xFF, NumBytes);
return *this;
}
BitSet<NumBits>& set(std::size_t pos, bool val = true)
{
validatePos(pos);
if (val)
{
data_[getByteNum(pos)] |= (1 << getBitNum(pos));
}
else
{
data_[getByteNum(pos)] &= ~(1 << getBitNum(pos));
}
return *this;
}
bool test(std::size_t pos) const
{
return (data_[getByteNum(pos)] & (1 << getBitNum(pos))) != 0;
}
bool any() const
{
for (std::size_t i = 0; i < NumBits; ++i)
{
if (test(i))
{
return true;
}
}
return false;
}
std::size_t count() const
{
std::size_t retval = 0;
for (std::size_t i = 0; i < NumBits; ++i)
{
retval += test(i) ? 1U : 0U;
}
return retval;
}
std::size_t size() const { return NumBits; }
bool operator[](std::size_t pos) const
{
return test(pos);
}
Reference operator[](std::size_t pos)
{
validatePos(pos);
return Reference(this, pos);
}
BitSet<NumBits>& operator=(const BitSet<NumBits> & rhs)
{
if (&rhs == this)
{
return *this;
}
for (std::size_t i = 0; i < NumBytes; ++i)
{
data_[i] = rhs.data_[i];
}
return *this;
}
bool operator!=(const BitSet<NumBits>& rhs) const { return !operator==(rhs); }
bool operator==(const BitSet<NumBits>& rhs) const
{
for (std::size_t i = 0; i < NumBits; ++i)
{
if (test(i) != rhs.test(i))
{
return false;
}
}
return true;
}
};
template <> class BitSet<0>; ///< Invalid instantiation
template <typename Stream, std::size_t NumBits>
Stream& operator<<(Stream& s, const BitSet<NumBits>& x)
{
for (std::size_t i = NumBits; i > 0; --i)
{
s << (x.test(i-1) ? "1" : "0");
}
return s;
}
}
+9 -6
View File
@@ -5,13 +5,12 @@
#pragma once
#include <cassert>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <stdexcept>
#include <cstring>
#include <cmath>
#include <uavcan/error.hpp>
#include <uavcan/bitset.hpp>
#include <uavcan/util/compile_time.hpp>
#include <uavcan/impl_constants.hpp>
#include <uavcan/marshal/type_util.hpp>
@@ -21,6 +20,10 @@
# error UAVCAN_EXCEPTIONS
#endif
#if UAVCAN_EXCEPTIONS
# include <stdexcept>
#endif
namespace uavcan
{
@@ -202,7 +205,7 @@ public:
*/
template <unsigned MaxSize, ArrayMode ArrayMode, CastMode CastMode>
class UAVCAN_EXPORT ArrayImpl<IntegerSpec<1, SignednessUnsigned, CastMode>, ArrayMode, MaxSize>
: public std::bitset<MaxSize>
: public BitSet<MaxSize>
, public Select<ArrayMode == ArrayModeDynamic, DynamicArrayBase<MaxSize>, StaticArrayBase<MaxSize> >::Result
{
typedef typename Select<ArrayMode == ArrayModeDynamic,
@@ -211,14 +214,14 @@ class UAVCAN_EXPORT ArrayImpl<IntegerSpec<1, SignednessUnsigned, CastMode>, Arra
public:
enum { IsStringLike = 0 };
typedef typename std::bitset<MaxSize>::reference Reference;
typedef typename BitSet<MaxSize>::Reference Reference;
typedef typename ArrayBase::SizeType SizeType;
using ArrayBase::size;
using ArrayBase::capacity;
Reference at(SizeType pos) { return std::bitset<MaxSize>::operator[](ArrayBase::validateRange(pos)); }
bool at(SizeType pos) const { return std::bitset<MaxSize>::operator[](ArrayBase::validateRange(pos)); }
Reference at(SizeType pos) { return BitSet<MaxSize>::operator[](ArrayBase::validateRange(pos)); }
bool at(SizeType pos) const { return BitSet<MaxSize>::operator[](ArrayBase::validateRange(pos)); }
Reference operator[](SizeType pos) { return at(pos); }
bool operator[](SizeType pos) const { return at(pos); }
@@ -5,11 +5,11 @@
#pragma once
#include <cassert>
#include <bitset>
#include <algorithm>
#include <uavcan/error.hpp>
#include <uavcan/stdint.hpp>
#include <uavcan/data_type.hpp>
#include <uavcan/bitset.hpp>
#include <uavcan/util/compile_time.hpp>
#include <uavcan/linked_list.hpp>
#if UAVCAN_DEBUG
@@ -19,7 +19,7 @@
namespace uavcan
{
typedef std::bitset<DataTypeID::Max + 1> DataTypeIDMask;
typedef BitSet<DataTypeID::Max + 1> DataTypeIDMask;
class UAVCAN_EXPORT GlobalDataTypeRegistry : Noncopyable
{
@@ -4,7 +4,7 @@
#pragma once
#include <bitset>
#include <uavcan/bitset.hpp>
#include <uavcan/util/method_binder.hpp>
#include <uavcan/node/subscriber.hpp>
#include <uavcan/node/service_client.hpp>
@@ -26,7 +26,7 @@ struct UAVCAN_EXPORT NetworkCompatibilityCheckResult
*/
class UAVCAN_EXPORT NetworkCompatibilityChecker : Noncopyable
{
typedef std::bitset<NodeID::Max + 1> NodeIDMask;
typedef BitSet<NodeID::Max + 1> NodeIDMask;
typedef MethodBinder<NetworkCompatibilityChecker*,
void (NetworkCompatibilityChecker::*)(const ReceivedDataStructure<protocol::NodeStatus>&)>
NodeStatusCallback;
+4 -1
View File
@@ -5,12 +5,15 @@
#include <uavcan/error.hpp>
#include <cassert>
#include <cstdlib>
#include <stdexcept>
#ifndef UAVCAN_EXCEPTIONS
# error UAVCAN_EXCEPTIONS
#endif
#if UAVCAN_EXCEPTIONS
# include <stdexcept>
#endif
namespace uavcan
{