细化参数的检查和判断
This commit is contained in:
parent
513a27b876
commit
f5c539c333
14
Stream.cpp
14
Stream.cpp
|
@ -24,7 +24,7 @@ Stream::Stream(uint len)
|
|||
Stream::Stream(byte* buf, uint len)
|
||||
{
|
||||
assert_ptr(buf);
|
||||
assert_param(len > 0);
|
||||
assert_param2(len > 0, "不能用0长度缓冲区来初始化数据流");
|
||||
|
||||
_Buffer = buf;
|
||||
_Capacity = len;
|
||||
|
@ -64,7 +64,7 @@ uint Stream::Position() { return _Position; }
|
|||
void Stream::SetPosition(uint p)
|
||||
{
|
||||
// 允许移动到最后一个字节之后,也就是Length
|
||||
assert_param(p <= Length);
|
||||
assert_param2(p <= Length, "设置的位置超出长度");
|
||||
|
||||
_Position = p;
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ byte* Stream::Current() { return &_Buffer[_Position]; }
|
|||
// 从当前位置读取数据
|
||||
uint Stream::Read(byte* buf, uint offset, int count)
|
||||
{
|
||||
assert_param(buf);
|
||||
assert_param2(buf, "从数据流读取数据需要有效的缓冲区");
|
||||
|
||||
if(count == 0) return 0;
|
||||
|
||||
|
@ -136,7 +136,7 @@ uint Stream::ReadEncodeInt()
|
|||
// 把数据写入当前位置
|
||||
void Stream::Write(byte* buf, uint offset, uint count)
|
||||
{
|
||||
assert_param(buf);
|
||||
assert_param2(buf, "向数据流写入数据需要有效的缓冲区");
|
||||
|
||||
if(!CheckCapacity(count)) return;
|
||||
|
||||
|
@ -238,7 +238,11 @@ uint Stream::ReadArray(ByteArray& bs)
|
|||
uint len = ReadEncodeInt();
|
||||
if(!len) return 0;
|
||||
|
||||
assert_param(len <= bs.Capacity());
|
||||
if(len <= bs.Capacity())
|
||||
{
|
||||
debug_printf("准备读取的数据长度是 0x%08X,而缓冲区数组容量是 0x%08X\r\n", len, bs.Capacity());
|
||||
assert_param2(len <= bs.Capacity(), "缓冲区大小不足");
|
||||
}
|
||||
|
||||
Read(bs.GetBuffer(), 0, len);
|
||||
|
||||
|
|
62
Sys.h
62
Sys.h
|
@ -6,6 +6,37 @@
|
|||
#include <string.h>
|
||||
#include "Platform\stm32.h"
|
||||
|
||||
// 强迫内联
|
||||
#define _force_inline __attribute__( ( always_inline ) ) __INLINE
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#if defined(DEBUG) || defined(MSGDEBUG)
|
||||
|
||||
#define debug_printf printf
|
||||
|
||||
#else
|
||||
|
||||
__inline void debug_printf( const char *format, ... ) {}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
|
||||
// 验证确保对象不为空,并且在有效的内存范围内
|
||||
#define assert_ptr(expr) (assert_ptr_(expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
|
||||
bool assert_ptr_(const void* p);
|
||||
|
||||
void assert_failed(const char* msg, uint8_t* file, uint32_t line);
|
||||
#define assert_param2(expr, msg) ((expr) ? (void)0 : assert_failed(msg, (uint8_t *)__FILE__, __LINE__))
|
||||
|
||||
#else
|
||||
|
||||
#define assert_ptr(expr) ((void)0)
|
||||
|
||||
#endif
|
||||
|
||||
#include "Type.h"
|
||||
|
||||
/* 引脚定义 */
|
||||
|
@ -108,37 +139,6 @@ public:
|
|||
|
||||
extern TSys Sys; //创建一个全局的Sys对象 会在main函数之前执行构造函数(!!!!!)
|
||||
|
||||
// 强迫内联
|
||||
#define _force_inline __attribute__( ( always_inline ) ) __INLINE
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#if defined(DEBUG) || defined(MSGDEBUG)
|
||||
|
||||
#define debug_printf printf
|
||||
|
||||
#else
|
||||
|
||||
__inline void debug_printf( const char *format, ... ) {}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
|
||||
// 验证确保对象不为空,并且在有效的内存范围内
|
||||
#define assert_ptr(expr) (assert_ptr_(expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
|
||||
bool assert_ptr_(const void* p);
|
||||
|
||||
void assert_failed(const char* msg, uint8_t* file, uint32_t line);
|
||||
#define assert_param2(expr, msg) ((expr) ? (void)0 : assert_failed(msg, (uint8_t *)__FILE__, __LINE__))
|
||||
|
||||
#else
|
||||
|
||||
#define assert_ptr(expr) ((void)0)
|
||||
|
||||
#endif
|
||||
|
||||
// 任务
|
||||
//#include "Task.h"
|
||||
|
||||
|
|
57
Type.h
57
Type.h
|
@ -143,16 +143,21 @@ public:
|
|||
virtual ~Array() { Release(); }
|
||||
|
||||
// 重载等号运算符,使用另一个固定数组来初始化
|
||||
Array& operator=(const Array& arr) { return Copy(arr); }
|
||||
Array& operator=(const Array& arr) { Copy(arr); return *this; }
|
||||
|
||||
// 设置数组元素为指定值
|
||||
Array& Set(T item, int index = 0, int count = 0)
|
||||
bool Set(T item, int index = 0, int count = 0)
|
||||
{
|
||||
// count==0 表示设置全部元素
|
||||
if(!count) count = _Length - index;
|
||||
// count<=0 表示设置全部元素
|
||||
if(count <= 0) count = _Length - index;
|
||||
|
||||
// 检查长度是否足够
|
||||
assert_param(index + count <= _Length);
|
||||
//assert_param(index + count <= _Length);
|
||||
if(index + count > _Capacity)
|
||||
{
|
||||
debug_printf("数组容量 %d 不足以从 %d 开始写入 %d\r\n", _Capacity, index, count);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果元素类型大小为1,那么可以直接调用内存设置函数
|
||||
if(sizeof(T) == 1)
|
||||
|
@ -160,12 +165,17 @@ public:
|
|||
else
|
||||
while(count-- > 0) _Arr[index++] = item;
|
||||
|
||||
return *this;
|
||||
// 扩大长度
|
||||
if(index + count > _Length) _Length = index + count;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 设置数组。采用浅克隆,不拷贝数据
|
||||
Array& Set(const T* data, int len = 0)
|
||||
bool Set(const T* data, int len = 0)
|
||||
{
|
||||
if(!data) return false;
|
||||
|
||||
// 自动计算长度,\0结尾
|
||||
if(!len)
|
||||
{
|
||||
|
@ -173,9 +183,6 @@ public:
|
|||
while(*p++) len++;
|
||||
}
|
||||
|
||||
// 检查长度是否足够
|
||||
//assert_param(len <= _Capacity);
|
||||
|
||||
// 销毁旧的
|
||||
Release();
|
||||
|
||||
|
@ -184,26 +191,31 @@ public:
|
|||
_Capacity = len;
|
||||
_needFree = false;
|
||||
|
||||
return *this;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 复制数组。深度克隆,拷贝数据
|
||||
Array& Copy(const Array& arr, int index = 0)
|
||||
bool Copy(const Array& arr, int index = 0)
|
||||
{
|
||||
int len = arr.Length();
|
||||
|
||||
// 检查长度是否足够
|
||||
assert_param(index + len <= _Capacity);
|
||||
//assert_param(index + len <= _Capacity);
|
||||
if(index + len > _Capacity)
|
||||
{
|
||||
debug_printf("数组容量 %d 不足以从 %d 开始写入 %d\r\n", _Capacity, index, len);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 拷贝数据
|
||||
memcpy(_Arr + index, arr._Arr, sizeof(T) * len);
|
||||
_Length = len;
|
||||
|
||||
return *this;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 复制数组。深度克隆,拷贝数据
|
||||
Array& Copy(const T* data, int len = 0, int index = 0)
|
||||
bool Copy(const T* data, int len = 0, int index = 0)
|
||||
{
|
||||
// 自动计算长度,\0结尾
|
||||
if(!len)
|
||||
|
@ -213,14 +225,19 @@ public:
|
|||
}
|
||||
|
||||
// 检查长度是否足够
|
||||
int len2 = index + len;
|
||||
assert_param(len2 <= _Capacity);
|
||||
//int len2 = index + len;
|
||||
//assert_param(len2 <= _Capacity);
|
||||
if(index + len > _Capacity)
|
||||
{
|
||||
debug_printf("数组容量 %d 不足以从 %d 开始写入 %d\r\n", _Capacity, index, len);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 拷贝数据
|
||||
memcpy(_Arr + index, data, sizeof(T) * len);
|
||||
if(len2 > _Length) _Length = len2;
|
||||
if(index + len > _Length) _Length = index + len;
|
||||
|
||||
return *this;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 清空已存储数据。长度放大到最大容量
|
||||
|
@ -236,7 +253,7 @@ public:
|
|||
// 重载索引运算符[],让它可以像数组一样使用下标索引。
|
||||
T& operator[](int i)
|
||||
{
|
||||
assert_param(_Arr && i >= 0 && i < _Length);
|
||||
assert_param2(_Arr && i >= 0 && i < _Length, "数组下标越界");
|
||||
|
||||
return _Arr[i];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue