// ===================================================================================================================== // open-cpp-utils, an open-source cpp library with data structures that extend the STL. // Copyright (C) 2024 Medusa Slockbower // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // ===================================================================================================================== #ifndef OPEN_CPP_UTILS_UNIQUE_ID_H #define OPEN_CPP_UTILS_UNIQUE_ID_H #include namespace open_cpp_utils { // Internal function for incrementing an id associated with a type template uint64_t _increment_id() { static uint64_t current = 0; return current++; } /** * \brief Generate a unique id for a type given a base type * \tparam Base Base type for id categorization * \tparam Type Type for id generation * \return Generated ID for Type and Base combination * * Example: * * unique_id() = 0 * unique_id() = 1 * * unique_id() = 0 */ template uint64_t unique_id() { static bool initialized = false; static uint64_t id = 0; if(initialized) return id; initialized = true; return id = _increment_id(); } } #endif // OPEN_CPP_UTILS_UNIQUE_ID_H