Class: Protobuf::Field::SignedIntegerField

Inherits:
VarintField show all
Defined in:
lib/protobuf/message/field.rb

Overview

Base class for sint32 and sint64

Direct Known Subclasses

Sint32Field, Sint64Field

Constant Summary

Constants inherited from VarintField

VarintField::INT32_MAX, VarintField::INT32_MIN, VarintField::INT64_MAX, VarintField::INT64_MIN, VarintField::UINT32_MAX, VarintField::UINT64_MAX

Instance Attribute Summary

Attributes inherited from BaseField

#default, #message_class, #name, #rule, #tag, #type

Instance Method Summary collapse

Methods inherited from VarintField

#acceptable?, decode, default, encode, #wire_type

Methods inherited from BaseField

#acceptable?, #clear, default, #default_value, descriptor, #descriptor, #error_message, #get, #initialize, #initialized?, #max, #merge, #merge_array, #merge_value, #min, #optional?, #ready?, #repeated?, #required?, #set, #to_s, #typed_default_value

Constructor Details

This class inherits a constructor from Protobuf::Field::BaseField

Instance Method Details

#decode(bytes) ⇒ Object



398
399
400
401
402
403
404
405
# File 'lib/protobuf/message/field.rb', line 398

def decode(bytes)
  value = VarintField.decode(bytes)
  if (value & 1).zero?
    value >> 1   # positive value
  else
    ~value >> 1  # negative value
  end
end

#encode(value) ⇒ Object



407
408
409
410
411
412
413
# File 'lib/protobuf/message/field.rb', line 407

def encode(value)
  if value >= 0
    VarintField.encode(value << 1)
  else
    VarintField.encode(~(value << 1))
  end
end