Screw the container double pointer it is
This commit is contained in:
parent
eb6250c030
commit
7943591f6e
@ -14,24 +14,26 @@ using state_register = std::atomic<uint64_t>;
|
|||||||
|
|
||||||
namespace NB {
|
namespace NB {
|
||||||
|
|
||||||
|
extern std::invalid_argument null_mask_error;
|
||||||
|
|
||||||
void NULL_FUNC();
|
void NULL_FUNC();
|
||||||
|
|
||||||
class NBEvent {
|
class NBEvents {
|
||||||
friend class NBEventListener;
|
friend class NBEventListener;
|
||||||
public:
|
public:
|
||||||
NBEvent();
|
NBEvents(const uint64_t, void (*initFunc)() = NULL_FUNC, const char* initName="");
|
||||||
NBEvent(const uint64_t, void (*initFunc)() = NULL_FUNC, const char* initName="");
|
NBEvents(const uint64_t, const char* initName="");
|
||||||
NBEvent(const uint64_t, const char* initName="");
|
NBEvents(const NBEvents&);
|
||||||
NBEvent(NBEvent&);
|
NBEvents& operator=(const NBEvents&);
|
||||||
NBEvent& operator=(NBEvent& cpy);
|
~NBEvents();
|
||||||
~NBEvent();
|
|
||||||
|
|
||||||
const std::string getName() const;
|
const std::string getName() const;
|
||||||
const uint64_t getMask() const;
|
const uint64_t getMask() const;
|
||||||
void setMask(const uint64_t);
|
void setMask(const uint64_t);
|
||||||
void setName(const char*);
|
void setName(const char*);
|
||||||
void setFunc(void (*newFunc)());
|
void setFunc(void (*newFunc)());
|
||||||
virtual const uint64_t check(const uint64_t) const;
|
virtual const uint64_t check(const uint64_t) const = 0;
|
||||||
|
virtual NBEvents* clone() const = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint64_t mask = 0x0;
|
uint64_t mask = 0x0;
|
||||||
@ -40,15 +42,25 @@ protected:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class NBState : public NBEvent {
|
class NBEventBasic : public NBEvents {
|
||||||
friend class NBEventListener;
|
friend class NBEventListener;
|
||||||
public:
|
public:
|
||||||
using NBEvent::NBEvent;
|
using NBEvents::NBEvents;
|
||||||
|
|
||||||
|
NBEventBasic* clone() const override;
|
||||||
const uint64_t check(const uint64_t) const override;
|
const uint64_t check(const uint64_t) const override;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class NBEventState : public NBEvents {
|
||||||
|
friend class NBEventListener;
|
||||||
|
public:
|
||||||
|
using NBEvents::NBEvents;
|
||||||
|
|
||||||
|
NBEventState* clone() const override;
|
||||||
|
const uint64_t check(const uint64_t) const override;
|
||||||
|
};
|
||||||
|
|
||||||
enum NBStateChangeType : uint8_t {
|
enum NBStateChangeType : uint8_t {
|
||||||
STATE_RAISE, STATE_DROP, STATE_SET
|
STATE_RAISE, STATE_DROP, STATE_SET
|
||||||
};
|
};
|
||||||
@ -60,26 +72,26 @@ struct NBStateChange {
|
|||||||
|
|
||||||
class NBEventListener {
|
class NBEventListener {
|
||||||
public:
|
public:
|
||||||
NBEventListener(NBEvent*, uint16_t, state_register* initStatePtr=nullptr, const uint64_t initState=(uint64_t)0x0);
|
NBEventListener(NBEvents**, uint16_t, const uint64_t initState=(uint64_t)0x0, state_register* initStatePtr=nullptr);
|
||||||
template<size_t SIZE>
|
template<size_t SIZE>
|
||||||
NBEventListener(std::array<NBEvent, SIZE> eventArray, state_register* initStatePtr=nullptr, const uint64_t initState=(uint64_t)0x0)
|
NBEventListener(std::array<NBEvents*, SIZE> eventArray, const uint64_t initState=(uint64_t)0x0, state_register* initStatePtr=nullptr)
|
||||||
: NBEventListener(eventArray.data(), eventArray.size(), initStatePtr, initState) {}
|
: NBEventListener(eventArray.data(), eventArray.size(), initState, initStatePtr) {}
|
||||||
NBEvent& operator[](int);
|
|
||||||
|
|
||||||
state_register* getStatePtr() const;
|
state_register* getStatePtr() const;
|
||||||
const uint64_t getState() const;
|
const uint64_t getState() const;
|
||||||
void raiseFlags(const uint64_t);
|
void raiseFlags(const uint64_t);
|
||||||
void raiseFlags(const NBEvent&);
|
void raiseFlags(const NBEvents&);
|
||||||
void dropFlags(const uint64_t);
|
void dropFlags(const uint64_t);
|
||||||
void dropFlags(const NBEvent&);
|
void dropFlags(const NBEvents&);
|
||||||
const bool snoop(const uint64_t) const;
|
const bool snoop(const uint64_t) const;
|
||||||
const bool snoop(const NBEvent&) const;
|
const bool snoop(const NBEvents&) const;
|
||||||
void setState(const uint64_t);
|
|
||||||
void listen();
|
void listen();
|
||||||
void listen(const NBEvent&);
|
void listen(const NBEvents&);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
NBEvent* eventList;
|
void _setState(const uint64_t);
|
||||||
|
|
||||||
|
NBEvents** eventList;
|
||||||
uint16_t numEvents;
|
uint16_t numEvents;
|
||||||
state_register* state;
|
state_register* state;
|
||||||
std::mutex bufferLock;
|
std::mutex bufferLock;
|
||||||
|
|||||||
@ -1,24 +1,27 @@
|
|||||||
#include "Events.h"
|
#include "Events.h"
|
||||||
namespace NB {
|
|
||||||
|
|
||||||
std::invalid_argument null_mask_error("NULL MASK NOT ALLOWED");
|
namespace NB {
|
||||||
|
|
||||||
void NULL_FUNC() {}
|
void NULL_FUNC() {}
|
||||||
|
|
||||||
NBEvent::NBEvent() {}
|
std::invalid_argument null_mask_error("NULL MASK NOT ALLOWED");
|
||||||
|
|
||||||
|
NBEvents::NBEvents(const uint64_t initMask, void (*initFunc)(), const char* initName):
|
||||||
|
mask(initMask), func(initFunc), name(initName) {
|
||||||
|
if (mask == 0x0) {
|
||||||
|
throw null_mask_error;
|
||||||
|
}
|
||||||
|
|
||||||
NBEvent::NBEvent(const uint64_t initMask, void (*initFunc)(), const char* initName) : mask{initMask}, func{initFunc}, name{initName} {
|
|
||||||
if (mask == 0x0) {
|
|
||||||
throw null_mask_error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NBEvent::NBEvent(const uint64_t initMask, const char* initName) : NBEvent(initMask, NULL_FUNC, initName) {}
|
NBEvents::NBEvents(const uint64_t initMask, const char* initName):
|
||||||
|
NBEvents(initMask, NULL_FUNC, initName) {}
|
||||||
|
|
||||||
NBEvent::NBEvent(NBEvent& cpy) : NBEvent(cpy.mask, cpy.func, cpy.name.c_str()) {}
|
NBEvents::NBEvents(const NBEvents& cpy)
|
||||||
|
:NBEvents(cpy.mask, cpy.func, cpy.name.c_str()) {}
|
||||||
|
|
||||||
NBEvent& NBEvent::operator=(NBEvent& cpy) {
|
NBEvents& NBEvents::operator=(const NBEvents& cpy) {
|
||||||
func = cpy.func;
|
func = cpy.func;
|
||||||
name = cpy.name;
|
name = cpy.name;
|
||||||
mask = cpy.mask;
|
mask = cpy.mask;
|
||||||
if (mask==0x0) {
|
if (mask==0x0) {
|
||||||
@ -27,29 +30,29 @@ NBEvent& NBEvent::operator=(NBEvent& cpy) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
NBEvent::~NBEvent() {}
|
NBEvents::~NBEvents() { func = nullptr; }
|
||||||
|
|
||||||
const uint64_t NBEvent::getMask() const {
|
const std::string NBEvents::getName() const { return name; }
|
||||||
return mask;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string NBEvent::getName() const {
|
const uint64_t NBEvents::getMask() const { return mask; }
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NBEvent::setFunc(void (*newFunc)()) {
|
void NBEvents::setMask(const uint64_t newMask) {
|
||||||
func = newFunc;
|
|
||||||
}
|
|
||||||
|
|
||||||
void NBEvent::setMask(const uint64_t newMask) {
|
|
||||||
mask = newMask;
|
mask = newMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NBEvent::setName(const char* newName) {
|
void NBEvents::setName(const char* newName) {
|
||||||
name = newName;
|
name = newName;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint64_t NBEvent::check(const uint64_t refState) const{
|
void NBEvents::setFunc(void (*newFunc)()) {
|
||||||
|
func = newFunc;
|
||||||
|
}
|
||||||
|
|
||||||
|
NBEventBasic* NBEventBasic::clone() const { return new NBEventBasic(*this); }
|
||||||
|
|
||||||
|
NBEventState* NBEventState::clone() const { return new NBEventState(*this); }
|
||||||
|
|
||||||
|
const uint64_t NBEventBasic::check(const uint64_t refState) const {
|
||||||
if ((mask!=0) && ((refState&mask)==mask)) {
|
if ((mask!=0) && ((refState&mask)==mask)) {
|
||||||
func();
|
func();
|
||||||
return refState&(~mask);
|
return refState&(~mask);
|
||||||
@ -57,37 +60,34 @@ const uint64_t NBEvent::check(const uint64_t refState) const{
|
|||||||
return refState;
|
return refState;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint64_t NBState::check(const uint64_t refState) const {
|
const uint64_t NBEventState::check(const uint64_t refState) const {
|
||||||
if ((mask!=0) && ((refState&mask)==mask)) {
|
if ((mask!=0) && ((refState&mask)==mask)) {
|
||||||
func();
|
func();
|
||||||
}
|
}
|
||||||
return refState;
|
return refState;
|
||||||
}
|
}
|
||||||
|
|
||||||
NBEventListener::NBEventListener(NBEvent* initEventList, uint16_t initNum, state_register* initStatePtr, const uint64_t initState)
|
NBEventListener::NBEventListener(NBEvents** initEventList, uint16_t initNum, const uint64_t initState, state_register* initStatePtr) {
|
||||||
: stateBuffer() {
|
|
||||||
if (initStatePtr == nullptr) {
|
if (initStatePtr == nullptr) {
|
||||||
state = new state_register;
|
state = new state_register;
|
||||||
|
} else {
|
||||||
|
state = initStatePtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::atomic_store<uint64_t>(state, initState);
|
state->store(initState);
|
||||||
numEvents = initNum;
|
numEvents = initNum;
|
||||||
eventList = new NBEvent[numEvents];
|
eventList = new NBEvents*[numEvents];
|
||||||
for (uint16_t i = 0; i < initNum; ++i) {
|
for (uint16_t i = 0; i < initNum; ++i) {
|
||||||
eventList[i] = initEventList[i];
|
eventList[i] = initEventList[i]->clone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NBEvent& NBEventListener::operator[](int ind) {
|
|
||||||
return eventList[ind % numEvents];
|
|
||||||
}
|
|
||||||
|
|
||||||
state_register* NBEventListener::getStatePtr() const {
|
state_register* NBEventListener::getStatePtr() const {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint64_t NBEventListener::getState() const {
|
const uint64_t NBEventListener::getState() const {
|
||||||
return std::atomic_load<uint64_t>(state);
|
return state->load();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NBEventListener::raiseFlags(const uint64_t newState) {
|
void NBEventListener::raiseFlags(const uint64_t newState) {
|
||||||
@ -96,7 +96,7 @@ void NBEventListener::raiseFlags(const uint64_t newState) {
|
|||||||
bufferLock.unlock();
|
bufferLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NBEventListener::raiseFlags(const NBEvent& newEvent) {
|
void NBEventListener::raiseFlags(const NBEvents& newEvent) {
|
||||||
raiseFlags(newEvent.mask);
|
raiseFlags(newEvent.mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ void NBEventListener::dropFlags(const uint64_t dropState) {
|
|||||||
bufferLock.unlock();
|
bufferLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NBEventListener::dropFlags(const NBEvent& dropEvent) {
|
void NBEventListener::dropFlags(const NBEvents& dropEvent) {
|
||||||
dropFlags(dropEvent.mask);
|
dropFlags(dropEvent.mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,21 +114,21 @@ const bool NBEventListener::snoop(const uint64_t refState) const {
|
|||||||
return ((getState()&refState)==refState);
|
return ((getState()&refState)==refState);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool NBEventListener::snoop(const NBEvent& refEvent) const {
|
const bool NBEventListener::snoop(const NBEvents& refEvent) const {
|
||||||
return snoop(refEvent.mask);
|
return snoop(refEvent.mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NBEventListener::setState(const uint64_t newState) {
|
void NBEventListener::_setState(const uint64_t newState) {
|
||||||
bufferLock.lock();
|
bufferLock.lock();
|
||||||
std::queue<NBStateChange>().swap(stateBuffer);
|
std::queue<NBStateChange>().swap(stateBuffer);
|
||||||
bufferLock.unlock();
|
bufferLock.unlock();
|
||||||
std::atomic_store<uint64_t>(state, newState);
|
state->store(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NBEventListener::listen() {
|
void NBEventListener::listen() {
|
||||||
uint64_t oldState = getState();
|
uint64_t oldState = getState();
|
||||||
for (uint16_t i = 0; i < numEvents; ++i) {
|
for (uint16_t i = 0; i < numEvents; ++i) {
|
||||||
oldState = eventList[i].check(oldState);
|
oldState = eventList[i]->check(oldState);
|
||||||
}
|
}
|
||||||
NBStateChange curr;
|
NBStateChange curr;
|
||||||
bufferLock.lock();
|
bufferLock.lock();
|
||||||
@ -149,13 +149,13 @@ void NBEventListener::listen() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
bufferLock.unlock();
|
bufferLock.unlock();
|
||||||
setState(oldState);
|
_setState(oldState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NBEventListener::listen(const NBEvent& refEvent) {
|
void NBEventListener::listen(const NBEvents& refEvent) {
|
||||||
uint64_t oldState = getState();
|
uint64_t oldState = getState();
|
||||||
oldState = refEvent.check(oldState);
|
oldState = refEvent.check(oldState);
|
||||||
setState(oldState);
|
_setState(oldState);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -8,24 +8,38 @@ void bPressAlert() {
|
|||||||
std::cout << "B PRESSED\n";
|
std::cout << "B PRESSED\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
NB::NBEvent aKeyEvent(A_PRESSED, aPressAlert, "A_PRESSED");
|
void windowCloseAlert() {
|
||||||
NB::NBEvent bKeyEvent(B_PRESSED, bPressAlert, "B_PRESSED");
|
std::cout << "WINDOW SHOULD CLOSE\n";
|
||||||
NB::NBEvent windowCloseEvent(CLOSE_WIND_SIG, stopWindows, "CLOSE_WINDOWS_SIG");
|
}
|
||||||
|
|
||||||
NB::NBState windowAIsReadyState(WINDOW_READY_A, "WINDOW_A_READY");
|
NB::NBEventBasic aKeyEvent(A_PRESSED, aPressAlert, "A_PRESSED");
|
||||||
NB::NBState windowBIsReadyState(WINDOW_READY_B, "WINDOW_B_READY");
|
NB::NBEventBasic bKeyEvent(B_PRESSED, bPressAlert, "B_PRESSED");
|
||||||
NB::NBState windowShouldCloseState(WINDOW_SHOULD_CLOSE, "WINDOW_CLOSING_STATE");
|
NB::NBEventBasic windowCloseEvent(CLOSE_WIND_SIG, stopWindows, "CLOSE_WINDOWS_SIG");
|
||||||
NB::NBState programCloseState(PROGRAM_SHOULD_CLOSE, "PROGRAM_CLOSING_STATE");
|
|
||||||
|
|
||||||
std::array<NB::NBEvent, 7> event_list= {
|
NB::NBEventState windowAIsReadyState(WINDOW_READY_A, "WINDOW_A_READY");
|
||||||
windowCloseEvent,
|
NB::NBEventState windowBIsReadyState(WINDOW_READY_B, "WINDOW_B_READY");
|
||||||
bKeyEvent,
|
NB::NBEventState windowShouldCloseState(WINDOW_SHOULD_CLOSE, windowCloseAlert, "WINDOW_CLOSING_STATE");
|
||||||
aKeyEvent,
|
NB::NBEventState programCloseState(PROGRAM_SHOULD_CLOSE, "PROGRAM_CLOSING_STATE");
|
||||||
|
|
||||||
windowAIsReadyState,
|
// std::array<NB::NBEventContainer, 7> event_list= {
|
||||||
windowBIsReadyState,
|
// windowCloseEvent,
|
||||||
windowShouldCloseState,
|
// bKeyEvent,
|
||||||
programCloseState
|
// aKeyEvent,
|
||||||
|
|
||||||
|
// windowAIsReadyState,
|
||||||
|
// windowBIsReadyState,
|
||||||
|
// windowShouldCloseState,
|
||||||
|
// programCloseState
|
||||||
|
// };
|
||||||
|
std::array<NB::NBEvents*, 7> event_list= {
|
||||||
|
&windowCloseEvent,
|
||||||
|
&bKeyEvent,
|
||||||
|
&aKeyEvent,
|
||||||
|
|
||||||
|
&windowAIsReadyState,
|
||||||
|
&windowBIsReadyState,
|
||||||
|
&windowShouldCloseState,
|
||||||
|
&programCloseState
|
||||||
};
|
};
|
||||||
NB::NBEventListener my_listener(event_list);
|
NB::NBEventListener my_listener(event_list);
|
||||||
|
|
||||||
@ -66,6 +80,7 @@ void stopWindows() {
|
|||||||
|
|
||||||
int renderingProcessA() {
|
int renderingProcessA() {
|
||||||
std::cout << "Howdy from rendering A!\n";
|
std::cout << "Howdy from rendering A!\n";
|
||||||
|
std::cout << "FROM RENDERING A I SEE IN EVENT LIST " << event_list[3]->getName() << "\n";
|
||||||
if ( aWindow->init() ) { return -1; }
|
if ( aWindow->init() ) { return -1; }
|
||||||
|
|
||||||
my_listener.raiseFlags(WINDOW_READY_A);
|
my_listener.raiseFlags(WINDOW_READY_A);
|
||||||
|
|||||||
@ -14,7 +14,6 @@ extern NB::NBEventListener my_listener;
|
|||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello World!\n";
|
std::cout << "Hello World!\n";
|
||||||
|
|
||||||
{
|
|
||||||
NB::NBWindow windowa(800, 600, "Multithreading?");
|
NB::NBWindow windowa(800, 600, "Multithreading?");
|
||||||
NB::NBWindow windowb(800, 600, "Multithreading!");
|
NB::NBWindow windowb(800, 600, "Multithreading!");
|
||||||
aWindow = &windowa;
|
aWindow = &windowa;
|
||||||
@ -33,7 +32,6 @@ int main() {
|
|||||||
}
|
}
|
||||||
std::cout << "YOUVE REACHED THE END OF THE MAIN THREAD!\n";
|
std::cout << "YOUVE REACHED THE END OF THE MAIN THREAD!\n";
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
state_register* state = my_listener.getStatePtr();
|
state_register* state = my_listener.getStatePtr();
|
||||||
delete state;
|
delete state;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user