完整实现List<>泛型
This commit is contained in:
parent
8f600d96cd
commit
76cdbf2fe7
|
@ -10,16 +10,16 @@
|
||||||
class AP0801
|
class AP0801
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TList<Pin> LedPins;
|
List<Pin> LedPins;
|
||||||
TList<Pin> ButtonPins;
|
List<Pin> ButtonPins;
|
||||||
TList<OutputPort*> Leds;
|
List<OutputPort*> Leds;
|
||||||
TList<InputPort*> Buttons;
|
List<InputPort*> Buttons;
|
||||||
|
|
||||||
Pin EthernetLed; // 以太网指示灯
|
Pin EthernetLed; // 以太网指示灯
|
||||||
Pin WirelessLed; // 无线指示灯
|
Pin WirelessLed; // 无线指示灯
|
||||||
|
|
||||||
TList<OutputPort*> Outputs;
|
List<OutputPort*> Outputs;
|
||||||
TList<InputPort*> Inputs;
|
List<InputPort*> Inputs;
|
||||||
|
|
||||||
ISocketHost* Host; // 网络主机
|
ISocketHost* Host; // 网络主机
|
||||||
ISocketHost* HostAP; // 网络主机
|
ISocketHost* HostAP; // 网络主机
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
#include "Buffer.h"
|
#include "Buffer.h"
|
||||||
#include "List.h"
|
#include "List.h"
|
||||||
|
|
||||||
List::List()
|
IList::IList()
|
||||||
{
|
{
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
List::List(const List& list)
|
IList::IList(const IList& list)
|
||||||
{
|
{
|
||||||
Init();
|
Init();
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ List::List(const List& list)
|
||||||
Buffer(_Arr, _Count << 2) = list._Arr;
|
Buffer(_Arr, _Count << 2) = list._Arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
List::List(List&& list)
|
IList::IList(IList&& list)
|
||||||
{
|
{
|
||||||
_Count = list._Count;
|
_Count = list._Count;
|
||||||
_Capacity = list._Capacity;
|
_Capacity = list._Capacity;
|
||||||
|
@ -41,12 +41,12 @@ List::List(List&& list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List::~List()
|
IList::~IList()
|
||||||
{
|
{
|
||||||
if(_Arr && _Arr != Arr) delete _Arr;
|
if(_Arr && _Arr != Arr) delete _Arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void List::Init()
|
void IList::Init()
|
||||||
{
|
{
|
||||||
_Arr = Arr;
|
_Arr = Arr;
|
||||||
_Count = 0;
|
_Count = 0;
|
||||||
|
@ -55,10 +55,10 @@ void List::Init()
|
||||||
Comparer = nullptr;
|
Comparer = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int List::Count() const { return _Count; }
|
int IList::Count() const { return _Count; }
|
||||||
|
|
||||||
// 添加单个元素
|
// 添加单个元素
|
||||||
void List::Add(void* item)
|
void IList::Add(void* item)
|
||||||
{
|
{
|
||||||
CheckCapacity(_Count + 1);
|
CheckCapacity(_Count + 1);
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ void List::Add(void* item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加多个元素
|
// 添加多个元素
|
||||||
void List::Add(void** items, uint count)
|
void IList::Add(void** items, uint count)
|
||||||
{
|
{
|
||||||
if(!items || !count) return;
|
if(!items || !count) return;
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ void List::Add(void** items, uint count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除指定位置元素
|
// 删除指定位置元素
|
||||||
void List::RemoveAt(uint index)
|
void IList::RemoveAt(uint index)
|
||||||
{
|
{
|
||||||
int len = _Count;
|
int len = _Count;
|
||||||
if(len <= 0 || index >= len) return;
|
if(len <= 0 || index >= len) return;
|
||||||
|
@ -92,7 +92,7 @@ void List::RemoveAt(uint index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除指定元素
|
// 删除指定元素
|
||||||
int List::Remove(const void* item)
|
int IList::Remove(const void* item)
|
||||||
{
|
{
|
||||||
int idx = FindIndex(item);
|
int idx = FindIndex(item);
|
||||||
if(idx >= 0) RemoveAt(idx);
|
if(idx >= 0) RemoveAt(idx);
|
||||||
|
@ -100,7 +100,7 @@ int List::Remove(const void* item)
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
int List::FindIndex(const void* item) const
|
int IList::FindIndex(const void* item) const
|
||||||
{
|
{
|
||||||
for(int i=0; i<_Count; i++)
|
for(int i=0; i<_Count; i++)
|
||||||
{
|
{
|
||||||
|
@ -112,7 +112,7 @@ int List::FindIndex(const void* item) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// 释放所有指针指向的内存
|
// 释放所有指针指向的内存
|
||||||
List& List::DeleteAll()
|
IList& IList::DeleteAll()
|
||||||
{
|
{
|
||||||
for(int i=0; i < _Count; i++)
|
for(int i=0; i < _Count; i++)
|
||||||
{
|
{
|
||||||
|
@ -122,20 +122,20 @@ List& List::DeleteAll()
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void List::Clear()
|
void IList::Clear()
|
||||||
{
|
{
|
||||||
_Count = 0;
|
_Count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重载索引运算符[],返回指定元素的第一个
|
// 重载索引运算符[],返回指定元素的第一个
|
||||||
void* List::operator[](int i) const
|
void* IList::operator[](int i) const
|
||||||
{
|
{
|
||||||
if(i<0 || i>=_Count) return nullptr;
|
if(i<0 || i>=_Count) return nullptr;
|
||||||
|
|
||||||
return _Arr[i];
|
return _Arr[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void*& List::operator[](int i)
|
void*& IList::operator[](int i)
|
||||||
{
|
{
|
||||||
if(i<0 || i>=_Count)
|
if(i<0 || i>=_Count)
|
||||||
{
|
{
|
||||||
|
@ -146,7 +146,7 @@ void*& List::operator[](int i)
|
||||||
return _Arr[i];
|
return _Arr[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool List::CheckCapacity(int count)
|
bool IList::CheckCapacity(int count)
|
||||||
{
|
{
|
||||||
// 是否超出容量
|
// 是否超出容量
|
||||||
if(_Arr && count <= _Capacity) return true;
|
if(_Arr && count <= _Capacity) return true;
|
||||||
|
|
28
Core/List.h
28
Core/List.h
|
@ -4,15 +4,15 @@
|
||||||
typedef int (*IComparer)(const void* v1, const void* v2);
|
typedef int (*IComparer)(const void* v1, const void* v2);
|
||||||
|
|
||||||
// 变长列表。仅用于存储指针
|
// 变长列表。仅用于存储指针
|
||||||
class List
|
class IList
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
IComparer Comparer; // 比较器
|
IComparer Comparer; // 比较器
|
||||||
|
|
||||||
List();
|
IList();
|
||||||
List(const List& list);
|
IList(const IList& list);
|
||||||
List(List&& list);
|
IList(IList&& list);
|
||||||
~List();
|
virtual ~IList();
|
||||||
|
|
||||||
int Count() const;
|
int Count() const;
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ public:
|
||||||
int FindIndex(const void* item) const;
|
int FindIndex(const void* item) const;
|
||||||
|
|
||||||
// 释放所有指针指向的内存
|
// 释放所有指针指向的内存
|
||||||
List& DeleteAll();
|
IList& DeleteAll();
|
||||||
|
|
||||||
// 重载索引运算符[],返回指定元素的第一个
|
// 重载索引运算符[],返回指定元素的第一个
|
||||||
void* operator[](int i) const;
|
void* operator[](int i) const;
|
||||||
|
@ -56,22 +56,24 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class TList : public List
|
class List : public IList
|
||||||
{
|
{
|
||||||
static_assert(sizeof(T) <= 4, "TList only support pointer or int");
|
static_assert(sizeof(T) <= 4, "List only support pointer or int");
|
||||||
public:
|
public:
|
||||||
|
virtual ~List() { };
|
||||||
|
|
||||||
// 添加单个元素
|
// 添加单个元素
|
||||||
void Add(T item) { List::Add(item); }
|
void Add(T item) { IList::Add(item); }
|
||||||
|
|
||||||
// 删除指定元素
|
// 删除指定元素
|
||||||
int Remove(const T item) { return List::Remove(item); }
|
int Remove(const T item) { return IList::Remove(item); }
|
||||||
|
|
||||||
// 查找指定项。不存在时返回-1
|
// 查找指定项。不存在时返回-1
|
||||||
int FindIndex(const T item) const { return List::FindIndex(item); }
|
int FindIndex(const T item) const { return IList::FindIndex(item); }
|
||||||
|
|
||||||
// 重载索引运算符[],返回指定元素的第一个
|
// 重载索引运算符[],返回指定元素的第一个
|
||||||
T operator[](int i) const { return (T)List::operator[](i); }
|
T operator[](int i) const { return (T)IList::operator[](i); }
|
||||||
T& operator[](int i) { return (T&)List::operator[](i); }
|
T& operator[](int i) { return (T&)IList::operator[](i); }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include "WatchDog.h"
|
#include "WatchDog.h"
|
||||||
|
|
||||||
// 低功耗处理器
|
// 低功耗处理器
|
||||||
static List _powers;
|
static List<Power*> _powers;
|
||||||
|
|
||||||
void Power::SetPower()
|
void Power::SetPower()
|
||||||
{
|
{
|
||||||
|
@ -35,7 +35,7 @@ void Power::Standby(uint msTime)
|
||||||
|
|
||||||
for(int i=0; i<_powers.Count(); i++)
|
for(int i=0; i<_powers.Count(); i++)
|
||||||
{
|
{
|
||||||
auto pwr = (Power*)_powers[i];
|
auto pwr = _powers[i];
|
||||||
if(pwr)
|
if(pwr)
|
||||||
{
|
{
|
||||||
debug_printf("Power::LowPower 0x%p\r\n", pwr);
|
debug_printf("Power::LowPower 0x%p\r\n", pwr);
|
||||||
|
|
|
@ -71,7 +71,7 @@ private:
|
||||||
OutputPort Rst;
|
OutputPort Rst;
|
||||||
|
|
||||||
// 8个硬件socket
|
// 8个硬件socket
|
||||||
List Sockets;
|
IList Sockets;
|
||||||
|
|
||||||
// spi 模式(默认变长)
|
// spi 模式(默认变长)
|
||||||
ushort PhaseOM;
|
ushort PhaseOM;
|
||||||
|
|
|
@ -58,7 +58,7 @@ private:
|
||||||
class TaskScheduler
|
class TaskScheduler
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
List _Tasks;
|
List<Task*> _Tasks;
|
||||||
|
|
||||||
friend class Task;
|
friend class Task;
|
||||||
|
|
||||||
|
|
|
@ -182,9 +182,9 @@ bool BinaryPair::Set(cstring name, const IPEndPoint& value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 字典名值对操作
|
// 字典名值对操作
|
||||||
Dictionary BinaryPair::GetAll() const
|
IDictionary BinaryPair::GetAll() const
|
||||||
{
|
{
|
||||||
Dictionary dic(String::Compare);
|
IDictionary dic(String::Compare);
|
||||||
|
|
||||||
auto& ms = *_s;
|
auto& ms = *_s;
|
||||||
while(ms.Remain())
|
while(ms.Remain())
|
||||||
|
@ -206,7 +206,7 @@ Dictionary BinaryPair::GetAll() const
|
||||||
return dic;
|
return dic;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BinaryPair::Set(const Dictionary& dic)
|
bool BinaryPair::Set(const IDictionary& dic)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,8 +34,8 @@ public:
|
||||||
bool Set(cstring name, const IPEndPoint& value);
|
bool Set(cstring name, const IPEndPoint& value);
|
||||||
|
|
||||||
// 字典名值对操作
|
// 字典名值对操作
|
||||||
Dictionary GetAll() const;
|
IDictionary GetAll() const;
|
||||||
bool Set(const Dictionary& dic);
|
bool Set(const IDictionary& dic);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint _p; // 写入时的位置
|
uint _p; // 写入时的位置
|
||||||
|
|
|
@ -25,7 +25,7 @@ public:
|
||||||
void Register(uint offset, IDataPort& port);
|
void Register(uint offset, IDataPort& port);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
List Areas;
|
IList Areas;
|
||||||
|
|
||||||
bool OnHook(uint offset, uint size, bool write);
|
bool OnHook(uint offset, uint size, bool write);
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,18 +2,13 @@
|
||||||
#include "Core\List.h"
|
#include "Core\List.h"
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
void List::Test()
|
static void TestEntity(IList& list)
|
||||||
{
|
{
|
||||||
TS("TestList");
|
|
||||||
|
|
||||||
debug_printf("TestList......\r\n");
|
|
||||||
|
|
||||||
//不同长度的原始数据
|
//不同长度的原始数据
|
||||||
byte buf1[] = {1,2,3,4,5};
|
byte buf1[] = {1,2,3,4,5};
|
||||||
byte buf2[] = {6,7,8,9};
|
byte buf2[] = {6,7,8,9};
|
||||||
byte buf3[] = {10,11,12,13,14,15,16,17,18,19,20};
|
byte buf3[] = {10,11,12,13,14,15,16,17,18,19,20};
|
||||||
|
|
||||||
TList<byte*> list;
|
|
||||||
list.Add(buf1);
|
list.Add(buf1);
|
||||||
list.Add(buf2);
|
list.Add(buf2);
|
||||||
list.Add(buf3);
|
list.Add(buf3);
|
||||||
|
@ -57,6 +52,19 @@ void List::Test()
|
||||||
}
|
}
|
||||||
assert(list.Count() == 3 + 16, "Count()");
|
assert(list.Count() == 3 + 16, "Count()");
|
||||||
assert(list[0] == buf1 && list[1] == buf3 && list[2] == buf3, "bool CheckCapacity(int count)");
|
assert(list[0] == buf1 && list[1] == buf3 && list[2] == buf3, "bool CheckCapacity(int count)");
|
||||||
|
}
|
||||||
|
|
||||||
|
void IList::Test()
|
||||||
|
{
|
||||||
|
TS("TestList");
|
||||||
|
|
||||||
|
debug_printf("TestList......\r\n");
|
||||||
|
|
||||||
|
IList list;
|
||||||
|
TestEntity(list);
|
||||||
|
|
||||||
|
List<byte*> list2;
|
||||||
|
TestEntity(list2);
|
||||||
|
|
||||||
debug_printf("TestList测试完毕......\r\n");
|
debug_printf("TestList测试完毕......\r\n");
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ public:
|
||||||
// Arp套接字
|
// Arp套接字
|
||||||
TinySocket* Arp;
|
TinySocket* Arp;
|
||||||
// 套接字列表。套接字根据类型来识别
|
// 套接字列表。套接字根据类型来识别
|
||||||
List Sockets;
|
List<TinySocket*> Sockets;
|
||||||
|
|
||||||
TinyIP();
|
TinyIP();
|
||||||
TinyIP(ITransport* port);
|
TinyIP(ITransport* port);
|
||||||
|
|
|
@ -19,7 +19,7 @@ public:
|
||||||
// BodyManagement();
|
// BodyManagement();
|
||||||
// ~BodyManagement();
|
// ~BodyManagement();
|
||||||
|
|
||||||
List Bodys;
|
List<DeviceBody*> Bodys;
|
||||||
DeviceBody* FindBody(byte id) const;
|
DeviceBody* FindBody(byte id) const;
|
||||||
|
|
||||||
// 下发 由 GateWay 调用
|
// 下发 由 GateWay 调用
|
||||||
|
|
|
@ -43,7 +43,7 @@ private:
|
||||||
int PushDev(Device* dv);
|
int PushDev(Device* dv);
|
||||||
public:
|
public:
|
||||||
// 设备列表
|
// 设备列表
|
||||||
List DevArr;
|
IList DevArr;
|
||||||
// 持久在线列表
|
// 持久在线列表
|
||||||
// List OnlineAlways;
|
// List OnlineAlways;
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@ TokenClient* Token::CreateClient(ISocketHost* host)
|
||||||
auto ctrl2 = new TokenController();
|
auto ctrl2 = new TokenController();
|
||||||
ctrl2->Port = dynamic_cast<ITransport*>(socket);
|
ctrl2->Port = dynamic_cast<ITransport*>(socket);
|
||||||
//client.Local = ctrl2;
|
//client.Local = ctrl2;
|
||||||
client.Controls.Add(&ctrl2);
|
client.Controls.Add(ctrl2);
|
||||||
}
|
}
|
||||||
|
|
||||||
return &client;
|
return &client;
|
||||||
|
|
|
@ -26,11 +26,11 @@ public:
|
||||||
int Delay; // 心跳延迟。一条心跳指令从发出到收到所花费的时间
|
int Delay; // 心跳延迟。一条心跳指令从发出到收到所花费的时间
|
||||||
int MaxNotActive; // 最大不活跃时间ms,超过该时间时重启系统。默认0
|
int MaxNotActive; // 最大不活跃时间ms,超过该时间时重启系统。默认0
|
||||||
|
|
||||||
List Controls; // 控制器集合
|
List<TokenController*> Controls; // 控制器集合
|
||||||
List Sessions; // 会话集合
|
//List Sessions; // 会话集合
|
||||||
TokenConfig* Cfg;
|
TokenConfig* Cfg;
|
||||||
DataStore Store; // 数据存储区
|
DataStore Store; // 数据存储区
|
||||||
Dictionary Routes; // 路由集合
|
IDictionary Routes; // 路由集合
|
||||||
|
|
||||||
TokenClient();
|
TokenClient();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue