Program Listing for File single_motor_message.hpp

Return to documentation for file (include/myactuator_rmd/protocol/single_motor_message.hpp)

#ifndef MYACTUATOR_RMD__PROTOCOL__SINGLE_MOTOR_MESSAGE
#define MYACTUATOR_RMD__PROTOCOL__SINGLE_MOTOR_MESSAGE
#pragma once

#include <array>
#include <cstdint>
#include <ios>
#include <sstream>

#include "myactuator_rmd/protocol/command_type.hpp"
#include "myactuator_rmd/protocol/message.hpp"
#include "myactuator_rmd/exceptions.hpp"


namespace myactuator_rmd {

  template <CommandType C>
  class SingleMotorMessage: public Message {
    protected:
      constexpr SingleMotorMessage(std::array<std::uint8_t,8> const& data);
      constexpr SingleMotorMessage() noexcept;
      SingleMotorMessage(SingleMotorMessage const&) = default;
      SingleMotorMessage& operator = (SingleMotorMessage const&) = default;
      SingleMotorMessage(SingleMotorMessage&&) = default;
      SingleMotorMessage& operator = (SingleMotorMessage&&) = default;
  };

  template <CommandType C>
  constexpr SingleMotorMessage<C>::SingleMotorMessage(std::array<std::uint8_t,8> const& data)
  : Message{data} {
    if (data[0] != C) {
      std::stringstream ss {};
      ss << std::showbase << std::hex << static_cast<std::uint16_t>(data[0]);
      throw ProtocolException("Unexpected response '" + ss.str() + "'");
    }
    return;
  }

  template <CommandType C>
  constexpr SingleMotorMessage<C>::SingleMotorMessage() noexcept
  : Message{} {
    data_[0] = static_cast<std::uint8_t>(C);
    return;
  }

  template <CommandType C>
  class SingleMotorRequest: public SingleMotorMessage<C> {
    public:
      constexpr SingleMotorRequest(std::array<std::uint8_t,8> const& data);
      constexpr SingleMotorRequest() = default;
      SingleMotorRequest(SingleMotorRequest const&) = default;
      SingleMotorRequest& operator = (SingleMotorRequest const&) = default;
      SingleMotorRequest(SingleMotorRequest&&) = default;
      SingleMotorRequest& operator = (SingleMotorRequest&&) = default;
  };

  template <CommandType C>
  constexpr SingleMotorRequest<C>::SingleMotorRequest(std::array<std::uint8_t,8> const& data)
  : SingleMotorMessage<C>{data} {
    return;
  }

  template <CommandType C>
  class SingleMotorResponse: public SingleMotorMessage<C> {
    public:
      constexpr SingleMotorResponse(std::array<std::uint8_t,8> const& data);
      constexpr SingleMotorResponse() = delete;
      SingleMotorResponse(SingleMotorResponse const&) = default;
      SingleMotorResponse& operator = (SingleMotorResponse const&) = default;
      SingleMotorResponse(SingleMotorResponse&&) = default;
      SingleMotorResponse& operator = (SingleMotorResponse&&) = default;
  };

  template <CommandType C>
  constexpr SingleMotorResponse<C>::SingleMotorResponse(std::array<std::uint8_t,8> const& data)
  : SingleMotorMessage<C>{data} {
    return;
  }

}

#endif // MYACTUATOR_RMD__PROTOCOL__SINGLE_MOTOR_MESSAGE