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