Commit 2f7f50b8 authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen

Merge pull request #3816 from armen/master

Implement __vector_as_bytes and methods to get [ubyte] efficiently
parents a1633055 28a3c939
...@@ -89,9 +89,15 @@ abstract class Table ...@@ -89,9 +89,15 @@ abstract class Table
return $offset + $this->bb->getInt($offset) + Constants::SIZEOF_INT; return $offset + $this->bb->getInt($offset) + Constants::SIZEOF_INT;
} }
// protected function __vector_as_bytebuffer($vector_offset, $elem_size) protected function __vector_as_bytes($vector_offset, $elem_size=1)
// { {
// } $o = $this->__offset($vector_offset);
if ($o == 0) {
return null;
}
return substr($this->bb->_buffer, $this->__vector($o), $this->__vector_len($o) * $elem_size);
}
/** /**
* @param Table $table * @param Table $table
......
...@@ -165,6 +165,22 @@ namespace php { ...@@ -165,6 +165,22 @@ namespace php {
code += Indent + "}\n\n"; code += Indent + "}\n\n";
} }
// Get a [ubyte] vector as a byte array.
static void GetUByte(const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
code += Indent + "/**\n";
code += Indent + " * @return string\n";
code += Indent + " */\n";
code += Indent + "public function get";
code += MakeCamel(field.name) + "Bytes()\n";
code += Indent + "{\n";
code += Indent + Indent + "return $this->__vector_as_bytes(";
code += NumToString(field.value.offset) + ");\n";
code += Indent + "}\n\n";
}
// Get the value of a struct's scalar. // Get the value of a struct's scalar.
static void GetScalarFieldOfStruct(const FieldDef &field, static void GetScalarFieldOfStruct(const FieldDef &field,
std::string *code_ptr) { std::string *code_ptr) {
...@@ -250,7 +266,7 @@ namespace php { ...@@ -250,7 +266,7 @@ namespace php {
");\n"; ");\n";
code += Indent + Indent; code += Indent + Indent;
code += "return $o != 0 ? $obj->init("; code += "return $o != 0 ? $obj->init(";
if (field.value.type.struct_def->fixed) if (field.value.type.struct_def->fixed)
{ {
code += "$o + $this->bb_pos, $this->bb) : "; code += "$o + $this->bb_pos, $this->bb) : ";
} else { } else {
...@@ -690,6 +706,9 @@ namespace php { ...@@ -690,6 +706,9 @@ namespace php {
} }
if (field.value.type.base_type == BASE_TYPE_VECTOR) { if (field.value.type.base_type == BASE_TYPE_VECTOR) {
GetVectorLen(field, code_ptr); GetVectorLen(field, code_ptr);
if (field.value.type.element == BASE_TYPE_UCHAR) {
GetUByte(field, code_ptr);
}
} }
} }
......
...@@ -98,6 +98,14 @@ class Monster extends Table ...@@ -98,6 +98,14 @@ class Monster extends Table
return $o != 0 ? $this->__vector_len($o) : 0; return $o != 0 ? $this->__vector_len($o) : 0;
} }
/**
* @return string
*/
public function getInventoryBytes()
{
return $this->__vector_as_bytes(14);
}
/** /**
* @return sbyte * @return sbyte
*/ */
...@@ -210,6 +218,14 @@ class Monster extends Table ...@@ -210,6 +218,14 @@ class Monster extends Table
return $o != 0 ? $this->__vector_len($o) : 0; return $o != 0 ? $this->__vector_len($o) : 0;
} }
/**
* @return string
*/
public function getTestnestedflatbufferBytes()
{
return $this->__vector_as_bytes(30);
}
public function getTestempty() public function getTestempty()
{ {
$obj = new Stat(); $obj = new Stat();
......
...@@ -128,6 +128,7 @@ function test_buffer(Assert $assert, Google\FlatBuffers\ByteBuffer $bb) { ...@@ -128,6 +128,7 @@ function test_buffer(Assert $assert, Google\FlatBuffers\ByteBuffer $bb) {
} }
$assert->strictEqual($invsum, 10); $assert->strictEqual($invsum, 10);
$assert->strictEqual(bin2hex($monster->GetInventoryBytes()), "0001020304");
$test_0 = $monster->GetTest4(0); $test_0 = $monster->GetTest4(0);
$test_1 = $monster->GetTest4(1); $test_1 = $monster->GetTest4(1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment