Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:33

0001 #pragma once
0002 
0003 #include <string>
0004 #include <iostream>
0005 #include <fstream>
0006 #include <utility>
0007 #include <stdexcept>
0008 
0009 #include "CondFormats/Serialization/interface/Archive.h"
0010 #include "CondFormats/Serialization/interface/Equal.h"
0011 
0012 // The compiler knows our default-constructed objects' members
0013 // may not be initialized when we serialize them.
0014 
0015 #if !defined(__clang__)
0016 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
0017 #endif
0018 
0019 // The main test: constructs an object using the default constructor,
0020 // serializes it, deserializes it and finally checks whether they are equal.
0021 // Mainly used to see if all the required templates compile.
0022 //
0023 // It is not possible to use (easily) the Boost text archive because:
0024 //
0025 //   * Uninitialized booleans are serialized as a byte (unsigned char),
0026 //     while on deserialization Boost asserts on the value being 0 or 1.
0027 //
0028 //   * Uninitialized floating point variables may be NaN (or Inf),
0029 //     which are serialized as "nan" and Boost fails to deserialize them.
0030 //
0031 // While the problems could be solved adding default constructors
0032 // or modifying them (C++11's in-class member initializers cannot be used
0033 // due to genreflex in ROOT 5 which would have been a bit easier),
0034 // for the floating point case there are too many members and classes
0035 // to modify. Instead, the Boost binary archive can be used, which does not
0036 // assert. However, this means it is required to support comparing NaNs and
0037 // Infs properly in cond::serialization::equal.
0038 //
0039 // The EOS' portable binary archive has the same problem with uninitialized
0040 // booleans as the Boost text archive, but not the uninitialized floating
0041 // point variables. Since the number of boolean members in CondFormats
0042 // is small, we initialized them in the default constructor or disabled
0043 // the test in other cases.
0044 //
0045 // Note that classes with STL containers of other classes may not be tested
0046 // (at runtime, since they have size 0 by default), unless they are explicitly
0047 // tested by themselves (which should be the case, since in the XML it was
0048 // required to write the "dependencies").
0049 template <typename T>
0050 void testSerialization() {
0051   const std::string filename(std::string(typeid(T).name()) + ".bin");
0052 
0053   // C++ does not allow to construct const objects
0054   // of non-POD types without user-provided default constructor
0055   // (since it would be uninitialized), so we always create
0056   // a non-const object.
0057   T originalObject{};
0058   const T& originalObjectRef = originalObject;
0059   {
0060     std::ofstream ofs(filename, std::ios::out | std::ios::binary);
0061     cond::serialization::OutputArchive oa(ofs);
0062     std::cout << "Serializing " << typeid(T).name() << " ..." << std::endl;
0063     oa << originalObjectRef;
0064   }
0065 
0066   T deserializedObject;
0067   {
0068     std::ifstream ifs(filename, std::ios::in | std::ios::binary);
0069     cond::serialization::InputArchive ia(ifs);
0070     std::cout << "Deserializing " << typeid(T).name() << " ..." << std::endl;
0071     ia >> deserializedObject;
0072   }
0073 
0074   // TODO: First m,ake the Boost IO compile and run properly,
0075   //       then focus again on the equal() functions.
0076   //std::cout << "Checking " << typeid(T).name() << " ..." << std::endl;
0077   //if (not cond::serialization::equal(originalObject, deserializedObject))
0078   //    throw std::logic_error("Object is not equal.");
0079 }