]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/php/lib/Base/TBase.php
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
23 namespace Thrift\Base
;
25 use Thrift\Type\TType
;
28 * Base class from which other Thrift structs extend. This is so that we can
29 * cut back on the size of the generated code which is turning out to have a
30 * nontrivial cost just to load thanks to the wondrously abysmal implementation
31 * of PHP. Note that code is intentionally duplicated in here to avoid making
32 * function calls for every field or member of a container..
36 public static $tmethod = array(
37 TType
::BOOL => 'Bool',
38 TType
::BYTE
=> 'Byte',
42 TType
::DOUBLE => 'Double',
43 TType
::STRING => 'String'
46 abstract public function read($input);
48 abstract public function write($output);
50 public function __construct($spec = null, $vals = null)
52 if (is_array($spec) && is_array($vals)) {
53 foreach ($spec as $fid => $fspec) {
55 if (isset($vals[$var])) {
56 $this->$var = $vals[$var];
62 public function __wakeup()
64 $this->__construct(get_object_vars($this));
67 private function _readMap(&$var, $spec, $input)
70 $ktype = $spec['ktype'];
71 $vtype = $spec['vtype'];
72 $kread = $vread = null;
73 if (isset(TBase
::$tmethod[$ktype])) {
74 $kread = 'read' . TBase
::$tmethod[$ktype];
76 $kspec = $spec['key'];
78 if (isset(TBase
::$tmethod[$vtype])) {
79 $vread = 'read' . TBase
::$tmethod[$vtype];
81 $vspec = $spec['val'];
84 $_ktype = $_vtype = $size = 0;
85 $xfer +
= $input->readMapBegin($_ktype, $_vtype, $size);
86 for ($i = 0; $i < $size; ++
$i) {
88 if ($kread !== null) {
89 $xfer +
= $input->$kread($key);
93 $class = $kspec['class'];
95 $xfer +
= $key->read($input);
98 $xfer +
= $this->_readMap($key, $kspec, $input);
101 $xfer +
= $this->_readList($key, $kspec, $input, false);
104 $xfer +
= $this->_readList($key, $kspec, $input, true);
108 if ($vread !== null) {
109 $xfer +
= $input->$vread($val);
113 $class = $vspec['class'];
115 $xfer +
= $val->read($input);
118 $xfer +
= $this->_readMap($val, $vspec, $input);
121 $xfer +
= $this->_readList($val, $vspec, $input, false);
124 $xfer +
= $this->_readList($val, $vspec, $input, true);
130 $xfer +
= $input->readMapEnd();
135 private function _readList(&$var, $spec, $input, $set = false)
138 $etype = $spec['etype'];
139 $eread = $vread = null;
140 if (isset(TBase
::$tmethod[$etype])) {
141 $eread = 'read' . TBase
::$tmethod[$etype];
143 $espec = $spec['elem'];
148 $xfer +
= $input->readSetBegin($_etype, $size);
150 $xfer +
= $input->readListBegin($_etype, $size);
152 for ($i = 0; $i < $size; ++
$i) {
154 if ($eread !== null) {
155 $xfer +
= $input->$eread($elem);
157 $espec = $spec['elem'];
160 $class = $espec['class'];
161 $elem = new $class();
162 $xfer +
= $elem->read($input);
165 $xfer +
= $this->_readMap($elem, $espec, $input);
168 $xfer +
= $this->_readList($elem, $espec, $input, false);
171 $xfer +
= $this->_readList($elem, $espec, $input, true);
182 $xfer +
= $input->readSetEnd();
184 $xfer +
= $input->readListEnd();
190 protected function _read($class, $spec, $input)
196 $xfer +
= $input->readStructBegin($fname);
198 $xfer +
= $input->readFieldBegin($fname, $ftype, $fid);
199 if ($ftype == TType
::STOP
) {
202 if (isset($spec[$fid])) {
203 $fspec = $spec[$fid];
204 $var = $fspec['var'];
205 if ($ftype == $fspec['type']) {
207 if (isset(TBase
::$tmethod[$ftype])) {
208 $func = 'read' . TBase
::$tmethod[$ftype];
209 $xfer +
= $input->$func($this->$var);
213 $class = $fspec['class'];
214 $this->$var = new $class();
215 $xfer +
= $this->$var->read($input);
218 $xfer +
= $this->_readMap($this->$var, $fspec, $input);
221 $xfer +
= $this->_readList($this->$var, $fspec, $input, false);
224 $xfer +
= $this->_readList($this->$var, $fspec, $input, true);
229 $xfer +
= $input->skip($ftype);
232 $xfer +
= $input->skip($ftype);
234 $xfer +
= $input->readFieldEnd();
236 $xfer +
= $input->readStructEnd();
241 private function _writeMap($var, $spec, $output)
244 $ktype = $spec['ktype'];
245 $vtype = $spec['vtype'];
246 $kwrite = $vwrite = null;
247 if (isset(TBase
::$tmethod[$ktype])) {
248 $kwrite = 'write' . TBase
::$tmethod[$ktype];
250 $kspec = $spec['key'];
252 if (isset(TBase
::$tmethod[$vtype])) {
253 $vwrite = 'write' . TBase
::$tmethod[$vtype];
255 $vspec = $spec['val'];
257 $xfer +
= $output->writeMapBegin($ktype, $vtype, count($var));
258 foreach ($var as $key => $val) {
259 if (isset($kwrite)) {
260 $xfer +
= $output->$kwrite($key);
264 $xfer +
= $key->write($output);
267 $xfer +
= $this->_writeMap($key, $kspec, $output);
270 $xfer +
= $this->_writeList($key, $kspec, $output, false);
273 $xfer +
= $this->_writeList($key, $kspec, $output, true);
277 if (isset($vwrite)) {
278 $xfer +
= $output->$vwrite($val);
282 $xfer +
= $val->write($output);
285 $xfer +
= $this->_writeMap($val, $vspec, $output);
288 $xfer +
= $this->_writeList($val, $vspec, $output, false);
291 $xfer +
= $this->_writeList($val, $vspec, $output, true);
296 $xfer +
= $output->writeMapEnd();
301 private function _writeList($var, $spec, $output, $set = false)
304 $etype = $spec['etype'];
306 if (isset(TBase
::$tmethod[$etype])) {
307 $ewrite = 'write' . TBase
::$tmethod[$etype];
309 $espec = $spec['elem'];
312 $xfer +
= $output->writeSetBegin($etype, count($var));
314 $xfer +
= $output->writeListBegin($etype, count($var));
316 foreach ($var as $key => $val) {
317 $elem = $set ?
$key : $val;
318 if (isset($ewrite)) {
319 $xfer +
= $output->$ewrite($elem);
323 $xfer +
= $elem->write($output);
326 $xfer +
= $this->_writeMap($elem, $espec, $output);
329 $xfer +
= $this->_writeList($elem, $espec, $output, false);
332 $xfer +
= $this->_writeList($elem, $espec, $output, true);
338 $xfer +
= $output->writeSetEnd();
340 $xfer +
= $output->writeListEnd();
346 protected function _write($class, $spec, $output)
349 $xfer +
= $output->writeStructBegin($class);
350 foreach ($spec as $fid => $fspec) {
351 $var = $fspec['var'];
352 if ($this->$var !== null) {
353 $ftype = $fspec['type'];
354 $xfer +
= $output->writeFieldBegin($var, $ftype, $fid);
355 if (isset(TBase
::$tmethod[$ftype])) {
356 $func = 'write' . TBase
::$tmethod[$ftype];
357 $xfer +
= $output->$func($this->$var);
361 $xfer +
= $this->$var->write($output);
364 $xfer +
= $this->_writeMap($this->$var, $fspec, $output);
367 $xfer +
= $this->_writeList($this->$var, $fspec, $output, false);
370 $xfer +
= $this->_writeList($this->$var, $fspec, $output, true);
374 $xfer +
= $output->writeFieldEnd();
377 $xfer +
= $output->writeFieldStop();
378 $xfer +
= $output->writeStructEnd();