The Battle for Wesnoth  1.17.0-dev
id.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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 
18 #include <ctime>
19 
20 namespace n_unit {
21 
22  struct unit_id
23  {
24  unit_id() : value(0) {}
25  explicit unit_id(std::size_t val) : value(val) {}
26  static const std::size_t highest_bit = static_cast<std::size_t>(1) << (sizeof(std::size_t) * 8 - 1);
27  std::size_t value;
28 
29  bool is_fake() const { return (value & highest_bit) != 0; }
30  bool is_empty() const { return !value; }
31 
32  static unit_id create_real(std::size_t val) { return unit_id(val); }
33  static unit_id create_fake(std::size_t val) { return unit_id(val | highest_bit); }
34 
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  friend bool operator >=(unit_id a, unit_id b) { return a.value >= b.value; }
39  friend bool operator >(unit_id a, unit_id b) { return a.value > b.value; }
40  };
41 
42  class id_manager
43  {
44  private:
45  std::size_t next_id_;
46  std::size_t fake_id_;
48  public:
49  id_manager(std::size_t next_id) : next_id_(next_id) , fake_id_(0) {}
50  /** returns id for unit that is created */
51  unit_id next_id();
52 
53  unit_id next_fake_id();
54 
55  /** Used for saving id to savegame */
56  std::size_t get_save_id() const;
57  void set_save_id(std::size_t);
58  /** Clears id counter after game */
59  void clear();
60  void reset_fake();
61  static id_manager& global_instance() {return manager_;}
62  };
63 
64 }
static unit_id create_fake(std::size_t val)
Definition: id.hpp:33
friend bool operator<(unit_id a, unit_id b)
Definition: id.hpp:35
friend bool operator>(unit_id a, unit_id b)
Definition: id.hpp:39
static id_manager & global_instance()
Definition: id.hpp:61
id_manager(std::size_t next_id)
Definition: id.hpp:49
bool is_empty() const
Definition: id.hpp:30
#define a
Definition: id.cpp:24
void clear(const std::string &key)
Definition: general.cpp:186
#define b
friend bool operator<=(unit_id a, unit_id b)
Definition: id.hpp:36
friend bool operator==(unit_id a, unit_id b)
Definition: id.hpp:37
std::size_t value
Definition: id.hpp:27
static unit_id create_real(std::size_t val)
Definition: id.hpp:32
friend bool operator>=(unit_id a, unit_id b)
Definition: id.hpp:38
bool is_fake() const
Definition: id.hpp:29
static id_manager manager_
Definition: id.hpp:47
std::size_t next_id_
Definition: id.hpp:45
std::size_t fake_id_
Definition: id.hpp:46
unit_id(std::size_t val)
Definition: id.hpp:25
static const std::size_t highest_bit
Definition: id.hpp:26