NTA_Size staticWriteBufferGetSize(NTA_WriteBufferHandle handle) { NTA_CHECK(handle != NULL); WriteBuffer * wb = reinterpret_cast<WriteBuffer *>(handle); return wb->getSize(); }
void RegionImpl::getParameterArray(const std::string& name, Int64 index, Array & array) { WriteBuffer wb; getParameterFromBuffer(name, index, wb); ReadBuffer rb(wb.getData(), wb.getSize(), false /* copy */); size_t count = array.getCount(); void *buffer = array.getBuffer(); for (size_t i = 0; i < count; i++) { int rc; switch (array.getType()) { case NTA_BasicType_Byte: rc = rb.read(((Byte*)buffer)[i]); break; case NTA_BasicType_Int32: rc = rb.read(((Int32*)buffer)[i]); break; case NTA_BasicType_UInt32: rc = rb.read(((UInt32*)buffer)[i]); break; case NTA_BasicType_Int64: rc = rb.read(((Int64*)buffer)[i]); break; case NTA_BasicType_UInt64: rc = rb.read(((UInt64*)buffer)[i]); break; case NTA_BasicType_Real32: rc = rb.read(((Real32*)buffer)[i]); break; case NTA_BasicType_Real64: rc = rb.read(((Real64*)buffer)[i]); break; default: NTA_THROW << "Unsupported basic type " << BasicType::getName(array.getType()) << " in getParameterArray for parameter " << name; break; } if (rc != 0) { NTA_THROW << "getParameterArray -- failure to get parameter '" << name << "' on node of type " << getType(); } } return; }
void BufferTest::testComplicatedSerialization() { struct X { X() : a((Real)3.4) , b(6) , c('c') , e((Real)-0.04) { for (int i = 0; i < 4; ++i) d[i] = 'A' + i; } Real a; UInt32 b; Byte c; Byte d[4]; Real e; }; X xi[2]; xi[0].a = (Real)8.8; xi[1].a = (Real)4.5; xi[1].c = 't'; xi[1].d[0] = 'X'; xi[1].e = (Real)3.14; // Write the two Xs to a buffer WriteBuffer wb; TEST2("BufferTest::testComplicatedSerialization(), empty WriteBuffer should have 0 size", wb.getSize() == 0); // Write the number of Xs UInt32 size = 2; wb.write((UInt32 &)size); // Write all Xs. for (UInt32 i = 0; i < size; ++i) { wb.write(xi[i].a); wb.write(xi[i].b); wb.write(xi[i].c); Size len = 4; wb.write((const Byte *)xi[i].d, len); wb.write(xi[i].e); } ReadBuffer rb(wb.getData(), wb.getSize()); // Read number of Xs rb.read(size); // Allocate array of Xs X * xo = new X[size]; for (Size i = 0; i < size; ++i) { rb.read(xo[i].a); rb.read(xo[i].b); rb.read(xo[i].c); Size size = 4; Int32 res = rb.read(xo[i].d, size); TEST2("BufferTest::testComplicatedSerialization(), rb.read(xi[i].d, 4) failed", res == 0); TEST2("BufferTest::testComplicatedSerialization(), rb.read(xi[i].d, 4) == 4", size == 4); rb.read(xo[i].e); NTA_INFO << "xo[" << i << "]={" << xo[i].a << " " << xo[i].b << " " << xo[i].c << " " << "'" << std::string(xo[i].d, 4) << "'" << " " << xo[i].e ; } TEST2("BufferTest::testComplicatedSerialization(), xo[0].a == 8.8", nearlyEqual(xo[0].a, nta::Real(8.8))); TEST2("BufferTest::testComplicatedSerialization(), xo[0].b == 6", xo[0].b == 6); TEST2("BufferTest::testComplicatedSerialization(), xo[0].c == 'c'", xo[0].c == 'c'); TEST2("BufferTest::testComplicatedSerialization(), xo[0].d == ABCD", std::string(xo[0].d, 4) == std::string("ABCD")); TEST2("BufferTest::testComplicatedSerialization(), xo[0].e == -0.04", nearlyEqual(xo[0].e, nta::Real(-0.04))); TEST2("BufferTest::testComplicatedSerialization(), xo[1].a == 4.5", xo[1].a == nta::Real(4.5)); TEST2("BufferTest::testComplicatedSerialization(), xo[1].b == 6", xo[1].b == 6); TEST2("BufferTest::testComplicatedSerialization(), xo[1].c == 't'", xo[1].c == 't'); TEST2("BufferTest::testComplicatedSerialization(), xo[1].d == XBCD", std::string(xo[1].d, 4) == std::string("XBCD")); TEST2("BufferTest::testComplicatedSerialization(), xo[1].e == 3.14", xo[1].e == nta::Real(3.14)); }
std::string RegionImpl::getParameterString(const std::string& name, Int64 index) { WriteBuffer wb; getParameterFromBuffer(name, index, wb); return std::string(wb.getData(), wb.getSize()); }