目录
注:例程摘自《C嵌入式编程设计模式》
一、基类
/*queue.h =========================================================*/
#ifndef _QUEUE_H
#define _QUEUE_H
#define QUEUE_SIZE 10
/* class queue */
typedef struct Queue Queue;
struct Queue {
int buffer[QUEUE_SIZE]; /* where the data things are */
int head;
int size;
int tail;
int (*isFull)(Queue *const me);
int (*isEmpty)(Queue *const me);
int (*getSize)(Queue *const me);
void (*insert)(Queue *const me, int k);
int (*remove)(Queue *const me);
};
/*constructors and destructors: */
void Queue_init(Queue *const me, int (*isFullfunction)(Queue *const me),
int (*isEmptyfunction)(Queue *const me),
int (*getSizefunction)Queue *const me,
int (*insertfunction)(Queue *const me, int k),
int (*removefunction)(Queue *const me));
void Queue_Cleanup(Queue *const me);
/* operations */
int Queue_isFull(Queue *const me);
int Queue_isEmpty(Queue *const me);
int Queue_getSize(Queue *const me);
void Queue_insert(Queue *const me, int k);
int Queue_remove(Queue *const me);
Queue *Queue_Create(void);
void Queue_Destroy(Queue *const me);
#endif /* _QUEUE_H */
/*queue.c ===================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
void Queue_init(Queue *const me, int (*isFullfunction)(Queue *const me),
int (*isEmptyfunction)(Queue *const me),
int (*getSizefunction)Queue *const me,
int (*insertfunction)(Queue *const me, int k),
int (*removefunction)(Queue *const me))
{
/* initialize attributes */
me->head = 0;
me->size = 0;
me->tail = 0;
me->tail = 0;
/* initialize memberfunction pointers */
me->isFull = isFullfunction;
me->isEmpty = isEmptyfunction;
me->getSize = getSizefunction;
me->insert = insertfunction;
me->remove = removefunction;
}
void Queue_Cleanup(Queue *const me)
{
}
/* operations */
int Queue_isFull(Queue *const me)
{
return (me->head + 1) %QUEUE_SIZWE == me->tail;
}
int Queue_isEmpty(Queue *const me)
{
return (me->head == me->tail);
}
int Queue_getSize(Queue *const me)
{
return me->size;
}
void Queue_insert(Queue *const me, int k)
{
if(!me->ifFull(me)) {
me->buffer[me->head] = k;
me->head = (me->head + 1) %QUEUE_SIZE;
++me->size;
}
}
int Queue_remove(Queue *const me)
{
int value = -9999; //sentinel value
if(!me->isEmpty(me)) {
value = me->buffer[me->tail];
me->tail = (me->tail + 1)%QUEUE_SIZE;
++me->size;
}
return value;
}
Queue *Queue_Create(void)
{
Queue *me = (Queue *)malloc(sizeof(Queue));
if (me != NULL) {
Queue_Init(me, Queue_isFull, Queue_isEmpty, Queue_getSize,
Queue_insert, Queue_remove);
}
return me;
}
void Queue_Destroy(Queue *const me)
{
if(me != NULL) {
Queue_Cleanup(me);
}
free(me);
}
/*main.c =============================================================*/
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
int main(void)
{
int j, k, h, t;
/* test normal queue */
Queue *myQ;
myQ = Queue_Create();
k = 1000;
for (j = 0; j < QUEUE_SIZE; j++) {
h = myQ->head;
myQ->insert(myQ, k);
printf("inserting %d at position %d, size = %d\n", k--, h, myQ->getSize(myQ));
}
printf("Inserted %d elements \n", myQ->getSize(myQ));
for (j = 0; j < QUEUE_SIZE; j++) {
t = myQ->tail;
k = myQ->remove(myQ);
printf("REMOVE %d at position %d, size = %d\n", k, t, myQ->getSize(myQ));
}
printf("Last item removed = %d\n", k);
printf("Current queu size %d\n", myQ->getSize(myQ));
puts("Queue test program");
return EXIT_SUCCESS;
}
二、继承
/* cached_queue.h =========================================*/
#ifndef _CACHED_QUEUE_H
#define _CACHED_QUEUE_H
#include "queue.h"
typedef struct CachedQueue CachedQueue;
struct CachedQueue {
Queue *queue; /* base class */
/* new attrbutes */
char finename[80];
int numberElementsOnDisk;
/* aggregation in subclass */
Queue *outputQueue;
/*inherited virtual function */
int (*isFull)(CachedQueue *const me);
int (*isEmpty)(CachedQueue *const me);
int (*getSize)(CachedQueue *const me);
void (*insert)(CachedQueue *const me, int k);
int (*remove)(CachedQueue *const me);
/* new virtual functions */
void (*flush)(CachedQueue *const me);
void (*load)(CachedQueue *const me);
};
void CachedQueue_Init(CachedQueue *const me, char *fName,
int (*isFullfunction)(CachedQueue *const me),
int (*ifEmptyfunction)(CachedQueue *const me),
int (*getSizefunction)(CachedQueue *const me),
int (*insertfunction)(CachedQueue *const me, int k),
int (*removefunction)(CachedQueue *const me),
int (*flushfunction)(CachedQueue *const me),
void (*loadfunction)(CachedQueue *const me));
void CachedQueue_Cleanup(CachedQueue *const me);
/* operations */
int CacheQueue_ifFull(CachedQueue *const me);
int CacheQueue_ifEmpty(CachedQueue *const me);
int CacheQueue_getSize(CachedQueue *const me);
int CacheQueue_insert(CachedQueue *const me, int k);
int CacheQueue_remove(CachedQueue *const me);
int CacheQueue_flush(CachedQueue *const me);
int CacheQueue_load(CachedQueue *const me);
CachedQueue *cachedQueue_create(void);
void CachedQueue_destroy(CachedQueue *const me);
#endif /* _CACHED_QUEUE_H */
/* cached_queue.c =====================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cachedqueu.h"
void CachedQueue_Init(CachedQueue *const me, char *fName,
int (*isFullfunction)(CachedQueue *const me),
int (*ifEmptyfunction)(CachedQueue *const me),
int (*getSizefunction)(CachedQueue *const me),
int (*insertfunction)(CachedQueue *const me, int k),
int (*removefunction)(CachedQueue *const me),
int (*flushfunction)(CachedQueue *const me),
void (*loadfunction)(CachedQueue *const me))
{
/* initialize base class */
me->queue = Queue_cteate();
/* initialize subclass attributes */
me->numberElementsOnDisk = 0;
strcpy(me->filename, fName);
me->outputQueue = Queue_Create();
me->isFull= ifFullfunction;
me->isEmpty = isEmptyfunction;
me->getSize = getSizefunction;
me->insert = insertfunction;
me->remove = removefunction;
me->flush = flushfunction;
me->load = loadfunction;
}
void CachedQueue_Cleanup(CachedQueue *const me)
{
Queue_Cleanup(me->queue);
}
/* operations */
int CacheQueue_isFull(CachedQueue *const me)
{
return me->queue->isFull(me->queue) &&
me->outputQueue->isFull(me->outputQueue);
}
int CacheQueue_isEmpty(CachedQueue *const me)
{
return me->queue->isEmpty(me->queue) &&
me->outputQueue->isEmpty(me->outputQueue) &&
(me->nujmberElementsOnDisk == 0);
}
int CacheQueue_getSize(CachedQueue *const me)
{
return me->queue->getSize(me->queue) +
me->outputQueue->getSize(me->outputQueue) +
me->numberElementsOnDisk;
}
int CacheQueue_insert(CachedQueue *const me, int k);
{
if (me->queue->isFull(me->queue))
me->flush(me);
me->queue->insert(me->queue, k);
}
int CacheQueue_remove(CachedQueue *const me)
{
if(!me->outputQueue->isEmpty(me->outputQueue))
return me->outputQueue->remove(me->outputQueue);
else if(me->numberElementsOnDisk > 0) {
me->load(me);
return me->queue->remove(me->queue);
}
else
return me->queue->remove(me->queue);
}
int CacheQueue_flush(CachedQueue *const me)
{
//write file I/O
}
int CacheQueue_load(CachedQueue *const me)
{
//read file I/O
}
CachedQueue *cachedQueue_Create(void)
{
CachedQueue *me = (CachedQueue *)malloc(sizeof(CachedQueue ));
if(me != NULL) {
CachedQueue_list(me, "C:\\queuebuffer.dat",
CachedQueue_isFull, CachedQueue_isEmpty,
CachedQueue_getSize, CachedQueue_insert, CachedQueue_remove,
CachedQueue_flush, CachedQueue_load);
}
return me;
}
void CachedQueue_destroy(CachedQueue *const me)
{
if(me != NULL) {
CachedQueue_Cleanup(me);
}
free(me);
}