0

I have code like this:

#include <vector>

struct pollfd; // incomplete struct, since i did not included <poll>

struct Status{
   // abstract representation of pollfd, epoll_event, kevent
   int fd;
   int status; // my own status representation
};

class iterator{
public:
    hidden_pointer_iterator(const pollfd *pos) : pos(pos){}

    bool operator !=(iterator const &other) const{
        return ! ( *this == other);
    }

    bool operator ==(iterator const &other) const;
    iterator &operator ++();
    Status operator *() const;

private:
    const pollfd *pos;
};

class PollSelector{
public:
    // ...
    iterator begin() const; // pull pointer to fds_.data()
    iterator end() const;   // pull pointer to fds_.data() + fds_.size()

private:
    std::vector<pollfd> fds_;
};

I was able to make it run, by implementing all specific operations in the CPP file.

My question is - is there more "standard" way to do this iterator?

Update

My code compiles and works.

I am qurious if there are something in std that can do all this automatically, without so much coding.

Community
  • 1
  • 1
Nick
  • 9,962
  • 4
  • 42
  • 80
  • 2
    I think you can make that work. For more details, check out [pimpl](https://en.cppreference.com/w/cpp/language/pimpl). – Ted Lyngmo Jun 18 '19 at 16:13
  • 1
    Oh, it works as a charm. I even template-ize it. Point is am I reinventing a wheel? – Nick Jun 18 '19 at 16:16
  • Sort of, but I actually "had" to do the same a month ago. Have never had the idea to do something like that before :) – Ted Lyngmo Jun 18 '19 at 16:18
  • Possible duplicate of [Why can't a forward declaration be used for a std::vector?](https://stackoverflow.com/questions/37346/why-cant-a-forward-declaration-be-used-for-a-stdvector) Please [note alternative answer](https://stackoverflow.com/a/15382714/1387438) (not accepted). – Marek R Jun 18 '19 at 16:43

1 Answers1

0

A bit off-topic, IMO, this code abstracts away the wrong thing.

You should rather have multiple implementations of your demultiplexer engine interface, one that uses epoll, another for kevent, etc.. Demultiplexer engine interface is the one that allows you to register callbacks for file descriptor events, timers, signals, cross-thread, etc..

On Linux, there may be little point to using anything else but epoll.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • this is the way i did it already. iterator is to iterate over statuses. However I do not like C headers to leak and make namespace "dirty" – Nick Jun 18 '19 at 17:28