2017-05-29 17:21:11 +10:00
|
|
|
/*
|
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/.
|
2017-05-29 17:21:11 +10:00
|
|
|
*
|
|
|
|
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CRUFT_UTIL_TYPEIDX_HPP
|
|
|
|
#define CRUFT_UTIL_TYPEIDX_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
namespace util {
|
2018-04-05 12:14:11 +10:00
|
|
|
namespace detail { int typeidx_next (void); }
|
2017-05-29 17:21:11 +10:00
|
|
|
|
|
|
|
/// 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
|
2018-04-05 12:14:11 +10:00
|
|
|
/// other non-determinism). however it _is_ threadsafe to call this.
|
2017-05-29 17:21:11 +10:00
|
|
|
///
|
|
|
|
/// 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>
|
2018-04-05 12:14:11 +10:00
|
|
|
int
|
2017-05-29 17:21:11 +10:00
|
|
|
typeidx (void)
|
|
|
|
{
|
|
|
|
static auto id = detail::typeidx_next ();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|