Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Double_wrapper vc++
#include <boost/serialization/split_member.hpp> #include <boost/serialization/wrapper.hpp> #include <boost/serialization/tracking.hpp> #include <limits> #include <cmath> #include <boost/archive/tmpdir.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> #include <fstream> #include <iostream> class Double_wrapper { private: enum double_type {DT_NONE, DT_NAN, DT_INF, DT_NINF}; double& value; public: Double_wrapper(double& val):value(val){} Double_wrapper(Double_wrapper const& rhs):value(rhs.value) {} private: friend class boost::serialization::access; template<class Archive> void save(Archive & ar, const unsigned int) const { double_type flag = DT_NONE; double val = value; if (!std::isfinite(val)) { if (std::isnan(val)) { flag = DT_NAN; } else if (val > 0) { flag = DT_INF; } else { flag = DT_NINF; } val = 0; } ar & val; ar & flag; } template<class Archive> void load(Archive & ar, const unsigned int) const { double_type flag; ar & value; ar & flag; switch (flag) { case DT_NONE: break; case DT_NAN: value = std::numeric_limits<double>::quiet_NaN(); break; case DT_INF: value = std::numeric_limits<double>::infinity(); break; case DT_NINF: value = -std::numeric_limits<double>::infinity(); } } BOOST_SERIALIZATION_SPLIT_MEMBER() }; BOOST_CLASS_IS_WRAPPER(Double_wrapper) BOOST_CLASS_TRACKING(Double_wrapper, boost::serialization::track_never) /////////////////////////////////////////////////////////////////////////////////////// class MyClass { public: double value; template <class Archive> void serialize(Archive &ar, const unsigned int){ Double_wrapper dw(value); ar & dw; } }; /////////////////////////////////////////////////////////////////////////////////////// int main() { MyClass tmp; tmp.value = std::numeric_limits<double>::quiet_NaN(); std::cout << "value=" << tmp.value << std::endl; std::string filename(boost::archive::tmpdir()); filename += "/tmp.txt"; //Output std::ofstream ofs(filename.c_str(), std::ios_base::out); boost::archive::text_oarchive oar(ofs); oar << tmp; ofs.close(); //Input MyClass newtmp; std::ifstream ifs(filename.c_str(), std::ios_base::in); boost::archive::text_iarchive iar(ifs); iar >> newtmp; std::cout << "value=" << newtmp.value << std::endl; }
run
|
edit
|
history
|
help
0
C++ standard library formatted input hexadecimal float without prefix or exponent test case
Zero
vector destruction - visual studio
delete from list
Say if number is prime and give its factors
MSVC_example_GetAllocLength
infix to postfix v 2.0
Error with move capture of a const ref in a lambda function
iota_30-Seconds-of-C++
RecursiveDivide