📄 datatypes.h
字号:
// ------------------------------------------------------------------------
/// @file datatypes.h
/// @brief Overloaded operators for 16bit device
/// @author Harald Axmann
/// @date 08.05.2006
// ------------------------------------------------------------------------
#ifndef _DATATYPES_H_
#define _DATATYPES_H_
// ------------------------------------------------------------------------
typedef unsigned short uint8;
typedef unsigned short uint16;
typedef unsigned long uint32;
// ------------------------------------------------------------------------
/// @brief Special 8bit unsigned integer class for 16bit devices
///
/// Internally this class uses a 16 integer
struct _uip_uint8
{
public:
_uip_uint8() : v(v & 0x00FF) {}
_uip_uint8(uint8 value) : v(value & 0x00FF) {}
_uip_uint8(const _uip_uint8 &src) { v = src.v; }
_uip_uint8 &operator=(const _uip_uint8 &value) { v = value.v; return *this; }
_uip_uint8 &operator=(uint8 value) { v = value & 0x00FF; return *this; }
// volatile...
volatile _uip_uint8 &operator=(const _uip_uint8 &value) volatile { v = value.v; return *this; }
volatile _uip_uint8 &operator=(uint8 value) volatile { v = value & 0x00FF; return *this; }
uint8 toUint8() const volatile { return (v & 0x00FF); }
operator uint8() const volatile { return toUint8(); }
// operators
_uip_uint8 &operator++() { inc(); return *this; }
_uip_uint8 &operator--() { dec(); return *this; }
_uip_uint8 operator++(int) { _uip_uint8 temp = v; inc(); return temp; }
_uip_uint8 operator--(int) { _uip_uint8 temp = v; dec(); return temp; }
_uip_uint8 &operator+=(uint8 b) { v += b; v &= 0x00FF; return *this; }
_uip_uint8 &operator-=(uint8 b) { v -= b; v &= 0x00FF; return *this; }
_uip_uint8 &operator+=(const _uip_uint8 &b) { v += b.v; v &= 0x00FF; return *this; }
_uip_uint8 &operator-=(const _uip_uint8 &b) { v -= b.v; v &= 0x00FF; return *this; }
_uip_uint8 &operator|=(uint8 value) { v |= value & 0x00FF; return *this; }
_uip_uint8 &operator&=(uint8 value) { v &= value; return *this; }
_uip_uint8 &operator|=(const _uip_uint8 &b) { v |= b.v; return *this; }
_uip_uint8 &operator&=(const _uip_uint8 &b) { v &= b.v; return *this; }
// volatile...
volatile _uip_uint8 &operator++() volatile { inc(); return *this; }
volatile _uip_uint8 &operator--() volatile { dec(); return *this; }
volatile _uip_uint8 operator++(int) volatile { _uip_uint8 temp = v; inc(); return temp; }
volatile _uip_uint8 operator--(int) volatile { _uip_uint8 temp = v; dec(); return temp; }
volatile _uip_uint8 &operator+=(uint8 b) volatile { v += b; v &= 0x00FF; return *this; }
volatile _uip_uint8 &operator-=(uint8 b) volatile { v -= b; v &= 0x00FF; return *this; }
volatile _uip_uint8 &operator|=(uint8 value) volatile { v |= value & 0x00FF; return *this; }
volatile _uip_uint8 &operator&=(uint8 value) volatile { v &= value; return *this; }
volatile _uip_uint8 &operator|=(const _uip_uint8 &b) volatile { v |= b.v; return *this; }
volatile _uip_uint8 &operator&=(const _uip_uint8 &b) volatile { v &= b.v; return *this; }
bool operator!() const { return v==0; }
protected:
uint16 v;
void inc() volatile { v++; v &= 0x00FF; }
void dec() volatile { v--; v &= 0x00FF; }
};
// ------------------------------------------------------------------------
/// @brief Special 16bit unsigned integer class for 16bit devicesstruct _uip_uint16
///
/// Internally this class used a 32bit integer
struct _uip_uint16
{
public:
_uip_uint16() : v(0) {}
_uip_uint16(uint16 value) : v((((uint32)(value & 0xFF00)) << 8) + (value & 0x00FF)) {}
_uip_uint16(const _uip_uint8 &src) { v = src.toUint8(); }
_uip_uint16(const _uip_uint16 &src) { v = src.v; }
_uip_uint16 &operator=(const _uip_uint8 &value) { v = value.toUint8(); return *this; }
_uip_uint16 &operator=(const _uip_uint16 &value) { v = value.v; return *this; }
_uip_uint16 &operator=(uint16 value) { bytes.l = value & 0x00FF; bytes.h = (value & 0xFF00) >> 8; return *this; }
// volatile...
_uip_uint16(const volatile _uip_uint8 &src) { v = src.toUint8(); }
_uip_uint16(const volatile _uip_uint16 &src) { v = src.v; }
volatile _uip_uint16 &operator=(const _uip_uint8 &value) volatile { v = value.toUint8(); return *this; }
volatile _uip_uint16 &operator=(const _uip_uint16 &value) volatile { v = value.v; return *this; }
volatile _uip_uint16 &operator=(uint16 value) volatile { bytes.l = value & 0x00FF; bytes.h = (value & 0xFF00) >> 8; return *this; }
uint16 toUint16() const volatile { return (bytes.l & 0x00FF) + (bytes.h<<8); }
operator uint16() const volatile { return toUint16(); }
// operators
_uip_uint16 &operator++() { inc(); return *this; }
_uip_uint16 &operator--() { dec(); return *this; }
_uip_uint16 operator++(int) { _uip_uint16 temp = v; inc(); return temp; }
_uip_uint16 operator--(int) { _uip_uint16 temp = v; dec(); return temp; }
_uip_uint16 &operator+=(unsigned long b) { bytes.l += b; correct(); return *this; }
_uip_uint16 &operator-=(unsigned long b) { bytes.l -= b; correct(); return *this; }
_uip_uint16 &operator+=(const _uip_uint8 &b) { v += b.toUint8(); correct(); return *this; }
_uip_uint16 &operator-=(const _uip_uint8 &b) { v -= b.toUint8(); v&=0x00FF00FF; return *this; }
_uip_uint16 &operator+=(const _uip_uint16 &b) { v += b.v; correct(); return *this; }
_uip_uint16 &operator-=(const _uip_uint16 &b) { v -= b.v; v&=0x00FF00FF; return *this; }
// volatile...
volatile _uip_uint16 &operator+=(unsigned long b) volatile { bytes.l += b; correct(); return *this; }
volatile _uip_uint16 &operator-=(unsigned long b) volatile { bytes.l -= b; correct(); return *this; }
volatile _uip_uint16 &operator+=(const volatile _uip_uint8 &b) volatile { v += b.toUint8(); correct(); return *this; }
volatile _uip_uint16 &operator-=(const volatile _uip_uint8 &b) volatile { v -= b.toUint8(); v&=0x00FF00FF; return *this; }
volatile _uip_uint16 &operator+=(const volatile _uip_uint16 &b) volatile { v += b.v; correct(); return *this; }
volatile _uip_uint16 &operator-=(const volatile _uip_uint16 &b) volatile { v -= b.v; v&=0x00FF00FF; return *this; }
volatile _uip_uint16 &operator++() volatile { inc(); return *this; }
volatile _uip_uint16 &operator--() volatile { dec(); return *this; }
volatile _uip_uint16 operator++(int) volatile { _uip_uint16 temp = v; inc(); return temp; }
volatile _uip_uint16 operator--(int) volatile { _uip_uint16 temp = v; dec(); return temp; }
bool operator!() const { return v==0; }
protected:
union
{
unsigned long v;
struct
{
unsigned short l;
unsigned short h;
} bytes;
};
_uip_uint16(unsigned long value,int) : v(value) {}
void inc() volatile { bytes.l++; correct(); }
void dec() volatile { bytes.l--; correct(); }
void correct() volatile { if(bytes.l & 0xFF00) { bytes.h += bytes.l>>8; v&=0x00FF00FF; } }
};
// ------------------------------------------------------------------------
/// @brief Special type for saving data in a packed form
///
/// Especially for the HTTP implementation it is necessary to store a
/// huge amount of byte data. By default those bytes consume 16bits each.
/// To get rid of this overhead, the bytes can be stored in a 16bit array
/// in packed form and then wrapped in an object of this class. With
/// this object, the data can be used in the same way as a pointer to a
/// character array on an 8bit machine.
///
/// Note, that internally only a pointer to the memory will be saved.
/// The programmer must ensure, that the pointer is valid during the
/// lifetime of this object.
class const_packed_t
{
public:
/// @brief Dereferenced representaion
class Byte
{
public:
friend class const_packed_t;
operator uint8() const { return (((*data_ >> 8) & odd_) | (*data_ & 0xFF & ~odd_)); }
const_packed_t operator&() { return const_packed_t(data_, odd_ & 0x01); }
const_packed_t operator&() const { return const_packed_t(data_, odd_ & 0x01); }
protected:
Byte(const uint16 *data, uint8 odd) : data_(data), odd_(-odd) {}
const uint16 *data_;
uint8 odd_;
};
friend class Byte;
const_packed_t() : data_(0), odd_(false) {}
const_packed_t(const uint16 *data) : data_(data), odd_(false) {}
const_packed_t(const const_packed_t &src) : data_(src.data_), odd_(src.odd_) {}
~const_packed_t() {}
const_packed_t &operator=(const uint16 *data) {
data_ = data; odd_ = false; return *this; }
const_packed_t &operator=(const const_packed_t &src) {
data_ = src.data_; odd_ = src.odd_; return *this; }
/// @brief Convert to 8bit integer (internal)
///
/// This method is intended for internal usage only. For compatibility
/// with normal character pointers you should not use it.
uint8 getValue() const { return (((*data_ >> 8) & (-odd_)) | (*data_ & 0xFF & ~(-odd_))); }
Byte operator*() const { return Byte(data_, odd_); }
Byte operator[](int index) const { return Byte(data_ + (index >> 1) + (odd_ & index), odd_ ^ (index & 0x1)); }
bool operator==(const const_packed_t &value) const { return value.data_ == data_ && value.odd_ == odd_; }
bool operator!=(const const_packed_t &value) const { return value.data_ != data_ || value.odd_ != odd_; }
bool operator==(const void *value) const { return data_ == value && !odd_; }
bool operator!=(const void *value) const { return data_ != value || odd_; }
const_packed_t &operator+=(int value) { inc(value); return *this; }
const_packed_t operator+ (int value) const { const_packed_t temp(*this); temp.inc(value); return temp; }
const_packed_t &operator++() { inc(); return *this; }
const_packed_t &operator--() { dec(); return *this; }
const const_packed_t operator++(int) {
const_packed_t temp = *this; inc(); return temp; }
const const_packed_t operator--(int) {
const_packed_t temp = *this; dec(); return temp; }
protected:
const_packed_t(const uint16 *data, uint8 odd) : data_(data), odd_(odd) {}
const uint16 *data_;
uint8 odd_;
void inc() { data_ += odd_; odd_ ^= 0x1; }
void dec() { odd_ ^= 0x1; data_ -= odd_; }
void inc(int value) { data_ += (value >> 1) + (odd_ & value); odd_ ^= value & 0x1; }
};
// ------------------------------------------------------------------------
/// @brief Constant data in packed or unpacked format
///
/// This class provides an easy way to deal with data arrays that might
/// be in packed or in unpacked format. A new object can be created with
/// either of the two formats by using the respective constructor. The
/// format can even be changed at lifetime by assigning a new array
/// using the assignment operator.
///
/// In advance you can use the object, as if it were a real pointer to
/// an array. Internally in all arithmetic operations both pointers will
/// be affected, as it is optimized for the DSP, which does not like
/// branches. Only when evaluated as 8bit integer it will check, if the
/// controlled array is in packed or in unpacked format.
///
/// Note: The class will not make a copy of the given data. The programmer
/// must ensure the appropriate lifetime of the addressed array.
class const_data_t
{
public:
/// @brief Dereferenced representaion
class Byte
{
public:
friend class const_data_t;
operator uint8() const { return (*packed_data_ & packed_) | (*data_ & ~packed_); }
const_data_t operator&() { return const_data_t(packed_data_, data_, packed_); }
const_data_t operator&() const { return const_data_t(packed_data_, data_, packed_); }
protected:
Byte(const const_packed_t &packed_data, const char *data, uint8 packed)
: packed_data_(packed_data), data_(data), packed_(packed) {}
const_packed_t packed_data_;
const char *data_;
uint8 packed_;
};
friend class Byte;
const_data_t()
: packed_data_(0), data_(0), packed_(FALSE) {}
const_data_t(const const_packed_t &packed_data)
: packed_data_(packed_data), data_(0), packed_(TRUE) {}
const_data_t(const char *data)
: packed_data_(0), data_(data), packed_(FALSE) {}
const_data_t(const const_data_t &src)
: packed_data_(src.packed_data_), data_(src.data_), packed_(src.packed_) {}
~const_data_t() {}
const_data_t &operator=(const const_packed_t &packed_data) {
packed_data_ = packed_data; data_ = 0; packed_ = TRUE; return *this; }
const_data_t &operator=(const char *data) {
packed_data_ = 0; data_ = data; packed_ = FALSE; return *this; }
const_data_t &operator=(const const_data_t &src) {
packed_data_ = src.packed_data_; data_ = src.data_; packed_ = src.packed_; return *this; }
Byte operator*() const { return Byte(packed_data_, data_, packed_); }
Byte operator[](int index) const { return Byte(packed_data_ + index, data_ + index, packed_); }
bool operator==(const const_data_t &value) const { return ((packed_ == value.packed_) &&
((packed_ && packed_data_ == value.packed_data_) || (!packed_ && data_ == value.data_))); }
bool operator!=(const const_data_t &value) const { return ((packed_ != value.packed_) ||
((packed_ && packed_data_ != value.packed_data_) || (!packed_ && data_ != value.data_))); }
bool operator==(const void *value) const { return
((packed_ && packed_data_ == value) || (!packed_ && data_ == value)); }
bool operator!=(const void *value) const { return
((packed_ && packed_data_ != value) || (!packed_ && data_ != value)); }
const_data_t &operator+=(int value) { packed_data_ += value; data_ += value; return *this; }
const_data_t &operator++() { inc(); return *this; }
const_data_t &operator--() { dec(); return *this; }
const const_data_t operator++(int) { const_data_t temp = *this; inc(); return temp; }
const const_data_t operator--(int) { const_data_t temp = *this; dec(); return temp; }
protected:
const_data_t(const const_packed_t &packed_data, const char *data, uint8 packed)
: packed_data_(packed_data), data_(data), packed_(packed) {}
const_packed_t packed_data_;
const char *data_;
uint8 packed_;
void inc() { ++packed_data_; ++data_; }
void dec() { --packed_data_; --data_; }
static const uint8 TRUE = ~0;
static const uint8 FALSE = 0;
};
// ------------------------------------------------------------------------
#endif // _DATATYPES_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -