Fix coverity warnings in VOM and VAPI
[vpp.git] / src / vpp-api / vom / types.cpp
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <algorithm>
17 #include <iomanip>
18 #include <iostream>
19 #include <sstream>
20
21 #include "vom/types.hpp"
22
23 namespace VOM {
24
25 rc_t::rc_t(int v, const std::string s)
26   : enum_base<rc_t>(v, s)
27 {
28 }
29 rc_t::~rc_t()
30 {
31 }
32
33 const rc_t&
34 rc_t::from_vpp_retval(int32_t rv)
35 {
36   if (0 == rv) {
37     return (rc_t::OK);
38   }
39   if (-68 == rv) {
40     // interface laready exists
41     return (rc_t::OK);
42   }
43
44   return (rc_t::INVALID);
45 }
46
47 const rc_t rc_t::UNSET(0, "un-set");
48 const rc_t rc_t::NOOP(1, "no-op");
49 const rc_t rc_t::OK(2, "ok");
50 const rc_t rc_t::INPROGRESS(3, "in-progess");
51 const rc_t rc_t::INVALID(4, "invalid");
52 const rc_t rc_t::TIMEOUT(5, "timeout");
53
54 const handle_t handle_t::INVALID(~0);
55
56 handle_t::handle_t(int value)
57   : m_value(value)
58 {
59 }
60
61 handle_t::handle_t()
62   : m_value(~0)
63 {
64 }
65
66 std::string
67 handle_t::to_string() const
68 {
69   return (std::to_string(m_value));
70 }
71
72 bool
73 handle_t::operator==(const handle_t& other) const
74 {
75   return (m_value == other.m_value);
76 }
77
78 bool
79 handle_t::operator!=(const handle_t& other) const
80 {
81   return (!(*this == other));
82 }
83
84 bool
85 handle_t::operator<(const handle_t& other) const
86 {
87   return (m_value < other.m_value);
88 }
89
90 uint32_t
91 handle_t::value() const
92 {
93   return (m_value);
94 }
95
96 std::ostream&
97 operator<<(std::ostream& os, const handle_t& h)
98 {
99   os << h.value();
100
101   return (os);
102 }
103
104 mac_address_t::mac_address_t(uint8_t b[6])
105 {
106   std::copy(b, b + 6, std::begin(bytes));
107 }
108
109 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
110 {
111   std::copy(i.begin(), i.end(), std::begin(bytes));
112 }
113
114 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
115
116 const mac_address_t mac_address_t::ZERO({ 0x0 });
117
118 void
119 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
120 {
121   for (int i = 0; i < 6 && i < len; i++) {
122     array[i] = bytes[i];
123   }
124 }
125
126 std::string
127 mac_address_t::to_string() const
128 {
129   std::ostringstream s;
130   bool first = true;
131
132   s.fill('0');
133   s << std::hex;
134   s << "mac:[";
135   for (auto byte : bytes) {
136     if (first)
137       first = false;
138     else
139       s << ":";
140     s << std::setw(2) << static_cast<unsigned int>(byte);
141   }
142   s << "]";
143
144   return (s.str());
145 }
146
147 bool
148 mac_address_t::operator==(const mac_address_t& mac) const
149 {
150   return (bytes == mac.bytes);
151 }
152 bool
153 mac_address_t::operator<(const mac_address_t& m) const
154 {
155   return (bytes < m.bytes);
156 }
157
158 std::ostream&
159 operator<<(std::ostream& os, const mac_address_t& mac)
160 {
161   os << mac.to_string();
162
163   return (os);
164 }
165
166 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
167   : bytes(n_bytes)
168 {
169   std::copy_n(b, n_bytes, std::begin(bytes));
170 }
171
172 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
173   : bytes(i)
174 {
175 }
176
177 l2_address_t::l2_address_t(const mac_address_t& mac)
178   : bytes(6)
179 {
180   std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
181 }
182
183 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
184                                        0xff });
185
186 const l2_address_t l2_address_t::ZERO({ 0x0 });
187
188 void
189 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
190 {
191   for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
192     array[i] = bytes[i];
193   }
194 }
195
196 mac_address_t
197 l2_address_t::to_mac() const
198 {
199   mac_address_t mac({});
200
201   std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
202
203   return (mac);
204 }
205
206 std::string
207 l2_address_t::to_string() const
208 {
209   std::ostringstream s;
210   bool first = true;
211
212   s.fill('0');
213   s << std::hex;
214   for (auto byte : bytes) {
215     if (first)
216       first = false;
217     else
218       s << ":";
219     s << std::setw(2) << static_cast<unsigned int>(byte);
220   }
221
222   return (s.str());
223 }
224
225 bool
226 l2_address_t::operator==(const l2_address_t& l2) const
227 {
228   return (bytes == l2.bytes);
229 }
230
231 bool
232 l2_address_t::operator!=(const l2_address_t& l2) const
233 {
234   return (bytes != l2.bytes);
235 }
236
237 std::ostream&
238 operator<<(std::ostream& os, const l2_address_t& l2)
239 {
240   os << l2.to_string();
241
242   return (os);
243 }
244
245 const direction_t direction_t::INPUT(1, "input");
246 const direction_t direction_t::OUTPUT(0, "output");
247
248 direction_t::direction_t(int v, const std::string s)
249   : enum_base(v, s)
250 {
251 }
252 }
253
254 /*
255  * fd.io coding-style-patch-verification: ON
256  *
257  * Local Variables:
258  * eval: (c-set-style "mozilla")
259  * End:
260  */