1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef CR_QUEUE_TYPE_IMPL_H
#define CR_QUEUE_TYPE_IMPL_H
#include "creg_cond.h"
#include <queue>
#ifdef USING_CREG
namespace creg {
// From http://stackoverflow.com/a/1385520
// It's insane and I hope it works
template <class T, class S, class C>
S& Container(std::priority_queue<T, S, C>& q) {
struct HackedQueue : private std::priority_queue<T, S, C> {
static S& Container(std::priority_queue<T, S, C>& q) {
return q.*&HackedQueue::c;
}
};
return HackedQueue::Container(q);
}
// IType implemention for std::list
template<class T, class S, class C>
struct PQueueType : public IType
{
PQueueType() : IType(sizeof(std::priority_queue<T, S, C>)) { }
~PQueueType() { }
void Serialize(ISerializer* s, void* inst) override {
S& ct = Container(*(std::priority_queue<T, S, C>*)inst);
if (s->IsWriting()) {
int size = (int)ct.size();
s->SerializeInt(&size,sizeof(int));
for (typename S::iterator it = ct.begin(); it != ct.end(); ++it)
{
DeduceType<T>::Get()->Serialize(s, &*it);
}
} else {
ct.clear();
int size;
s->SerializeInt(&size, sizeof(int));
ct.resize(size);
for (typename S::iterator it = ct.begin(); it != ct.end(); ++it)
{
DeduceType<T>::Get()->Serialize(s, &*it);
}
}
}
std::string GetName() const override { return "priority_queue<" + DeduceType<T>::Get()->GetName() + ">"; }
};
// List type
template<class T, class S, class C>
struct DeduceType< std::priority_queue<T, S, C> > {
static std::unique_ptr<IType> Get() {
return std::unique_ptr<IType>(new PQueueType<T, S, C>());
}
};
}
#endif // USING_CREG
#endif // CR_QUEUE_TYPE_IMPL_H
|