//#####################################################################
// Copyright 2020, Craig Schroeder.
// This file is part of PhysBAM whose distribution is governed by the license contained in the accompanying file PHYSBAM_COPYRIGHT.txt.
//#####################################################################

#include <Core/Symbolics/SYMBOLIC.h>
#include <Core/Read_Write/READ_WRITE.h>
#include <Core/Symbolics/SYMBOLIC_TRIVIAL.h>
namespace PhysBAM{

SYMBOLIC_TRIVIAL sym_trivial;
SYMBOLIC_BASE* sym_prototype=&sym_trivial;

void Unregister_Symbolic()
{
    if(sym_prototype!=&sym_trivial) delete sym_prototype;
    sym_prototype=&sym_trivial;
}

SYMBOLIC& SYMBOLIC::operator=(const SYMBOLIC& s)
{
    x=s.x;
    delete z;
    z=nullptr;
    if(sym_prototype)
    {
        z=sym_prototype->Clone();
        if(s.z) z->Unary(x,s,SYMBOLIC_BASE::type_1::copy);
        else z->Set(x);
    }
    return *this;
}

//#####################################################################
// Function Read
//#####################################################################
template<class RW> void SYMBOLIC::
Read(UNTYPED_ISTREAM<RW>& input)
{
    Read_Binary(input,x);
    if(z) z->Set(x);
}
//#####################################################################
// Function Write
//#####################################################################
template<class RW> void SYMBOLIC::
Write(UNTYPED_OSTREAM<RW>& output) const
{
    Write_Binary(output,x);
}

SYMBOLIC modf(const SYMBOLIC& a, SYMBOLIC *b)
{
    double r,s=modf(a.x,&r);
    *b=SYMBOLIC(r,a,SYMBOLIC_BASE::type_1::modf_arg);
    return SYMBOLIC(s,a,SYMBOLIC_BASE::type_1::modf_ret);
}
template void Read_Binary(UNTYPED_ISTREAM<char>&,std::complex<SYMBOLIC>&);
template void Read_Binary(UNTYPED_ISTREAM<double>&,std::complex<SYMBOLIC>&);
template void Read_Binary(UNTYPED_ISTREAM<float>&,std::complex<SYMBOLIC>&);
template void Write_Binary(UNTYPED_OSTREAM<char>&,std::complex<SYMBOLIC> const&);
template void Write_Binary(UNTYPED_OSTREAM<double>&,std::complex<SYMBOLIC> const&);
template void Write_Binary(UNTYPED_OSTREAM<float>&,std::complex<SYMBOLIC> const&);
}
