//
// LDASTools frameCPP - A library implementing the LIGO/Virgo frame
// specification
//
// Copyright (C) 2019 California Institute of Technology
//
// LDASTools frameCPP is free software; you may redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 (GPLv2) of the
// License or at your discretion, any later version.
//
// LDASTools frameCPP is distributed in the hope that it will be useful, but
// without any warranty or even the implied warranty of merchantability
// or fitness for a particular purpose. See the GNU General Public
// License (GPLv2) for more details.
//
// Neither the names of the California Institute of Technology (Caltech),
// The Massachusetts Institute of Technology (M.I.T), The Laser
// Interferometer Gravitational-Wave Observatory (LIGO), nor the names
// of its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// You should have received a copy of the licensing terms for this
// software included in the file LICENSE located in the top-level
// directory of this package. If you did not, you can view a copy at
// http://dcc.ligo.org/M1500244/LICENSE
//

#ifndef FrameCPP_UNIVERSAL__IMPL__FrSHSerialIO_HH
#define FrameCPP_UNIVERSAL__IMPL__FrSHSerialIO_HH

#if !defined( SWIGIMPORTED )
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/version.hpp>
#endif /* !defined( SWIGIMPORTED ) */

#if !defined( SWIGIMPORTED )
#include "framecpp/Common/FrameSpec.hh"
#include "framecpp/Universal/impl/HelperSerialIO.hh"
#endif /* !defined( SWIGIMPORTED ) */
#include "framecpp/Universal/impl/FrSHData.hh"

#if !defined( SWIGIMPORTED )
namespace FrameCPP
{
    namespace Universal
    {
        class FrSH;
        namespace FrSHImpl
        {
            /// \brief Serialization I/O implementation for FrSH data
            /// structures
            ///
            /// This class provides versioned serialization support for FrSH
            /// (Structure Header) objects using Boost.Serialization. It handles
            /// the stable FrSH data structure across different frame specification
            /// versions (v3 through v9).
            ///
            /// \tparam FR_SH_T The FrSH type being serialized (typically FrSH)
            template < typename FR_SH_T >
            class SerialIO : public virtual Data
            {
            public:
                /// \brief Default constructor
                ///
                /// Constructs an empty SerialIO object ready for serialization
                /// operations. The object inherits data members from the Data
                /// base class.
                SerialIO( )
                {
                }
                
                SerialIO( const SerialIO& ) = default;
                SerialIO( SerialIO&& ) = default;
                SerialIO& operator=( const SerialIO& ) = delete;
                SerialIO& operator=( SerialIO&& ) = delete;

            private:
                friend class boost::serialization::access;

                /// \brief Serialize/deserialize FrSH data
                ///
                /// This unified method handles both serialization and deserialization
                /// of FrSH data. Since the FrSH structure has been stable from v3
                /// onwards and requires identical field processing for both operations,
                /// we can use a single method instead of separate save/load methods.
                ///
                /// \tparam Archive The Boost.Serialization archive type
                ///
                /// \param[in,out] ar The archive to read from or write to
                /// \param[in] version The serialization version (3-9)
                ///
                /// \throws boost::archive::archive_exception on serialization
                /// errors
                ///
                /// Version-specific handling:
                /// - v3+: All fields supported (name, class, comment)
                /// - Pre-v3: Initialize with default values for loading
                ///
                /// \note The "class" field uses explicit name mapping to avoid
                /// C++ keyword conflicts
                /// \note For loading pre-v3 data, fields are initialized to defaults
                template < class Archive >
                void
                serialize( Archive& ar, const unsigned int version )
                {
                    using boost::serialization::make_nvp;

                    if ( version >= 3 )
                    {
                        // Structure name
                        ar& make_nvp( "name", name );

                        // Class number (using explicit name to avoid C++
                        // keyword conflict)
                        ar& make_nvp( "class", klass );

                        // Structure comment
                        ar& make_nvp( "comment", comment );
                    }
                    else
                    {
                        // Pre-v3: Only relevant for loading - initialize with default values
                        // For saving, this path would never be reached with version < 3
                        if ( typename Archive::is_loading() )
                        {
                            name.clear( );
                            comment.clear( );
                            klass = 0;
                        }
                    }
                }
            };

        } // namespace FrSHImpl
    } // namespace Universal
} // namespace FrameCPP

/// \brief Specify the class version for Boost.Serialization
///
/// This macro tells Boost.Serialization that FrSHImpl::Data is at version 9,
/// which corresponds to the v9 frame specification. The serialization framework
/// uses this version number to determine how to serialize/deserialize the data.
///
/// \note This version number is passed to the save() and load() methods
BOOST_CLASS_VERSION( FrameCPP::Universal::FrSHImpl::Data, 9 )

#endif /* !defined( SWIGIMPORTED ) */

#endif /* FrameCPP_UNIVERSAL__IMPL__FrSHSerialIO_HH */
