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