libki/include/ki/protocol/dml/MessageBuilder.h

43 lines
882 B
C
Raw Normal View History

2018-04-03 15:11:06 +00:00
#pragma once
#include "Message.h"
2018-04-06 03:24:10 +00:00
#include "ki/protocol/exception.h"
2018-04-03 15:11:06 +00:00
#include <string>
2018-04-06 03:24:10 +00:00
#include <sstream>
2018-04-03 15:11:06 +00:00
namespace ki
{
namespace protocol
{
namespace dml
{
class MessageBuilder
{
public:
MessageBuilder(uint8_t service_id = 0, uint8_t type = 0);
MessageBuilder &set_service_id(uint8_t service_id);
MessageBuilder &set_message_type(uint8_t type);
MessageBuilder &use_template_record(const ki::dml::Record &record);
template <typename ValueT>
MessageBuilder &set_field_value(std::string name, ValueT value)
{
2018-04-06 03:24:10 +00:00
auto *field = m_message->get_record()->get_field<ValueT>(name);
2018-04-03 15:11:06 +00:00
if (!field)
{
2018-04-06 03:24:10 +00:00
std::ostringstream oss;
oss << "No field with name " << name << " exists with specified type.";
throw value_error(oss.str());
2018-04-03 15:11:06 +00:00
}
field->set_value(value);
return *this;
}
Message *get_message() const;
private:
Message *m_message;
};
}
}
2018-04-06 03:24:10 +00:00
}