#define Destroy(_p) {if (((layer_type *)(_p))->Canvas != NULL) delete[] ((layer_type *)(_p))->Canvas; free(_p);}
#define Destroy_(Head, _p) {if (((layer_type *)(_p))->src == (ListNode *)((layer_type *)(_p))) \
SrcReplace((Head), ((layer_type *)(_p))); \
delete[] ((layer_type *)(_p))->Canvas; free(_p);}
#define InsertFromHead(Head, _p) {(_p)->next = (Head)->next; ((_p)->prev = (ListNode *)(Head))->next = (ListNode *)(_p); \
if ((Head)->tail == NULL) (Head)->tail = (ListNode *)(_p); \
else ((layer_type *)(_p)->next)->prev = (ListNode *)(_p);}
#define NextInsert(TargetNode, NewNode) {if (((NewNode)->next = (TargetNode)->next) == NULL) \
m_LayerListHead->tail = (ListNode *)(NewNode); \
else ((layer_type *)(NewNode)->next)->prev = (ListNode *)(NewNode); \
((layer_type *)((TargetNode)->next = (ListNode *)(NewNode)))->prev = (ListNode *)(TargetNode);}
#define PriorInsert(TargetNode, NewNode) {((NewNode)->prev = (TargetNode)->prev)->next = (ListNode *)(NewNode); \
((layer_type *)((NewNode)->next = (ListNode *)(TargetNode)))->prev = (ListNode *)(NewNode);}
#define RemoveLayer_(Head, Node, bDel) {bool bfirstnode = ((Node) == (layer_type *)(Head)->next); \
if (((Node)->prev->next = (Node)->next) != NULL) { \
((layer_type *)(Node)->next)->prev = (Node)->prev; } else { \
(Head)->tail = ((Head)->next == NULL) ? NULL : (Node)->prev; } \
(Head)->curActive = (layer_type *)((!bfirstnode) ? (Node)->prev : (Head)->next); (Head)->Count--; \
if (bDel) Destroy_((Head), (Node));}
#define RemoveLayerTo(_p, From, To) {RemoveLayer_((From), (_p), false); InsertFromHead((To), (_p));}
#define SrcReplaceEach(List, SrcList) {layer_type* _p = (layer_type *)(SrcList)->next; while(_p) \
{if (_p->src == _p) SrcReplace((List), _p); _p = (layer_type *)_p->next;}}
#define RemoveLayers(Head, FirstNode, LastNode) {if ((FirstNode->prev->next = LastNode->next) != NULL) { \
((layer_type *)LastNode->next)->prev = FirstNode->prev;} else { \
Head->tail = (Head->next == NULL) ? NULL : FirstNode->prev;}}
#define InsertNodesFromHead(Head, FirstNode, LastNode) {(LastNode)->next = (Head)->next; \
((FirstNode)->prev = (ListNode *)(Head))->next = (ListNode *)(FirstNode); if ((Head)->tail == NULL) \
(Head)->tail = (ListNode *)(LastNode); else ((layer_type *)(LastNode)->next)->prev = (ListNode *)(LastNode);}
#define RemoveNodesTo(FirstNode, LastNode, From, To) {RemoveLayers((From), (FirstNode), (LastNode)); \
InsertNodesFromHead((To), (FirstNode), (LastNode));}