Skip to content
Snippets Groups Projects
Forked from mlz / Frida
552 commits behind the upstream repository.
obj.cpp 6.07 KiB
//**************************************************************************************************
//*  FRIDA: fast reliable interactive data analysis
//*  (C) Joachim Wuttke 1990-, v2(C++) 2001-
//*  http://apps.jcns.fz-juelich.de/frida
//**************************************************************************************************

//! \file  obj.cpp
//! \brief Data containers: CObj and its children.

#include <boost/format.hpp>
#include "../trivia/string_ops.hpp"
#include "defs.hpp"
#include "ptr.hpp"
#include "obj.hpp"

using boost::format;


//**************************************************************************************************
//  Static methods
//**************************************************************************************************

PObj CObj::objectify( int val ) { return PObjInt( new CObjInt(val) ); }
PObj CObj::objectify( double val ) { return PObjDbl( new CObjDbl(val) ); }
PObj CObj::objectify( double val, double err ) { return PObjEnu( new CObjEnu(val, err) ); }
PObj CObj::objectify( string val ) { return PObjStr( new CObjStr(val) ); }


//**************************************************************************************************
//  CObjInt
//**************************************************************************************************

//! Returns string representation.

string CObjInt::to_s( int maxlen, int minlen, int prec ) const
{
    // TODO: respect minlen
    string form = str( format( "%%#%ii" ) % maxlen );
    string ret = triv::strip( str( format( form ) % val ) );
    return ret;
}


//**************************************************************************************************
//  CObjDbl
//**************************************************************************************************

//! Returns string representation.

string CObjDbl::to_s( int maxlen, int minlen, int prec ) const
{
    // TODO: respect minlen
    string form = str( format( "%%#%i.%ig" ) % maxlen % prec );
    string ret = triv::strip( str( format( form ) % val ) );
    return ret;
}

//! Returns textual representation of the class instance, for debugging.

string CObjDbl::result_info() const
{
    if( std::isnan(val) )
        return "CObjDbl(no value assigned)";
    else
        return "CObjDbl("+to_s()+")";
}


//**************************************************************************************************
//  CObjEnu
//**************************************************************************************************

//! Returns string representation.

string CObjEnu::to_s( int maxlen, int minlen, int prec ) const
{
    return S(val) + "+-" + S(err);
}

//! Returns textual representation of the class instance, for debugging.

string CObjEnu::result_info() const
{
    if( std::isnan(val) )
        return "CObjEnu(no value assigned)";
    else
        return "CObjDbl("+to_s()+")";
}


//**************************************************************************************************
//  CObjVec
//**************************************************************************************************


//**************************************************************************************************
//  CObjVecObj
//**************************************************************************************************

//! Returns pointer to vector of pure numeric type.

RObj CObjVecObj::to_vecnum() const
{
    if( v.size()<1 )
        throw S("BUG: to_vecnum called for empty vector");
    try {
        PObjVecInt ret( new CObjVecInt );
        for( RObj ele: v ) {
            ret->v.push_back( ele->to_i() );
        }
        return ret;
    } catch (... ) {}
    try {
        PObjVecDbl ret( new CObjVecDbl );
        for( RObj ele: v ) {
            if ( ele->to_dr() )
                throw "would loose dr";
            ret->v.push_back( ele->to_r() );
        }
        return ret;
    } catch (... ) {}
    try {
        PObjVecEnu ret( new CObjVecEnu );
        for( RObj ele: v ) {
            ret->v.push_back( ele->to_r() );
            ret->dv.push_back( ele->to_dr() );
        }
        return ret;
    } catch (... ) {}
    throw S("vector has non-numeric elements");
}

//! Returns string representation.

string CObjVecObj::to_s( int maxlen, int minlen, int prec ) const
{
    string ret;
    for ( int i=0; i<v.size(); ++i ) {
        ret += v[i]->to_s();
        if ( i<v.size()-1 )
            ret += ",";
    }
    return ret;
}

//**************************************************************************************************
//  CObjVecInt
//**************************************************************************************************

//! Returns string representation.

string CObjVecInt::to_s( int maxlen, int minlen, int prec ) const
{
    string ret;
    for ( int i=0; i<v.size(); ++i ) {
        ret += S(v[i]);
        if ( i<v.size()-1 )
            ret += ",";
    }
    return ret;
}


//! Returns textual representation of the class instance, for debugging.

string CObjVecInt::result_info() const
{
    return to_s();
}


//**************************************************************************************************
//  CObjVecDbl
//**************************************************************************************************

//! Returns string representation.

string CObjVecDbl::to_s( int maxlen, int minlen, int prec ) const
{
    string ret;
    for ( int i=0; i<v.size(); ++i ) {
        ret += S(v[i]);
        if ( i<v.size()-1 )
            ret += ",";
    }
    return ret;
}


//! Returns textual representation of the class instance, for debugging.

string CObjVecDbl::result_info() const
{
    return to_s();
}


//**************************************************************************************************
//  CObjVecEnu
//**************************************************************************************************


//! Returns string representation.

string CObjVecEnu::to_s( int maxlen, int minlen, int prec ) const
{
    return "["+S(size())+" floating-point entries with errors]";
}

//! Returns textual representation of the class instance, for debugging.

string CObjVecEnu::result_info() const
{
    return to_s();
}