//
// 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__FrHistoryClassicIO_HH
#define FrameCPP_UNIVERSAL__IMPL__FrHistoryClassicIO_HH

#include "framecpp/Common/Description.hh"
#include "framecpp/Common/FrameStream.hh"

#include "framecpp/Universal/impl/FrHistoryData.hh"
#include "framecpp/Universal/impl/FrameSpec.hh"
#include "framecpp/Universal/impl/STRING.hh"

namespace FrameCPP
{
    namespace Universal
    {
        class FrHistory;

        namespace FrHistoryImpl
        {
            template < typename T >
            class ClassicIO
                : public virtual Data,
                  public ObjectWithChecksum< Common::FrameSpec::Object >
            {
            public:
                // Bring base class GetDescription methods into scope
                using FrameCPP::Common::FrameSpec::Object::GetDescription;

                //-------------------------------------------------------
                //
                //-------------------------------------------------------
                ClassicIO( )
                    : ObjectWithChecksum< Common::FrameSpec::Object >(
                          FrameSpecId( ), nullptr )
                {
                }

                ClassicIO( const ClassicIO& ) = default;
                ClassicIO( ClassicIO&& ) = default;
                ClassicIO& operator=( const ClassicIO& ) = delete;
                ClassicIO& operator=( ClassicIO&& ) = delete;

                //-----------------------------------------------------------------
                /// @brief The data structure description
                ///
                /// @return
                ///     The description of the data structure used to represent
                ///     this object when written to a GWF stream
                //-----------------------------------------------------------------
                virtual const Common::Description*
                GetDescription( unsigned int version ) const;

                //-----------------------------------------------------------------
                /// \brief equality operator for abstract data type
                ///
                /// Required override of the pure-virtual operator== on the
                /// base Common::FrameSpec::Object. Routes through
                /// Common::compare(), which does an identity check, a
                /// GetClass() check, and a dynamic_cast before delegating
                /// to the typed operator== on FrHistory (declared as a
                /// hidden friend on the leaf class).
                ///
                /// Note: typed operator==(const FrHistory&) intentionally
                /// lives on FrHistory itself as a hidden friend, not as a
                /// member here. Pairing a typed member with the virtual
                /// operator==(Object&) below triggers C++20's reversed-
                /// candidate ambiguity warning at every T==T use site.
                ///
                /// \param[in] RHS
                ///     The object to be compared.
                ///
                /// \return
                ///     The value true is returned if this object is equivelent
                ///     to the RHS instance; false otherwise.
                //-----------------------------------------------------------------
                virtual bool
                operator==( const Common::FrameSpec::Object& RHS ) const;

            protected:
                struct v3
                {
                    //-----------------------------------------------------------------
                    /// \brief Type for name of history record
                    //-----------------------------------------------------------------
                    using io_name_type = STRING;
                    //-----------------------------------------------------------------
                    /// \brief Type for comment field.
                    ///
                    /// Program name and relevant comments needed to
                    /// define post-processing.
                    //-----------------------------------------------------------------
                    using io_comment_type = STRING;
                };

                using v4 = v3;
                using v5 = v4;
                using v6 = v5;
                using v7 = v6;
                using v8 = v7;
                using v9 = v8;

                using current_frame_spec = v9;

                using io_name_type = typename current_frame_spec::io_name_type;
                using io_comment_type =
                    typename current_frame_spec::io_name_type;
                //-----------------------------------------------------------------
                /// \brief Number of bytes needed to write this structure
                ///
                /// \param[in] Stream
                ///     The stream from which to the object is being read or
                ///     written.
                ///
                /// \return
                ///     The number of bytes need to read or write this object.
                //-----------------------------------------------------------------
                cmn_streamsize_type
                pBytes( const Common::StreamBase& Stream ) const
                {
                    return io_name_type::Bytes( name ) + sizeof( time ) +
                        io_comment_type::Bytes( comment ) +
                        Stream.PtrStructBytes( ) // next
                        ;
                }

                //-----------------------------------------------------------------
                /// \brief Virtual constructor
                ///
                /// \param[in] Stream
                ///     The input stream from where the object is being read.
                //-----------------------------------------------------------------
                virtual self_type*
                pCreate( istream_type& Stream ) const
                {
                    io_name_type    io_name;
                    io_comment_type io_comment;

                    T* obj( new T( ) );

                    Stream >> io_name >> obj->time >> io_comment;
                    Stream.Next( obj );

                    obj->name = io_name;
                    obj->comment = io_comment;

                    return ( obj );
                }

                //-----------------------------------------------------------------
                /// \brief Write the structure to the stream
                ///
                /// \param[in] Stream
                ///     The output stream where the object is to be written.
                //-----------------------------------------------------------------
                virtual void
                pWrite( ostream_type& Stream ) const
                {
                    Stream << io_name_type( name ) << time
                           << io_comment_type( comment );
                    WriteNext( Stream );
                }

            private:
                //-----------------------------------------------------------------
                //-----------------------------------------------------------------
                typedef ObjectWithChecksum< Common::FrameSpec::Object >
                    object_type;

                using Common::FrameSpec::Object::Create;
            };
        } // namespace FrHistoryImpl
    } // namespace Universal
} // namespace FrameCPP

#endif /* FrameCPP_UNIVERSAL__IMPL__FrHistoryClassicIO_HH */
