BLT/include/blt/std/utility.h

125 lines
3.2 KiB
C
Raw Normal View History

2023-11-24 13:45:14 -05:00
/*
* <Short Description>
* Copyright (C) 2023 Brett Terpstra
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_UTILITY_H
#define BLT_UTILITY_H
#include <optional>
#if defined(__GNUC__)
2023-12-08 20:11:58 -05:00
#include <cxxabi.h>
#include <blt/compatibility.h>
#include <string>
2023-12-08 20:11:58 -05:00
namespace blt
{
static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str)
{
2023-12-08 20:11:58 -05:00
int status;
// only defined for GNU C++11?
char* demangled_name = abi::__cxa_demangle(str.c_str(), nullptr, nullptr, &status);
if (demangled_name == nullptr)
return str;
std::string ret_name = demangled_name;
std::free(demangled_name);
return ret_name;
}
2023-12-08 20:11:58 -05:00
}
#else
2023-12-08 20:11:58 -05:00
namespace blt
{
static BLT_CPP20_CONSTEXPR inline std::string demangle(const std::string& str)
{
2023-12-08 20:11:58 -05:00
return str;
}
2023-12-08 20:11:58 -05:00
}
#endif
2023-11-24 13:45:14 -05:00
namespace blt
{
2023-12-08 20:11:58 -05:00
template<typename TYPE_ITR>
2023-11-24 13:45:14 -05:00
class enumerate
{
private:
2023-12-08 20:11:58 -05:00
size_t index = 0;
TYPE_ITR begin;
TYPE_ITR end;
public:
class enumerate_itr
{
public:
using iterator_category = std::input_iterator_tag;
using value_type = typename TYPE_ITR::value_type;
using difference_type = typename TYPE_ITR::difference_type;
using pointer = typename TYPE_ITR::pointer;
using reference = typename TYPE_ITR::reference;
private:
size_t index = 0;
public:
};
2023-11-24 13:45:14 -05:00
};
2023-11-26 17:35:06 -05:00
#if defined(__GNUC__) || defined(__llvm__)
#define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline))
#else
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#define BLT_ATTRIB_NO_INLINE __declspec(noinline)
#else
#define BLT_ATTRIB_NO_INLINE
#endif
#endif
template<typename T>
void BLT_ATTRIB_NO_INLINE black_box_ref(const T& val)
{
volatile void* hell;
hell = (void*) &val;
2023-11-26 18:05:38 -05:00
(void) hell;
2023-11-26 17:35:06 -05:00
}
template<typename T>
void BLT_ATTRIB_NO_INLINE black_box(T val)
{
volatile void* hell2;
hell2 = (void*) &val;
2023-11-26 18:05:38 -05:00
(void) hell2;
2023-11-26 17:35:06 -05:00
}
template<typename T>
const T& BLT_ATTRIB_NO_INLINE black_box_ref_ret(const T& val)
{
volatile void* hell;
hell = (void*) &val;
2023-11-26 18:05:38 -05:00
(void) hell;
2023-11-26 17:35:06 -05:00
return val;
}
template<typename T>
T BLT_ATTRIB_NO_INLINE black_box_ret(T val)
{
volatile void* hell2;
hell2 = (void*) &val;
2023-11-26 18:05:38 -05:00
(void) hell2;
2023-11-26 17:35:06 -05:00
return val;
}
2023-11-24 13:45:14 -05:00
}
#endif //BLT_UTILITY_H