typeidx: add lightweight runtime type id call
This commit is contained in:
parent
998c0171e8
commit
606a46a128
@ -391,6 +391,8 @@ list (
|
||||
time.ipp
|
||||
tuple.cpp
|
||||
tuple.hpp
|
||||
typeidx.cpp
|
||||
typeidx.hpp
|
||||
types/bits.hpp
|
||||
types/comparator.hpp
|
||||
types/comparator.ipp
|
||||
@ -496,6 +498,7 @@ if (TESTS)
|
||||
stringid
|
||||
strongdef
|
||||
tuple
|
||||
typeidx
|
||||
uri
|
||||
vector
|
||||
version
|
||||
|
17
test/typeidx.cpp
Normal file
17
test/typeidx.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#include "tap.hpp"
|
||||
|
||||
#include "./typeidx.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
int
|
||||
main (int, char**)
|
||||
{
|
||||
util::TAP::logger tap;
|
||||
|
||||
tap.expect_eq (util::typeidx<int> (), util::typeidx<int> (), "equality for int");
|
||||
tap.expect_eq (util::typeidx<float> (), util::typeidx<float> (), "equality for float");
|
||||
tap.expect_neq (util::typeidx<int> (), util::typeidx<float> (), "inequality for int/float");
|
||||
|
||||
return tap.status ();
|
||||
}
|
26
typeidx.cpp
Normal file
26
typeidx.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "./typeidx.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
std::uintptr_t
|
||||
util::detail::typeidx_next ()
|
||||
{
|
||||
static std::uintptr_t counter = 0;
|
||||
return counter++;
|
||||
}
|
46
typeidx.hpp
Normal file
46
typeidx.hpp
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2017 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef CRUFT_UTIL_TYPEIDX_HPP
|
||||
#define CRUFT_UTIL_TYPEIDX_HPP
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace util {
|
||||
namespace detail { std::uintptr_t 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).
|
||||
///
|
||||
/// 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>
|
||||
std::uintptr_t
|
||||
typeidx (void)
|
||||
{
|
||||
static auto id = detail::typeidx_next ();
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user