Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / SMESHUtils / SMESH_BoostTxtArchive.hxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File      : SMESH_BoostTxtArchive.hxx
23 // Created   : Thu Aug  5 19:10:24 2021
24 // Author    : Edward AGAPOV (eap)
25
26
27 #ifndef __SMESH_BoostTxtArchive_HXX__
28 #define __SMESH_BoostTxtArchive_HXX__
29
30 #include "SMESH_Utils.hxx"
31
32 #include <boost/archive/text_iarchive.hpp>
33
34 namespace SMESHUtils
35 {
36   /*!
37    * \brief Load an object from a string created by boost::archive::text_oarchive.
38    *
39    * Try to workaround the issue that loading fails if the archive string
40    * is created by a newer version of boost::archive library.
41    *
42    * Usage: ObjType obj;  SMESHUtils::BoostTxtArchive( arcString ) >> obj;
43    */
44   class SMESHUtils_EXPORT BoostTxtArchive
45   {
46   public:
47
48     BoostTxtArchive( const std::string& s );
49     BoostTxtArchive( std::istream& stream );
50     ~BoostTxtArchive();
51
52     template< class T >
53       BoostTxtArchive & operator>>( T & t )
54     {
55       if ( myArchiveReader )
56         try
57         {
58           (*myArchiveReader) >> t;
59         }
60         catch (...)
61         {
62           if ( fixString() )
63             try
64             {
65               (*myArchiveReader) >> t;
66             }
67             catch(...)
68             {
69             }
70         }
71       return *this;
72     }
73
74   private:
75
76     void makeReader();
77     bool fixString();
78
79     boost::archive::text_iarchive* myArchiveReader;
80     std::string                    myString;        // archive to read
81     bool                           myStringFixed;   // is archive version changed
82     std::istream*                  myStream;        // stream holding myString
83     bool                           myOwnStream;     // is myStream created by me
84
85     // persistence used to create a current version archive
86     friend class boost::serialization::access;
87     template<class Archive>
88       void serialize(Archive & /*ar*/, const unsigned int /*version*/)
89     {
90     }
91   };
92 }
93
94
95 #endif