Skip to content

Commit 2f9471b

Browse files
committed
Merge branch 'develop' for release 1.1.2
2 parents a248e3a + 774fe61 commit 2f9471b

File tree

7 files changed

+227
-6
lines changed

7 files changed

+227
-6
lines changed

include/cereal/archives/json.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,15 @@ namespace cereal
491491
const auto len = std::strlen( searchName );
492492
size_t index = 0;
493493
for( auto it = itsMemberItBegin; it != itsMemberItEnd; ++it, ++index )
494-
if( std::strncmp( searchName, it->name.GetString(), len ) == 0 )
494+
{
495+
const auto currentName = it->name.GetString();
496+
if( ( std::strncmp( searchName, currentName, len ) == 0 ) &&
497+
( std::strlen( currentName ) == len ) )
495498
{
496499
itsIndex = index;
497500
return;
498501
}
502+
}
499503

500504
throw Exception("JSON Parsing failed - provided NVP not found");
501505
}

include/cereal/types/valarray.hpp

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*! \file valarray.hpp
2+
\brief Support for types found in \<valarray\>
3+
\ingroup STLSupport */
4+
5+
/*
6+
Copyright (c) 2014, Randolph Voorhies, Shane Grant
7+
All rights reserved.
8+
9+
Redistribution and use in source and binary forms, with or without
10+
modification, are permitted provided that the following conditions are met:
11+
* Redistributions of source code must retain the above copyright
12+
notice, this list of conditions and the following disclaimer.
13+
* Redistributions in binary form must reproduce the above copyright
14+
notice, this list of conditions and the following disclaimer in the
15+
documentation and/or other materials provided with the distribution.
16+
* Neither the name of cereal nor the
17+
names of its contributors may be used to endorse or promote products
18+
derived from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
24+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef CEREAL_TYPES_VALARRAY_HPP_
33+
#define CEREAL_TYPES_VALARRAY_HPP_
34+
35+
#include <cereal/cereal.hpp>
36+
#include <valarray>
37+
38+
namespace cereal
39+
{
40+
//! Saving for std::valarray arithmetic types, using binary serialization, if supported
41+
template <class Archive, class T> inline
42+
typename std::enable_if<traits::is_output_serializable<BinaryData<T>, Archive>::value
43+
&& std::is_arithmetic<T>::value, void>::type
44+
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
45+
{
46+
ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
47+
ar( binary_data( &valarray[0], valarray.size() * sizeof(T) ) ); // &valarray[0] ok since guaranteed contiguous
48+
}
49+
50+
//! Loading for std::valarray arithmetic types, using binary serialization, if supported
51+
template <class Archive, class T> inline
52+
typename std::enable_if<traits::is_input_serializable<BinaryData<T>, Archive>::value
53+
&& std::is_arithmetic<T>::value, void>::type
54+
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
55+
{
56+
size_type valarraySize;
57+
ar( make_size_tag( valarraySize ) );
58+
59+
valarray.resize( static_cast<std::size_t>( valarraySize ) );
60+
ar( binary_data( &valarray[0], static_cast<std::size_t>( valarraySize ) * sizeof(T) ) );
61+
}
62+
63+
//! Saving for std::valarray all other types
64+
template <class Archive, class T> inline
65+
typename std::enable_if<!traits::is_output_serializable<BinaryData<T>, Archive>::value
66+
|| !std::is_arithmetic<T>::value, void>::type
67+
CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::valarray<T> const & valarray )
68+
{
69+
ar( make_size_tag( static_cast<size_type>(valarray.size()) ) ); // number of elements
70+
for(auto && v : valarray)
71+
ar(v);
72+
}
73+
74+
//! Loading for std::valarray all other types
75+
template <class Archive, class T> inline
76+
typename std::enable_if<!traits::is_input_serializable<BinaryData<T>, Archive>::value
77+
|| !std::is_arithmetic<T>::value, void>::type
78+
CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::valarray<T> & valarray )
79+
{
80+
size_type valarraySize;
81+
ar( make_size_tag( valarraySize ) );
82+
83+
valarray.resize( static_cast<size_t>( valarraySize ) );
84+
for(auto && v : valarray)
85+
ar(v);
86+
}
87+
} // namespace cereal
88+
89+
#endif // CEREAL_TYPES_VALARRAY_HPP_

unittests/common.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <cereal/types/memory.hpp>
3131
#include <cereal/types/array.hpp>
32+
#include <cereal/types/valarray.hpp>
3233
#include <cereal/types/vector.hpp>
3334
#include <cereal/types/deque.hpp>
3435
#include <cereal/types/forward_list.hpp>

unittests/unordered_loads.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
struct unordered_naming
3131
{
3232
int x;
33+
int xx;
3334
int y;
3435
int z;
3536

@@ -38,26 +39,28 @@ struct unordered_naming
3839
{
3940
ar( CEREAL_NVP(x),
4041
CEREAL_NVP(z),
41-
CEREAL_NVP(y) );
42+
CEREAL_NVP(y),
43+
CEREAL_NVP(xx) );
4244
}
4345

4446
template <class Archive>
4547
void load( Archive & ar )
4648
{
4749
ar( x,
4850
CEREAL_NVP(y),
49-
CEREAL_NVP(z) );
51+
CEREAL_NVP(z),
52+
CEREAL_NVP(xx) );
5053
}
5154

5255
bool operator==( unordered_naming const & other ) const
5356
{
54-
return x == other.x && y == other.y && z == other.z;
57+
return x == other.x && xx == other.xx && y == other.y && z == other.z;
5558
}
5659
};
5760

5861
std::ostream& operator<<(std::ostream& os, unordered_naming const & s)
5962
{
60-
os << "[x: " << s.x << " y: " << s.y << " z: " << s.z << "]";
63+
os << "[x: " << s.x << " xx: " << s.xx << " y: " << s.y << " z: " << s.z << "]";
6164
return os;
6265
}
6366

@@ -92,6 +95,7 @@ void test_unordered_loads()
9295
std::pair<float, unordered_naming> o_un7;
9396
o_un7.first = rngF();
9497
o_un7.second.x = rngI();
98+
o_un7.second.xx = rngI();
9599
o_un7.second.y = rngI();
96100
o_un7.second.z = rngI();
97101

unittests/valarray.cpp

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright (c) 2014, Randolph Voorhies, Shane Grant
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
* Neither the name of cereal nor the
13+
names of its contributors may be used to endorse or promote products
14+
derived from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES AND SHANE GRANT BE LIABLE FOR ANY
20+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#include "common.hpp"
29+
#include <boost/test/unit_test.hpp>
30+
31+
template <class IArchive, class OArchive>
32+
void test_valarray()
33+
{
34+
std::random_device rd;
35+
std::mt19937 gen(rd());
36+
37+
for (int ii = 0; ii<100; ++ii)
38+
{
39+
std::valarray<int> o_podvalarray(100);
40+
for (auto & elem : o_podvalarray)
41+
elem = random_value<int>(gen);
42+
43+
std::valarray<StructInternalSerialize> o_iservalarray(100);
44+
for (auto & elem : o_iservalarray)
45+
elem = StructInternalSerialize(random_value<int>(gen), random_value<int>(gen));
46+
47+
std::valarray<StructInternalSplit> o_isplvalarray(100);
48+
for (auto & elem : o_isplvalarray)
49+
elem = StructInternalSplit(random_value<int>(gen), random_value<int>(gen));
50+
51+
std::valarray<StructExternalSerialize> o_eservalarray(100);
52+
for (auto & elem : o_eservalarray)
53+
elem = StructExternalSerialize(random_value<int>(gen), random_value<int>(gen));
54+
55+
std::valarray<StructExternalSplit> o_esplvalarray(100);
56+
for (auto & elem : o_esplvalarray)
57+
elem = StructExternalSplit(random_value<int>(gen), random_value<int>(gen));
58+
59+
std::ostringstream os;
60+
{
61+
OArchive oar(os);
62+
63+
oar(o_podvalarray);
64+
oar(o_iservalarray);
65+
oar(o_isplvalarray);
66+
oar(o_eservalarray);
67+
oar(o_esplvalarray);
68+
}
69+
70+
std::valarray<int> i_podvalarray;
71+
std::valarray<StructInternalSerialize> i_iservalarray;
72+
std::valarray<StructInternalSplit> i_isplvalarray;
73+
std::valarray<StructExternalSerialize> i_eservalarray;
74+
std::valarray<StructExternalSplit> i_esplvalarray;
75+
76+
std::istringstream is(os.str());
77+
{
78+
IArchive iar(is);
79+
80+
iar(i_podvalarray);
81+
iar(i_iservalarray);
82+
iar(i_isplvalarray);
83+
iar(i_eservalarray);
84+
iar(i_esplvalarray);
85+
}
86+
87+
BOOST_CHECK_EQUAL(i_podvalarray.size(), o_podvalarray.size());
88+
BOOST_CHECK_EQUAL(i_iservalarray.size(), o_iservalarray.size());
89+
BOOST_CHECK_EQUAL(i_isplvalarray.size(), o_isplvalarray.size());
90+
BOOST_CHECK_EQUAL(i_eservalarray.size(), o_eservalarray.size());
91+
BOOST_CHECK_EQUAL(i_esplvalarray.size(), o_esplvalarray.size());
92+
93+
BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_podvalarray), std::end(i_podvalarray), std::begin(o_podvalarray), std::end(o_podvalarray));
94+
BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_iservalarray), std::end(i_iservalarray), std::begin(o_iservalarray), std::end(o_iservalarray));
95+
BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_isplvalarray), std::end(i_isplvalarray), std::begin(o_isplvalarray), std::end(o_isplvalarray));
96+
BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_eservalarray), std::end(i_eservalarray), std::begin(o_eservalarray), std::end(o_eservalarray));
97+
BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_esplvalarray), std::end(i_esplvalarray), std::begin(o_esplvalarray), std::end(o_esplvalarray));
98+
}
99+
}
100+
101+
BOOST_AUTO_TEST_CASE(binary_valarray)
102+
{
103+
test_valarray<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
104+
}
105+
106+
BOOST_AUTO_TEST_CASE(portable_binary_valarray)
107+
{
108+
test_valarray<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
109+
}
110+
111+
BOOST_AUTO_TEST_CASE(xml_valarray)
112+
{
113+
test_valarray<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
114+
}
115+
116+
BOOST_AUTO_TEST_CASE(json_valarray)
117+
{
118+
test_valarray<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
119+
}

vs2013/unittests/unittests.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<ClCompile Include="..\..\unittests\unordered_set.cpp" />
5353
<ClCompile Include="..\..\unittests\user_data_adapters.cpp" />
5454
<ClCompile Include="..\..\unittests\vector.cpp" />
55+
<ClCompile Include="..\..\unittests\valarray.cpp" />
5556
<ClCompile Include="..\..\unittests\versioning.cpp" />
5657
<ClCompile Include="main.cpp" />
5758
</ItemGroup>

vs2013/unittests/unittests.vcxproj.filters

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup>
44
<Filter Include="Source Files">
@@ -110,6 +110,9 @@
110110
</ClCompile>
111111
<ClCompile Include="..\..\unittests\vector.cpp">
112112
<Filter>Source Files</Filter>
113+
</ClCompile>
114+
<ClCompile Include="..\..\unittests\valarray.cpp">
115+
<Filter>Source Files</Filter>
113116
</ClCompile>
114117
<ClCompile Include="..\..\unittests\versioning.cpp">
115118
<Filter>Source Files</Filter>

0 commit comments

Comments
 (0)