//
// LDASTools frameCPP - A library implementing the LIGO/Virgo frame
// specification
//
// Copyright (C) 2018-2020 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__FrRawData_ClassicIO_HH
#define FrameCPP_UNIVERSAL__IMPL__FrRawData_ClassicIO_HH

#include "framecpp/Universal/impl/FrRawDataData.hh"

#if defined( __cplusplus ) && !defined( SWIG )

namespace FrameCPP
{
    namespace Universal
    {

        class FrRawData;

        namespace FrRawDataImpl
        {
            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 name structure name of this object.
                ///
                /// @return
                ///     The name of the structure as specified by the frame
                ///     specification.
                //-----------------------------------------------------------------
                virtual const char*
                ObjectStructName( ) const
                {
                    return StructName( );
                }

                //-----------------------------------------------------------------
                /// @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;

            protected:
                //-----------------------------------------------------------------
                /// @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.
                //-----------------------------------------------------------------
                virtual cmn_streamsize_type
                pBytes( const Common::StreamBase& Stream ) const
                {
                    auto frame_stream_version = StreamVersion( Stream );

                    cmn_streamsize_type retval = //
                        io_name_type::Bytes( name ) //
                        + Stream.PtrStructBytes( ) // firstSer
                        + Stream.PtrStructBytes( ) // firstAdc
                        + ( ( frame_stream_version >= 4 ) // firstTable
                                ? Stream.PtrStructBytes( )
                                : 0 ) +
                        Stream.PtrStructBytes( ) // logMsg
                        + Stream.PtrStructBytes( ) // more
                        ;
                    return retval;
                }

                //-----------------------------------------------------------------
                /// @brief Virtual constructor
                ///
                /// @param[in] Stream
                ///     The input stream from where the object is being read.
                //-----------------------------------------------------------------
                virtual self_type*
                pCreate( istream_type& Stream ) const
                {
                    auto frame_stream_version = StreamVersion( Stream );

                    T* obj( new T( ) );

                    io_name_type io_name;

                    Stream >> io_name;

                    Stream >> obj->firstSer //
                        >> obj->firstAdc //
                        ;
                    if ( frame_stream_version >= 4 )
                    {
                        Stream >> obj->firstTable;
                    }
                    Stream >> obj->logMsg //
                        >> obj->more //
                        ;

                    obj->name = io_name;

                    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( Common::OStream& Stream ) const
                {
                    auto frame_stream_version = StreamVersion( Stream );

                    Stream << io_name_type( name );
                    if ( frame_stream_version >= 9 )
                    {
                        Stream << firstSer << firstAdc << firstTable << logMsg
                               << more;
                    }
                    else
                    {
                        Stream << firstSer << firstAdc;
                        if ( frame_stream_version >= 4 )
                        {
                            Stream << firstTable;
                        }
                        Stream << logMsg << more;
                    }
                }

            private:
                typedef STRING io_name_type;
            }; // class ClassicIO
        } // namespace FrRawDataImpl
    } // namespace Universal
} // namespace FrameCPP

#endif /* defined( __cplusplus ) && !defined( SWIG ) */

#endif /* FrameCPP_UNIVERSAL__IMPL__FrRawData_ClassicIO_HH */
