개발하는 리프터 꽃게맨입니다.
void*를 이용한 지연처리 테스트 본문
#include
#include
using namespace std;
class Player
{
public:
Player(string name)
: mName(name)
{
}
string GetName() const
{
return mName;
}
private:
string mName;
};
struct MyEvent
{
void (*eventFunction)(void*, void*);
void* lParam;
void* rParam;
};
class EventProcessor
{
public:
EventProcessor() = default;
~EventProcessor() = default;
void Push(MyEvent eventStruct)
{
mQueue.push(eventStruct);
}
void AllExecute()
{
while (mQueue.empty() == false)
{
MyEvent eventValue = mQueue.front();
mQueue.pop();
const auto& lpm = eventValue.lParam;
const auto& rpm = eventValue.rParam;
eventValue.eventFunction(lpm, rpm);
}
}
private:
queue mQueue;
};
vector<player*> objectList;</player*>
EventProcessor eProc;
void CreatePlayer(void* playerObject, void* unUsed)
{
const auto& myPlayer = static_cast<player*>(playerObject);</player*>
objectList.push_back(myPlayer);
}
void RemovePlayer(void* playerObject, void* unUsed)
{
const auto& myPlayer = static_cast<player*>(playerObject);</player*>
auto playerToDelete = std::find(objectList.begin(), objectList.end(), myPlayer);
if (playerToDelete == objectList.end()) return;
auto player = *playerToDelete;
objectList.erase(playerToDelete);
delete player;
}
void ReserveEvent(void (*eventFunction)(void*, void*), void* lParam, void* rParam)
{
MyEvent myEvent = {};
myEvent.eventFunction = eventFunction;
myEvent.lParam = lParam;
myEvent.rParam = rParam;
eProc.Push(myEvent);
}
Player* playerRonaldo = new Player("ronaldo");
int main()
{
// 이벤트 예약
{
ReserveEvent(CreatePlayer, new Player("kdb"), nullptr);
ReserveEvent(CreatePlayer, playerRonaldo, nullptr);
ReserveEvent(CreatePlayer, new Player("messi"), nullptr);
ReserveEvent(CreatePlayer, new Player("son"), nullptr);
ReserveEvent(CreatePlayer, new Player("park"), nullptr);
}
// 이벤트 실행
{
eProc.AllExecute();
}
// 이벤트 예약
{
ReserveEvent(RemovePlayer, playerRonaldo, nullptr);
}
// 이벤트 실행
{
eProc.AllExecute();
}
return 0;
}
대충 생각나는대로 짠거라
가독성 개판
그냥 void*를 한 번 써보고 싶었음
'잡담' 카테고리의 다른 글
union은 이렇게 쓰는거군요? (0) | 2024.07.02 |
---|---|
글 열심히 썼는데 날아갔습니다. (0) | 2024.01.02 |
개발 블로그를 시작했습니다. (1) | 2023.12.23 |