libcruft-util/typeidx.hpp

39 lines
1.1 KiB
C++
Raw Normal View History

/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_TYPEIDX_HPP
#define CRUFT_UTIL_TYPEIDX_HPP
#include <cstdint>
namespace util {
namespace detail { int typeidx_next (void); }
/// return a globally unique runtime ID for a given type.
///
/// this is intended to be used as a lightweight type check for variants
/// and such without requiring RTTI.
///
/// the identifier is constructed at runtime and is not guaranteed to be
/// stable across executions (particularly in the case of threads and
/// other non-determinism). however it _is_ threadsafe to call this.
///
/// the range of identifiers is _probably_ contiguous starting from zero.
/// this should not be relied upon for correctness, but may be used for
/// performance concerns.
template <typename T>
int
typeidx (void)
{
static auto id = detail::typeidx_next ();
return id;
}
}
#endif