only support dump&load is_trivially_copyable types

This commit is contained in:
sunkaicheng
2019-08-20 00:48:22 +08:00
parent 21c032c9a5
commit 6ae6e2ee49
5 changed files with 34 additions and 112 deletions
+10 -62
View File
@@ -1538,7 +1538,7 @@ public:
}
template<typename OutputArchive, typename V = value_type>
typename std::enable_if<type_traits_internal::IsArithmeticType<V>::value, bool>::type
typename std::enable_if<type_traits_internal::IsDumpableType<V>::value, bool>::type
dump(OutputArchive& ar) {
typename OutputArchive::Guard guard(&ar);
if (!ar.dump(size_)) {
@@ -1547,7 +1547,7 @@ public:
}
if (size_ == 0) {
return true;
}
}
if (!ar.dump(capacity_)) {
std::cerr << "Failed to dump capacity_" << std::endl;
return false;
@@ -1561,12 +1561,12 @@ public:
if (!ar.dump(reinterpret_cast<char*>(slots_), sizeof(slot_type) * capacity_)) {
std::cerr << "Failed to dump slot_" << std::endl;
return false;
}
}
return true;
}
template<typename InputArchive, typename V = value_type>
typename std::enable_if<type_traits_internal::IsArithmeticType<V>::value, bool>::type
typename std::enable_if<type_traits_internal::IsDumpableType<V>::value, bool>::type
load(InputArchive& ar) {
typename InputArchive::Guard guard(&ar);
if (!ar.load(&size_)){
@@ -1594,60 +1594,6 @@ public:
return true;
}
// V will be V for hash_set and std::pair<const K, V> for hash_map
template<typename OutputArchive, typename V = value_type>
typename std::enable_if<! type_traits_internal::IsArithmeticType<V>::value
&& type_traits_internal::IsStringOrArithmeticType<V>::value, bool>::type
dump(OutputArchive& ar) {
typename OutputArchive::Guard guard(&ar);
if (!ar.template dump<size_t>(size_)) {
std::cerr << "Failed to dump size" << std::endl;
return false;
}
if (size_ == 0) {
return true;
}
for (auto it = this->begin(); it != this->end(); ++it) {
if (!ar.template dump<V>(*it)) {
std::cerr << "Failed to dump element" << std::endl;
return false;
}
}
return true;
}
template<typename InputArchive, typename V = value_type>
typename std::enable_if<! type_traits_internal::IsArithmeticType<V>::value
&& type_traits_internal::IsStringOrArithmeticType<V>::value, bool>::type
load(InputArchive& ar) {
typename InputArchive::Guard guard(&ar);
size_t sz = 0;
ar.template load<size_t>(&sz);
for (size_t i = 0; i < sz; i ++) {
V v;
if (!ar.template load<V>(&v)) {
std::cerr << "Failed to load element " << i << std::endl;
return false;
}
this->insert(v);
}
return true;
}
template<typename OutputArchive, typename V = value_type>
typename std::enable_if<!type_traits_internal::IsStringOrArithmeticType<V>::value, bool>::type
dump(OutputArchive&) {
std::cerr << "Does not support this type now!" << std::endl;
return false;
}
template<typename InputArchive, typename V = value_type>
typename std::enable_if<!type_traits_internal::IsStringOrArithmeticType<V>::value, bool>::type
load(InputArchive&) {
std::cerr << "Does not support this type now!" << std::endl;
return false;
}
void rehash(size_t n) {
if (n == 0 && capacity_ == 0) return;
if (n == 0 && size_ == 0) {
@@ -3253,8 +3199,9 @@ public:
a.swap(b);
}
template<typename OutputArchiveWrapper>
bool dump(OutputArchiveWrapper& w) {
template<typename OutputArchiveWrapper, typename V = value_type>
typename std::enable_if<type_traits_internal::IsDumpableType<V>::value, bool>::type
dump(OutputArchiveWrapper& w) {
for (size_t i = 0; i < sets_.size(); ++i) {
auto& inner = sets_[i];
auto ar = w.create_archive(i);
@@ -3272,8 +3219,9 @@ public:
return true;
}
template<typename InputArchiveWrapper>
bool load(InputArchiveWrapper& w) {
template<typename InputArchiveWrapper, typename V = value_type>
typename std::enable_if<type_traits_internal::IsDumpableType<V>::value, bool>::type
load(InputArchiveWrapper& w) {
size_t submap_count = w.load_meta();
if (submap_count != subcnt()) {
+7 -18
View File
@@ -78,27 +78,16 @@ struct PairTrait : public std::false_type {
template<typename T1, typename T2>
struct PairTrait<std::pair<T1, T2>>: public std::true_type {
using first_type = typename std::remove_cv<T1>::type;
using second_type = typename std::remove_cv<T2>::type;
using first_type = T1;
using second_type = T2;
};
template<typename V>
struct IsArithmeticType {
static constexpr bool value = std::is_arithmetic<V>::value
|| (PairTrait<V>::value &&
std::is_arithmetic<typename PairTrait<V>::first_type>::value
&& std::is_arithmetic<typename PairTrait<V>::second_type>::value);
};
template<typename V>
struct IsStringOrArithmeticType {
static constexpr bool value = IsArithmeticType<V>::value
|| std::is_same<V, std::string>::value
|| (PairTrait<V>::value
&& (std::is_arithmetic<typename PairTrait<V>::first_type>::value
|| std::is_same<typename PairTrait<V>::first_type, std::string>::value)
&& (std::is_arithmetic<typename PairTrait<V>::second_type>::value
|| std::is_same<typename PairTrait<V>::second_type, std::string>::value));
struct IsDumpableType {
static constexpr bool value = std::is_trivially_copyable<V>::value
|| (PairTrait<V>::value
&& std::is_trivially_copyable<typename PairTrait<V>::first_type>::value
&& std::is_trivially_copyable<typename PairTrait<V>::second_type>::value);
};
template <typename... Ts>
+12 -31
View File
@@ -351,29 +351,21 @@ public:
}
template<typename V>
typename std::enable_if<std::is_arithmetic<V>::value, bool>::type
typename std::enable_if<std::is_trivially_copyable<V>::value, bool>::type
dump(const V& v) {
CHECK_FILE(ofs_);
ofs_.write(reinterpret_cast<char*>(const_cast<V*>(&v)), sizeof(V));
return true;
}
template<typename V>
typename std::enable_if<std::is_same<std::string, typename std::remove_cv<V>::type>::value, bool>::type
dump(const V& v) {
CHECK_FILE(ofs_);
uint32_t sz = v.length();
ofs_.write(reinterpret_cast<char*>(&sz), sizeof(sz));
ofs_.write(const_cast<char*>(v.data()), sz);
return true;
}
template<typename V>
typename std::enable_if<type_traits_internal::PairTrait<V>::value
&& type_traits_internal::IsStringOrArithmeticType<V>::value, bool>::type
&& type_traits_internal::IsDumpableType<V>::value, bool>::type
dump(const V& v) {
return dump<typename type_traits_internal::PairTrait<V>::first_type>(v.first)
&& dump<typename type_traits_internal::PairTrait<V>::second_type>(v.second);
using first_type = typename type_traits_internal::PairTrait<V>::first_type;
using second_type = typename type_traits_internal::PairTrait<V>::second_type;
return dump<first_type>(v.first)
&& dump<second_type>(v.second);
}
void finish() {
@@ -405,32 +397,21 @@ public:
}
template<typename V>
typename std::enable_if<std::is_arithmetic<V>::value, bool>::type
typename std::enable_if<std::is_trivially_copyable<V>::value, bool>::type
load(V* v) {
CHECK_FILE(ifs_);
ifs_.read(reinterpret_cast<char*>(v), sizeof(V));
return true;
}
template<typename V>
typename std::enable_if<std::is_same<std::string, typename std::remove_cv<V>::type>::value, bool>::type
load(V* v) {
CHECK_FILE(ifs_);
uint32_t sz = 0;
ifs_.read(reinterpret_cast<char*>(&sz), sizeof(sz));
const_cast<std::string*>(v)->resize(sz);
ifs_.read(const_cast<char*>(v->data()), sz);
return true;
}
template<typename V>
typename std::enable_if<type_traits_internal::PairTrait<V>::value
&& type_traits_internal::IsStringOrArithmeticType<V>::value, bool>::type
&& type_traits_internal::IsDumpableType<V>::value, bool>::type
load(V* v) {
using first_type = typename type_traits_internal::PairTrait<V>::first_type;
using second_type = typename type_traits_internal::PairTrait<V>::second_type;
return load<first_type>(const_cast<first_type*>(&v->first))
&& load<second_type>(const_cast<second_type*>(&v->second));
using first_type = typename std::remove_cv<typename type_traits_internal::PairTrait<V>::first_type>::type;
using second_type = typename std::remove_cv<typename type_traits_internal::PairTrait<V>::second_type>::type;
return load<first_type>(const_cast<first_type*>(&v->first))
&& load<second_type>(const_cast<second_type*>(&v->second));
}
void finish() {