-
Notifications
You must be signed in to change notification settings - Fork 4
/
stringLibs.h
56 lines (45 loc) · 997 Bytes
/
stringLibs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* stringLibs.h
*
* Created on: 10 авг. 2016 г.
* Author: tihonov
*/
#ifndef STRINGLIBS_H_
#define STRINGLIBS_H_
#include <algorithm>
#include <string>
#include <string.h>
#include <sstream>
const char * const BoolToString(bool b);
std::string bufferToString(char* buffer, int bufflen);
template<typename T>
std::string toString(const T& value)
{
std::ostringstream s;
s << value;
return s.str();
}
inline std::string toString(const uint8_t& value)
{
std::ostringstream s;
s << (int)value;
return s.str();
}
enum class SerializeOrder
{
forward,
backward
};
template <typename A, typename B>
void serialize(const A& from, B& to, size_t size = sizeof(A), const SerializeOrder order = SerializeOrder::forward)
{
const void* fromptr = &from;
void* toptr = &to;
if (order == SerializeOrder::forward)
memcpy(toptr, fromptr, size);
if (order == SerializeOrder::backward)
{
std::reverse_copy(toptr, toptr + size, fromptr);
}
}
#endif /* STRINGLIBS_H_ */