remotemono
util.h
1 /*
2  Copyright 2020 David "Alemarius Nexus" Lerch
3 
4  This file is part of RemoteMono.
5 
6  RemoteMono is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  RemoteMono is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with RemoteMono. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include "config.h"
23 
24 #include <stdint.h>
25 #include <string>
26 
27 
28 
29 namespace remotemono
30 {
31 
32 
33 
37 template <typename UIntT>
38 constexpr uint8_t static_ilog2(UIntT x)
39 {
40  // TODO: Check that it's an exact ilog2 (static_assert() didn't seem to work with constexpr on VS2019)
41  uint8_t res = 0;
42  while (x > 1) {
43  x >>= 1;
44  res++;
45  }
46  return res;
47 }
48 
56 template <typename UIntT>
57 constexpr UIntT static_align(UIntT x, size_t al)
58 {
59  return UIntT((x % al == 0) ? x : (x/al + 1) * al);
60 }
61 
62 
70 template <typename UIntT>
71 UIntT align(UIntT x, size_t al)
72 {
73  return UIntT((x % al == 0) ? x : (x/al + 1) * al);
74 }
75 
76 
80 template <typename T>
81 std::string qualified_type_name()
82 {
83  // Based on https://stackoverflow.com/a/20170989/1566841
84  typedef typename std::remove_reference_t<T> TR;
85 
86  std::string name(typeid(TR).name());
87 
88  if constexpr(std::is_const_v<TR>) {
89  name += " const";
90  }
91  if constexpr(std::is_volatile_v<TR>) {
92  name += " volatile";
93  }
94  if constexpr(std::is_lvalue_reference_v<T>) {
95  name += "&";
96  }
97  if constexpr(std::is_rvalue_reference_v<T>) {
98  name += "&&";
99  }
100  return name;
101 }
102 
103 
104 template <typename T>
105 inline void hash_combine(size_t& s, const T& v)
106 {
107  // Taken from Boost
108  s ^= std::hash<T>()(v) + 0x9e3779b9 + (s << 6) + (s >> 2);
109 }
110 
111 
115 inline std::string DumpByteArray(const char* data, size_t size)
116 {
117  if (size == 0) {
118  return std::string();
119  }
120 
121  const char* hexDigits = "0123456789ABCDEF";
122 
123  const uint8_t* udata = (const uint8_t*) data;
124  std::string dump;
125 
126  dump.append({hexDigits[udata[0]/16], hexDigits[udata[0]%16]});
127 
128  for (size_t i = 1 ; i < size ; i++) {
129  unsigned char c = udata[i];
130  dump.append({' ', hexDigits[c/16], hexDigits[c%16]});
131  }
132 
133  return dump;
134 }
135 
136 
137 
138 
139 
140 
141 
142 
146 template <typename... TypeT>
147 struct PackHelper {};
148 
149 
153 template <typename TypeT>
154 struct Identity
155 {
156  typedef TypeT Type;
157 };
158 
159 
160 
161 }
remotemono::Identity
Definition: util.h:155
remotemono::PackHelper
Definition: util.h:147