2021-04-28 20:30:34 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Created by jief in 1997.
|
|
|
|
* Copyright (c) 2020 Jief
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
2020-08-11 14:43:53 +02:00
|
|
|
|
|
|
|
#if !defined(__XRBUFFER_H__)
|
|
|
|
#define __XRBUFFER_H__
|
|
|
|
|
|
|
|
#include <XToolsConf.h>
|
|
|
|
#include "XToolsCommon.h"
|
|
|
|
|
|
|
|
|
|
|
|
class XStringW;
|
|
|
|
class XString8;
|
|
|
|
class XString8Array;
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
template <typename T>
|
2020-08-11 14:43:53 +02:00
|
|
|
class XRBuffer
|
|
|
|
{
|
|
|
|
protected:
|
2020-08-12 17:15:47 +02:00
|
|
|
const T* _RData;
|
|
|
|
size_t m_size;
|
2020-08-11 14:43:53 +02:00
|
|
|
size_t _Index;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void Initialize(const void *p, size_t count);
|
2020-08-12 17:15:47 +02:00
|
|
|
XRBuffer() : _RData(0), m_size(0), _Index(0) { }
|
|
|
|
XRBuffer(const T* p, size_t count) : _RData(p), m_size(count), _Index(0) { }
|
2020-08-11 14:43:53 +02:00
|
|
|
XRBuffer(const XRBuffer& aXRBuffer, size_t pos = 0, size_t count = MAX_XSIZE);
|
2020-08-12 17:15:47 +02:00
|
|
|
// {
|
|
|
|
// if ( pos < aXRBuffer.Length() ) {
|
|
|
|
// _Len = count;
|
|
|
|
// if ( _Len > aXRBuffer.Length()-pos ) _Len = aXRBuffer.Length()-pos;
|
|
|
|
// _RData = (unsigned char*)aXRBuffer.Data(pos);
|
|
|
|
// }
|
|
|
|
// }
|
2020-08-11 14:43:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
// XRBuffer(const XBuffer &aBuffer, unsigned int pos = 0, unsigned int count = MAX_XSIZE);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
const T* data() const { return _RData; }
|
|
|
|
|
2020-08-11 14:43:53 +02:00
|
|
|
const char *CData(size_t ui=0) const { return (char *)(_RData+ui); }
|
2020-08-12 17:15:47 +02:00
|
|
|
const unsigned char *UCData(size_t ui=0) const { return (unsigned char*)(_RData+ui); }
|
2020-08-11 14:43:53 +02:00
|
|
|
|
|
|
|
size_t Sizeof() const;
|
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
size_t size() const { return m_size; }
|
|
|
|
size_t index() const { return _Index; }
|
2020-08-30 21:53:40 +02:00
|
|
|
|
|
|
|
bool operator == (const XRBuffer<T>& other) const {
|
|
|
|
if ( m_size != other.m_size ) return false;
|
|
|
|
if ( m_size == 0 ) return true;
|
|
|
|
if ( memcmp(_RData, other._RData, m_size) != 0 ) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool operator != (const XRBuffer<T>& other) const { return !(*this == other); }
|
2020-08-12 17:15:47 +02:00
|
|
|
|
|
|
|
template<typename IntegralType, enable_if(is_integral(IntegralType))>
|
|
|
|
void setIndex(IntegralType Idx)
|
|
|
|
{
|
|
|
|
if (Idx < 0) {
|
2022-01-23 13:19:15 +01:00
|
|
|
#ifdef JIEF_DEBUG
|
2021-02-21 14:49:09 +01:00
|
|
|
panic("XBuffer::setIndex : Idx < 0. System halted\n");
|
|
|
|
#else
|
2021-03-26 03:59:11 +01:00
|
|
|
_Index = 0;
|
2021-02-21 14:49:09 +01:00
|
|
|
#endif
|
2020-08-12 17:15:47 +02:00
|
|
|
}
|
|
|
|
_Index = Idx;
|
|
|
|
}
|
2020-08-11 14:43:53 +02:00
|
|
|
|
2020-08-12 17:15:47 +02:00
|
|
|
bool isEmpty() const { return m_size == 0 ; }
|
|
|
|
bool notEmpty() const { return m_size > 0 ; }
|
2020-08-11 14:43:53 +02:00
|
|
|
|
|
|
|
// Cast //
|
2020-08-30 21:53:40 +02:00
|
|
|
// Automatic cast are a bit dangerous...
|
|
|
|
// operator const char *() const { return CData(); }
|
2020-08-11 14:43:53 +02:00
|
|
|
|
|
|
|
// [] //
|
|
|
|
template<typename IntegralType, enable_if(is_integral(IntegralType))>
|
2020-08-12 17:15:47 +02:00
|
|
|
T& operator [](IntegralType i)
|
|
|
|
{
|
2020-08-11 14:43:53 +02:00
|
|
|
if (i < 0) {
|
2022-01-23 13:19:15 +01:00
|
|
|
#ifdef JIEF_DEBUG
|
2020-08-11 14:43:53 +02:00
|
|
|
panic("XRBuffer::operator [] : i < 0. System halted\n");
|
2021-02-21 14:49:09 +01:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2020-08-11 14:43:53 +02:00
|
|
|
}
|
2020-08-12 17:15:47 +02:00
|
|
|
if ( (unsigned_type(IntegralType))i >= m_size ) {
|
2022-01-23 13:19:15 +01:00
|
|
|
#ifdef JIEF_DEBUG
|
2021-02-21 14:49:09 +01:00
|
|
|
panic("XRBuffer::operator [] : index >= m_size. System halted\n");
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2020-08-11 14:43:53 +02:00
|
|
|
}
|
2020-08-30 21:53:40 +02:00
|
|
|
return _RData[i];
|
|
|
|
} // underflow ? overflow ?
|
|
|
|
template<typename IntegralType, enable_if(is_integral(IntegralType))>
|
|
|
|
const T& operator [](IntegralType i) const
|
|
|
|
{
|
|
|
|
if (i < 0) {
|
2022-01-23 13:19:15 +01:00
|
|
|
#ifdef JIEF_DEBUG
|
2020-08-30 21:53:40 +02:00
|
|
|
panic("XRBuffer::operator [] : i < 0. System halted\n");
|
2021-02-21 14:49:09 +01:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
2020-08-30 21:53:40 +02:00
|
|
|
if ( (unsigned_type(IntegralType))i >= m_size ) {
|
2022-01-23 13:19:15 +01:00
|
|
|
#ifdef JIEF_DEBUG
|
2020-08-30 21:53:40 +02:00
|
|
|
panic("XRBuffer::operator [] : index > len. System halted\n");
|
2021-02-21 14:49:09 +01:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2020-08-30 21:53:40 +02:00
|
|
|
}
|
|
|
|
return _RData[i];
|
2020-08-11 14:43:53 +02:00
|
|
|
} // underflow ? overflow ?
|
|
|
|
|
|
|
|
bool Get(void *buf, size_t count);
|
|
|
|
|
|
|
|
bool Get(bool *b) { return Get(b, sizeof(*b)); }
|
|
|
|
|
|
|
|
bool Get(char *c) { return Get(c, sizeof(*c)); }
|
|
|
|
bool Get(unsigned char *b) { return Get(b, sizeof(*b)); }
|
|
|
|
bool Get(short *s) { return Get(s, sizeof(*s)); }
|
|
|
|
bool Get(unsigned short *us) { return Get(us, sizeof(*us)); }
|
|
|
|
bool Get(int *i) { return Get(i, sizeof(*i)); };
|
|
|
|
bool Get(unsigned int *ui) { return Get(ui, sizeof(*ui)); };
|
|
|
|
bool Get(long *l) { return Get(l, sizeof(*l)); };
|
|
|
|
bool Get(unsigned long *ul) { return Get(ul, sizeof(*ul)); };
|
|
|
|
bool Get(long long *ll) { return Get(ll, sizeof(*ll)); };
|
|
|
|
bool Get(unsigned long long *ull) { return Get(ull, sizeof(*ull)); };
|
|
|
|
|
|
|
|
bool Get(float *f) { return Get(f, sizeof(*f)); };
|
|
|
|
bool Get(double *d) { return Get(d, sizeof(*d)); };
|
|
|
|
|
|
|
|
bool GetBool(bool *b) { return Get(b, sizeof(*b)); }
|
|
|
|
bool GetChar(char *c) { return Get(c, sizeof(*c)); }
|
|
|
|
bool GetByte(unsigned char *b) { return Get(b, sizeof(*b)); }
|
|
|
|
bool GetUShort(unsigned short *us) { return Get(us, sizeof(*us)); }
|
|
|
|
bool GetUInt(unsigned int *ui) { return Get(ui, sizeof(*ui)); };
|
|
|
|
bool GetULong(unsigned long *ul) { return Get(ul, sizeof(*ul)); };
|
|
|
|
bool GetULongLong(unsigned long long *ull) { return Get(ull, sizeof(*ull)); };
|
|
|
|
|
|
|
|
bool GetSize_t(size_t *size) { return Get(size, sizeof(*size)); };
|
|
|
|
|
|
|
|
size_t IdxOf(const XString8& aXString8, size_t Pos = 0) const;
|
|
|
|
size_t IdxOf(const XString8Array& aXString8Array, size_t Pos = 0, size_t *n = NULL) const;
|
|
|
|
|
|
|
|
// bool WriteToBuf(char *buf, size_t *idx, size_t count) const;
|
|
|
|
// void WriteToFileNameT(const char* FileName) const;
|
|
|
|
// bool WriteToFileName(const char* FileName) const;
|
|
|
|
|
|
|
|
// bool WriteToXFILE(XFILE *f) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|