mavtables  0.2.1
MAVLink router and firewall.
utility.hpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2017-2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #ifndef UTILITY_HPP_
19 #define UTILITY_HPP_
20 
21 
22 #include <array>
23 #include <iterator>
24 #include <ostream>
25 #include <sstream>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include <boost/range/irange.hpp>
31 
32 
33 template <typename T>
34 typename std::vector<T>::iterator append(
35  std::vector<T> &dest, const std::vector<T> &source);
36 
37 template <typename T>
38 typename std::vector<T>::iterator append(
39  std::vector<T> &dest, std::vector<T> &&source);
40 
41 template <class T>
42 std::string str(const T &object);
43 
44 template <class ByteType = unsigned char, class T>
45 std::array<ByteType, sizeof(T)> to_bytes(T number);
46 
47 std::string to_lower(std::string string);
48 
49 template <class T>
50 std::ostream &operator<<(std::ostream &os, const std::vector<T> &vector);
51 
52 
53 /** Append one vector to another.
54  *
55  * Taken from https://stackoverflow.com/a/37210097
56  *
57  * \ingroup utility
58  * \param dest Vector to append to.
59  * \param source Vector to append the elements from.
60  * \returns Iterator pointing to the first element appended, or the end of the
61  * destination vector if the source vector is empty.
62  */
63 template <typename T>
64 typename std::vector<T>::iterator append(
65  std::vector<T> &dest, const std::vector<T> &source)
66 {
67  typename std::vector<T>::iterator result;
68 
69  if (dest.empty())
70  {
71  dest = source;
72  result = std::begin(dest);
73  }
74  else
75  {
76  result =
77  dest.insert(std::end(dest), std::cbegin(source), std::cend(source));
78  }
79 
80  return result;
81 }
82 
83 
84 /** Append one vector to another (move from source).
85  *
86  * Taken from https://stackoverflow.com/a/37210097
87  *
88  * \ingroup utility
89  * \param dest Vector to append to.
90  * \param source Vector to append the elements from. \p source will be a valid
91  * empty vector after this call.
92  * \returns Iterator pointing to the first element appended, or the end of the
93  * destination vector if the source vector is empty.
94  */
95 template <typename T>
96 typename std::vector<T>::iterator append(
97  std::vector<T> &dest, std::vector<T> &&source)
98 {
99  typename std::vector<T>::iterator result;
100 
101  if (dest.empty())
102  {
103  dest = std::move(source);
104  result = std::begin(dest);
105  }
106  else
107  {
108  result = dest.insert(
109  std::end(dest),
110  std::make_move_iterator(std::begin(source)),
111  std::make_move_iterator(std::end(source)));
112  }
113 
114  source.clear();
115  source.shrink_to_fit();
116  return result;
117 }
118 
119 
120 /** Convert any object supporting the output stream operator (<<) to a string.
121  *
122  * \ingroup utility
123  * \tparam T Type of the object to convert to a string.
124  * \param object The object to convert to a string.
125  * \returns The string representing the object.
126  */
127 template <class T>
128 std::string str(const T &object)
129 {
130  std::ostringstream oss;
131  oss << object;
132  return oss.str();
133 }
134 
135 
136 /** Convert numeric types to bytes.
137  *
138  * \ingroup utility
139  * \tparam ByteType Numeric type to return in the array of bytes.
140  * \tparam T Type of the number being converted to bytes.
141  * \param number Number to convert to bytes
142  * \returns The array of bytes from the given number, in LSB order.
143  */
144 template <class ByteType, class T>
145 std::array<ByteType, sizeof(T)> to_bytes(T number)
146 {
147  std::array<ByteType, sizeof(T)> n;
148  n.fill(10);
149 
150  for (auto i : boost::irange(static_cast<size_t>(0), sizeof(T)))
151  {
152  n[i] = (number >> i * 8) & 0xFF;
153  }
154 
155  return n;
156 }
157 
158 
159 /** Print a vector to the given output stream.
160  *
161  * \ingroup utility
162  * \tparam T The type stored in the vector, it must support the << operator.
163  * \param os The output stream to print to.
164  * \param vector The vector of elements to print.
165  * \returns The output stream.
166  */
167 template <class T>
168 std::ostream &operator<<(std::ostream &os, const std::vector<T> &vector)
169 {
170  os << "[";
171 
172  if (vector.size() > 0)
173  {
174  auto it = vector.begin();
175  os << *(it++);
176 
177  for (; it != vector.end(); ++it)
178  {
179  os << ", " << *it;
180  }
181  }
182 
183  os << "]";
184  return os;
185 }
186 
187 
188 #endif // UTILITY_HPP_
std::string str(const T &object)
Definition: utility.hpp:128
std::string to_lower(std::string string)
Definition: utility.cpp:37
std::array< ByteType, sizeof(T)> to_bytes(T number)
Definition: utility.hpp:145
std::vector< T >::iterator append(std::vector< T > &dest, const std::vector< T > &source)
Definition: utility.hpp:64