CondFormats
Serialization
interface
Test.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <string>
4
#include <iostream>
5
#include <fstream>
6
#include <utility>
7
#include <stdexcept>
8
9
#include "
CondFormats/Serialization/interface/Archive.h
"
10
#include "
CondFormats/Serialization/interface/Equal.h
"
11
12
// The compiler knows our default-constructed objects' members
13
// may not be initialized when we serialize them.
14
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
15
16
// The main test: constructs an object using the default constructor,
17
// serializes it, deserializes it and finally checks whether they are equal.
18
// Mainly used to see if all the required templates compile.
19
//
20
// It is not possible to use (easily) the Boost text archive because:
21
//
22
// * Uninitialized booleans are serialized as a byte (unsigned char),
23
// while on deserialization Boost asserts on the value being 0 or 1.
24
//
25
// * Uninitialized floating point variables may be NaN (or Inf),
26
// which are serialized as "nan" and Boost fails to deserialize them.
27
//
28
// While the problems could be solved adding default constructors
29
// or modifying them (C++11's in-class member initializers cannot be used
30
// due to genreflex in ROOT 5 which would have been a bit easier),
31
// for the floating point case there are too many members and classes
32
// to modify. Instead, the Boost binary archive can be used, which does not
33
// assert. However, this means it is required to support comparing NaNs and
34
// Infs properly in cond::serialization::equal.
35
//
36
// The EOS' portable binary archive has the same problem with uninitialized
37
// booleans as the Boost text archive, but not the uninitialized floating
38
// point variables. Since the number of boolean members in CondFormats
39
// is small, we initialized them in the default constructor or disabled
40
// the test in other cases.
41
//
42
// Note that classes with STL containers of other classes may not be tested
43
// (at runtime, since they have size 0 by default), unless they are explicitly
44
// tested by themselves (which should be the case, since in the XML it was
45
// required to write the "dependencies").
46
template
<
typename
T>
47
void
testSerialization
() {
48
const
std::string
filename
(
std::string
(
typeid
(
T
).
name
()) +
".bin"
);
49
50
// C++ does not allow to construct const objects
51
// of non-POD types without user-provided default constructor
52
// (since it would be uninitialized), so we always create
53
// a non-const object.
54
T
originalObject{};
55
const
T
& originalObjectRef = originalObject;
56
{
57
std::ofstream ofs(
filename
,
std::ios::out
| std::ios::binary);
58
cond::serialization::OutputArchive
oa(ofs);
59
std::cout
<<
"Serializing "
<<
typeid
(
T
).
name
() <<
" ..."
<< std::endl;
60
oa << originalObjectRef;
61
}
62
63
T
deserializedObject;
64
{
65
std::ifstream ifs(
filename
,
std::ios::in
| std::ios::binary);
66
cond::serialization::InputArchive
ia(ifs);
67
std::cout
<<
"Deserializing "
<<
typeid
(
T
).
name
() <<
" ..."
<< std::endl;
68
ia >> deserializedObject;
69
}
70
71
// TODO: First m,ake the Boost IO compile and run properly,
72
// then focus again on the equal() functions.
73
//std::cout << "Checking " << typeid(T).name() << " ..." << std::endl;
74
//if (not cond::serialization::equal(originalObject, deserializedObject))
75
// throw std::logic_error("Object is not equal.");
76
}
gather_cfg.cout
cout
Definition:
gather_cfg.py:144
testSerialization
void testSerialization()
Definition:
Test.h:47
cond::serialization::OutputArchive
eos::portable_oarchive OutputArchive
Definition:
Archive.h:17
Equal.h
corrVsCorr.filename
filename
Definition:
corrVsCorr.py:123
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition:
AlCaHLTBitMon_QueryRunRegistry.py:256
Archive.h
recoMuon::in
Definition:
RecoMuonEnumerators.h:6
cond::serialization::InputArchive
eos::portable_iarchive InputArchive
Definition:
Archive.h:16
T
long double T
Definition:
Basic3DVectorLD.h:48
Skims_PA_cff.name
name
Definition:
Skims_PA_cff.py:17
MillePedeFileConverter_cfg.out
out
Definition:
MillePedeFileConverter_cfg.py:31
Generated for CMSSW Reference Manual by
1.8.16