2
0

util.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #ifndef NMRPFLASH_UTIL_H
  2. #define NMRPFLASH_UTIL_H
  3. #include <boost/algorithm/string.hpp>
  4. #include <boost/range/adaptors.hpp>
  5. #include <boost/lexical_cast.hpp>
  6. #include <gsl/gsl>
  7. #include <cerrno>
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <cstdint>
  11. #include <sstream>
  12. #include <array>
  13. #if BOOST_OS_MACOS
  14. #include <CoreFoundation/CoreFoundation.h>
  15. #endif
  16. namespace nmrpflash {
  17. namespace detail {
  18. // Apple's clang doesn't yet support std::to_array. This is taken from
  19. // https://en.cppreference.com/w/cpp/container/array/to_array
  20. template <class T, std::size_t N, std::size_t... I>
  21. constexpr std::array<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...>)
  22. {
  23. return {{ a[I]... }};
  24. }
  25. }
  26. template <class T, std::size_t N>
  27. constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N])
  28. {
  29. return detail::to_array_impl(a, std::make_index_sequence<N>{});
  30. }
  31. template<typename T> std::string stringify(const T& t)
  32. {
  33. return boost::lexical_cast<std::string>(t);
  34. }
  35. template<typename T> auto as_bytes(const T& t)
  36. {
  37. return gsl::span<const uint8_t, sizeof(T)>(reinterpret_cast<const uint8_t*>(&t), sizeof(T));
  38. }
  39. template<typename T> std::string to_hex(const T& t)
  40. {
  41. std::ostringstream ostr;
  42. for (auto b : as_bytes(t)) {
  43. ostr << std::setw(2) << std::setfill('0') << std::hex << int(b);
  44. }
  45. return ostr.str();
  46. }
  47. template<typename To, typename From> To ugly_c_cast(From from)
  48. {
  49. return (To)from;
  50. }
  51. class errno_error : public std::system_error
  52. {
  53. errno_error(const std::string& msg, int val = errno)
  54. : std::system_error(val, std::system_category(), msg)
  55. {}
  56. };
  57. #if BOOST_OS_LINUX
  58. bool nm_is_managed(const std::string& dev);
  59. void nm_set_managed(const std::string& dev, bool managed);
  60. std::string nm_get_connection(const std::string& dev);
  61. struct nm_unmanaged_scope
  62. {
  63. const std::string dev;
  64. const bool is_managed;
  65. nm_unmanaged_scope(const std::string& dev)
  66. : dev(dev), is_managed(nm_is_managed(dev))
  67. {
  68. if (is_managed) {
  69. nm_set_managed(dev, false);
  70. }
  71. }
  72. ~nm_unmanaged_scope()
  73. {
  74. if (is_managed) {
  75. nm_set_managed(dev, true);
  76. }
  77. }
  78. };
  79. #elif BOOST_OS_WINDOWS
  80. class winapi_error : public std::system_error
  81. {
  82. winapi_error(const std::string& msg, int val = GetLastError())
  83. : std::system_error(val, std::system_category(), msg)
  84. {}
  85. };
  86. #elif BOOST_OS_MACOS
  87. namespace detail {
  88. template<typename T> struct cf_type_id;
  89. template<> struct cf_type_id<CFDictionaryRef>
  90. {
  91. static constexpr bool check = true;
  92. static CFTypeID value() { return CFDictionaryGetTypeID(); }
  93. };
  94. template<> struct cf_type_id<CFStringRef>
  95. {
  96. static constexpr bool check = true;
  97. static CFTypeID value() { return CFStringGetTypeID(); }
  98. };
  99. template<> struct cf_type_id<CFTypeRef>
  100. {
  101. static constexpr bool check = false;
  102. };
  103. template<> struct cf_type_id<void*>
  104. {
  105. static constexpr bool check = false;
  106. };
  107. }
  108. std::string from_cf_string(const CFStringRef str);
  109. template<typename To, typename From> To cf_cast(From from)
  110. {
  111. if constexpr (detail::cf_type_id<To>::check) {
  112. auto from_type = CFGetTypeID(from);
  113. auto to_type = detail::cf_type_id<To>::value();
  114. if (from_type != to_type) {
  115. throw std::invalid_argument("cannot cast " + from_cf_string(CFCopyTypeIDDescription(from_type))
  116. + " to " + from_cf_string(CFCopyTypeIDDescription(to_type)));
  117. }
  118. }
  119. return reinterpret_cast<To>(from);
  120. }
  121. template<typename T> class cf_ref
  122. {
  123. public:
  124. typedef T element_type;
  125. cf_ref()
  126. : m_ref(nullptr), m_view(true)
  127. {}
  128. cf_ref(T ref, bool view)
  129. : cf_ref(ref, view, false)
  130. {}
  131. cf_ref(const cf_ref& other)
  132. : cf_ref(other.m_ref, other.m_view, true)
  133. {}
  134. cf_ref(cf_ref&& other)
  135. : m_ref(other.m_ref), m_view(other.m_view)
  136. {
  137. other.m_ref = nullptr;
  138. }
  139. ~cf_ref()
  140. {
  141. if (!m_view && m_ref) {
  142. CFRelease(m_ref);
  143. }
  144. }
  145. template<typename U> cf_ref<U> as() const
  146. {
  147. return cf_ref<U>(cf_cast<U>(m_ref), m_view).retain();
  148. }
  149. T get()
  150. {
  151. return const_cast<T>(std::as_const(*this).get());
  152. }
  153. const T get() const
  154. {
  155. if (m_ref) {
  156. return m_ref;
  157. }
  158. throw std::runtime_error("accessing NULL CFTypeRef");
  159. }
  160. const void** get_p() const
  161. {
  162. if (!m_view) {
  163. throw std::runtime_error("accessing pointer to non-view CFTypeRef");
  164. }
  165. return ugly_c_cast<const void**>(&m_ref);
  166. }
  167. cf_ref& retain()
  168. {
  169. if (m_ref && !m_view) {
  170. CFRetain(m_ref);
  171. }
  172. return *this;
  173. }
  174. explicit operator bool() const
  175. {
  176. return m_ref != nullptr;
  177. }
  178. private:
  179. cf_ref(T ref, bool view, bool copy)
  180. : m_ref(ref), m_view(view)
  181. {
  182. if (copy) {
  183. retain();
  184. }
  185. }
  186. T m_ref;
  187. bool m_view;
  188. };
  189. template<typename U> cf_ref<U> make_cf_ref(U ref)
  190. {
  191. return cf_ref<U>(ref, false);
  192. }
  193. template<typename U> cf_ref<U> make_cf_view(U ref)
  194. {
  195. return cf_ref<U>(ref, true);
  196. }
  197. inline std::string from_cf_string(const cf_ref<CFStringRef>& str)
  198. {
  199. return from_cf_string(str.get());
  200. }
  201. cf_ref<CFStringRef> to_cf_string(const std::string& str);
  202. #endif
  203. }
  204. #endif