typeidx: prohibit return of zero values

This commit is contained in:
Danny Robson 2019-05-30 10:43:05 +10:00
parent e4ce92454c
commit d3f23ed754
2 changed files with 22 additions and 14 deletions

View File

@ -11,9 +11,17 @@
#include <atomic>
///////////////////////////////////////////////////////////////////////////////
static std::atomic<int> counter;
//-----------------------------------------------------------------------------
int
cruft::detail::typeidx_next ()
{
static std::atomic<int> counter = 0;
return counter++;
// We can't return zero, but we want to optimise access to the static
// (which means we can't have it locally due to initialisation locking).
//
// Ensure we use pre-increment so that we can utilise zero static
// initialisation instead.
return ++counter;
}

View File

@ -3,29 +3,31 @@
* 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>
* Copyright 2017-2019 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_TYPEIDX_HPP
#define CRUFT_UTIL_TYPEIDX_HPP
#pragma once
#include <cstdint>
namespace cruft {
namespace detail { int typeidx_next (void); }
/// return a globally unique runtime ID for a given type.
/// Return a globally unique runtime ID for a given type.
///
/// this is intended to be used as a lightweight type check for variants
/// 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
/// 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.
/// 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.
/// The value 0 will never be returned from this function and can be used
/// to indicate an 'unknown' state.
///
/// The range of identifiers is _probably_ contiguous. This should not
/// be relied upon for correctness, but may be used for performance
/// concerns.
template <typename T>
int
typeidx (void)
@ -34,5 +36,3 @@ namespace cruft {
return id;
}
}
#endif