Improve ChunkFile.h:

- Add support for std::set and std:pair.

- Switch from std::is_pod to std::is_trivially_copyable, to allow for
  types that have constructors but trivial copy constructors.  Easy,
  except there are three different nonstandard versions of it required
  on different platforms, in addition to the standard one.
This commit is contained in:
comex 2013-08-25 20:06:58 -04:00
parent 0f061e4e7c
commit 4c7bbd96e4
2 changed files with 54 additions and 11 deletions

View File

@ -16,6 +16,7 @@
// - Serialization code for anything complex has to be manually written. // - Serialization code for anything complex has to be manually written.
#include <map> #include <map>
#include <set>
#include <vector> #include <vector>
#include <list> #include <list>
#include <deque> #include <deque>
@ -25,6 +26,21 @@
#include "Common.h" #include "Common.h"
#include "FileUtil.h" #include "FileUtil.h"
// ewww
#if _LIBCPP_VERSION
#define IsTriviallyCopyable(T) std::is_trivially_copyable<T>::value
#elif __GNUC__
#define IsTriviallyCopyable(T) std::has_trivial_copy_constructor<T>::value
#elif _MSC_VER >= 1800
// work around bug
#define IsTriviallyCopyable(T) (std::is_trivially_copyable<T>::value || std::is_pod<T>::value)
#elif defined(_MSC_VER)
#define IsTriviallyCopyable(T) std::has_trivial_copy<T>::value
#else
#error No version of is_trivially_copyable
#endif
template <class T> template <class T>
struct LinkedListItem : public T struct LinkedListItem : public T
{ {
@ -83,6 +99,34 @@ public:
} }
} }
template <typename V>
void Do(std::set<V>& x)
{
u32 count = (u32)x.size();
Do(count);
switch (mode)
{
case MODE_READ:
for (x.clear(); count != 0; --count)
{
V value;
Do(value);
x.insert(value);
}
break;
case MODE_WRITE:
case MODE_MEASURE:
case MODE_VERIFY:
for (auto itr = x.begin(); itr != x.end(); ++itr)
{
Do(*itr);
}
break;
}
}
template <typename T> template <typename T>
void DoContainer(T& x) void DoContainer(T& x)
{ {
@ -118,6 +162,13 @@ public:
DoContainer(x); DoContainer(x);
} }
template <typename T, typename U>
void Do(std::pair<T, U>& x)
{
Do(x.first);
Do(x.second);
}
template <typename T> template <typename T>
void DoArray(T* x, u32 count) void DoArray(T* x, u32 count)
{ {
@ -128,12 +179,11 @@ public:
template <typename T> template <typename T>
void Do(T& x) void Do(T& x)
{ {
// Ideally this would be std::is_trivially_copyable, but not enough support yet static_assert(IsTriviallyCopyable(T), "Only sane for trivially copyable types");
static_assert(std::is_pod<T>::value, "Only sane for POD types");
DoVoid((void*)&x, sizeof(x)); DoVoid((void*)&x, sizeof(x));
} }
template <typename T> template <typename T>
void DoPOD(T& x) void DoPOD(T& x)
{ {

View File

@ -39,13 +39,6 @@ struct SQueuedEvent
} }
}; };
// Hacks for ChunkFile to accept SQueuedEvent as POD
namespace std
{
template <>
struct is_pod<SQueuedEvent> : std::true_type {};
}
// Important to remember that this class is for /dev/usb/oh1/57e/305 ONLY // Important to remember that this class is for /dev/usb/oh1/57e/305 ONLY
// /dev/usb/oh1 -> internal usb bus // /dev/usb/oh1 -> internal usb bus
// 57e/305 -> VendorID/ProductID of device on usb bus // 57e/305 -> VendorID/ProductID of device on usb bus