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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <boost/algorithm/string.hpp>
23 #include "vom/types.hpp"
27 rc_t::rc_t(int v, const std::string s)
28 : enum_base<rc_t>(v, s)
36 rc_t::from_vpp_retval(int32_t rv)
42 // interface laready exists
46 return (rc_t::INVALID);
49 const rc_t rc_t::UNSET(0, "un-set");
50 const rc_t rc_t::NOOP(1, "no-op");
51 const rc_t rc_t::OK(2, "ok");
52 const rc_t rc_t::INVALID(3, "invalid");
53 const rc_t rc_t::TIMEOUT(4, "timeout");
55 const handle_t handle_t::INVALID(~0);
57 handle_t::handle_t(int value)
68 handle_t::to_string() const
70 return (std::to_string(m_value));
74 handle_t::operator==(const handle_t& other) const
76 return (m_value == other.m_value);
80 handle_t::operator!=(const handle_t& other) const
82 return (!(*this == other));
86 handle_t::operator<(const handle_t& other) const
88 return (m_value < other.m_value);
92 handle_t::value() const
98 operator<<(std::ostream& os, const handle_t& h)
105 mac_address_t::mac_address_t(uint8_t b[6])
107 std::copy(b, b + 6, std::begin(bytes));
110 mac_address_t::mac_address_t(std::initializer_list<uint8_t> i)
112 std::copy(i.begin(), i.end(), std::begin(bytes));
115 mac_address_t::mac_address_t(const std::string& str)
117 std::vector<std::string> parts;
119 boost::split(parts, str, boost::is_any_of(":"));
121 size_t n_bytes = std::min(bytes.size(), parts.size());
123 for (uint32_t ii = 0; ii < n_bytes; ii++) {
124 bytes[ii] = std::stoul(parts[ii], nullptr, 16);
128 const mac_address_t mac_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
130 const mac_address_t mac_address_t::ZERO({ 0x0 });
133 mac_address_t::to_bytes(uint8_t* array, uint8_t len) const
135 for (int i = 0; i < 6 && i < len; i++) {
141 mac_address_t::to_string() const
143 std::ostringstream s;
148 for (auto byte : bytes) {
153 s << std::setw(2) << static_cast<unsigned int>(byte);
160 mac_address_t::operator==(const mac_address_t& mac) const
162 return (bytes == mac.bytes);
165 mac_address_t::operator<(const mac_address_t& m) const
167 return (bytes < m.bytes);
171 operator<<(std::ostream& os, const mac_address_t& mac)
173 os << mac.to_string();
178 l2_address_t::l2_address_t(const uint8_t b[8], uint8_t n_bytes)
181 std::copy_n(b, n_bytes, std::begin(bytes));
184 l2_address_t::l2_address_t(std::initializer_list<uint8_t> i)
189 l2_address_t::l2_address_t(const mac_address_t& mac)
192 std::copy(begin(mac.bytes), std::end(mac.bytes), std::begin(bytes));
195 const l2_address_t l2_address_t::ONE({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
198 const l2_address_t l2_address_t::ZERO({ 0x0 });
201 l2_address_t::to_bytes(uint8_t* array, uint8_t len) const
203 for (uint8_t i = 0; i < bytes.size() && i < len; i++) {
209 l2_address_t::to_mac() const
211 mac_address_t mac({});
213 std::copy_n(bytes.begin(), mac.bytes.size(), mac.bytes.begin());
219 l2_address_t::to_string() const
221 std::ostringstream s;
226 for (auto byte : bytes) {
231 s << std::setw(2) << static_cast<unsigned int>(byte);
238 l2_address_t::operator==(const l2_address_t& l2) const
240 return (bytes == l2.bytes);
244 l2_address_t::operator!=(const l2_address_t& l2) const
246 return (bytes != l2.bytes);
250 operator<<(std::ostream& os, const l2_address_t& l2)
252 os << l2.to_string();
257 const direction_t direction_t::INPUT(1, "input");
258 const direction_t direction_t::OUTPUT(0, "output");
260 direction_t::direction_t(int v, const std::string s)
265 operator<<(std::ostream& os, const direction_t& dir)
267 os << dir.to_string();
274 * fd.io coding-style-patch-verification: ON
277 * eval: (c-set-style "mozilla")