增加列表模版,编译例程未通过

This commit is contained in:
Stone 2014-07-26 16:21:56 +00:00
parent 0e2a66845a
commit f3eb6b123f
4 changed files with 99 additions and 6 deletions

90
List.h Normal file
View File

@ -0,0 +1,90 @@
#ifndef _List_H_
#define _List_H_
#include "Sys.h"
// 列表模版
template<typename T>
class List
{
public:
List() { _count = _total = 0; }
List(int size = 4)
{
_count = 0;
_total = size;
arr = new T[size];
}
List(T* items) { }
~List() { if(arr) delete[] arr; arr = NULL; }
void Add(T item)
{
// 检查大小
CheckSize();
arr[_count++] = item;
}
void Add(T* items, int count)
{
int size = _count + count;
if(size >= _total) ChangeSize(size * 2);
for(int i=0; i<count; i++)
{
arr[_count++] = *items++;
}
}
T* ToArray()
{
// 如果刚好完整则直接返回,否则重新调整空间
if(_count != _total)
{
T* arr2 = new T[_count];
memcpy(arr, arr2, _count);
delete[] arr;
arr = arr2;
}
return arr;
}
int Count() { return _count; }
// 重载索引运算符[],让它可以像数组一样使用下标索引。内部不检查下标越界,外部好自为之
T operator[](int i) { return arr[i]; }
T* operator=(List list) { return list.ToArray(); }
private:
T* arr;
uint _count;
uint _total;
void ChangeSize(int newSize)
{
if(_total == newSize) return;
T* arr2 = new T[newSize];
if(arr)
{
// 如果新数组较小,则直接复制;如果新数组较大,则先复制,再清空余下部分
if(newSize < _total)
memcpy(arr, arr2, newSize);
else
{
memcpy(arr, arr2, _total);
memset(arr2 + _total, newSize - _total);
}
delete[] arr;
}
arr = arr2;
}
void CheckSize()
{
// 如果数组空间已用完,则两倍扩容
if(_count >= _total) ChangeSize(_count * 2);
}
};
#endif //_List_H_

View File

@ -35,11 +35,11 @@ void Port::SetPort(GPIO_TypeDef* group, ushort pinbit)
}
// 用一组引脚来初始化,引脚组由第一个引脚决定,请确保所有引脚位于同一组
void Port::SetPort(Pin pins[])
void Port::SetPort(List<Pin> pins)
{
Group = IndexToGroup(pins[0] >> 4);
PinBit = 0;
for(int i=0; i<sizeof(pins)/sizeof(Pin); i++)
for(int i=0; i<pins.Count(); i++)
PinBit |= IndexToBits(pins[i] & 0x0F);
}

8
Port.h
View File

@ -36,7 +36,7 @@ protected:
}
void SetPort(Pin pin); // 单一引脚初始化
void SetPort(Pin pins[], int count); // 用一组引脚来初始化引脚组GPIOx由第一个引脚决定请确保所有引脚位于同一组GPIOx
void SetPort(List<Pin> pins); // 用一组引脚来初始化引脚组GPIOx由第一个引脚决定请确保所有引脚位于同一组GPIOx
void SetPort(GPIO_TypeDef* group, ushort pinbit = GPIO_Pin_All);
// 配置过程由Config调用最后GPIO_Init
@ -87,7 +87,7 @@ public:
bool OpenDrain; // 是否开漏输出
OutputPort(Pin pin, bool openDrain = false, uint speed = 50) { SetPort(pin); Init(openDrain, speed); Config(); }
OutputPort(Pin pins[], int count, bool openDrain = false, uint speed = 50) { SetPort(pins, count); Init(openDrain, speed); Config(); }
OutputPort(List<Pin> pins, bool openDrain = false, uint speed = 50) { SetPort(pins); Init(openDrain, speed); Config(); }
OutputPort(GPIO_TypeDef* group, ushort pinbit = GPIO_Pin_All) { SetPort(group, pinbit); Init(); Config(); }
void Write(bool value); // 按位值写入
@ -124,7 +124,7 @@ class AlternatePort : public OutputPort
{
public:
AlternatePort(Pin pin, bool openDrain = false, uint speed = 10) : OutputPort(pin, openDrain, speed) { Init(); Config(); }
AlternatePort(Pin pins[], int count, bool openDrain = false, uint speed = 10) : OutputPort(pins, count, openDrain, speed) { Init(); Config(); }
AlternatePort(List<Pin> pins, bool openDrain = false, uint speed = 10) : OutputPort(pins, openDrain, speed) { Init(); Config(); }
AlternatePort(GPIO_TypeDef* group, ushort pinbit = GPIO_Pin_All) : OutputPort(group, pinbit) { Init(); Config(); }
protected:
@ -165,7 +165,7 @@ public:
bool Floating; // 是否浮空输入
InputPort(Pin pin, bool floating = true, uint speed = 50, PuPd_TypeDef pupd = PuPd_NOPULL) { SetPort(pin); Init(floating, speed, pupd); }
InputPort(Pin pins[], int count, bool floating = true, uint speed = 50, PuPd_TypeDef pupd = PuPd_NOPULL) { SetPort(pins, count); Init(floating, speed, pupd); }
InputPort(List<Pin> pins, bool floating = true, uint speed = 50, PuPd_TypeDef pupd = PuPd_NOPULL) { SetPort(pins); Init(floating, speed, pupd); }
InputPort(GPIO_TypeDef* group, ushort pinbit = GPIO_Pin_All) { SetPort(group, pinbit); Init(); }
virtual ushort ReadGroup() // 整组读取

3
Sys.h
View File

@ -18,6 +18,9 @@ typedef char* string;
typedef ushort Pin;
#include "Pin.h"
// 列表模版
#include "List.h"
// 系统类
class TSys
{