/** \file
 *
 *  Contains the Float2 class implementation.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 *
 */

#include "Float2.h"
#include "io/OutStream.h"
#include <math.h>

/// Constructor for non-array storage
Float2::Float2( const Unit& unit, float value, bool scaleValue )
    : Float( unit, value, scaleValue )
{
    typeSize_ = 2,
    binaryType_ = FLOAT2;
}

/// Constructor from binary
Float2::Float2( const Unit& unit, const void *binary )
    : Float( unit, 0, false )
{
    typeSize_ = 2,
    binaryType_ = FLOAT2;
    fromBinary( binary );
}

/// Return a pointer to a new copy
Float2* Float2::copy() const
{
    return new Float2( *unit_, value_, false );
}

/// A quick equality check used to see if value has changed enough
/// to matter in logging (useful for reduced resolution data storage
bool Float2::equalsForStorage( const float& compareTo ) const
{
    return ( *( unsigned int* )&value_ & 0xFFFF0000 ) == ( *( unsigned int* )&compareTo & 0xFFFF0000 )
           || ( isnan( value_ ) && isnan( compareTo ) );
}

/// Send the data value to the indicated stream.
/// Return the number of bytes written.
/// For now, assume little-endian.
unsigned int Float2::toStream( OutStream& outStream ) const
{
    return outStream.writeFloat2( value_ ).bytesWritten();
}

void Float2::fromBinary( const void *buffer )
{
    value_ = 0;
    memcpy( ( ( unsigned char * ) & value_ ) + 2, ( const void * ) buffer, typeSize_ );
}

/// Protected Empty constructor for use bya DataEntry class
Float2::Float2()
    : Float()
{
    typeSize_ = 2,
    binaryType_ = FLOAT2;
}

