Renamed EventNotifer to EventFdNotifier and employed new NotifierInterface

This commit is contained in:
Nav
2022-04-15 22:05:50 +01:00
parent 3509d0de78
commit ec060a469b
14 changed files with 100 additions and 94 deletions

View File

@@ -1,4 +1,4 @@
#include "EventNotifier.hpp"
#include "EventFdNotifier.hpp"
#include <sys/eventfd.h>
#include <unistd.h>
@@ -11,7 +11,7 @@ namespace Bloom
{
using Exceptions::Exception;
EventNotifier::EventNotifier() {
EventFdNotifier::EventFdNotifier() {
this->fileDescriptor = ::eventfd(0, EFD_NONBLOCK);
if (this->fileDescriptor < 0) {
@@ -22,32 +22,32 @@ namespace Bloom
}
}
EventNotifier::EventNotifier(EventNotifier&& other) noexcept
EventFdNotifier::EventFdNotifier(EventFdNotifier&& other) noexcept
: fileDescriptor(other.fileDescriptor)
{
other.fileDescriptor = std::nullopt;
}
EventNotifier::~EventNotifier() noexcept {
EventFdNotifier::~EventFdNotifier() noexcept {
this->close();
}
void EventNotifier::notify() {
void EventFdNotifier::notify() {
if (::eventfd_write(this->fileDescriptor.value(), 1) < 0) {
throw Exceptions::Exception("Failed to increment eventfd counter - error number: "
+ std::to_string(errno));
}
}
void EventNotifier::clear() {
void EventFdNotifier::clear() {
eventfd_t counter = {};
if (::eventfd_read(this->fileDescriptor.value(), &counter) < 0 && errno != EAGAIN) {
throw Exceptions::Exception("Failed to clear EventNotifier object - eventfd_read failed - "
throw Exceptions::Exception("Failed to clear EventFdNotifier object - eventfd_read failed - "
"error number: " + std::to_string(errno));
}
}
void EventNotifier::close() {
void EventFdNotifier::close() {
if (this->fileDescriptor.value_or(-1) >= 0) {
::close(this->fileDescriptor.value());
}

View File

@@ -0,0 +1,46 @@
#pragma once
#include <optional>
#include "NotifierInterface.hpp"
namespace Bloom
{
/**
* The EventFdNotifier class is an RAII wrapper for a Linux eventfd object.
*
* It uses an eventfd object to implement the NotifierInterface.
*/
class EventFdNotifier: public NotifierInterface
{
public:
EventFdNotifier();
/*
* EventNotifier objects should not be copied.
*/
EventFdNotifier(EventFdNotifier& other) = delete;
EventFdNotifier& operator = (EventFdNotifier& other) = delete;
/*
* TODO: Implement this. For now, use the move constructor.
*/
EventFdNotifier& operator = (EventFdNotifier&& other) = delete;
EventFdNotifier(EventFdNotifier&& other) noexcept;
~EventFdNotifier() noexcept override;
[[nodiscard]] int getFileDescriptor() const {
return this->fileDescriptor.value();
}
void notify() override;
void clear();
private:
std::optional<int> fileDescriptor;
void close();
};
}

View File

@@ -1,45 +0,0 @@
#pragma once
#include <optional>
namespace Bloom
{
/**
* The EventNotifier class is an RAII wrapper for a Linux eventfd object.
*
* The EventListener can hold an instance to EventNotifier, where it will invoke EventNotifier::notify() everytime
* a new event is registered on the listener.
*/
class EventNotifier
{
public:
EventNotifier();
/*
* EpollInstance objects should not be copied.
*/
EventNotifier(EventNotifier& other) = delete;
EventNotifier& operator = (EventNotifier& other) = delete;
/*
* TODO: Implement this. For now, use the move constructor.
*/
EventNotifier& operator = (EventNotifier&& other) = delete;
EventNotifier(EventNotifier&& other) noexcept;
~EventNotifier() noexcept;
[[nodiscard]] int getFileDescriptor() const {
return this->fileDescriptor.value();
}
void notify();
void clear();
private:
std::optional<int> fileDescriptor;
void close();
};
}