/* -*- 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 #pragma once #include #include #include // IWYU pragma: keep (for swap) #include template constexpr void* gjs_int_to_pointer(T v) { static_assert(std::is_integral_v, "Need integer value"); if constexpr (std::is_signed_v) return reinterpret_cast(static_cast(v)); else return reinterpret_cast(static_cast(v)); } template constexpr T gjs_pointer_to_int(void* p) { static_assert(std::is_integral_v, "Need integer value"); if constexpr (std::is_signed_v) return static_cast(reinterpret_cast(p)); else return static_cast(reinterpret_cast(p)); } template <> inline void* gjs_int_to_pointer(bool v) { return gjs_int_to_pointer(!!v); } template <> inline bool gjs_pointer_to_int(void* p) { return !!gjs_pointer_to_int(p); } namespace Gjs { template inline bool remove_one_from_unsorted_vector(std::vector* 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