现在的位置: 首页 > 综合 > 正文

ACE 消息队列 ACE_Message_Queue的遍历(2)

2019年01月10日 ⁄ 综合 ⁄ 共 2206字 ⁄ 字号 评论关闭

以下的代码是对ACE_Message_Queue的遍历方法。

先定义一个学生对象:

#pragma once

#include "iostream"

using namespace std;

class student
{
public:
	student(void);
	student(int number, char* name, int english);
	~student(void);
public:
	int    number;
	char   name[10];
	int    english;
	void output(int i);
};

#include "stdafx.h"
#include "student.h"


student::student(void)
{
}

student::student(int number, char* name, int english)
{
	this->number = number;
	strcpy(this->name, name);
	this->english = english;
}

student::~student(void)
{
}

void student::output(int i)
{
	if (i == 1)
	{
		cout<<"insert " << "number=" << number << " name=" << name << " english=" << english <<endl; 
	}
	else if( i == 2)
	{
		cout<<"delete " << "number=" << number << " name=" << name << " english=" << english <<endl; 
	}
	else if( i == 3)
	{
		cout<<"scan " << "number=" << number << " name=" << name << " english=" << english <<endl; 
	}
}

再定义一个学生队列的对象:

#pragma once
#include "ace/Message_Queue.h"
#include "ace/Synch.h"
#include "iostream"
#include "student.h"

using namespace std;

class studentqueue
{
public:
	studentqueue(void);
	~studentqueue(void);
private:
	ACE_Message_Queue<ACE_MT_SYNCH> mq;
public:
	int insertinto(student stu); 
	int deletefrom(int number);
	int update(int number, int english);
	int scan();
};

#include "stdafx.h"
#include "studentqueue.h"


studentqueue::studentqueue(void)
{
}


studentqueue::~studentqueue(void)
{
}

int studentqueue::insertinto(student stu)
{
	int length = sizeof(stu);
	ACE_Message_Block *mb = new ACE_Message_Block(length, ACE_Message_Block::MB_DATA);
	mb->copy((char*)&stu, length);
	return mq.enqueue(mb);
}

int studentqueue::deletefrom(int number)
{
	ACE_Message_Block *mb;
	ACE_Message_Queue_Iterator<ACE_MT_SYNCH> LI(mq);
	while(LI.next(mb))
	{
		student* pstu = (student*)mb->base();
		LI.advance();
	}

	return 0;
}

int studentqueue::scan()
{
	ACE_Message_Block *mb;
	mq.peek_dequeue_head(mb);
	do
	{
		student* pstu = (student*)mb->base();
		pstu->output(3);
	}
	while(mb = mb->next());
	
	return 0;
}

int studentqueue::update(int number, int english)
{
	ACE_Message_Block *mb;
	mq.peek_dequeue_head(mb);
	do
	{
		student* pstu = (student*)mb->base();
		if (pstu->number == number)
		{
			pstu->english = english;
		}
	}
	while(mb = mb->next());

	return 0;
}

主函数进行测试:

 

// scanACEMessageQueue.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "studentqueue.h"


int _tmain(int argc, _TCHAR* argv[])
{
	studentqueue sq;

	for(int i = 0; i < 10; i++)
	{
		student stu(i, "wangxu", i * 10);
		sq.insertinto(stu);
	}


	sq.deletefrom(1);

	sq.scan();


	system("pause");

	return 0;
}

 

抱歉!评论已关闭.