Skip to content

CoMeta - metaprogramming

array_ref

array_ref class

template <typename T> array_ref

Reference to array

Source code
template <typename T>
struct array_ref
{
public:
    using value_type             = T;
    using pointer                = value_type*;
    using const_pointer          = const value_type*;
    using reference              = value_type&;
    using const_reference        = const value_type&;
    using iterator               = pointer;
    using const_iterator         = const_pointer;
    using reverse_iterator       = std::reverse_iterator<pointer>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;
    using size_type              = std::size_t;
    using difference_type        = std::ptrdiff_t;

    constexpr array_ref() CMT_NOEXCEPT : m_data(nullptr), m_size(0) {}
    constexpr array_ref(const array_ref&) CMT_NOEXCEPT = default;
    constexpr array_ref(array_ref&&) CMT_NOEXCEPT      = default;
#ifdef CMT_COMPILER_GNU
    constexpr array_ref& operator=(const array_ref&) CMT_NOEXCEPT = default;
    constexpr array_ref& operator=(array_ref&&) CMT_NOEXCEPT = default;
#else
    array_ref& operator=(const array_ref&) = default;
    array_ref& operator=(array_ref&&) = default;
#endif

    template <size_t N>
    constexpr array_ref(value_type (&arr)[N]) CMT_NOEXCEPT : m_data(arr), m_size(N)
    {
    }
    template <size_t N>
    constexpr array_ref(const std::array<T, N>& arr) CMT_NOEXCEPT : m_data(arr.data()), m_size(N)
    {
    }
    template <size_t N>
    constexpr array_ref(std::array<T, N>& arr) CMT_NOEXCEPT : m_data(arr.data()), m_size(N)
    {
    }
    template <typename Alloc>
    constexpr array_ref(const std::vector<T, Alloc>& vec) CMT_NOEXCEPT : m_data(vec.data()),
                                                                         m_size(vec.size())
    {
    }

    template <typename Container, CMT_ENABLE_IF(has_data_size<Container>::value)>
    array_ref(Container& cont) : array_ref(cont.data(), cont.size())
    {
    }

    constexpr array_ref(const std::initializer_list<T>& vec) CMT_NOEXCEPT : m_data(vec.begin()),
                                                                            m_size(vec.size())
    {
    }
    template <typename InputIter>
    constexpr array_ref(InputIter first, InputIter last) CMT_NOEXCEPT : m_data(std::addressof(*first)),
                                                                        m_size(std::distance(first, last))
    {
    }
    constexpr array_ref(T* data, size_type size) CMT_NOEXCEPT : m_data(data), m_size(size) {}

    constexpr reference front() const CMT_NOEXCEPT { return m_data[0]; }
    constexpr reference back() const CMT_NOEXCEPT { return m_data[m_size - 1]; }
    constexpr iterator begin() const CMT_NOEXCEPT { return m_data; }
    constexpr iterator end() const CMT_NOEXCEPT { return m_data + m_size; }
    constexpr const_iterator cbegin() const CMT_NOEXCEPT { return m_data; }
    constexpr const_iterator cend() const CMT_NOEXCEPT { return m_data + m_size; }
    constexpr pointer data() const CMT_NOEXCEPT { return m_data; }
    constexpr std::size_t size() const CMT_NOEXCEPT { return m_size; }
    constexpr bool empty() const CMT_NOEXCEPT { return !m_size; }
    constexpr reference operator[](std::size_t index) const { return m_data[index]; }

private:
    pointer m_data;
    size_type m_size;
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/array.hpp#L16

b8 typedef

b8 = bool

Short names for common types

Source code
using b8   = bool

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/numeric.hpp#L12

choose_const function

template <typename T, typename C1, typename... Cs,
          CMT_ENABLE_IF(std::is_same<T, C1>::value)>
constexpr inline T choose_const(C1 c1, Cs...)

Selects constant of the specific type

CHECK( choose_const<f32>( 32.0f, 64.0 ) == 32.0f );
CHECK( choose_const<f64>( 32.0f, 64.0 ) == 64.0 );

Source code
template <typename T, typename C1, typename... Cs, CMT_ENABLE_IF(std::is_same<T, C1>::value)>
constexpr inline T choose_const(C1 c1, Cs...)
{
    return static_cast<T>(c1);
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1609

ctypeid function

template <typename T> constexpr inline type_id_t ctypeid()

Gets unique value associated with the type @tparam T type @return value of type that supports operator== and operator!=

Source code
template <typename T>
constexpr inline type_id_t ctypeid()
{
    return details::typeident_impl<T>();
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/ctti.hpp#L103

template <typename T>
constexpr inline type_id_t ctypeid(T x)

Gets unique value associated with the type
x value of specific type @return value of type that supports operator== and operator!=

Source code
template <typename T>
constexpr inline type_id_t ctypeid(T x)
{
    (void)x;
    return details::typeident_impl<T>();
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/ctti.hpp#L113

datatype

datatype enum

enum class datatype : int

An enumeration representing data type

Source code
enum class datatype : int
{
    typebits_mask  = 0xFF,
    f              = 0x100, // floating point
    i              = 0x200, // signed integer
    u              = 0x300, // unsigned integer
    c              = 0x400, // complex floating point
    b              = 0x500, // boolean
    typeclass_mask = 0xF00,
    f16            = static_cast<int>(f) | 16,
    f32            = static_cast<int>(f) | 32,
    f64            = static_cast<int>(f) | 64,
    f80            = static_cast<int>(f) | 80,
    i8             = static_cast<int>(i) | 8,
    i16            = static_cast<int>(i) | 16,
    i24            = static_cast<int>(i) | 24,
    i32            = static_cast<int>(i) | 32,
    i64            = static_cast<int>(i) | 64,
    u8             = static_cast<int>(u) | 8,
    u16            = static_cast<int>(u) | 16,
    u24            = static_cast<int>(u) | 24,
    u32            = static_cast<int>(u) | 32,
    u64            = static_cast<int>(u) | 64,
    c32            = static_cast<int>(c) | 32,
    c64            = static_cast<int>(c) | 64,
    b8             = static_cast<int>(b) | 8
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/numeric.hpp#L86

function

function class

template <typename Result, typename... Args> function

std::function-like lightweight function wrapper

function<int( float )> f = []( float x ){ return static_cast<int>( x ); };
CHECK( f( 3.4f ) == 3 )

Source code
template <typename Result, typename... Args>
struct function<Result(Args...)>
{
    using this_t = function<Result(Args...)>;

    function(function&& other) : fn(other.fn) { other.fn = nullptr; }
    function& operator=(function&& other)
    {
        fn       = other.fn;
        other.fn = nullptr;
        return *this;
    }

    CMT_MEM_INTRINSIC function() : fn(nullptr) {}
    CMT_MEM_INTRINSIC function(std::nullptr_t) : fn(nullptr) {}
    template <typename Func>
    CMT_MEM_INTRINSIC function(const Func& x)
        : fn(new details::virtual_function_impl<typename details::func_filter<Func>::type, Result, Args...>(
              x))
    {
    }
    function(const this_t& other) : fn(other.fn ? other.fn->make_copy() : nullptr) {}
    CMT_MEM_INTRINSIC function& operator=(const this_t& other)
    {
        if ((&other != this) && (other.fn))
        {
            auto* temp = other.fn->make_copy();
            delete fn;
            fn = temp;
        }
        return *this;
    }
    CMT_MEM_INTRINSIC function& operator=(std::nullptr_t)
    {
        delete fn;
        fn = nullptr;
        return *this;
    }
    template <typename Fn>
    CMT_MEM_INTRINSIC function& operator=(const Fn& x)
    {
        using FnImpl =
            details::virtual_function_impl<typename details::func_filter<Fn>::type, Result, Args...>;
        FnImpl* temp = new FnImpl(x);
        delete fn;
        fn = temp;
        return *this;
    }
    CMT_MEM_INTRINSIC Result operator()(Args... args) const { return (*fn)(std::forward<Args>(args)...); }
    template <typename TResult>
    CMT_MEM_INTRINSIC Result call(TResult&& default_result, Args... args) const
    {
        return fn ? (*fn)(std::forward<Args>(args)...) : std::forward<TResult>(default_result);
    }
    CMT_MEM_INTRINSIC explicit operator bool() const CMT_NOEXCEPT { return !!fn; }

    CMT_MEM_INTRINSIC ~function() { delete fn; }

private:
    details::virtual_function<Result, Args...>* fn;
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/function.hpp#L72

gcd function

template <typename T> constexpr inline T gcd(T a)

Greatest common divisor

Source code
template <typename T>
constexpr inline T gcd(T a)
{
    return a;
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L866

template <typename T> constexpr inline T gcd(T a, T b)

Greatest common divisor

Source code
template <typename T>
constexpr inline T gcd(T a, T b)
{
    return a < b ? gcd(b, a) : ((a % b == 0) ? b : gcd(b, a % b));
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L873

template <typename T, typename... Ts>
constexpr inline T gcd(T a, T b, T c, Ts... rest)

Greatest common divisor

Source code
template <typename T, typename... Ts>
constexpr inline T gcd(T a, T b, T c, Ts... rest)
{
    return gcd(a, gcd(b, c, rest...));
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L880

get_first function

template <typename T1, typename... Ts>
CMT_INTRINSIC constexpr T1 &&
get_first(T1 &&x, Ts &&...) CMT_NOEXCEPT

Function that returns its first argument and ignores all other arguments

Source code
template <typename T1, typename... Ts>
CMT_INTRINSIC constexpr T1&& get_first(T1&& x, Ts&&...) CMT_NOEXCEPT
{
    return std::forward<T1>(x);
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1208

get_second function

template <typename T1, typename T2, typename... Ts>
CMT_INTRINSIC constexpr T2 &&
get_second(T1, T2 &&x, Ts &&...) CMT_NOEXCEPT

Function that returns its second argument and ignores all other arguments

Source code
template <typename T1, typename T2, typename... Ts>
CMT_INTRINSIC constexpr T2&& get_second(T1, T2&& x, Ts&&...) CMT_NOEXCEPT
{
    return std::forward<T2>(x);
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1215

get_third function

template <typename T1, typename T2, typename T3,
          typename... Ts>
CMT_INTRINSIC constexpr T3 &&
get_third(T1 &&, T2 &&, T3 &&x, Ts &&...) CMT_NOEXCEPT

Function that returns its third argument and ignores all other arguments

Source code
template <typename T1, typename T2, typename T3, typename... Ts>
CMT_INTRINSIC constexpr T3&& get_third(T1&&, T2&&, T3&& x, Ts&&...) CMT_NOEXCEPT
{
    return std::forward<T3>(x);
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1222

identity typedef

template <typename T>
identity = typename details::identity_impl<T>::type

Utility typedef used to disable type deduction

Source code
template <typename T>
using identity = typename details::identity_impl<T>::type

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1044

identity = typename details::identity_impl<T>::type

Utility typedef used to disable type deduction

Source code
using identity = typename details::identity_impl<T>::type

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1044

lcm function

template <typename T> constexpr inline T lcm(T a)

Least common multiple

Source code
template <typename T>
constexpr inline T lcm(T a)
{
    return a;
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L887

template <typename T> constexpr inline T lcm(T a, T b)

Least common multiple

Source code
template <typename T>
constexpr inline T lcm(T a, T b)
{
    return a * b / gcd(a, b);
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L894

template <typename T, typename... Ts>
constexpr inline T lcm(T a, T b, T c, Ts... rest)

Least common multiple

Source code
template <typename T, typename... Ts>
constexpr inline T lcm(T a, T b, T c, Ts... rest)
{
    return lcm(a, lcm(b, c, rest...));
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L901

make_range function

template <typename T>
constexpr range<T> make_range(T begin, T end)

Make iterable range object

Source code
template <typename T>
constexpr range<T> make_range(T begin, T end)
{
    return range<T>(begin, end, end > begin ? 1 : -1);
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/range.hpp#L61

template <typename T, typename D>
constexpr range<std::common_type_t<T, D>>
make_range(T begin, T end, D step)

Make iterable range object with step

Source code
template <typename T, typename D>
constexpr range<std::common_type_t<T, D>> make_range(T begin, T end, D step)
{
    return range<std::common_type_t<T, D>>(begin, end, step);
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/range.hpp#L68

next_poweroftwo function

template <typename T>
constexpr inline T next_poweroftwo(T n)

Returns a nearest power of two that is greater or equal than n

Source code
template <typename T>
constexpr inline T next_poweroftwo(T n)
{
    return n > 2 ? T(1) << (ilog2(n - 1) + 1) : n;
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L846

noop function

template <typename... Ts>
CMT_INTRINSIC constexpr void noop(Ts &&...) CMT_NOEXCEPT

Function that returns void and ignores all its arguments

Source code
template <typename... Ts>
CMT_INTRINSIC constexpr void noop(Ts&&...) CMT_NOEXCEPT
{
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1202

pass_through function

template <typename T>
CMT_INTRINSIC constexpr T &&pass_through(T &&x) CMT_NOEXCEPT

Function that returns its first argument

Source code
template <typename T>
CMT_INTRINSIC constexpr T&& pass_through(T&& x) CMT_NOEXCEPT
{
    return std::forward<T>(x);
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1195

prev_poweroftwo function

template <typename T>
constexpr inline T prev_poweroftwo(T n)

Returns a nearest power of two that is less or equal than n

Source code
template <typename T>
constexpr inline T prev_poweroftwo(T n)
{
    return n > 2 ? T(1) << (ilog2(n)) : n;
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L853

range

range class

template <typename T> range

Iterable range

Source code
template <typename T>
struct range
{
    using value_type      = T;
    using reference       = T&;
    using const_reference = const T&;
    using pointer         = T*;
    using const_pointer   = const T*;
    using diff_type       = decltype(std::declval<T>() - std::declval<T>());

    constexpr range(value_type begin, value_type end, diff_type step) CMT_NOEXCEPT : min(begin),
                                                                                     max(end),
                                                                                     step(step)
    {
    }

    struct iterator
    {
        value_type value;
        diff_type step;
        constexpr const_reference operator*() const { return value; }
        constexpr const_pointer operator->() const { return &value; }
        constexpr iterator& operator++()
        {
            value += step;
            return *this;
        }
        constexpr iterator operator++(int)
        {
            iterator copy = *this;
            ++(*this);
            return copy;
        }
        constexpr bool operator!=(const iterator& other) const
        {
            return step > 0 ? value < other.value : value > other.value;
        }
    };
    value_type min;
    value_type max;
    diff_type step;
    constexpr iterator begin() const { return iterator{ min, step }; }
    constexpr iterator end() const { return iterator{ max, step }; }

    constexpr T distance() const { return max - min; }
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/range.hpp#L13

return_constant function

template <typename T, T value, typename... Args>
CMT_INTRINSIC constexpr T return_constant(Args &&...)

Function that returns constant of type T and ignores all its arguments

Source code
template <typename T, T value, typename... Args>
CMT_INTRINSIC constexpr T return_constant(Args&&...)
{
    return value;
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1236

returns function

template <typename T, typename... Ts>
CMT_INTRINSIC constexpr T returns(Ts &&...)

Function that returns value-initialization of type T and ignores all its arguments

Source code
template <typename T, typename... Ts>
CMT_INTRINSIC constexpr T returns(Ts&&...)
{
    return T();
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1229

swallow

swallow class

swallow

Utility class to use in list-initialization context

Source code
struct swallow
{
    template <typename... T>
    CMT_MEM_INTRINSIC constexpr swallow(T&&...) CMT_NOEXCEPT
    {
    }
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa.hpp#L1047

swallow

swallow

Utility class to use in list-initialization context

Source code
struct swallow

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/tuple.hpp#L20

type_name function

template <typename T>
inline const char *type_name() CMT_NOEXCEPT

Gets the fully qualified name of the type, including namespace and template parameters (if any) @tparam T type @return name of the type

Source code
template <typename T>
inline const char* type_name() CMT_NOEXCEPT
{
    static const auto name = ctype_name<T>();
    return name.c_str();
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/ctti.hpp#L78

template <typename T>
inline const char *type_name(T x) CMT_NOEXCEPT

Gets the fully qualified name of the type, including namespace and template parameters (if any)
x value of specific type @return name of the type

Source code
template <typename T>
inline const char* type_name(T x) CMT_NOEXCEPT
{
    (void)x;
    return type_name<T>();
}

https://github.com/kfrlib/kfr/blob//include/kfr/cometa/ctti.hpp#L91


Auto-generated from sources, Revision , https://github.com/kfrlib/kfr/blob//include/kfr/