utils.h 4.56 KB
Newer Older
Ronan's avatar
Ronan committed
1 2
/*
 * utils.h
Ronan's avatar
Ronan committed
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
Ronan's avatar
Ronan committed
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
Ronan's avatar
Ronan committed
9 10 11 12 13 14 15
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Ronan's avatar
Ronan committed
18 19
 */

20 21
#ifndef _L_UTILS_H_
#define _L_UTILS_H_
Ronan's avatar
Ronan committed
22

23
#include <ctime>
Erwan Croze's avatar
Erwan Croze committed
24
#include <list>
25
#include <sstream>
Ronan's avatar
Ronan committed
26
#include <string>
Erwan Croze's avatar
Erwan Croze committed
27
#include <utility>
Ronan's avatar
Ronan committed
28 29
#include <vector>

30
#include "linphone/utils/enum-generator.h"
31

Ronan's avatar
Ronan committed
32 33
// =============================================================================

34
LINPHONE_BEGIN_NAMESPACE
Ronan's avatar
Ronan committed
35

36
namespace Utils {
Ronan's avatar
Ronan committed
37
	template<typename T>
38
	constexpr T *getPtr (std::shared_ptr<T> &object) {
Ronan's avatar
Ronan committed
39 40 41 42
		return object.get();
	}

	template<typename T>
43
	constexpr T *getPtr (const std::shared_ptr<T> &object) {
44
		return object.get();
Ronan's avatar
Ronan committed
45 46
	}

47
	template<typename T>
48
	constexpr T *getPtr (std::unique_ptr<T> &object) {
49 50 51 52
		return object.get();
	}

	template<typename T>
53
	constexpr T *getPtr (const std::unique_ptr<T> &object) {
54 55 56
		return object.get();
	}

Ronan's avatar
Ronan committed
57
	template<typename T>
58
	constexpr T *getPtr (T *object) {
59
		return object;
Ronan's avatar
Ronan committed
60 61 62
	}

	template<typename T>
63
	constexpr T *getPtr (T &object) {
64
		return &object;
Ronan's avatar
Ronan committed
65 66
	}

67
	LINPHONE_PUBLIC bool iequals (const std::string &a, const std::string &b);
Ronan's avatar
Ronan committed
68

69
	LINPHONE_PUBLIC std::vector<std::string> split (const std::string &str, const std::string &delimiter);
Ronan's avatar
Ronan committed
70

71 72 73
	LINPHONE_PUBLIC inline std::vector<std::string> split (const std::string &str, char delimiter) {
		return split(str, std::string(1, delimiter));
	}
74

75 76 77 78 79 80 81 82 83
	LINPHONE_PUBLIC std::string toString (int val);
	LINPHONE_PUBLIC std::string toString (long val);
	LINPHONE_PUBLIC std::string toString (long long val);
	LINPHONE_PUBLIC std::string toString (unsigned val);
	LINPHONE_PUBLIC std::string toString (unsigned long val);
	LINPHONE_PUBLIC std::string toString (unsigned long long val);
	LINPHONE_PUBLIC std::string toString (float val);
	LINPHONE_PUBLIC std::string toString (double val);
	LINPHONE_PUBLIC std::string toString (long double val);
84
	LINPHONE_PUBLIC std::string toString (const void *val);
85

86
	template<typename T, typename = typename std::enable_if<IsDefinedEnum<T>::value, T>::type>
87
	LINPHONE_PUBLIC inline std::string toString (const T &val) { return getEnumValueAsString(val); }
88

89
	LINPHONE_PUBLIC int stoi (const std::string &str, size_t *idx = 0, int base = 10);
90
	LINPHONE_PUBLIC long long stoll (const std::string &str, size_t *idx = 0, int base = 10);
91
	LINPHONE_PUBLIC unsigned long long stoull (const std::string &str, size_t *idx = 0, int base = 10);
92
	LINPHONE_PUBLIC double stod (const std::string &str, size_t *idx = 0);
93
	LINPHONE_PUBLIC float stof (const std::string &str, size_t *idx = 0);
94
	LINPHONE_PUBLIC bool stob (const std::string &str);
Ghislain MARY's avatar
Ghislain MARY committed
95

Benjamin REIS's avatar
Benjamin REIS committed
96
	LINPHONE_PUBLIC int stoi (const char *str, size_t *idx = 0, int base = 10);
97
	LINPHONE_PUBLIC long long stoll (const char *str, size_t *idx = 0, int base = 10);
98
	LINPHONE_PUBLIC unsigned long long stoull (const char *str, size_t *idx = 0, int base = 10);
Benjamin REIS's avatar
Benjamin REIS committed
99 100 101
	LINPHONE_PUBLIC double stod (const char *str, size_t *idx = 0);
	LINPHONE_PUBLIC float stof (const char *str, size_t *idx = 0);

Ronan's avatar
Ronan committed
102 103
	LINPHONE_PUBLIC std::string stringToLower (const std::string &str);

Ghislain MARY's avatar
Ghislain MARY committed
104
	LINPHONE_PUBLIC char *utf8ToChar (uint32_t ic);
105

106 107 108 109
	LINPHONE_PUBLIC inline std::string cStringToCppString (const char *str) {
		return str ? str : "";
	}

110
	template<typename S, typename T>
111
	inline std::string join (const std::vector<T>& elems, const S& delim) {
112 113 114 115 116 117 118
		std::stringstream ss;
		auto e = elems.begin();
		ss << *e++;
		for (; e != elems.end(); ++e)
			ss << delim << *e;
		return ss.str();
	}
119 120
	LINPHONE_PUBLIC std::string trim (const std::string &str);

121
	template<typename T>
122
	inline const T &getEmptyConstRefObject () {
123
		static const T object{};
124 125
		return object;
	}
126

127 128
	LINPHONE_PUBLIC std::tm getTimeTAsTm (time_t time);
	LINPHONE_PUBLIC time_t getTmAsTimeT (const std::tm &time);
129 130 131

	LINPHONE_PUBLIC std::string localeToUtf8 (const std::string &str);
	LINPHONE_PUBLIC std::string utf8ToLocale (const std::string &str);
132
	LINPHONE_PUBLIC std::string convertAnyToUtf8 (const std::string &str, const std::string &encoding);
Ronan's avatar
Ronan committed
133 134
}

135 136
LINPHONE_END_NAMESPACE

137
#endif // ifndef _L_UTILS_H_