/*
  WARNING: THIS FILE IS AUTO-GENERATED. DO NOT MODIFY.

  This file was generated from test.idl using "rtiddsgen".
  The rtiddsgen tool is part of the RTI Data Distribution Service distribution.
  For more information, type 'rtiddsgen -help' at a command shell
  or consult the RTI Data Distribution Service manual.
*/

#include "testPlugin.h"


#pragma unmanaged
#include "ndds/ndds_cpp.h"
#include "osapi/osapi_utility.h"
#pragma managed

using namespace System::Runtime::InteropServices;
    
/* ------------------------------------------------------------------------
 *  Type TestData_t
 * ------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------
    Support functions:
 * ------------------------------------------------------------------------ */

void 
TestData_tPlugin::print_data(
        TestData_t^ sample,
        String^ desc,
        UInt32 indent_level) {

    for (UInt32 i = 0; i < indent_level; ++i) { Console::Write("   "); }

    if (desc != nullptr) {
        Console::WriteLine("{0}:", desc);
    } else {
        Console::WriteLine();
    }

    if (sample == nullptr) {
        Console::WriteLine("null");
        return;
    }


    DataPrintUtility::print_objects(
        sample->key, "key", indent_level);
            
    DataPrintUtility::print_object(
        sample->entity_id, "entity_id", indent_level);
            
    DataPrintUtility::print_object(
        sample->seq_num, "seq_num", indent_level);
            
    DataPrintUtility::print_object(
        sample->timestamp_sec, "timestamp_sec", indent_level);
            
    DataPrintUtility::print_object(
        sample->timestamp_usec, "timestamp_usec", indent_level);
            
    DataPrintUtility::print_object(
        sample->latency_ping, "latency_ping", indent_level);
            
    DataPrintUtility::print_objects(
        sample->bin_data, "bin_data", indent_level);
            
}


/* ------------------------------------------------------------------------
    (De)Serialize functions:
 * ------------------------------------------------------------------------ */

Boolean 
TestData_tPlugin::serialize(
    TypePluginDefaultEndpointData^ endpoint_data,
    TestData_t^ sample,
    CdrStream% stream,    
    Boolean serialize_encapsulation,
    UInt16 encapsulation_id,
    Boolean serialize_sample, 
    Object^ endpoint_plugin_qos)
{
    CdrStreamPosition position;

    if (serialize_encapsulation) {
        /* Encapsulate sample */
        if (!stream.serialize_and_set_cdr_encapsulation(encapsulation_id)) {
            return false;
        }


        position = stream.reset_alignment();

    }

    if (serialize_sample) {

    if (!stream.serialize_primitive_array(
            sample->key, ((KEY_SIZE::VALUE)), RTI_CDR_OCTET_TYPE)) {
        return false;
    }
            
    if (!stream.serialize_long(sample->entity_id)) {
        return false;
    }
            
    if (!stream.serialize_unsigned_long(sample->seq_num)) {
        return false;
    }
            
    if (!stream.serialize_long(sample->timestamp_sec)) {
        return false;
    }
            
    if (!stream.serialize_unsigned_long(sample->timestamp_usec)) {
        return false;
    }
            
    if (!stream.serialize_long(sample->latency_ping)) {
        return false;
    }
            
    if (!stream.serialize_primitive_sequence(sample->bin_data)) {
        return false;
    }
            
    }


    if(serialize_encapsulation) {
        stream.restore_alignment(position);
    }


    return true;
}


Boolean 
TestData_tPlugin::deserialize_sample(
    TypePluginDefaultEndpointData^ endpoint_data,
    TestData_t^ sample,
    CdrStream% stream,   
    Boolean deserialize_encapsulation,
    Boolean deserialize_data, 
    Object^ endpoint_plugin_qos)
{
    CdrStreamPosition position;

    if(deserialize_encapsulation) {
        /* Deserialize encapsulation */
        if (!stream.deserialize_and_set_cdr_encapsulation()) {
            return false;
        }


        position = stream.reset_alignment();

    }

    if (deserialize_data) {

    if (!stream.deserialize_primitive_array(
            sample->key, ((KEY_SIZE::VALUE)), RTI_CDR_OCTET_TYPE)) {
        return false;
    }
            
    sample->entity_id = stream.deserialize_long();
            
    sample->seq_num = stream.deserialize_unsigned_long();
            
    sample->timestamp_sec = stream.deserialize_long();
            
    sample->timestamp_usec = stream.deserialize_unsigned_long();
            
    sample->latency_ping = stream.deserialize_long();
            
    if (!stream.deserialize_primitive_sequence(sample->bin_data)) {
        return false;
    }
            
    }


    if(deserialize_encapsulation) {
        stream.restore_alignment(position);
    }


    return true;
}


Boolean
TestData_tPlugin::skip(
    TypePluginDefaultEndpointData^ endpoint_data,
    CdrStream% stream,   
    Boolean skip_encapsulation,
    Boolean skip_sample, 
    Object^ endpoint_plugin_qos)
{
    CdrStreamPosition position;

    UInt32 sequence_length = 0;


    if (skip_encapsulation) {
        if (!stream.skip_encapsulation()) {
            return false;
        }

        position = stream.reset_alignment();

    }

    if (skip_sample) {

    if (!stream.skip_primitive_array(
            ((KEY_SIZE::VALUE)), RTI_CDR_OCTET_TYPE)) {
        return false;
    }
            
    if (!stream.skip_long()) {
        return false;
    }
            
    if (!stream.skip_unsigned_long()) {
        return false;
    }
            
    if (!stream.skip_long()) {
        return false;
    }
            
    if (!stream.skip_unsigned_long()) {
        return false;
    }
            
    if (!stream.skip_long()) {
        return false;
    }
            
    if (!stream.skip_primitive_sequence(RTI_CDR_OCTET_TYPE)) {
        return false;
    }
            
    }


    if(skip_encapsulation) {
        stream.restore_alignment(position);
    }


    return true;
}

/*
  size is the offset from the point where we have do a logical reset
  Return difference in size, not the final offset.
*/
UInt32 
TestData_tPlugin::get_serialized_sample_max_size(
    TypePluginDefaultEndpointData^ endpoint_data,
    Boolean include_encapsulation,
    UInt16 encapsulation_id,
    UInt32 current_alignment)
{

    UInt32 initial_alignment = current_alignment;

    UInt32 encapsulation_size = current_alignment;


    if (include_encapsulation) {
        if (!CdrStream::valid_encapsulation_id(encapsulation_id)) {
            return 1;
        }


        encapsulation_size = CdrSizes::ENCAPSULATION->serialized_size(
            current_alignment);
        encapsulation_size -= current_alignment;
        current_alignment = 0;
        initial_alignment = 0;

    }


    current_alignment +=  CdrSizes::OCTET->serialized_array_size(
        current_alignment, ((KEY_SIZE::VALUE)));
            
    current_alignment +=  CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::UNSIGNED_LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::UNSIGNED_LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::OCTET->serialized_sequence_size(
        current_alignment, ((MAX_BINDATA_SIZE::VALUE)));
            
    if (include_encapsulation) {
        current_alignment += encapsulation_size;
    }

    return current_alignment - initial_alignment;
}


UInt32
TestData_tPlugin::get_serialized_sample_min_size(
    TypePluginDefaultEndpointData^ endpoint_data,
    Boolean include_encapsulation,
    UInt16 encapsulation_id,
    UInt32 current_alignment)
{

    UInt32 initial_alignment = current_alignment;

    UInt32 encapsulation_size = current_alignment;


    if (include_encapsulation) {
        if (!CdrStream::valid_encapsulation_id(encapsulation_id)) {
            return 1;
        }


        encapsulation_size = CdrSizes::ENCAPSULATION->serialized_size(
            encapsulation_size);
        current_alignment = 0;
        initial_alignment = 0;

    }


    current_alignment +=  CdrSizes::OCTET->serialized_array_size(
        current_alignment, ((KEY_SIZE::VALUE)));
            
    current_alignment +=  CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::UNSIGNED_LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::UNSIGNED_LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment +=  CdrSizes::OCTET->serialized_sequence_size(
        current_alignment, 0);
            
    if (include_encapsulation) {
        current_alignment += encapsulation_size;
    }

    return current_alignment - initial_alignment;
}


UInt32 
TestData_tPlugin::get_serialized_sample_size(
    TypePluginDefaultEndpointData^ endpoint_data,
    Boolean include_encapsulation,
    UInt16 encapsulation_id,
    UInt32 current_alignment,
    TestData_t^ sample)
{

    UInt32 initial_alignment = current_alignment;

    UInt32 encapsulation_size = current_alignment;


    if (include_encapsulation) {
        if (!CdrStream::valid_encapsulation_id(encapsulation_id)) {
            return 1;
        }


        encapsulation_size = CdrSizes::ENCAPSULATION->serialized_size(
            current_alignment);
        encapsulation_size -= current_alignment;
        current_alignment = 0;
        initial_alignment = 0;

    }


    current_alignment += CdrSizes::OCTET->serialized_array_size(
        current_alignment, ((KEY_SIZE::VALUE)));
            
    current_alignment += CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment += CdrSizes::UNSIGNED_LONG->serialized_size(
        current_alignment);
            
    current_alignment += CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment += CdrSizes::UNSIGNED_LONG->serialized_size(
        current_alignment);
            
    current_alignment += CdrSizes::LONG->serialized_size(
        current_alignment);
            
    current_alignment += CdrSizes::OCTET->serialized_sequence_size(
        current_alignment, sample->bin_data->length);
            
    if (include_encapsulation) {
        current_alignment += encapsulation_size;
    }

    return current_alignment - initial_alignment;
}


UInt32
TestData_tPlugin::get_serialized_key_max_size(
    TypePluginDefaultEndpointData^ endpoint_data,
    Boolean include_encapsulation,
    UInt16 encapsulation_id,
    UInt32 current_alignment)
{

    UInt32 encapsulation_size = current_alignment;


    UInt32 initial_alignment = current_alignment;

    if (include_encapsulation) {
        if (!CdrStream::valid_encapsulation_id(encapsulation_id)) {
            return 1;
        }


        encapsulation_size = CdrSizes::ENCAPSULATION->serialized_size(
            current_alignment);
        current_alignment = 0;
        initial_alignment = 0;

    }
        

    current_alignment += get_serialized_sample_max_size(
        endpoint_data,false,encapsulation_id,current_alignment);
    
    if (include_encapsulation) {
        current_alignment += encapsulation_size;
    }

    return current_alignment - initial_alignment;
}

/* ------------------------------------------------------------------------
    Key Management functions:
 * ------------------------------------------------------------------------ */

Boolean 
TestData_tPlugin::serialize_key(
    TypePluginDefaultEndpointData^ endpoint_data,
    TestData_t^ sample,
    CdrStream% stream,    
    Boolean serialize_encapsulation,
    UInt16 encapsulation_id,
    Boolean serialize_key,
    Object^ endpoint_plugin_qos)
{
    CdrStreamPosition position;

    if (serialize_encapsulation) {
        /* Encapsulate sample */
        if (!stream.serialize_and_set_cdr_encapsulation(encapsulation_id)) {
            return false;
        }


        position = stream.reset_alignment();

    }

    if (serialize_key) {

        if (!serialize(
                endpoint_data,
                sample,
                stream,
                serialize_encapsulation,
                encapsulation_id, 
                serialize_key,
                endpoint_plugin_qos)) {
            return false;
        }
    
    }


    if(serialize_encapsulation) {
        stream.restore_alignment(position);
    }


    return true;
}


Boolean TestData_tPlugin::deserialize_key_sample(
    TypePluginDefaultEndpointData^ endpoint_data,
    TestData_t^ sample,
    CdrStream% stream, 
    Boolean deserialize_encapsulation,
    Boolean deserialize_key,
    Object^ endpoint_plugin_qos)
{
    CdrStreamPosition position;

    if (deserialize_encapsulation) {
        /* Deserialize encapsulation */
        if (!stream.deserialize_and_set_cdr_encapsulation()) {
            return false;  
        }


        position = stream.reset_alignment();

    }

    if (deserialize_key) {

        if (!deserialize_sample(
                endpoint_data, sample, stream,
                deserialize_encapsulation,
                deserialize_key,
                endpoint_plugin_qos)) {
            return false;
        }
    
    }


    if(deserialize_encapsulation) {
        stream.restore_alignment(position);
    }


    return true;
}


Boolean
TestData_tPlugin::serialized_sample_to_key(
    TypePluginDefaultEndpointData^ endpoint_data,
    TestData_t^ sample,
    CdrStream% stream, 
    Boolean deserialize_encapsulation,  
    Boolean deserialize_key, 
    Object^ endpoint_plugin_qos)
{
    CdrStreamPosition position;

    if(deserialize_encapsulation) {
        if (!stream.deserialize_and_set_cdr_encapsulation()) {
            return false;
        }

        position = stream.reset_alignment();

    }

    if (deserialize_key) {

        if (!deserialize_sample(
                endpoint_data,
                sample,
                stream,
                deserialize_encapsulation,
                deserialize_key,
                endpoint_plugin_qos)) {
            return false;
        }

    }


    if(deserialize_encapsulation) {
        stream.restore_alignment(position);
    }


    return true;
}




/* ------------------------------------------------------------------------
 * Plug-in Lifecycle Methods
 * ------------------------------------------------------------------------ */

TestData_tPlugin^
TestData_tPlugin::get_instance() {
    if (_singleton == nullptr) {
        _singleton = gcnew TestData_tPlugin();
    }
    return _singleton;
}


void
TestData_tPlugin::dispose() {
    delete _singleton;
    _singleton = nullptr;
}
