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
|
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2020 Marco Trevisan <marco.trevisan@canonical.com>
#pragma once
#include <config.h>
#include <stdint.h>
#include <algorithm> // IWYU pragma: keep (for find)
#include <utility> // IWYU pragma: keep (for swap)
#include <vector>
template <typename T>
constexpr void* gjs_int_to_pointer(T v) {
static_assert(std::is_integral_v<T>, "Need integer value");
if constexpr (std::is_signed_v<T>)
return reinterpret_cast<void*>(static_cast<intptr_t>(v));
else
return reinterpret_cast<void*>(static_cast<uintptr_t>(v));
}
template <typename T>
constexpr T gjs_pointer_to_int(void* p) {
static_assert(std::is_integral_v<T>, "Need integer value");
if constexpr (std::is_signed_v<T>)
return static_cast<T>(reinterpret_cast<intptr_t>(p));
else
return static_cast<T>(reinterpret_cast<uintptr_t>(p));
}
template <>
inline void* gjs_int_to_pointer<bool>(bool v) {
return gjs_int_to_pointer<int8_t>(!!v);
}
template <>
inline bool gjs_pointer_to_int<bool>(void* p) {
return !!gjs_pointer_to_int<int8_t>(p);
}
namespace Gjs {
template <typename T>
inline bool remove_one_from_unsorted_vector(std::vector<T>* v, const T& value) {
// This assumes that there's only a copy of the same value in the vector
// so this needs to be ensured when populating it.
// We use the swap and pop idiom to avoid moving all the values.
auto it = std::find(v->begin(), v->end(), value);
if (it != v->end()) {
std::swap(*it, v->back());
v->pop_back();
g_assert(std::find(v->begin(), v->end(), value) == v->end());
return true;
}
return false;
}
} // namespace Gjs
|