diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d568c41..606054ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/test/typeidx.cpp b/test/typeidx.cpp new file mode 100644 index 00000000..ed3ae9bf --- /dev/null +++ b/test/typeidx.cpp @@ -0,0 +1,17 @@ +#include "tap.hpp" + +#include "./typeidx.hpp" + + +/////////////////////////////////////////////////////////////////////////////// +int +main (int, char**) +{ + util::TAP::logger tap; + + tap.expect_eq (util::typeidx (), util::typeidx (), "equality for int"); + tap.expect_eq (util::typeidx (), util::typeidx (), "equality for float"); + tap.expect_neq (util::typeidx (), util::typeidx (), "inequality for int/float"); + + return tap.status (); +} \ No newline at end of file diff --git a/typeidx.cpp b/typeidx.cpp new file mode 100644 index 00000000..3231bd10 --- /dev/null +++ b/typeidx.cpp @@ -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 + */ + +#include "./typeidx.hpp" + + +/////////////////////////////////////////////////////////////////////////////// +std::uintptr_t +util::detail::typeidx_next () +{ + static std::uintptr_t counter = 0; + return counter++; +} diff --git a/typeidx.hpp b/typeidx.hpp new file mode 100644 index 00000000..31a5c849 --- /dev/null +++ b/typeidx.hpp @@ -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 + */ + +#ifndef CRUFT_UTIL_TYPEIDX_HPP +#define CRUFT_UTIL_TYPEIDX_HPP + +#include + +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 + std::uintptr_t + typeidx (void) + { + static auto id = detail::typeidx_next (); + return id; + } +} + +#endif