mirror of https://github.com/davisking/dlib.git
Added serialization support for std::vector<bool>.
This commit is contained in:
parent
b88c47d958
commit
721d55a610
|
@ -806,6 +806,43 @@ namespace dlib
|
|||
{ throw serialization_error(e.info + "\n while deserializing object of type std::set"); }
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <typename alloc>
|
||||
void serialize (
|
||||
const std::vector<bool,alloc>& item,
|
||||
std::ostream& out
|
||||
)
|
||||
{
|
||||
std::vector<unsigned char> temp(item.size());
|
||||
for (unsigned long i = 0; i < item.size(); ++i)
|
||||
{
|
||||
if (item[i])
|
||||
temp[i] = '1';
|
||||
else
|
||||
temp[i] = '0';
|
||||
}
|
||||
serialize(temp, out);
|
||||
}
|
||||
|
||||
template <typename alloc>
|
||||
void deserialize (
|
||||
std::vector<bool,alloc>& item,
|
||||
std::istream& in
|
||||
)
|
||||
{
|
||||
std::vector<unsigned char> temp;
|
||||
deserialize(temp, in);
|
||||
item.resize(temp.size());
|
||||
for (unsigned long i = 0; i < temp.size(); ++i)
|
||||
{
|
||||
if (temp[i] == '1')
|
||||
item[i] = true;
|
||||
else
|
||||
item[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
template <typename T, typename alloc>
|
||||
|
|
|
@ -530,6 +530,32 @@ namespace
|
|||
}
|
||||
}
|
||||
|
||||
void test_vector_bool (
|
||||
)
|
||||
{
|
||||
std::vector<bool> a, b;
|
||||
|
||||
a.push_back(true);
|
||||
a.push_back(true);
|
||||
a.push_back(false);
|
||||
a.push_back(true);
|
||||
a.push_back(false);
|
||||
a.push_back(true);
|
||||
|
||||
ostringstream sout;
|
||||
dlib::serialize(a, sout);
|
||||
istringstream sin(sout.str());
|
||||
|
||||
dlib::deserialize(b, sin);
|
||||
|
||||
|
||||
DLIB_TEST(a.size() == b.size());
|
||||
DLIB_TEST(a.size() == 6);
|
||||
for (unsigned long i = 0; i < a.size(); ++i)
|
||||
{
|
||||
DLIB_TEST(a[i] == b[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class serialize_tester : public tester
|
||||
|
@ -554,6 +580,7 @@ namespace
|
|||
test_vector<char>();
|
||||
test_vector<unsigned char>();
|
||||
test_vector<int>();
|
||||
test_vector_bool();
|
||||
}
|
||||
} a;
|
||||
|
||||
|
|
Loading…
Reference in New Issue