lundi 28 juin 2021

Include different file with defining preprocessor variable

I saw very nice example for using different classes using -D compile switch.

However, in my use case, the thing is bit different and I can don't know how to do it.

For multiplexing API, I have 3 classes in 3 different files:

poll.h   -> class PollSelector   -> generic poll() works everywhere
epoll.h  -> class EPollSelector  -> Linux only epoll support
kqueue.h -> class KqueueSelector -> MacOS only kqueue

All 3 clases have same methods, so I use:

#include "selector/poll.h"
using MySelector = PollSelector;

I am thinking, if there a way to be able to do it with -D switch, but in same time, if I add additional multiplexing class, to be able to switch it only with -D flag, without edit the file that use it.

The way I imaging it is to have command like:

gcc -Dkqueue file.h

this automatically do

#include "selector/kqueue.h";
using MySelector = kqueue_selector; // I will do this typedef in kqueue.h so it will compile

Can this be done in clean way, without "diving" in C preprocessor?

Here are the original link to source
https://github.com/nmmmnu/HM4/tree/master/net/selector

Aucun commentaire:

Enregistrer un commentaire