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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*****************************************************************************
* Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2.1 of the *
* License, or (at your option) version 3, or any later version accepted *
* by the membership of KDE e.V. (or its successor approved by the *
* membership of KDE e.V.), which shall act as a proxy defined in *
* Section 6 of version 3 of the license. *
* *
* 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library. If not, *
* see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#ifndef _QTC_UTILS_UTILS_H_
#define _QTC_UTILS_UTILS_H_
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <math.h>
#include "macros.h"
#include <memory>
#include <utility>
#include <type_traits>
#include <initializer_list>
/**
* \file utils.h
* \author Yichao Yu <yyc1992@gmail.com>
* \brief Some generic functions and macros.
*/
/**
* Generic call back function.
*/
typedef void (*QtcCallback)(void*);
/**
* Allocate memory and initialize it to zero.
* \param size size of the memory
*/
QTC_ALWAYS_INLINE static inline void*
qtcAlloc0(size_t size)
{
void *p = malloc(size);
memset(p, 0, size);
return p;
}
/**
* Allocate memory of size \param size for a \param type.
* The memory is initialized to zero.
* \param type type pointed to by the pointer
* \param size size of the memory
* \sa qtcAlloc0
*/
#define qtcNewSize(type, size) ((type*)qtcAlloc0(size))
/**
* Allocate memory for a \param type or an array of \param type.
* The memory is initialized to zero.
* \param type type pointed to by the pointer
* \param n (optional) elements in the array (default: 1)
* \sa qtcNewSize
*/
#define qtcNew(type, n...) \
qtcNewSize(type, sizeof(type) * (QTC_DEFAULT(n, 1)))
namespace QtCurve {
template<typename T>
using remove_cvr_t = typename std::remove_cv<
typename std::remove_reference<T>::type>::type;
template<typename T>
struct _isChar : std::is_same<remove_cvr_t<T>, char> {};
template<typename T>
struct _isCharPtr : std::integral_constant<
bool,
std::is_pointer<remove_cvr_t<T> >::value &&
_isChar<typename std::remove_pointer<remove_cvr_t<T> >::type>::value> {};
template<typename T>
struct _isCharAry : std::integral_constant<
bool,
std::is_array<remove_cvr_t<T> >::value &&
_isChar<typename std::remove_extent<remove_cvr_t<T> >::type>::value> {};
template<typename T>
struct _isCharStr : std::integral_constant<
bool, _isCharPtr<T>::value || _isCharAry<T>::value> {};
template<typename T1, typename T2, class=void>
struct _oneOfCmp {
inline bool operator()(T1 &&v1, T2 &&v2)
{
return std::forward<T1>(v1) == std::forward<T2>(v2);
}
};
template<typename T1, typename T2>
struct _oneOfCmp<T1, T2, typename std::enable_if<_isCharStr<T1>::value &&
_isCharStr<T2>::value>::type> {
inline bool operator()(T1 &&v1, T2 &&v2)
{
return v1 && strcmp(v1, v2) == 0;
}
};
template<typename...>
struct CaseCmp {
inline bool operator()(const char *str1, const char *str2)
{
return str1 && strcasecmp(str1, str2) == 0;
}
};
template<template<typename...> class _Cmp=_oneOfCmp,
typename T1, typename T2>
static inline bool
oneOf(T1 &&v1, T2 &&v2)
{
return _Cmp<T1, T2>()(std::forward<T1>(v1), std::forward<T2>(v2));
}
template<template<typename...> class _Cmp=_oneOfCmp,
typename T, typename First, typename... Rest>
static inline bool
oneOf(T &&value, First &&first, Rest&&... rest)
{
return (oneOf<_Cmp>(std::forward<T>(value), std::forward<First>(first)) ||
oneOf<_Cmp>(std::forward<T>(value), std::forward<Rest>(rest)...));
}
template<template<typename...> class _Cmp=_oneOfCmp,
typename... Args>
static inline bool
noneOf(Args&&... args)
{
return !oneOf<_Cmp>(std::forward<Args>(args)...);
}
/**
* Turn a variable into a const reference. This is useful for range-based for
* loop where a non-const variable may cause unnecessary copy.
*/
template <class T>
static inline const T&
const_(const T &t)
{
return t;
}
struct CDeleter {
template<typename T>
void operator()(T *p)
{
free((void*)p);
}
};
template<typename T>
using uniqueCPtr = std::unique_ptr<T, CDeleter>;
template<typename T, size_t N>
class LocalBuff {
LocalBuff(const LocalBuff&) = delete;
public:
LocalBuff(size_t size=N, const T *ary=nullptr)
: m_ptr(size > N ? qtcNew(T, size) : m_static_buf),
m_size(size),
m_static_buf{}
{
if (ary) {
memcpy(m_ptr, ary, sizeof(T) * size);
}
}
bool
is_static() const
{
return m_ptr == m_static_buf;
}
void
resize(size_t size)
{
if (is_static()) {
if (size > N) {
m_ptr = qtcNew(T, size);
memcpy(m_ptr, m_static_buf, sizeof(T) * m_size);
}
} else {
m_ptr = (T*)realloc(m_ptr, sizeof(T) * size);
}
m_size = size;
}
T*
get() const
{
return m_ptr;
}
T&
operator[](size_t i) const
{
return get()[i];
}
size_t
size() const
{
return m_size;
}
~LocalBuff()
{
if (!is_static()) {
free(m_ptr);
}
}
protected:
T *m_ptr;
size_t m_size;
private:
T m_static_buf[N];
};
const char *getProgName();
}
extern "C" const char *qtcVersion();
template<typename T>
using qtcPtrType = QtCurve::remove_cvr_t<typename std::remove_pointer<T>::type>;
#define qtcMemPtr(ptr, name) &qtcPtrType<decltype(ptr)>::name
// Use lambda for lazy evaluation of \param def
#define qtcDefault(val, def) ([&] { \
auto __val = (val); \
return __val ? __val : (def); \
}())
// Use lambda for lazy evaluation of \param args
// C++ allows returning void expression! =) See the quote of the standard
// (here)[http://gcc.gnu.org/ml/gcc/2006-10/msg00697.html]
// The current c++ implementation of this macro does not support functions
// with types that do not have accessible default constructor (including
// references) as return type.
#define qtcCall(func, args...) ([&] { \
auto __func = (func); \
return __func ? __func(args) : decltype(__func(args))(); \
}())
#define qtcAssign(addr, exp) do { \
auto __addr = (addr); \
if (__addr) { \
*__addr = (exp); \
} \
} while(0)
// Returning a void expression is valid c++, see above
// Or https://gcc.gnu.org/ml/gcc/2006-10/msg00697.html
#define QTC_RET_IF_FAIL(exp, val...) do { \
if (!qtcLikely(exp)) { \
return (QTC_DEFAULT(val, (void)0)); \
} \
} while (0)
#endif
|