open-cpp-utils 0.0.1
Loading...
Searching...
No Matches
unique_id.h
1// =====================================================================================================================
2// Copyright 2024 Medusa Slockbower
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// =====================================================================================================================
15
16#ifndef ENGINE_UNIQUEID_H
17#define ENGINE_UNIQUEID_H
18
19#include "types.h"
20
21namespace open_cpp_utils
22{
23
24// Internal function for incrementing an id associated with a type
25template<typename Base> uint64_t _increment_id()
26{
27 static uint64_t current = 0;
28 return current++;
29}
30
44template<typename Base, typename Type> uint64_t unique_id()
45{
46 static bool initialized = false;
47 static uint64_t id = 0;
48
49 if(initialized) return id;
50 initialized = true;
51 return id = _increment_id<Base>();
52}
53
54}
55
56#endif //ENGINE_UNIQUEID_H