The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
id.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
3 by David White <dave@whitevine.net>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#pragma once
17
18namespace n_unit {
19
20 struct unit_id
21 {
22 unit_id() : value(0) {}
23 explicit unit_id(std::size_t val) : value(val) {}
24 static const std::size_t highest_bit = static_cast<std::size_t>(1) << (sizeof(std::size_t) * 8 - 1);
25 std::size_t value;
26
27 bool is_fake() const { return (value & highest_bit) != 0; }
28 bool is_empty() const { return !value; }
29
30 static unit_id create_real(std::size_t val) { return unit_id(val); }
31 static unit_id create_fake(std::size_t val) { return unit_id(val | highest_bit); }
32
33 friend bool operator <(unit_id a, unit_id b) { return a.value < b.value; }
34 friend bool operator <=(unit_id a, unit_id b) { return a.value <= b.value; }
35 friend bool operator ==(unit_id a, unit_id b) { return a.value == b.value; }
36 friend bool operator >=(unit_id a, unit_id b) { return a.value >= b.value; }
37 friend bool operator >(unit_id a, unit_id b) { return a.value > b.value; }
38 };
39
41 {
42 private:
43 std::size_t next_id_;
44 std::size_t fake_id_;
46 public:
47 id_manager(std::size_t next_id) : next_id_(next_id) , fake_id_(0) {}
48 /** returns id for unit that is created */
50
52
53 /** Used for saving id to savegame */
54 std::size_t get_save_id() const;
55 void set_save_id(std::size_t);
56 /** Clears id counter after game */
57 void clear();
58 void reset_fake();
60 };
61
62}
void reset_fake()
Definition id.cpp:54
std::size_t get_save_id() const
Used for saving id to savegame.
Definition id.cpp:42
id_manager(std::size_t next_id)
Definition id.hpp:47
void clear()
Clears id counter after game.
Definition id.cpp:59
static id_manager manager_
Definition id.hpp:45
std::size_t next_id_
Definition id.hpp:43
void set_save_id(std::size_t)
Definition id.cpp:47
unit_id next_fake_id()
Definition id.cpp:35
std::size_t fake_id_
Definition id.hpp:44
static id_manager & global_instance()
Definition id.hpp:59
unit_id next_id()
returns id for unit that is created
Definition id.cpp:28
Definition id.cpp:25
friend bool operator>=(unit_id a, unit_id b)
Definition id.hpp:36
static unit_id create_fake(std::size_t val)
Definition id.hpp:31
bool is_empty() const
Definition id.hpp:28
bool is_fake() const
Definition id.hpp:27
static unit_id create_real(std::size_t val)
Definition id.hpp:30
friend bool operator>(unit_id a, unit_id b)
Definition id.hpp:37
friend bool operator<(unit_id a, unit_id b)
Definition id.hpp:33
unit_id(std::size_t val)
Definition id.hpp:23
friend bool operator<=(unit_id a, unit_id b)
Definition id.hpp:34
static const std::size_t highest_bit
Definition id.hpp:24
friend bool operator==(unit_id a, unit_id b)
Definition id.hpp:35
std::size_t value
Definition id.hpp:25
#define b