Last active
March 10, 2017 11:10
-
-
Save makulik/7963331 to your computer and use it in GitHub Desktop.
A bitset index converter to convert mask flag values to their corresponding index in a std::bitset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* BitIndexConverter.hpp | |
* | |
* Created on: Dec 14, 2013 | |
* Author: user | |
*/ | |
#ifndef BITINDEXCONVERTER_HPP_ | |
#define BITINDEXCONVERTER_HPP_ | |
#include <stdint.h> | |
#include <limits.h> | |
#include <bitset> | |
template<typename BitFieldType, BitFieldType BitValue, int8_t CurIndex> | |
struct BitIndexSelector; | |
template<typename BitFieldType, BitFieldType BitValue, int8_t CurIndex> | |
struct BitIndexSelector | |
{ | |
static const int8_t ResultIndex = | |
((CurIndex != -1) && | |
(((((BitFieldType)1) << CurIndex) & BitValue) > 0) | |
? CurIndex | |
: BitIndexSelector | |
<BitFieldType | |
,BitValue | |
,CurIndex - 1>::ResultIndex); | |
}; | |
template<typename BitFieldType, BitFieldType BitValue> | |
struct BitIndexSelector<BitFieldType,BitValue,-1> | |
{ | |
static const int8_t ResultIndex = -1; | |
}; | |
template<typename BitFieldType, BitFieldType BitValue = 0> | |
struct GetBitIndex | |
{ | |
static const int8_t Index = | |
BitIndexSelector<BitFieldType,BitValue,sizeof(BitFieldType) * sizeof(char) * CHAR_BIT>::ResultIndex; | |
typedef std::bitset<sizeof(BitFieldType) * CHAR_BIT> BitsetType; | |
}; | |
template<typename BitFieldType> | |
class BitFieldModifier | |
{ | |
public: | |
typedef std::bitset<sizeof(BitFieldType) * CHAR_BIT> BitsetType; | |
BitFieldModifier(BitFieldType& bitfield) : bitfield_(bitfield) {} | |
private: | |
BitFieldType& bitfield_; | |
}; | |
#endif /* BITINDEXCONVERTER_HPP_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* main.cpp | |
* | |
* Created on: Dec 14, 2013 | |
* Author: user | |
*/ | |
#include <cstddef> | |
#include <poll.h> | |
#include <iostream> | |
#include "PollEvents.hpp" | |
int main() | |
{ | |
PollEvents ev; | |
ev.event(PollEvents::EV_POLLIN,true); | |
ev.event(PollEvents::EV_POLLOUT,true); | |
std::cout << ev << std::endl; | |
std::cout << "POLLIN: " << ev[PollEvents::EV_POLLIN] << std::endl; | |
std::cout << "POLLOUT: " << ev[PollEvents::EV_POLLOUT] << std::endl; | |
std::cout << "POLLPRI: " << ev[PollEvents::EV_POLLPRI] << std::endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* PollEvents.hpp | |
* | |
* Created on: Dec 14, 2013 | |
* Author: user | |
*/ | |
#ifndef POLLEVENTS_HPP_ | |
#define POLLEVENTS_HPP_ | |
#include <bitset> | |
#include <poll.h> | |
#include "BitIndexConverter.hpp" | |
class PollEvents | |
{ | |
public: | |
typedef typename GetBitIndex<short>::BitsetType BitsetType; | |
enum Events | |
{ | |
EV_POLLIN = GetBitIndex<short,POLLIN>::Index , | |
EV_POLLOUT = GetBitIndex<short,POLLOUT>::Index , | |
EV_POLLPRI = GetBitIndex<short,POLLPRI>::Index , | |
EV_POLLRDHUP = GetBitIndex<short,POLLRDHUP>::Index , | |
EV_POLLERR = GetBitIndex<short,POLLERR>::Index , | |
EV_POLLHUP = GetBitIndex<short,POLLHUP>::Index , | |
}; | |
bool operator[](Events key) const | |
{ | |
return event(key); | |
} | |
void event(Events key, bool value) | |
{ | |
events_[(std::size_t)key] = value; | |
} | |
bool event(Events key) const | |
{ | |
return events_[(std::size_t)key]; | |
} | |
void dump(std::ostream& os) const | |
{ | |
os << events_ << std::endl; | |
} | |
private: | |
BitsetType events_; | |
}; | |
std::ostream& operator<<(std::ostream& os, const PollEvents& pollEevents) | |
{ | |
pollEevents.dump(os); | |
return os; | |
} | |
#endif /* POLLEVENTS_HPP_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment