-
HandleBased Operation SystemOperations Dev/Handle 2010. 12. 10. 17:43
-- Process.h
#pragma once#include <string>
class Process
{
public:
Process(std::string const & name);public:
void excute(std::string const & message);//const 변경하지말라! &빠르게 넘기겠다!private:
std::string _name;
};
-- Process.cpp
#include "stdafx.h"#include <iostream>
using namespace std;#include "Process.h"
Process::Process( std::string const & name)
: _name( name )
{
}void Process::excute(const std::string & message)//const 변경하지말라! &빠르게 넘기겠다!
{
cout<<_name<<"executed"<<message<<endl;
}
--Operation System.h
#pragma once#include <map>
#include <string>class Process;
class OperatingSystem
{public:
OperatingSystem();
~OperatingSystem();
public:
int createProcess(std::string const & processName);
void sendMessage(int processHandle, std::string const & message);
public:
void getHandle (std::string const & processName);
private:
std::map<int, Process *> _table;
std::map<std::string, int> _handeltable;private:
static int _HandleCouter;// 클래스안에서 하나만 생성된다 인스턴스를 아무리 많이 만들어도
};
--Operation.cpp
#include "stdafx.h"
#include "Process.h"
#include "OperatingSystem.h"#include <iostream>
using namespace std;int OperatingSystem::_HandleCouter =0;
OperatingSystem::OperatingSystem()
{}
OperatingSystem::~OperatingSystem()
{
auto iter = _table.begin();
for(;iter != _table.end(); ++iter)
{
delete (*iter).second;
}
_table.clear();_handeltable.clear();
}
int OperatingSystem::createProcess(std::string const & processName)
{
_table.insert( make_pair( OperatingSystem::_HandleCouter,new Process( processName)));_handeltable.insert( make_pair( processName, OperatingSystem::_HandleCouter));
return OperatingSystem::_HandleCouter++;
}
void OperatingSystem::sendMessage(int processHandle, std::string const & message)
{
auto iter = _table.find(processHandle);
(*iter).second->excute( message);
}void OperatingSystem::getHandle(std::string const & processName)
{auto iter = _handeltable.find(processName);
cout<<processName<<"의 HandleCounter는 "<<(*iter).second<<"입니다."<<endl;}
--HandleBased System.cpp
// HandleBasedSystem.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//#include "stdafx.h"
#include "OperatingSystem.h"
int _tmain(int argc, _TCHAR* argv[])
{
OperatingSystem os;int p1Han1d = os.createProcess("Process1");
int p2Han1d = os.createProcess("Process2");
int p3Han1d = os.createProcess("Process3");
os.sendMessage(p2Han1d, "running");
os.sendMessage(p1Han1d, "eating");
os.sendMessage(p2Han1d, "sleeping");os.getHandle("Process1");
os.getHandle("Process2");
os.getHandle("Process3");return 0;
}'Operations Dev > Handle' 카테고리의 다른 글
map과 make_pair (0) 2010.12.10