mirror of https://github.com/SeanOMik/libki.git
Just a bunch of small fixes
This commit is contained in:
parent
cb27ab578c
commit
6b119112fc
|
@ -36,6 +36,7 @@ namespace dml
|
|||
|
||||
void write_to(std::ostream &ostream) const override final;
|
||||
void read_from(std::istream &istream) override final;
|
||||
void read_from(std::istream& istream, size_t bit_count = sizeof(ValueT));
|
||||
size_t get_size() const override final;
|
||||
|
||||
/**
|
||||
|
@ -121,6 +122,7 @@ namespace dml
|
|||
void set_value_from_string(std::string value) override final;
|
||||
private:
|
||||
ValueT m_value;
|
||||
size_t m_bit_count = 0;
|
||||
|
||||
/**
|
||||
* Returns a new Field with the same name, transferability,
|
||||
|
|
|
@ -97,11 +97,33 @@ namespace dml
|
|||
return field;
|
||||
}
|
||||
|
||||
template <typename ValueT>
|
||||
Field<ValueT>* add_field(std::string name, size_t bit_count, bool transferable = true) {
|
||||
// Does this field already exist?
|
||||
if (has_field(name)) {
|
||||
// Return nullptr if the type is not the same
|
||||
auto* field = m_field_map.at(name);
|
||||
if (!field->is_type<ValueT>())
|
||||
return nullptr;
|
||||
return dynamic_cast<Field<ValueT>*>(field);
|
||||
}
|
||||
|
||||
// Create the field
|
||||
auto* field = new Field<ValueT>(name);
|
||||
field->m_transferable = transferable;
|
||||
field->m_bit_count = bit_count;
|
||||
add_field(field);
|
||||
return field;
|
||||
}
|
||||
|
||||
size_t get_field_count() const;
|
||||
|
||||
FieldList::const_iterator fields_begin() const;
|
||||
FieldList::const_iterator fields_end() const;
|
||||
|
||||
FieldList::iterator fields_begin();
|
||||
FieldList::iterator fields_end();
|
||||
|
||||
void write_to(std::ostream &ostream) const override final;
|
||||
void read_from(std::istream &istream) override final;
|
||||
size_t get_size() const override final;
|
||||
|
|
|
@ -36,6 +36,19 @@ namespace dml
|
|||
void write_to(std::ostream &ostream) const override final;
|
||||
void read_from(std::istream &istream) override final;
|
||||
size_t get_size() const override final;
|
||||
|
||||
void set_raw_data(const std::vector<char>& data);
|
||||
const std::vector<char> get_raw_data() const {
|
||||
return m_raw_data;
|
||||
}
|
||||
|
||||
void set_header(MessageHeader header) {
|
||||
m_header = header;
|
||||
}
|
||||
|
||||
void set_record(ki::dml::Record* record) {
|
||||
m_record = record;
|
||||
}
|
||||
private:
|
||||
const MessageTemplate *m_template;
|
||||
ki::dml::Record *m_record;
|
||||
|
|
|
@ -36,7 +36,7 @@ namespace dml
|
|||
void set_record(ki::dml::Record *record);
|
||||
|
||||
Message *create_message() const;
|
||||
private:
|
||||
protected:
|
||||
std::string m_name;
|
||||
uint8_t m_type;
|
||||
uint8_t m_service_id;
|
||||
|
|
|
@ -59,6 +59,14 @@ namespace dml
|
|||
return m_fields.end();
|
||||
}
|
||||
|
||||
FieldList::iterator Record::fields_begin() {
|
||||
return m_fields.begin();
|
||||
}
|
||||
|
||||
FieldList::iterator Record::fields_end() {
|
||||
return m_fields.end();
|
||||
}
|
||||
|
||||
void Record::write_to(std::ostream &ostream) const
|
||||
{
|
||||
for (auto it = m_fields.begin(); it != m_fields.end(); ++it)
|
||||
|
|
|
@ -16,8 +16,10 @@ namespace dml
|
|||
template <>
|
||||
void BytField::read_from(std::istream &istream)
|
||||
{
|
||||
size_t bit_count = (m_bit_count != 0) ? m_bit_count : sizeof(BYT);
|
||||
|
||||
ValueBytes<BYT> data;
|
||||
istream.read(data.buff, sizeof(BYT));
|
||||
istream.read(data.buff, bit_count);
|
||||
if (istream.fail())
|
||||
{
|
||||
std::ostringstream oss;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include "..\..\..\include\ki\protocol\dml\Message.h"
|
||||
#include "ki/protocol/dml/Message.h"
|
||||
#include "ki/protocol/dml/MessageTemplate.h"
|
||||
#include "ki/protocol/exception.h"
|
||||
|
@ -135,7 +136,11 @@ namespace dml
|
|||
|
||||
void Message::read_from(std::istream &istream)
|
||||
{
|
||||
// Check if we need to read for the header
|
||||
if (m_header.get_size() == 0) {
|
||||
m_header.read_from(istream);
|
||||
}
|
||||
|
||||
if (m_template)
|
||||
{
|
||||
// Check for mismatches between the header and template
|
||||
|
@ -168,6 +173,10 @@ namespace dml
|
|||
return m_header.get_size() + m_record->get_size();
|
||||
return 4 + m_raw_data.size();
|
||||
}
|
||||
|
||||
void Message::set_raw_data(const std::vector<char>& data) {
|
||||
m_raw_data = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -250,9 +250,10 @@ namespace dml
|
|||
|
||||
// Create a new Message from the template
|
||||
auto *message = new Message(message_template);
|
||||
message->set_header(header);
|
||||
try
|
||||
{
|
||||
message->get_record()->read_from(istream);
|
||||
message->read_from(istream);
|
||||
}
|
||||
catch (ki::dml::parse_error &e)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue