| 1 |
#ifndef ME_PROXYPACK_HH_ |
| 2 |
#define ME_PROXYPACK_HH_ |
| 3 |
|
| 4 |
#include "geners/tupleIO.hh" |
| 5 |
|
| 6 |
namespace gs { |
| 7 |
// |
| 8 |
// The class ClassIdComparator must provide a static function |
| 9 |
// "bool compatible(const ClassId& idOnFile, const ClassId& idProxyPack);" |
| 10 |
// |
| 11 |
template<class ClassIdComparator, typename... Args> |
| 12 |
class ProxyPack : public std::tuple<Args...> |
| 13 |
{ |
| 14 |
public: |
| 15 |
typedef std::tuple<Args...> Base; |
| 16 |
|
| 17 |
ProxyPack() : firstRead_(true) {} |
| 18 |
|
| 19 |
inline explicit ProxyPack(const Args&... args) : |
| 20 |
std::tuple<Args...>(args...), firstRead_(true) {} |
| 21 |
|
| 22 |
inline ClassId classId() const {return ClassId(*this);} |
| 23 |
static inline const char* classname() |
| 24 |
{ |
| 25 |
static std::string name; |
| 26 |
if (name.size() == 0) |
| 27 |
{ |
| 28 |
std::ostringstream os; |
| 29 |
os << "gs::ProxyPack<"; |
| 30 |
Private::TupleClassIdCycler< |
| 31 |
Base, std::tuple_size<Base>::value>::collectClassIds(os); |
| 32 |
os << '>'; |
| 33 |
name = os.str(); |
| 34 |
} |
| 35 |
return name.c_str(); |
| 36 |
} |
| 37 |
static inline unsigned version() {return 1;} |
| 38 |
|
| 39 |
inline bool write(std::ostream& of) const |
| 40 |
{ |
| 41 |
return write_item(of, *(static_cast<const Base*>(this)), false); |
| 42 |
} |
| 43 |
|
| 44 |
static inline bool restore(const ClassId& id, std::istream& is, |
| 45 |
ProxyPack* pack) |
| 46 |
{ |
| 47 |
// Checking ClassId every time would take too long... |
| 48 |
assert(pack); |
| 49 |
if (pack->firstRead_) |
| 50 |
{ |
| 51 |
assert(ClassIdComparator::compatible(id, pack->classId())); |
| 52 |
pack->firstRead_ = false; |
| 53 |
pack->iostack_.reserve(1); |
| 54 |
pack->iostack_.push_back(id); |
| 55 |
} |
| 56 |
return GenericReader<std::istream, std::vector<ClassId>, Base, |
| 57 |
Int2Type<IOTraits<int>::ISTUPLE> >::process( |
| 58 |
*(static_cast<Base*>(pack)), is, &pack->iostack_, false); |
| 59 |
} |
| 60 |
|
| 61 |
private: |
| 62 |
bool firstRead_; |
| 63 |
std::vector<ClassId> iostack_; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
// Functions to simplify creation of proxy packs |
| 68 |
template<typename... Args> |
| 69 |
inline ProxyPack<SameClassName, Args...> make_proxy_pack(Args&&... args) |
| 70 |
{ |
| 71 |
return ProxyPack<SameClassName, Args...>(args...); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
#endif // ME_PROXYPACK_HH_ |