Luna/libluna/include/luna/Bitset.h
apio a35ca0b367
All checks were successful
continuous-integration/drone/push Build is passing
libluna+kernel: Add Bitset and use it for signals
2023-08-23 10:51:02 +02:00

59 lines
957 B
C++

#pragma once
#include <luna/Check.h>
template <typename T> class Bitset
{
public:
Bitset() : m_value()
{
}
Bitset(T value) : m_value(value)
{
}
Bitset(const Bitset<T>& other) : m_value(other.m_value)
{
}
Bitset<T>& operator=(T value)
{
m_value = value;
return *this;
}
Bitset<T>& operator=(const Bitset<T>& other)
{
if (&other == this) return *this;
m_value = other.m_value;
return *this;
}
T value() const
{
return m_value;
}
void set(int index, bool b)
{
check((usize)index < (sizeof(T) * 8));
if (b) m_value |= (((T)1) << index);
else
m_value &= ~(((T)1) << index);
}
bool get(int index)
{
check((usize)index < (sizeof(T) * 8));
return m_value & (((T)1) << index);
}
void clear()
{
m_value = 0;
}
private:
T m_value;
};