• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 
6 //#include <cstring>
7 
8 #ifdef VSOMEIP_DEBUGGING
9 #include <iomanip>
10 #include <sstream>
11 #endif
12 
13 #include <vsomeip/internal/serializable.hpp>
14 
15 #include "../include/serializer.hpp"
16 #include "../../utility/include/byteorder.hpp"
17 #include <vsomeip/internal/logger.hpp>
18 
19 namespace vsomeip_v3 {
20 
serializer(std::uint32_t _buffer_shrink_threshold)21 serializer::serializer(std::uint32_t _buffer_shrink_threshold) :
22         data_(0),
23         shrink_count_(0),
24         buffer_shrink_threshold_(_buffer_shrink_threshold) {
25 }
26 
~serializer()27 serializer::~serializer() {
28 }
29 
serialize(const serializable * _from)30 bool serializer::serialize(const serializable *_from) {
31     return (_from && _from->serialize(this));
32 }
33 
serialize(const uint8_t _value)34 bool serializer::serialize(const uint8_t _value) {
35     data_.push_back(_value);
36     return true;
37 }
38 
serialize(const uint16_t _value)39 bool serializer::serialize(const uint16_t _value) {
40     data_.push_back(VSOMEIP_WORD_BYTE1(_value));
41     data_.push_back(VSOMEIP_WORD_BYTE0(_value));
42     return true;
43 }
44 
serialize(const uint32_t _value,bool _omit_last_byte)45 bool serializer::serialize(const uint32_t _value, bool _omit_last_byte) {
46     if (!_omit_last_byte) {
47         data_.push_back(VSOMEIP_LONG_BYTE3(_value));
48     }
49     data_.push_back(VSOMEIP_LONG_BYTE2(_value));
50     data_.push_back(VSOMEIP_LONG_BYTE1(_value));
51     data_.push_back(VSOMEIP_LONG_BYTE0(_value));
52     return true;
53 }
54 
serialize(const uint8_t * _data,uint32_t _length)55 bool serializer::serialize(const uint8_t *_data, uint32_t _length) {
56     try {
57         data_.insert(data_.end(), _data, _data + _length);
58     } catch(const std::bad_alloc &e) {
59         VSOMEIP_ERROR << "Couldn't allocate memory in serializer::serialize(*_data, length)" << e.what();
60         return false;
61     }
62     return true;
63 }
64 
serialize(const std::vector<byte_t> & _data)65 bool serializer::serialize(const std::vector<byte_t> &_data) {
66     try {
67         data_.insert(data_.end(),_data.begin(), _data.end());
68     } catch(const std::bad_alloc &e) {
69         VSOMEIP_ERROR << "Couldn't allocate memory in serializer::serialize(vector)" << e.what();
70         return false;
71     }
72     return true;
73 }
74 
get_data() const75 const byte_t * serializer::get_data() const {
76     return data_.data();
77 }
78 
get_capacity() const79 uint32_t serializer::get_capacity() const {
80     return static_cast<std::uint32_t>(data_.max_size());
81 }
82 
get_size() const83 uint32_t serializer::get_size() const {
84     return static_cast<std::uint32_t>(data_.size());
85 }
86 
set_data(byte_t * _data,uint32_t _capacity)87 void serializer::set_data(byte_t *_data, uint32_t _capacity) {
88     data_.clear();
89     try {
90         data_.insert(data_.end(), _data, _data + _capacity);
91     } catch(const std::bad_alloc &e) {
92         VSOMEIP_ERROR << "Couldn't allocate memory in serializer::set_data" << e.what();
93     }
94 }
95 
reset()96 void serializer::reset() {
97     if (buffer_shrink_threshold_) {
98         if (data_.size() < (data_.capacity() >> 1)) {
99             shrink_count_++;
100         } else {
101             shrink_count_ = 0;
102         }
103     }
104     data_.clear();
105     if (buffer_shrink_threshold_ && shrink_count_ > buffer_shrink_threshold_) {
106         data_.shrink_to_fit();
107         shrink_count_ = 0;
108     }
109 }
110 
111 #ifdef VSOMEIP_DEBUGGING
show()112 void serializer::show() {
113     std::stringstream its_data;
114     its_data << "SERIALIZED: ";
115     for (const byte_t& e : data_)
116         its_data << std::setw(2) << std::setfill('0')
117                  << std::hex << (int)e;
118     VSOMEIP_INFO << its_data.str();
119 }
120 #endif
121 
122 } // namespace vsomeip_v3
123