0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Serializable.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #include <Common/Compat.h>
28 
29 #include "Serializable.h"
30 
31 #include <Common/Error.h>
32 #include <Common/Logger.h>
33 #include <Common/Serialization.h>
34 
35 using namespace Hypertable;
36 
38  size_t length = encoded_length_internal();
39  return 1 + Serialization::encoded_length_vi32(length) + length;
40 }
41 
64 void Serializable::encode(uint8_t **bufp) const {
67  encode_internal(bufp);
68 }
69 
70 void Serializable::decode(const uint8_t **bufp, size_t *remainp) {
71  uint8_t version = Serialization::decode_i8(bufp, remainp);
72  if (version > encoding_version())
73  HT_THROWF(Error::PROTOCOL_ERROR, "Unsupported version %d", (int)version);
74  size_t encoding_length = Serialization::decode_vi32(bufp, remainp);
75  const uint8_t *end = *bufp + encoding_length;
76  size_t tmp_remain = encoding_length;
77  decode_internal(version, bufp, &tmp_remain);
78  HT_ASSERT(*bufp <= end);
79  *remainp -= encoding_length;
80  // If encoding is longer than we expect, that means we're decoding a newer
81  // version, so skip the newer portion that we don't know about
82  if (*bufp < end)
83  *bufp = end;
84 }
virtual void decode_internal(uint8_t version, const uint8_t **bufp, size_t *remainp)=0
Reads serialized representation of object from a buffer.
virtual size_t encoded_length_internal() const =0
Returns internal serialized length.
virtual size_t encoded_length() const
Returns serialized object length.
Definition: Serializable.cc:37
#define HT_ASSERT(_e_)
Definition: Logger.h:396
uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Decode a 8-bit integer (a byte/character)
Definition: Serialization.h:60
int encoded_length_vi32(uint32_t val)
Length of a variable length encoded 32-bit integer (up to 5 bytes)
virtual void encode(uint8_t **bufp) const
Writes serialized representation of object to a buffer.
Definition: Serializable.cc:64
Logging routines and macros.
Compatibility Macros for C/C++.
Functions to serialize/deserialize primitives to/from a memory buffer.
virtual void decode(const uint8_t **bufp, size_t *remainp)
Reads serialized representation of object from a buffer.
Definition: Serializable.cc:70
Declarations for Serializable.
Hypertable definitions
void encode_vi32(uint8_t **bufp, uint32_t val)
Encode a integer (up to 32-bit) in variable length encoding.
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
virtual uint8_t encoding_version() const =0
Returns encoding version.
Error codes, Exception handling, error logging.
uint32_t decode_vi32(const uint8_t **bufp, size_t *remainp)
Decode a variable length encoded integer up to 32-bit.
virtual void encode_internal(uint8_t **bufp) const =0
Writes serialized representation of object to a buffer.
void encode_i8(uint8_t **bufp, uint8_t val)
Encodes a byte into the given buffer.
Definition: Serialization.h:49