"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/Serializable.h" (10 Oct 2018, 2588 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Serializable.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #ifndef TC_HEADER_Platform_Serializable
   14 #define TC_HEADER_Platform_Serializable
   15 
   16 #include <stdexcept>
   17 #include "PlatformBase.h"
   18 #include "ForEach.h"
   19 #include "Serializer.h"
   20 #include "SerializerFactory.h"
   21 
   22 namespace VeraCrypt
   23 {
   24     class Serializable
   25     {
   26     public:
   27         virtual ~Serializable () { }
   28 
   29         virtual void Deserialize (shared_ptr <Stream> stream) = 0;
   30         static string DeserializeHeader (shared_ptr <Stream> stream);
   31         static Serializable *DeserializeNew (shared_ptr <Stream> stream);
   32 
   33         template <class T>
   34         static shared_ptr <T> DeserializeNew (shared_ptr <Stream> stream)
   35         {
   36             shared_ptr <T> p (dynamic_cast <T *> (DeserializeNew (stream)));
   37             if (!p)
   38                 throw std::runtime_error (SRC_POS);
   39             return p;
   40         }
   41 
   42         template <class T>
   43         static void DeserializeList (shared_ptr <Stream> stream, list < shared_ptr <T> > &dataList)
   44         {
   45             if (DeserializeHeader (stream) != string ("list<") + SerializerFactory::GetName (typeid (T)) + ">")
   46                 throw std::runtime_error (SRC_POS);
   47 
   48             Serializer sr (stream);
   49             uint64 listSize;
   50             sr.Deserialize ("ListSize", listSize);
   51 
   52             for (size_t i = 0; i < listSize; i++)
   53             {
   54                 shared_ptr <T> p (dynamic_cast <T *> (DeserializeNew (stream)));
   55                 if (!p)
   56                     throw std::runtime_error (SRC_POS);
   57                 dataList.push_back (p);
   58             }
   59         }
   60 
   61         virtual void Serialize (shared_ptr <Stream> stream) const;
   62 
   63         template <class T>
   64         static void SerializeList (shared_ptr <Stream> stream, const list < shared_ptr <T> > &dataList)
   65         {
   66             Serializer sr (stream);
   67             SerializeHeader (sr, string ("list<") + SerializerFactory::GetName (typeid (T)) + ">");
   68 
   69             sr.Serialize ("ListSize", (uint64) dataList.size());
   70             foreach_ref (const T &item, dataList)
   71                 item.Serialize (stream);
   72         }
   73 
   74         static void SerializeHeader (Serializer &serializer, const string &name);
   75 
   76     protected:
   77         Serializable () { }
   78     };
   79 }
   80 
   81 #define TC_SERIALIZABLE(TYPE) \
   82     static Serializable *GetNewSerializable () { return new TYPE(); } \
   83     virtual void Deserialize (shared_ptr <Stream> stream); \
   84     virtual void Serialize (shared_ptr <Stream> stream) const
   85 
   86 #endif // TC_HEADER_Platform_Serializable