1 // Copyright (C) 2018 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 #include "../include/selective_option_impl.hpp"
9 #include "../../message/include/deserializer.hpp"
10 #include "../../message/include/serializer.hpp"
11 
12 namespace vsomeip_v3 {
13 namespace sd {
14 
selective_option_impl()15 selective_option_impl::selective_option_impl() {
16     length_ = 1; // always contains "Reserved"
17     type_ = option_type_e::SELECTIVE;
18 }
19 
~selective_option_impl()20 selective_option_impl::~selective_option_impl() {
21 }
22 
23 bool
operator ==(const option_impl & _other) const24 selective_option_impl::operator ==(const option_impl &_other) const {
25     bool is_equal(option_impl::operator ==(_other));
26     if (is_equal) {
27         const selective_option_impl &its_other
28             = dynamic_cast<const selective_option_impl &>(_other);
29         is_equal = (clients_ == its_other.clients_);
30     }
31     return is_equal;
32 }
33 
get_clients() const34 std::set<client_t> selective_option_impl::get_clients() const {
35     std::set<client_t> its_clients(clients_);
36     return (its_clients);
37 }
38 
set_clients(const std::set<client_t> & _clients)39 void selective_option_impl::set_clients(const std::set<client_t> &_clients) {
40     clients_ = _clients;
41     length_ = uint16_t(1 + clients_.size() * sizeof(client_t));
42 }
43 
add_client(client_t _client)44 bool selective_option_impl::add_client(client_t _client) {
45     auto its_result = clients_.insert(_client);
46     length_ = uint16_t(1 + clients_.size() * sizeof(client_t));
47     return (its_result.second);
48 }
49 
remove_client(client_t _client)50 bool selective_option_impl::remove_client(client_t _client) {
51     auto its_size = clients_.size();
52     clients_.erase(_client);
53     length_ = uint16_t(1 + clients_.size() * sizeof(client_t));
54     return (clients_.size() < its_size);
55 }
56 
has_clients() const57 bool selective_option_impl::has_clients() const {
58     return (!clients_.empty());
59 }
60 
has_client(client_t _client)61 bool selective_option_impl::has_client(client_t _client) {
62     auto find_client = clients_.find(_client);
63     return (find_client != clients_.end());
64 }
65 
serialize(vsomeip_v3::serializer * _to) const66 bool selective_option_impl::serialize(vsomeip_v3::serializer *_to) const {
67     bool is_successful = option_impl::serialize(_to);
68     if (is_successful) {
69         for (auto &its_client : clients_)
70             _to->serialize(its_client);
71     }
72     return is_successful;
73 }
74 
deserialize(vsomeip_v3::deserializer * _from)75 bool selective_option_impl::deserialize(vsomeip_v3::deserializer *_from) {
76     bool is_successful = option_impl::deserialize(_from);
77     if (is_successful) {
78         uint16_t i = 1;
79         while (i < length_) {
80             client_t its_client;
81             is_successful = _from->deserialize(its_client);
82 
83             clients_.insert(its_client);
84             i = uint16_t(i + sizeof(client_t));
85         }
86     }
87     return is_successful;
88 }
89 
90 } // namespace sd
91 } // namespace vsomeip_v3
92