/*
 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
 * SPDX-License-Identifier: BSD-3-Clause
 */

// Autogenerated by Thrift Compiler (0.14.1-yarped)
//
// This is an automatically generated file.
// It could get re-generated if the ALLOW_IDL_GENERATION flag is on.

#include <return_getLastVelocityCommand.h>

// Constructor with field values
return_getLastVelocityCommand::return_getLastVelocityCommand(const bool retvalue,
                                                             const double x_vel,
                                                             const double y_vel,
                                                             const double theta_vel) :
        WirePortable(),
        retvalue(retvalue),
        x_vel(x_vel),
        y_vel(y_vel),
        theta_vel(theta_vel)
{
}

// Read structure on a Wire
bool return_getLastVelocityCommand::read(yarp::os::idl::WireReader& reader)
{
    if (!read_retvalue(reader)) {
        return false;
    }
    if (!read_x_vel(reader)) {
        return false;
    }
    if (!read_y_vel(reader)) {
        return false;
    }
    if (!read_theta_vel(reader)) {
        return false;
    }
    return !reader.isError();
}

// Read structure on a Connection
bool return_getLastVelocityCommand::read(yarp::os::ConnectionReader& connection)
{
    yarp::os::idl::WireReader reader(connection);
    if (!reader.readListHeader(4)) {
        return false;
    }
    return read(reader);
}

// Write structure on a Wire
bool return_getLastVelocityCommand::write(const yarp::os::idl::WireWriter& writer) const
{
    if (!write_retvalue(writer)) {
        return false;
    }
    if (!write_x_vel(writer)) {
        return false;
    }
    if (!write_y_vel(writer)) {
        return false;
    }
    if (!write_theta_vel(writer)) {
        return false;
    }
    return !writer.isError();
}

// Write structure on a Connection
bool return_getLastVelocityCommand::write(yarp::os::ConnectionWriter& connection) const
{
    yarp::os::idl::WireWriter writer(connection);
    if (!writer.writeListHeader(4)) {
        return false;
    }
    return write(writer);
}

// Convert to a printable string
std::string return_getLastVelocityCommand::toString() const
{
    yarp::os::Bottle b;
    b.read(*this);
    return b.toString();
}

// Editor: default constructor
return_getLastVelocityCommand::Editor::Editor()
{
    group = 0;
    obj_owned = true;
    obj = new return_getLastVelocityCommand;
    dirty_flags(false);
    yarp().setOwner(*this);
}

// Editor: constructor with base class
return_getLastVelocityCommand::Editor::Editor(return_getLastVelocityCommand& obj)
{
    group = 0;
    obj_owned = false;
    edit(obj, false);
    yarp().setOwner(*this);
}

// Editor: destructor
return_getLastVelocityCommand::Editor::~Editor()
{
    if (obj_owned) {
        delete obj;
    }
}

// Editor: edit
bool return_getLastVelocityCommand::Editor::edit(return_getLastVelocityCommand& obj, bool dirty)
{
    if (obj_owned) {
        delete this->obj;
    }
    this->obj = &obj;
    obj_owned = false;
    dirty_flags(dirty);
    return true;
}

// Editor: validity check
bool return_getLastVelocityCommand::Editor::isValid() const
{
    return obj != nullptr;
}

// Editor: state
return_getLastVelocityCommand& return_getLastVelocityCommand::Editor::state()
{
    return *obj;
}

// Editor: grouping begin
void return_getLastVelocityCommand::Editor::start_editing()
{
    group++;
}

// Editor: grouping end
void return_getLastVelocityCommand::Editor::stop_editing()
{
    group--;
    if (group == 0 && is_dirty) {
        communicate();
    }
}
// Editor: retvalue setter
void return_getLastVelocityCommand::Editor::set_retvalue(const bool retvalue)
{
    will_set_retvalue();
    obj->retvalue = retvalue;
    mark_dirty_retvalue();
    communicate();
    did_set_retvalue();
}

// Editor: retvalue getter
bool return_getLastVelocityCommand::Editor::get_retvalue() const
{
    return obj->retvalue;
}

// Editor: retvalue will_set
bool return_getLastVelocityCommand::Editor::will_set_retvalue()
{
    return true;
}

// Editor: retvalue did_set
bool return_getLastVelocityCommand::Editor::did_set_retvalue()
{
    return true;
}

// Editor: x_vel setter
void return_getLastVelocityCommand::Editor::set_x_vel(const double x_vel)
{
    will_set_x_vel();
    obj->x_vel = x_vel;
    mark_dirty_x_vel();
    communicate();
    did_set_x_vel();
}

// Editor: x_vel getter
double return_getLastVelocityCommand::Editor::get_x_vel() const
{
    return obj->x_vel;
}

// Editor: x_vel will_set
bool return_getLastVelocityCommand::Editor::will_set_x_vel()
{
    return true;
}

// Editor: x_vel did_set
bool return_getLastVelocityCommand::Editor::did_set_x_vel()
{
    return true;
}

// Editor: y_vel setter
void return_getLastVelocityCommand::Editor::set_y_vel(const double y_vel)
{
    will_set_y_vel();
    obj->y_vel = y_vel;
    mark_dirty_y_vel();
    communicate();
    did_set_y_vel();
}

// Editor: y_vel getter
double return_getLastVelocityCommand::Editor::get_y_vel() const
{
    return obj->y_vel;
}

// Editor: y_vel will_set
bool return_getLastVelocityCommand::Editor::will_set_y_vel()
{
    return true;
}

// Editor: y_vel did_set
bool return_getLastVelocityCommand::Editor::did_set_y_vel()
{
    return true;
}

// Editor: theta_vel setter
void return_getLastVelocityCommand::Editor::set_theta_vel(const double theta_vel)
{
    will_set_theta_vel();
    obj->theta_vel = theta_vel;
    mark_dirty_theta_vel();
    communicate();
    did_set_theta_vel();
}

// Editor: theta_vel getter
double return_getLastVelocityCommand::Editor::get_theta_vel() const
{
    return obj->theta_vel;
}

// Editor: theta_vel will_set
bool return_getLastVelocityCommand::Editor::will_set_theta_vel()
{
    return true;
}

// Editor: theta_vel did_set
bool return_getLastVelocityCommand::Editor::did_set_theta_vel()
{
    return true;
}

// Editor: clean
void return_getLastVelocityCommand::Editor::clean()
{
    dirty_flags(false);
}

// Editor: read
bool return_getLastVelocityCommand::Editor::read(yarp::os::ConnectionReader& connection)
{
    if (!isValid()) {
        return false;
    }
    yarp::os::idl::WireReader reader(connection);
    reader.expectAccept();
    if (!reader.readListHeader()) {
        return false;
    }
    int len = reader.getLength();
    if (len == 0) {
        yarp::os::idl::WireWriter writer(reader);
        if (writer.isNull()) {
            return true;
        }
        if (!writer.writeListHeader(1)) {
            return false;
        }
        writer.writeString("send: 'help' or 'patch (param1 val1) (param2 val2)'");
        return true;
    }
    std::string tag;
    if (!reader.readString(tag)) {
        return false;
    }
    if (tag == "help") {
        yarp::os::idl::WireWriter writer(reader);
        if (writer.isNull()) {
            return true;
        }
        if (!writer.writeListHeader(2)) {
            return false;
        }
        if (!writer.writeTag("many", 1, 0)) {
            return false;
        }
        if (reader.getLength() > 0) {
            std::string field;
            if (!reader.readString(field)) {
                return false;
            }
            if (field == "retvalue") {
                if (!writer.writeListHeader(1)) {
                    return false;
                }
                if (!writer.writeString("bool retvalue")) {
                    return false;
                }
            }
            if (field == "x_vel") {
                if (!writer.writeListHeader(1)) {
                    return false;
                }
                if (!writer.writeString("double x_vel")) {
                    return false;
                }
            }
            if (field == "y_vel") {
                if (!writer.writeListHeader(1)) {
                    return false;
                }
                if (!writer.writeString("double y_vel")) {
                    return false;
                }
            }
            if (field == "theta_vel") {
                if (!writer.writeListHeader(1)) {
                    return false;
                }
                if (!writer.writeString("double theta_vel")) {
                    return false;
                }
            }
        }
        if (!writer.writeListHeader(5)) {
            return false;
        }
        writer.writeString("*** Available fields:");
        writer.writeString("retvalue");
        writer.writeString("x_vel");
        writer.writeString("y_vel");
        writer.writeString("theta_vel");
        return true;
    }
    bool nested = true;
    bool have_act = false;
    if (tag != "patch") {
        if (((len - 1) % 2) != 0) {
            return false;
        }
        len = 1 + ((len - 1) / 2);
        nested = false;
        have_act = true;
    }
    for (int i = 1; i < len; ++i) {
        if (nested && !reader.readListHeader(3)) {
            return false;
        }
        std::string act;
        std::string key;
        if (have_act) {
            act = tag;
        } else if (!reader.readString(act)) {
            return false;
        }
        if (!reader.readString(key)) {
            return false;
        }
        if (key == "retvalue") {
            will_set_retvalue();
            if (!obj->nested_read_retvalue(reader)) {
                return false;
            }
            did_set_retvalue();
        } else if (key == "x_vel") {
            will_set_x_vel();
            if (!obj->nested_read_x_vel(reader)) {
                return false;
            }
            did_set_x_vel();
        } else if (key == "y_vel") {
            will_set_y_vel();
            if (!obj->nested_read_y_vel(reader)) {
                return false;
            }
            did_set_y_vel();
        } else if (key == "theta_vel") {
            will_set_theta_vel();
            if (!obj->nested_read_theta_vel(reader)) {
                return false;
            }
            did_set_theta_vel();
        } else {
            // would be useful to have a fallback here
        }
    }
    reader.accept();
    yarp::os::idl::WireWriter writer(reader);
    if (writer.isNull()) {
        return true;
    }
    writer.writeListHeader(1);
    writer.writeVocab32('o', 'k');
    return true;
}

// Editor: write
bool return_getLastVelocityCommand::Editor::write(yarp::os::ConnectionWriter& connection) const
{
    if (!isValid()) {
        return false;
    }
    yarp::os::idl::WireWriter writer(connection);
    if (!writer.writeListHeader(dirty_count + 1)) {
        return false;
    }
    if (!writer.writeString("patch")) {
        return false;
    }
    if (is_dirty_retvalue) {
        if (!writer.writeListHeader(3)) {
            return false;
        }
        if (!writer.writeString("set")) {
            return false;
        }
        if (!writer.writeString("retvalue")) {
            return false;
        }
        if (!obj->nested_write_retvalue(writer)) {
            return false;
        }
    }
    if (is_dirty_x_vel) {
        if (!writer.writeListHeader(3)) {
            return false;
        }
        if (!writer.writeString("set")) {
            return false;
        }
        if (!writer.writeString("x_vel")) {
            return false;
        }
        if (!obj->nested_write_x_vel(writer)) {
            return false;
        }
    }
    if (is_dirty_y_vel) {
        if (!writer.writeListHeader(3)) {
            return false;
        }
        if (!writer.writeString("set")) {
            return false;
        }
        if (!writer.writeString("y_vel")) {
            return false;
        }
        if (!obj->nested_write_y_vel(writer)) {
            return false;
        }
    }
    if (is_dirty_theta_vel) {
        if (!writer.writeListHeader(3)) {
            return false;
        }
        if (!writer.writeString("set")) {
            return false;
        }
        if (!writer.writeString("theta_vel")) {
            return false;
        }
        if (!obj->nested_write_theta_vel(writer)) {
            return false;
        }
    }
    return !writer.isError();
}

// Editor: send if possible
void return_getLastVelocityCommand::Editor::communicate()
{
    if (group != 0) {
        return;
    }
    if (yarp().canWrite()) {
        yarp().write(*this);
        clean();
    }
}

// Editor: mark dirty overall
void return_getLastVelocityCommand::Editor::mark_dirty()
{
    is_dirty = true;
}

// Editor: retvalue mark_dirty
void return_getLastVelocityCommand::Editor::mark_dirty_retvalue()
{
    if (is_dirty_retvalue) {
        return;
    }
    dirty_count++;
    is_dirty_retvalue = true;
    mark_dirty();
}

// Editor: x_vel mark_dirty
void return_getLastVelocityCommand::Editor::mark_dirty_x_vel()
{
    if (is_dirty_x_vel) {
        return;
    }
    dirty_count++;
    is_dirty_x_vel = true;
    mark_dirty();
}

// Editor: y_vel mark_dirty
void return_getLastVelocityCommand::Editor::mark_dirty_y_vel()
{
    if (is_dirty_y_vel) {
        return;
    }
    dirty_count++;
    is_dirty_y_vel = true;
    mark_dirty();
}

// Editor: theta_vel mark_dirty
void return_getLastVelocityCommand::Editor::mark_dirty_theta_vel()
{
    if (is_dirty_theta_vel) {
        return;
    }
    dirty_count++;
    is_dirty_theta_vel = true;
    mark_dirty();
}

// Editor: dirty_flags
void return_getLastVelocityCommand::Editor::dirty_flags(bool flag)
{
    is_dirty = flag;
    is_dirty_retvalue = flag;
    is_dirty_x_vel = flag;
    is_dirty_y_vel = flag;
    is_dirty_theta_vel = flag;
    dirty_count = flag ? 4 : 0;
}

// read retvalue field
bool return_getLastVelocityCommand::read_retvalue(yarp::os::idl::WireReader& reader)
{
    if (reader.noMore()) {
        reader.fail();
        return false;
    }
    if (!reader.readBool(retvalue)) {
        reader.fail();
        return false;
    }
    return true;
}

// write retvalue field
bool return_getLastVelocityCommand::write_retvalue(const yarp::os::idl::WireWriter& writer) const
{
    if (!writer.writeBool(retvalue)) {
        return false;
    }
    return true;
}

// read (nested) retvalue field
bool return_getLastVelocityCommand::nested_read_retvalue(yarp::os::idl::WireReader& reader)
{
    if (reader.noMore()) {
        reader.fail();
        return false;
    }
    if (!reader.readBool(retvalue)) {
        reader.fail();
        return false;
    }
    return true;
}

// write (nested) retvalue field
bool return_getLastVelocityCommand::nested_write_retvalue(const yarp::os::idl::WireWriter& writer) const
{
    if (!writer.writeBool(retvalue)) {
        return false;
    }
    return true;
}

// read x_vel field
bool return_getLastVelocityCommand::read_x_vel(yarp::os::idl::WireReader& reader)
{
    if (reader.noMore()) {
        reader.fail();
        return false;
    }
    if (!reader.readFloat64(x_vel)) {
        reader.fail();
        return false;
    }
    return true;
}

// write x_vel field
bool return_getLastVelocityCommand::write_x_vel(const yarp::os::idl::WireWriter& writer) const
{
    if (!writer.writeFloat64(x_vel)) {
        return false;
    }
    return true;
}

// read (nested) x_vel field
bool return_getLastVelocityCommand::nested_read_x_vel(yarp::os::idl::WireReader& reader)
{
    if (reader.noMore()) {
        reader.fail();
        return false;
    }
    if (!reader.readFloat64(x_vel)) {
        reader.fail();
        return false;
    }
    return true;
}

// write (nested) x_vel field
bool return_getLastVelocityCommand::nested_write_x_vel(const yarp::os::idl::WireWriter& writer) const
{
    if (!writer.writeFloat64(x_vel)) {
        return false;
    }
    return true;
}

// read y_vel field
bool return_getLastVelocityCommand::read_y_vel(yarp::os::idl::WireReader& reader)
{
    if (reader.noMore()) {
        reader.fail();
        return false;
    }
    if (!reader.readFloat64(y_vel)) {
        reader.fail();
        return false;
    }
    return true;
}

// write y_vel field
bool return_getLastVelocityCommand::write_y_vel(const yarp::os::idl::WireWriter& writer) const
{
    if (!writer.writeFloat64(y_vel)) {
        return false;
    }
    return true;
}

// read (nested) y_vel field
bool return_getLastVelocityCommand::nested_read_y_vel(yarp::os::idl::WireReader& reader)
{
    if (reader.noMore()) {
        reader.fail();
        return false;
    }
    if (!reader.readFloat64(y_vel)) {
        reader.fail();
        return false;
    }
    return true;
}

// write (nested) y_vel field
bool return_getLastVelocityCommand::nested_write_y_vel(const yarp::os::idl::WireWriter& writer) const
{
    if (!writer.writeFloat64(y_vel)) {
        return false;
    }
    return true;
}

// read theta_vel field
bool return_getLastVelocityCommand::read_theta_vel(yarp::os::idl::WireReader& reader)
{
    if (reader.noMore()) {
        reader.fail();
        return false;
    }
    if (!reader.readFloat64(theta_vel)) {
        reader.fail();
        return false;
    }
    return true;
}

// write theta_vel field
bool return_getLastVelocityCommand::write_theta_vel(const yarp::os::idl::WireWriter& writer) const
{
    if (!writer.writeFloat64(theta_vel)) {
        return false;
    }
    return true;
}

// read (nested) theta_vel field
bool return_getLastVelocityCommand::nested_read_theta_vel(yarp::os::idl::WireReader& reader)
{
    if (reader.noMore()) {
        reader.fail();
        return false;
    }
    if (!reader.readFloat64(theta_vel)) {
        reader.fail();
        return false;
    }
    return true;
}

// write (nested) theta_vel field
bool return_getLastVelocityCommand::nested_write_theta_vel(const yarp::os::idl::WireWriter& writer) const
{
    if (!writer.writeFloat64(theta_vel)) {
        return false;
    }
    return true;
}
