// **********************************************************************************************************
//    coeMessageSerialized.cs
//    6/1/2022
//    NGI - Next Generation Interceptor
//    
//    Contract No. HQ0856-21-C-0003/1022000209
//    
//    THIS DOCUMENT DOES NOT CONTAIN TECHNOLOGY OR TECHNICAL DATA CONTROLLED UNDER EITHER THE U.S. 
//    INTERNATIONAL TRAFFIC IN ARMS REGULATIONS OR THE U.S. EXPORT ADMINISTRATION REGULATIONS.
//    
//    RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION PROPRIETARY TO RAYTHEON 
//    COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. 
//    DISCLOSURE TO UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO RAYTHEON 
//    COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS CONTENTS SHALL BE FURNISHED OR DISCLOSED 
//    TO OR COPIED OR USED BY PERSONS OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF 
//    RAYTHEON COMPANY.
//    
//    UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
//    
//    DESTRUCTION NOTICE: FOR CLASSIFIED DOCUMENTS FOLLOW THE PROCEDURES IN DOD 5220.22-M, 
//    NATIONAL INDUSTRIAL SECURITY PROGRAM OPERATING MANUAL, FEBRUARY 2006, 
//    INCORPORATING CHANGE 1, MARCH 28, 2013, CHAPTER 5, SECTION 7, OR DODM 5200.01-VOLUME 3, 
//    DOD INFORMATION SECURITY PROGRAM: PROTECTION OF CLASSIFIED INFORMATION, ENCLOSURE 3, 
//    SECTION 17. FOR CONTROLLED UNCLASSIFIED INFORMATION FOLLOW THE PROCEDURES IN DODM 5200.01-VOLUME 4, 
//    INFORMATION SECURITY PROGRAM: CONTROLLED UNCLASSIFIED INFORMATION.
//    
//    CONTROLLED BY: MISSILE DEFENSE AGENCY
//    CONTROLLED BY: GROUND-BASED MIDCOURSE DEFENSE PROGRAM OFFICE
//    CUI CATEGORY: CTI 
//    DISTRIBUTION/DISSEMINATION CONTROL: F 
//    POC: Alex Kravchenko (1118268)
// **********************************************************************************************************

//\\<Unclassified>
//----------------------------------------------------------------------------//
//                                 UNCLASSIFIED                               //
//----------------------------------------------------------------------------//
//\\<\Unclassified>

//\\<UnlimitedRights>
//----------------------------------------------------------------------------//
// Copyright %(copyright)s Raytheon Company.                                  //
// This software was developed pursuant to Contract Number %(contractNumber)s //
// with the U.S. government. The U.S. government's rights in and to this      //
// copyrighted software are as specified in DFARS 252.227-7014 which was made //
// part of the above contract.                                                //
//----------------------------------------------------------------------------//
//\\<\UnlimitedRights>

//\\<EximUndetermined>
//----------------------------------------------------------------------------//
// WARNING - This document contains technical data and / or technology whose  //
// export or disclosure to Non-U.S. Persons, wherever located, is restricted  //
// by the International Traffic in Arms Regulations (ITAR) (22 C.F.R.         //
// Section 120-130) or the Export Administration Regulations (EAR) (15 C.F.R. //
// Section 730-774). This document CANNOT be exported (e.g., provided to a    //
// supplier outside of the United States) or disclosed to a Non-U.S. Person,  //
// wherever located, until a final jurisdiction and classification            //
// determination has been completed and approved by Raytheon, and any         //
// required U.S. Government approvals have been obtained. Violations are      //
// subject to severe criminal penalties.                                      //
//----------------------------------------------------------------------------//
//\\<\EximUndetermined>

using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Raytheon.Instruments.coeCSharp
{
    //
    //
    //
    // Message: serializable option; This class provides automatic serialization
    //          of all defined public members. All members not defined as public
    //          will be ignored during serialization.
    //
    //          Types currently supported include all scalar types and arrays of scalar types.
    //          Structures are also supported, which can contain single dimension arrays of scalar types.
    //          Structures can also be nested. This message class does support arrays of structures.
    //          
    //          The presence of the constructor is optional, and can be used to force a particular size
    //          of a buffer. Omitting the size on the call to the base class, or providing a size value
    //          of 0 will cause the constructor to automatically size the data, as will omitting the
    //          constuctor declaration entirely.
    //          
    // Example:
    // 
    //  public class exampleMessage : coeMessageSerialized
    //  {
    //     public uint field1;
    //     public ushort field2;
    //     public ushort field3;
    //     public uint field4;
    //     public uint field5;
    //     public uint field6;
    //
    //     public exampleMessage() : base(SIZE) { }
    //  }
    //
    //          Special care needs to be taken when using arrays in a message definition, or in any class
    //          that is used as a member of a message. Since C# arrays are dynamic, the array member must
    //          be initialized with a new directive to declare the specific size of the array. This must
    //          also be done for any sequences or strings used as message or class members. In addition,
    //          any objects in that array must be specifically created and added to the array in the
    //          constructor, if auto-sizing of the using message is being done. This initialization only
    //          needs to be done for classes, not for data types. However, before the message can be
    //          serialized for transmission, the array must be completely filled with the total number
    //          of objects for which it was created.
    //
    // Example:
    //
    //  public class nestedStructure
    //  {
    //     public uint field1;
    //  }
    //
    //  public class exampleStructure
    //  {
    //     public uint field1;
    //     public uint[] field2 = new uint[4];
    //     public nestedStructure[] field3 = new nestedStructure[3];
    //
    //     public exampleStructure()
    //     {
    //        field3[0] = new nestedStructure();
    //        field3[1] = new nestedStructure();
    //        field3[2] = new nestedStructure();
    //     }
    //  }
    //
    //  public class exampleMessage : coeMessageSerialized
    //  {
    //     public uint field1;
    //     public coeSequence<uint> mySequence = new coeSequence<uint>(8);
    //     public coeString myString = new coeString(12);
    //     public exampleStructure mystructure = new exampleStructure();
    //  }
    //
    //
    //
    public abstract class coeMessageSerialized : coeMessage, IDisposable
    {
        protected coeMessageSerialized() : this(0) { }
        protected coeMessageSerialized(uint size) : this(size, 0) { }
        protected coeMessageSerialized(uint size, uint label) : this(size, label, 0) { }
        protected coeMessageSerialized(uint size, uint label, int priority) : base((int)size, label, priority) { }

        ~coeMessageSerialized()
        {
            Dispose(false);
        }

        new public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        new protected void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        override public void Serialize()
        {
            FieldInfo[] fields = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
            int dataIndex = 0;
            foreach (FieldInfo field in fields)
            {
                Type myType = field.GetType();
                object myObject = field.GetValue(this);
                serializationSupport.serializeField(field, m_Buffer, ref dataIndex, this);
            }
            Size = (uint)dataIndex;
            Marshal.Copy(m_Buffer, 0, m_UnmanagedBuffer, m_Buffer.Length);
        }

        override public void Deserialize()
        {
            byte[] data = copyFromMessageBuffer();
            int dataIndex = 0;
            FieldInfo[] fields = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                serializationSupport.deserializeField(field, data, ref dataIndex, this);
            }
        }

    }
}

//\\<Unclassified>
//----------------------------------------------------------------------------//
//                                 UNCLASSIFIED                               //
//----------------------------------------------------------------------------//
//\\<\Unclassified>
