Commit c7e3fd1a authored by Marco Trevisan's avatar Marco Trevisan 🎺 Committed by Philip Chimento

gi: Add inline utilities header to do template-based pointer-to-int conversion

parent ccd10f94
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
* SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
*
* Copyright (c) 2020 Marco Trevisan <marco.trevisan@canonical.com>
*/
#pragma once
#include <stdint.h>
#include <type_traits>
template <typename T>
inline std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>, void*>
gjs_int_to_pointer(T v) {
return reinterpret_cast<void*>(static_cast<intptr_t>(v));
}
template <typename T>
inline std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>, void*>
gjs_int_to_pointer(T v) {
return reinterpret_cast<void*>(static_cast<uintptr_t>(v));
}
template <typename T>
inline std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>, T>
gjs_pointer_to_int(void* p) {
return static_cast<T>(reinterpret_cast<intptr_t>(p));
}
template <typename T>
inline std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>, T>
gjs_pointer_to_int(void* p) {
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);
}
......@@ -377,6 +377,7 @@ libgjs_sources = [
'gi/repo.cpp', 'gi/repo.h',
'gi/toggle.cpp', 'gi/toggle.h',
'gi/union.cpp', 'gi/union.h',
'gi/utils-inl.h',
'gi/value.cpp', 'gi/value.h',
'gi/wrapperutils.cpp', 'gi/wrapperutils.h',
'gjs/atoms.cpp', 'gjs/atoms.h',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment