thor-os/tstl/include/bit_field.hpp
Baptiste Wicht 223f40820a Cleanup
2016-09-13 19:16:01 +02:00

37 lines
916 B
C++

//=======================================================================
// Copyright Baptiste Wicht 2013-2016.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
// http://www.opensource.org/licenses/MIT)
//=======================================================================
#ifndef BITFIELD_HPP
#define BITFIELD_HPP
#include <types.hpp>
namespace std {
template<typename S, typename T, size_t Position, size_t Size>
struct bit_field {
S* value;
bit_field(S* value) : value(value) {}
T operator*() const {
return (*value >> Position) & ((1ULL << Size) - 1);
}
bit_field& operator=(T new_value_real){
S new_value(new_value_real);
size_t mask = ((S(1) << Size) - 1) << Position;
*value = (*value & ~mask) | ((new_value << Position) & mask);
return *this;
}
};
} //end of namespace std
#endif