Skip to content

Commit 3711f97

Browse files
committed
Improvements to the C++ API
1 parent 34d62b1 commit 3711f97

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

events.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ class EventManager
473473
EventManager(struct EventState *state) : ctx(state),
474474
free_state(false) {}
475475
virtual ~EventManager() { if (free_state) EVT_free_handler(ctx); }
476+
void state(struct EventState *state, bool frees = false) {
477+
if (free_state && ctx)
478+
EVT_free_handler(ctx);
479+
free_state = frees;
480+
ctx = state;
481+
}
476482

477483
int EventLoop() { return EVT_start_loop(ctx); }
478484
void Exit() { EVT_exit_loop(ctx); }
@@ -482,7 +488,8 @@ class EventManager
482488
void RemoveWriteEvent(int fd) { RemoveEvent(fd, EVENT_FD_WRITE); }
483489
void RemoveErrorEvent(int fd) { RemoveEvent(fd, EVENT_FD_ERROR); }
484490

485-
struct EventState *state() { return ctx; }
491+
EVTHandler *state() { return ctx; }
492+
operator EVTHandler*() { return ctx; }
486493

487494
void AddEvent(int fd, EVT_fd_cb cb, void *p, int event,
488495
EVT_fd_cb cleanup = NULL)

proclib.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -476,25 +476,27 @@ class Process
476476
Process(const char *name, enum WatchdogMode wdMode) : events(NULL)
477477
{ process = PROC_init(name, wdMode); }
478478
virtual ~Process() {
479-
delete events;
480479
if (process)
481480
PROC_cleanup(process);
482481
}
483-
EventManager *event_manager() {
484-
if (!events)
485-
events = new EventManager(PROC_evt(process));
482+
EventManager &event_manager() {
483+
if (!events.state())
484+
events.state(PROC_evt(process));
486485
return events;
487486
}
488487

489488
int AddSignalEvent(int sigNum, PROC_signal_cb cb, void *p)
490489
{ return PROC_signal(process, sigNum, cb, p); }
491490

491+
operator EVTHandler*() { return event_manager().state(); }
492+
operator ProcessData*() { return process; }
493+
492494
ProcessData *getProcessData(void) { return process; }
493495

494496
private:
495497
Process() { process = NULL; }
496498
ProcessData *process;
497-
EventManager *events;
499+
EventManager events;
498500
};
499501

500502
#endif

0 commit comments

Comments
 (0)