// generated from rosidl_generator_c/resource/idl__functions.c.em
// with input from interface_protocol:msg/JointMotionPlanRequest.idl
// generated code does not contain a copyright notice
#include "interface_protocol/msg/detail/joint_motion_plan_request__functions.h"

#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "rcutils/allocator.h"


// Include directives for member types
// Member `joint_indices`
// Member `target_positions`
// Member `target_velocities`
// Member `stiffness`
// Member `damping`
#include "rosidl_runtime_c/primitives_sequence_functions.h"

bool
interface_protocol__msg__JointMotionPlanRequest__init(interface_protocol__msg__JointMotionPlanRequest * msg)
{
  if (!msg) {
    return false;
  }
  // request_id
  // request_type
  // use_gravity_compensation
  // joint_indices
  if (!rosidl_runtime_c__int32__Sequence__init(&msg->joint_indices, 0)) {
    interface_protocol__msg__JointMotionPlanRequest__fini(msg);
    return false;
  }
  // target_positions
  if (!rosidl_runtime_c__double__Sequence__init(&msg->target_positions, 0)) {
    interface_protocol__msg__JointMotionPlanRequest__fini(msg);
    return false;
  }
  // target_velocities
  if (!rosidl_runtime_c__double__Sequence__init(&msg->target_velocities, 0)) {
    interface_protocol__msg__JointMotionPlanRequest__fini(msg);
    return false;
  }
  // execution_time
  // stiffness
  if (!rosidl_runtime_c__double__Sequence__init(&msg->stiffness, 0)) {
    interface_protocol__msg__JointMotionPlanRequest__fini(msg);
    return false;
  }
  // damping
  if (!rosidl_runtime_c__double__Sequence__init(&msg->damping, 0)) {
    interface_protocol__msg__JointMotionPlanRequest__fini(msg);
    return false;
  }
  return true;
}

void
interface_protocol__msg__JointMotionPlanRequest__fini(interface_protocol__msg__JointMotionPlanRequest * msg)
{
  if (!msg) {
    return;
  }
  // request_id
  // request_type
  // use_gravity_compensation
  // joint_indices
  rosidl_runtime_c__int32__Sequence__fini(&msg->joint_indices);
  // target_positions
  rosidl_runtime_c__double__Sequence__fini(&msg->target_positions);
  // target_velocities
  rosidl_runtime_c__double__Sequence__fini(&msg->target_velocities);
  // execution_time
  // stiffness
  rosidl_runtime_c__double__Sequence__fini(&msg->stiffness);
  // damping
  rosidl_runtime_c__double__Sequence__fini(&msg->damping);
}

bool
interface_protocol__msg__JointMotionPlanRequest__are_equal(const interface_protocol__msg__JointMotionPlanRequest * lhs, const interface_protocol__msg__JointMotionPlanRequest * rhs)
{
  if (!lhs || !rhs) {
    return false;
  }
  // request_id
  if (lhs->request_id != rhs->request_id) {
    return false;
  }
  // request_type
  if (lhs->request_type != rhs->request_type) {
    return false;
  }
  // use_gravity_compensation
  if (lhs->use_gravity_compensation != rhs->use_gravity_compensation) {
    return false;
  }
  // joint_indices
  if (!rosidl_runtime_c__int32__Sequence__are_equal(
      &(lhs->joint_indices), &(rhs->joint_indices)))
  {
    return false;
  }
  // target_positions
  if (!rosidl_runtime_c__double__Sequence__are_equal(
      &(lhs->target_positions), &(rhs->target_positions)))
  {
    return false;
  }
  // target_velocities
  if (!rosidl_runtime_c__double__Sequence__are_equal(
      &(lhs->target_velocities), &(rhs->target_velocities)))
  {
    return false;
  }
  // execution_time
  if (lhs->execution_time != rhs->execution_time) {
    return false;
  }
  // stiffness
  if (!rosidl_runtime_c__double__Sequence__are_equal(
      &(lhs->stiffness), &(rhs->stiffness)))
  {
    return false;
  }
  // damping
  if (!rosidl_runtime_c__double__Sequence__are_equal(
      &(lhs->damping), &(rhs->damping)))
  {
    return false;
  }
  return true;
}

bool
interface_protocol__msg__JointMotionPlanRequest__copy(
  const interface_protocol__msg__JointMotionPlanRequest * input,
  interface_protocol__msg__JointMotionPlanRequest * output)
{
  if (!input || !output) {
    return false;
  }
  // request_id
  output->request_id = input->request_id;
  // request_type
  output->request_type = input->request_type;
  // use_gravity_compensation
  output->use_gravity_compensation = input->use_gravity_compensation;
  // joint_indices
  if (!rosidl_runtime_c__int32__Sequence__copy(
      &(input->joint_indices), &(output->joint_indices)))
  {
    return false;
  }
  // target_positions
  if (!rosidl_runtime_c__double__Sequence__copy(
      &(input->target_positions), &(output->target_positions)))
  {
    return false;
  }
  // target_velocities
  if (!rosidl_runtime_c__double__Sequence__copy(
      &(input->target_velocities), &(output->target_velocities)))
  {
    return false;
  }
  // execution_time
  output->execution_time = input->execution_time;
  // stiffness
  if (!rosidl_runtime_c__double__Sequence__copy(
      &(input->stiffness), &(output->stiffness)))
  {
    return false;
  }
  // damping
  if (!rosidl_runtime_c__double__Sequence__copy(
      &(input->damping), &(output->damping)))
  {
    return false;
  }
  return true;
}

interface_protocol__msg__JointMotionPlanRequest *
interface_protocol__msg__JointMotionPlanRequest__create()
{
  rcutils_allocator_t allocator = rcutils_get_default_allocator();
  interface_protocol__msg__JointMotionPlanRequest * msg = (interface_protocol__msg__JointMotionPlanRequest *)allocator.allocate(sizeof(interface_protocol__msg__JointMotionPlanRequest), allocator.state);
  if (!msg) {
    return NULL;
  }
  memset(msg, 0, sizeof(interface_protocol__msg__JointMotionPlanRequest));
  bool success = interface_protocol__msg__JointMotionPlanRequest__init(msg);
  if (!success) {
    allocator.deallocate(msg, allocator.state);
    return NULL;
  }
  return msg;
}

void
interface_protocol__msg__JointMotionPlanRequest__destroy(interface_protocol__msg__JointMotionPlanRequest * msg)
{
  rcutils_allocator_t allocator = rcutils_get_default_allocator();
  if (msg) {
    interface_protocol__msg__JointMotionPlanRequest__fini(msg);
  }
  allocator.deallocate(msg, allocator.state);
}


bool
interface_protocol__msg__JointMotionPlanRequest__Sequence__init(interface_protocol__msg__JointMotionPlanRequest__Sequence * array, size_t size)
{
  if (!array) {
    return false;
  }
  rcutils_allocator_t allocator = rcutils_get_default_allocator();
  interface_protocol__msg__JointMotionPlanRequest * data = NULL;

  if (size) {
    data = (interface_protocol__msg__JointMotionPlanRequest *)allocator.zero_allocate(size, sizeof(interface_protocol__msg__JointMotionPlanRequest), allocator.state);
    if (!data) {
      return false;
    }
    // initialize all array elements
    size_t i;
    for (i = 0; i < size; ++i) {
      bool success = interface_protocol__msg__JointMotionPlanRequest__init(&data[i]);
      if (!success) {
        break;
      }
    }
    if (i < size) {
      // if initialization failed finalize the already initialized array elements
      for (; i > 0; --i) {
        interface_protocol__msg__JointMotionPlanRequest__fini(&data[i - 1]);
      }
      allocator.deallocate(data, allocator.state);
      return false;
    }
  }
  array->data = data;
  array->size = size;
  array->capacity = size;
  return true;
}

void
interface_protocol__msg__JointMotionPlanRequest__Sequence__fini(interface_protocol__msg__JointMotionPlanRequest__Sequence * array)
{
  if (!array) {
    return;
  }
  rcutils_allocator_t allocator = rcutils_get_default_allocator();

  if (array->data) {
    // ensure that data and capacity values are consistent
    assert(array->capacity > 0);
    // finalize all array elements
    for (size_t i = 0; i < array->capacity; ++i) {
      interface_protocol__msg__JointMotionPlanRequest__fini(&array->data[i]);
    }
    allocator.deallocate(array->data, allocator.state);
    array->data = NULL;
    array->size = 0;
    array->capacity = 0;
  } else {
    // ensure that data, size, and capacity values are consistent
    assert(0 == array->size);
    assert(0 == array->capacity);
  }
}

interface_protocol__msg__JointMotionPlanRequest__Sequence *
interface_protocol__msg__JointMotionPlanRequest__Sequence__create(size_t size)
{
  rcutils_allocator_t allocator = rcutils_get_default_allocator();
  interface_protocol__msg__JointMotionPlanRequest__Sequence * array = (interface_protocol__msg__JointMotionPlanRequest__Sequence *)allocator.allocate(sizeof(interface_protocol__msg__JointMotionPlanRequest__Sequence), allocator.state);
  if (!array) {
    return NULL;
  }
  bool success = interface_protocol__msg__JointMotionPlanRequest__Sequence__init(array, size);
  if (!success) {
    allocator.deallocate(array, allocator.state);
    return NULL;
  }
  return array;
}

void
interface_protocol__msg__JointMotionPlanRequest__Sequence__destroy(interface_protocol__msg__JointMotionPlanRequest__Sequence * array)
{
  rcutils_allocator_t allocator = rcutils_get_default_allocator();
  if (array) {
    interface_protocol__msg__JointMotionPlanRequest__Sequence__fini(array);
  }
  allocator.deallocate(array, allocator.state);
}

bool
interface_protocol__msg__JointMotionPlanRequest__Sequence__are_equal(const interface_protocol__msg__JointMotionPlanRequest__Sequence * lhs, const interface_protocol__msg__JointMotionPlanRequest__Sequence * rhs)
{
  if (!lhs || !rhs) {
    return false;
  }
  if (lhs->size != rhs->size) {
    return false;
  }
  for (size_t i = 0; i < lhs->size; ++i) {
    if (!interface_protocol__msg__JointMotionPlanRequest__are_equal(&(lhs->data[i]), &(rhs->data[i]))) {
      return false;
    }
  }
  return true;
}

bool
interface_protocol__msg__JointMotionPlanRequest__Sequence__copy(
  const interface_protocol__msg__JointMotionPlanRequest__Sequence * input,
  interface_protocol__msg__JointMotionPlanRequest__Sequence * output)
{
  if (!input || !output) {
    return false;
  }
  if (output->capacity < input->size) {
    const size_t allocation_size =
      input->size * sizeof(interface_protocol__msg__JointMotionPlanRequest);
    rcutils_allocator_t allocator = rcutils_get_default_allocator();
    interface_protocol__msg__JointMotionPlanRequest * data =
      (interface_protocol__msg__JointMotionPlanRequest *)allocator.reallocate(
      output->data, allocation_size, allocator.state);
    if (!data) {
      return false;
    }
    // If reallocation succeeded, memory may or may not have been moved
    // to fulfill the allocation request, invalidating output->data.
    output->data = data;
    for (size_t i = output->capacity; i < input->size; ++i) {
      if (!interface_protocol__msg__JointMotionPlanRequest__init(&output->data[i])) {
        // If initialization of any new item fails, roll back
        // all previously initialized items. Existing items
        // in output are to be left unmodified.
        for (; i-- > output->capacity; ) {
          interface_protocol__msg__JointMotionPlanRequest__fini(&output->data[i]);
        }
        return false;
      }
    }
    output->capacity = input->size;
  }
  output->size = input->size;
  for (size_t i = 0; i < input->size; ++i) {
    if (!interface_protocol__msg__JointMotionPlanRequest__copy(
        &(input->data[i]), &(output->data[i])))
    {
      return false;
    }
  }
  return true;
}
