MRPT  2.0.1
CSerializable_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/io/CMemoryStream.h>
14 
15 using namespace mrpt::serialization;
16 
17 namespace MyNS
18 {
19 class Foo : public CSerializable
20 {
22  public:
23  int16_t value;
24 };
25 } // namespace MyNS
26 
28 
29 uint8_t MyNS::Foo::serializeGetVersion() const { return 0; }
30 void MyNS::Foo::serializeTo(CArchive& out) const { out << value; }
31 void MyNS::Foo::serializeFrom(CArchive& in, uint8_t serial_version)
32 {
33  in >> value;
34 }
35 
36 TEST(Serialization, CustomClassSerialize)
37 {
39 
40  MyNS::Foo a;
41  a.value = 123;
42 
44  auto arch = mrpt::serialization::archiveFrom(buf);
45  arch << a;
46 
47  buf.Seek(0);
48  MyNS::Foo b;
49  arch >> b;
50 
51  EXPECT_EQ(a.value, b.value);
52 }
53 
54 TEST(Serialization, ArchiveSharedPtrs)
55 {
57  auto arch_ptr = mrpt::serialization::archivePtrFrom(buf);
58  auto arch_ptr2 = mrpt::serialization::archiveUniquePtrFrom(buf);
59 
60  int a = 42;
61  (*arch_ptr) << a;
62  buf.Seek(0);
63 
64  int b;
65  (*arch_ptr2) >> b;
66 
67  EXPECT_EQ(a, b);
68 }
IMPLEMENTS_SERIALIZABLE(Foo, CSerializable, MyNS)
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT&#39;s CStream, std::istream, std::ostream, std::stringstream.
Definition: CArchive.h:592
#define CLASS_ID(T)
Access to runtime class ID for a defined class name.
Definition: CObject.h:102
This CStream derived class allow using a memory buffer as a CStream.
CArchive::Ptr archivePtrFrom(STREAM &s)
Like archiveFrom(), returning a shared_ptr<>.
Definition: CArchive.h:599
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::vision::TStereoCalibResults out
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:30
CArchive::UniquePtr archiveUniquePtrFrom(STREAM &s)
Like archiveFrom(), returning a unique_ptr<>.
Definition: CArchive.h:606
void registerClass(const mrpt::rtti::TRuntimeClassId *pNewClass)
Register a class into the MRPT internal list of "CObject" descendents.
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
[example-define-class]
TEST(Observations, WriteReadToMem)



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020