-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This file has been on my disk far too long, time to commit it, though…
… it's not used anywhere(yet)
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#include "creg_cond.h" | ||
#include <bitset> | ||
#include <bit> | ||
#include <array> | ||
#include <cstdint> | ||
#include "../TemplateUtils.hpp" | ||
|
||
#ifdef USING_CREG | ||
|
||
namespace creg | ||
{ | ||
template<size_t N> | ||
class BitsetType : public IType | ||
{ | ||
public: | ||
using T = std::bitset<N>; | ||
BitsetType() : IType(sizeof(T)) { } | ||
~BitsetType() { } | ||
|
||
void Serialize(ISerializer* s, void* instance) | ||
{ | ||
T& ct = *(T*)instance; | ||
|
||
std::array<uint8_t, sizeof(T)> bytesRep = {0}; | ||
|
||
if (s->IsWriting()) | ||
bytesRep = std::bit_cast<decltype(bytesRep)>(ct); | ||
|
||
for (auto& b : bytesRep) { | ||
s->SerializeInt(b, sizeof(b)); | ||
} | ||
|
||
if (!s->IsWriting()) | ||
ct = std::bit_cast<T>(bytesRep); | ||
} | ||
std::string GetName() const { | ||
return std::string("bitset<" + std::to_string(N) + ">"); | ||
} | ||
}; | ||
|
||
template<size_t N> | ||
struct DeduceType<std::bitset<N> > { | ||
static std::unique_ptr<IType> Get() { | ||
return std::unique_ptr<IType>(new BitsetType<N>()); | ||
} | ||
}; | ||
} | ||
|
||
#endif // USING_CREG |