/*
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <tango/server/AttrProperty.h>

#include <tango/client/Database.h>
#include <tango/server/Attribute.h>
#include <tango/server/LoggerStream.h>

#include <algorithm>

namespace Tango
{

//+------------------------------------------------------------------------------------------------------------------
//
// method :
//        AttrProperty::AttrProperty
//
// description :
//        Constructors for the AttrProperty class. These constructor change the property name to lowercase letters and
//        also change the property value to lowercase letters for the data_format and data_type property
//
// argument :
//        in :
//            - name : The property name
//            - value : The property value
//
//------------------------------------------------------------------------------------------------------------------

AttrProperty::AttrProperty(const std::string &name, const std::string &value) :
    attr_name(name),
    attr_value(value)
{
    attr_lg = 0;

    //
    // Property name in lower case letters
    //

    std::transform(attr_name.begin(), attr_name.end(), attr_name.begin(), ::tolower);

    //
    // For data_type or data_format properties, also change property value to lowercase letters
    //

    if((attr_name == "data_type") || (attr_name == "data_format"))
    {
        std::transform(attr_value.begin(), attr_value.end(), attr_value.begin(), ::tolower);
    }
}

AttrProperty::AttrProperty(const std::string &name, long value) :
    attr_name(name),
    attr_lg(value)
{
}

AttrProperty::~AttrProperty() = default;

AttrProperty::AttrProperty(const AttrProperty &) = default;
AttrProperty &AttrProperty::operator=(const AttrProperty &) = default;

AttrProperty::AttrProperty(AttrProperty &&) = default;
AttrProperty &AttrProperty::operator=(AttrProperty &&) = default;

//+-----------------------------------------------------------------------------------------------------------------
//
// method :
//        AttrProperty::convert
//
// description :
//        Convert the property value into a long. The long data is also stored in the AttrProperty class
//
//------------------------------------------------------------------------------------------------------------------

void AttrProperty::convert(const char *prop_name)
{
    TangoSys_MemStream o;
    o << attr_value;
    if(!(o >> attr_lg && o.eof()))
    {
        std::stringstream ss;
        ss << "Can't convert property value for property " << prop_name;
        TANGO_THROW_EXCEPTION(API_AttrOptProp, ss.str());
    }
}

//+-----------------------------------------------------------------------------------------------------------------
//
// operator overloading :     <<
//
// description :
//        Friend function to ease printing instance of the AttrProperty class
//
//-------------------------------------------------------------------------------------------------------------------

} // namespace Tango
