From 99c3ca72f5a94eab25b5ea0df417682443fc63c3 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 29 Apr 2017 08:15:31 -0500 Subject: [PATCH] Add array support to MetalClaw --- src/ox/mc/read.hpp | 5 +++-- src/ox/mc/test/tests.cpp | 10 ++++++++++ src/ox/mc/write.hpp | 12 +++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/ox/mc/read.hpp b/src/ox/mc/read.hpp index 8a41923b6..12086a468 100644 --- a/src/ox/mc/read.hpp +++ b/src/ox/mc/read.hpp @@ -39,7 +39,7 @@ class MetalClawReader { int op(const char*, bool *val); template - int op(const char*, T **val, size_t len); + int op(const char*, T *val, size_t len); template int op(const char*, T *val); @@ -111,7 +111,7 @@ int MetalClawReader::readInteger(I *val) { }; template -int MetalClawReader::op(const char*, T **val, size_t valLen) { +int MetalClawReader::op(const char*, T *val, size_t valLen) { int err = 0; if (m_fieldPresence.get(m_field)) { // read the length @@ -131,6 +131,7 @@ int MetalClawReader::op(const char*, T **val, size_t valLen) { for (size_t i = 0; i < len; i++) { err |= reader.op("", &val[i]); } + m_buffIt += reader.m_buffIt; } else { err = MC_OUTBUFFENDED; } diff --git a/src/ox/mc/test/tests.cpp b/src/ox/mc/test/tests.cpp index 0139fe9b4..b4407517d 100644 --- a/src/ox/mc/test/tests.cpp +++ b/src/ox/mc/test/tests.cpp @@ -27,6 +27,7 @@ struct TestStruct { bool Bool = false; int32_t Int = 0; bstring<32> String = ""; + int List[4] = {0, 0, 0 , 0}; TestStructNest Struct; }; @@ -47,6 +48,7 @@ int ioOp(T *io, TestStruct *obj) { err |= io->op("Bool", &obj->Bool); err |= io->op("Int", &obj->Int); err |= io->op("String", &obj->String); + err |= io->op("List", obj->List, 4); err |= io->op("Struct", &obj->Struct); return err; } @@ -81,6 +83,10 @@ map tests = { testIn.Bool = true; testIn.Int = 42; testIn.String = "Test String 1"; + testIn.List[0] = 1; + testIn.List[1] = 2; + testIn.List[2] = 3; + testIn.List[3] = 4; testIn.Struct.Bool = false; testIn.Struct.Int = 300; testIn.Struct.String = "Test String 2"; @@ -91,6 +97,10 @@ map tests = { err |= !(testIn.Bool == testOut.Bool); err |= !(testIn.Int == testOut.Int); err |= !(testIn.String == testOut.String); + err |= !(testIn.List[0] == testOut.List[0]); + err |= !(testIn.List[1] == testOut.List[1]); + err |= !(testIn.List[2] == testOut.List[2]); + err |= !(testIn.List[3] == testOut.List[3]); err |= !(testIn.Struct.Bool == testOut.Struct.Bool); err |= !(testIn.Struct.Int == testOut.Struct.Int); err |= !(testIn.Struct.String == testOut.Struct.String); diff --git a/src/ox/mc/write.hpp b/src/ox/mc/write.hpp index c0c243b88..577a148e8 100644 --- a/src/ox/mc/write.hpp +++ b/src/ox/mc/write.hpp @@ -39,7 +39,7 @@ class MetalClawWriter { int op(const char*, bool *val); template - int op(const char*, T **val, size_t len); + int op(const char*, T *val, size_t len); template int op(const char*, ox::bstring *val); @@ -106,11 +106,9 @@ int MetalClawWriter::appendInteger(I val) { }; template -int MetalClawWriter::op(const char *fieldName, T **val, size_t len) { +int MetalClawWriter::op(const char*, T *val, size_t len) { int err = 0; bool fieldSet = false; - MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt); - writer.setFields(len); // write the length typedef uint32_t ArrayLength; @@ -121,11 +119,15 @@ int MetalClawWriter::op(const char *fieldName, T **val, size_t len) { err = MC_BUFFENDED; } + MetalClawWriter writer(m_buff + m_buffIt, m_buffLen - m_buffIt); + writer.setFields(len); + // write the string for (size_t i = 0; i < len; i++) { - err |= writer.op("", val[i]); + err |= writer.op("", &val[i]); } + m_buffIt += writer.m_buffIt; fieldSet = true; err |= m_fieldPresence.set(m_field, fieldSet);