aidemingsi / pb4php

Automatically exported from code.google.com/p/pb4php
0 stars 0 forks source link

My implementation for type "double" | Patch #16

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
File "pb_double.php":
-------------------------
<?php
/**
 * @author Dmitry Vorobyev (http://dmitry.vorobyev.name)
 */
class PBDouble extends PBScalar
{
    var $wired_type = PBMessage::WIRED_64BIT;

    public function ParseFromArray()
    {
        $this->value = '';

        // just extract the string
        $pointer = $this->reader->get_pointer();
        $this->reader->add_pointer(8);
        $this->value = unpack('d', $this->reader->get_message_from($pointer));
    }

    /**
     * Serializes type
     */
    public function SerializeToString($rec=-1)
    {
        $string = '';
        if ($rec > -1)
        {
            $string .= $this->base128->set_value($rec << 3 |
$this->wired_type);
        }

        $string .= pack("d", (double)$this->value); 

        return $string;
    }
}
-------------------------

Don't forget to include this file in pb_message.php:
-------------------------
require_once(dirname(__FILE__). '/' . 'type/pb_double.php');
-------------------------

And specify this class as class to work with this type in pb_parser.php:
-------------------------
var $scalar_types = array(..., 'double' => 'PBDouble', 'float', 'int32' =>
'PBInt', 'int64' => 'PBInt',
-------------------------

Original issue reported on code.google.com by VoD...@gmail.com on 26 Apr 2010 at 12:25

GoogleCodeExporter commented 9 years ago
Thank you dude ... do you have the same kind of things for : floats, uint32, 
uint64, fixed 32, fixed64, sfixed32, sfixed64 ? :D

Original comment by vjoig...@gmail.com on 15 Apr 2013 at 1:22

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
use function unpack will return a array,so i use function array_shift bug it.
the same to the File "pb_double.php" i suggest.

Original comment by fort...@gmail.com on 20 Jun 2013 at 6:20

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
File "pb_float.php":
-------------------------
<?php
/**
 * @author forthxu (http://forthxu.com)
 */
class PBFloat extends PBScalar
{
    var $wired_type = PBMessage::WIRED_64BIT;

    public function ParseFromArray()
    {
        $this->value = '';

        // just extract the string
        $pointer = $this->reader->get_pointer();
        $this->reader->add_pointer(4);
        $www_forthxu_com = unpack('f', $this->reader->get_message_from($pointer));
        $this->value = array_shift($www_forthxu_com);
    }

    /**
     * Serializes type
     */
    public function SerializeToString($rec=-1)
    {
        $string = '';
        if ($rec > -1)
        {
            $string .= $this->base128->set_value($rec << 3 |
$this->wired_type);
        }

        $string .= pack("f", (double)$this->value); 

        return $string;
    }
}
-------------------------

Don't forget to include this file in pb_message.php:
-------------------------
require_once(dirname(__FILE__). '/' . 'type/pb_float.php');
-------------------------

And specify this class as class to work with this type in pb_parser.php:
-------------------------
var $scalar_types = array(..., 'double', 'float' => 'PBFloat', 'int32' =>
'PBInt', 'int64' => 'PBInt',
-------------------------

Original comment by fort...@gmail.com on 20 Jun 2013 at 6:32

GoogleCodeExporter commented 9 years ago
Beware, if you want to use both pbDouble and pbFloat, the related pbFloat 
weird_type is WIRED_32BIT.

Original comment by Baptiste...@gmail.com on 20 Aug 2013 at 7:41