1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
/************************ PROCESS.HPP - General Stuff ************************/
#pragma once
#ifndef _ASCENDANTENGINE_PROCESS_HPP
#define _ASCENDANTENGINE_PROCESS_HPP
#include <vector>
#include <boost/any.hpp>
namespace AscendantEngine {
/// Calling purpose for process
enum CallingPurpose { Purpose_Init, Purpose_Loop, Purpose_Shutdown };
/// Definition of Process type
typedef void (*Process)(boost::any, CallingPurpose);
/// A list of arbitrary data
typedef std::vector<boost::any> Datalist;
}
#endif
/*********************** PROCESSLIST.HPP *****************************/
#pragma once
#ifndef _ASCENDANTENGINE_PROCESSLIST_HPP
#define _ASCENDANTENGINE_PROCESSLIST_HPP
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <SFML/System.hpp>
#include <boost/any.hpp>
#include "Process.hpp"
namespace AscendantEngine {
/// The process list class
class ProcessList {
//class thread : public sf::Thread {
// std::vector<Process> processes;
// void Run(void* data) { for (int i=0; i<processes.size(); i++) processes[i](
//}
int nThreads; // number of threads: not yet implemented
std::map<std::string, Process> processes;
//std::vector<sf::Thread> threads;
void _execute(std::vector<boost::any>* data = 0)
{
unsigned datItr = 0;
std::map<std::string, Process>::iterator procItr = processes.begin();
for (; procItr != processes.end(); datItr++, procItr++)
if (data && datItr < data->size())
procItr->second(data[datItr], Purpose_Loop);
else
procItr->second(0, Purpose_Loop);
}
void _remove_all(Datalist* data = 0)
{
unsigned datItr = 0;
std::map<std::string, Process>::iterator procItr = processes.begin();
for (; procItr!=processes.end(); procItr++, datItr++)
if (data && datItr < data->size())
procItr->second(data[datItr], Purpose_Shutdown);
else
procItr->second(0, Purpose_Shutdown);
processes.clear();
}
public:
ProcessList()
{
}
~ProcessList() { RemoveAll(); }
void Add(Process process, std::string ID, /*unsigned threadNumber = 0,*/ boost::any data = 0)
{
if (process == 0) return;
processes.insert(std::pair<std::string, Process>(ID, process));
process(data, Purpose_Init);
// TODO: threading
}
void Push(Process process, std::string ID, /*unsigned threadNumber = 0,*/ boost::any data = 0) { Add(process, ID, /*threadNumber,*/ data); }
void Remove(std::string ID, boost::any data = 0)
{
if (processes.find(ID) != processes.end())
processes.find(ID)->second(data, Purpose_Shutdown);
processes.erase(ID);
}
void Pop(std::string ID, boost::any data = 0) { Remove(ID, data); }
void RemoveAll()
{
_remove_all();
}
void RemoveAll(Datalist data)
{
_remove_all(&data);
}
void PopAll() { RemoveAll(); }
void PopAll(Datalist data) { RemoveAll(data); }
void Execute()
{
_execute();
}
void Execute(Datalist data)
{
_execute(&data);
}
};
}
#endif
/***************************** PROCESS STACK ***************************/
#pragma once
#ifndef _ASCENDANTENGINE_PROCESSSTACK_HPP
#define _ASCENDANTENGINE_PROCESSSTACK_HPP
#include <stack>
#include <vector>
#include <boost/any.hpp>
#include "Process.hpp"
namespace AscendantEngine {
class ProcessStack {
std::stack<Process> processes;
void _pop_all(Datalist* data = 0)
{
unsigned dataItr = 0;
while (Pop((data&&(dataItr < data->size()))?(*data)[dataItr]:0))
;
}
public:
~ProcessStack() { PopAll(); }
void Push(Process process, boost::any data = 0)
{
if (process == 0) return;
process(data, Purpose_Init);
processes.push(process);
}
void Add(Process process, boost::any data = 0) { Push(process, data); }
bool Pop(boost::any data = 0)
{
if (processes.empty()) return false;
processes.top()(data, Purpose_Shutdown);
processes.pop();
if (processes.empty()) return false; else return true;
}
bool Remove(boost::any data = 0) { return Pop(data); }
void PopAll() { _pop_all(); }
void PopAll(Datalist data) { _pop_all(&data); }
void RemoveAll() { PopAll(); }
void RemoveAll(Datalist data) { PopAll(data); }
void Execute(boost::any data = 0)
{
processes.top()(data, Purpose_Loop);
}
};
}
#endif
|