I’m trying to find better methods to handle the events of a polled socket (fd). I’m stuck on this to exploit the poll() function with a different pollfd like this:
#define out std::cout
#define el std::endl
#define line(string) out<<string<<el
class int_t
{
private:
int _int;
int _owner;
public:
operator int() const;
int_t();
int_t(const int& in);
int_t& operator=(const int& in);
int_t operator|(const int& in) const;
int_t operator&(const int& in) const;
/*
...
...
*/
void setowner(const int& fd);
~int_t();
};
int_t::operator int() const
{
return this->_int;
}
int_t::int_t()
{
this->_int = 0;
this->_owner = 0;
}
int_t::int_t(const int& in)
{
this->_int = in;
this->_owner = 0;
}
int_t& int_t::operator=(const int& in)
{
line("operator '=' called"<<" owner:"<<this->_owner);
this->_int = in;
return *this;
}
int_t int_t::operator|(const int& in) const
{
line("operator '|' called"<<" owner:"<<this->_owner);
return (this->_int|in);
}
int_t int_t::operator&(const int& in) const
{
line("operator '&' with arg called"<<" owner:"<<this->_owner);
return (this->_int&in);
}
/*
...
...
*/
void int_t::setowner(const int& fd)
{
this->_owner = fd;
}
int_t::~int_t()
{
this->_int = 0;
}
struct pollfd_other
{
// Valgrind returns me an error when i changing the type of the `revent` only
// but when i changing the type of the `event` and `revent` works without error
// and `poll()` gets the flags normally from the `event` if ill put for example
// a `POLLIN` flag.
int fd;
int_t events;
int_t revents;
void setowner(const int& pollfdowner){
this->fd = pollfdowner;
this->revents.setowner(pollfdowner);
}
};
int main(int argc,char* argv[])
{
Server server;
pollfd_other pfd[1];
pfd[0].setowner(server.socket);
::poll(reinterpret_cast<pollfd*>(&pfd),1,-1);
// ...
}
The class int_t works very well as an integer and struct pollfd_other … but the poll() doesn’t access it like a class to invoke the operators …
The purpose of this is to create a thread when an operator will be invoked on the revents member of the struct pollfd_other.
There is any other method to do something like this? Suggestions are welcomed …
That looks very convoluted. If you want to encapsulate the poll functionality in an object, don’t encapsulate an int, encapsulate the poll. Something like this:
This is how I’d do it. Have a class that encapsulates
poll(), which is parameterized on what it should do on events.poller_callbackis an interface for objects that handle events. You can then writemy_poller_callbackwhich, in itshandle_pollin, creates the thread.