The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
id.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 
16 #ifndef UNIT_ID_HPP_INCLUDED
17 #define UNIT_ID_HPP_INCLUDED
18 
19 #include <ctime>
20 
21 #include <boost/noncopyable.hpp>
22 
23 namespace n_unit {
24 
25  struct unit_id
26  {
27  unit_id() : value(0) {}
28  explicit unit_id(size_t val) : value(val) {}
29  static const size_t highest_bit = static_cast<size_t>(1) << (sizeof(size_t) * 8 - 1);
30  size_t value;
31 
32  bool is_fake() const { return (value & highest_bit) != 0; }
33  bool is_empty() const { return !value; }
34 
35  static unit_id create_real(size_t val) { return unit_id(val); }
36  static unit_id create_fake(size_t val) { return unit_id(val + highest_bit); }
37 
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  friend bool operator ==(unit_id a, unit_id b) { return a.value == b.value; }
41  friend bool operator >=(unit_id a, unit_id b) { return a.value >= b.value; }
42  friend bool operator >(unit_id a, unit_id b) { return a.value > b.value; }
43  };
44 
45  class id_manager //: private boost::noncopyable
46  {
47  private:
48  size_t next_id_;
49  size_t fake_id_;
51  id_manager();
52  public:
53  id_manager(size_t next_id) : next_id_(next_id) , fake_id_(0) {}
54  /** returns id for unit that is created */
55  unit_id next_id();
56 
58 
59  /** Used for saving id to savegame */
60  size_t get_save_id() const;
61  void set_save_id(size_t);
62  /** Clears id counter after game */
63  void clear();
64  void reset_fake();
65  };
66 
67 }
68 
69 #endif
static unit_id create_real(size_t val)
Definition: id.hpp:35
unit_id next_fake_id()
Definition: id.cpp:37
friend bool operator<(unit_id a, unit_id b)
Definition: id.hpp:38
friend bool operator>(unit_id a, unit_id b)
Definition: id.hpp:42
size_t get_save_id() const
Used for saving id to savegame.
Definition: id.cpp:44
unit_id next_id()
returns id for unit that is created
Definition: id.cpp:30
Definition: id.cpp:23
GLuint const GLfloat * val
Definition: glew.h:2614
void set_save_id(size_t)
Definition: id.cpp:49
id_manager(size_t next_id)
Definition: id.hpp:53
bool is_fake() const
Definition: id.hpp:32
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
friend bool operator<=(unit_id a, unit_id b)
Definition: id.hpp:39
void clear()
Clears id counter after game.
Definition: id.cpp:61
GLsizei const GLfloat * value
Definition: glew.h:1817
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
size_t value
Definition: id.hpp:30
friend bool operator==(unit_id a, unit_id b)
Definition: id.hpp:40
friend bool operator>=(unit_id a, unit_id b)
Definition: id.hpp:41
size_t fake_id_
Definition: id.hpp:49
unit_id(size_t val)
Definition: id.hpp:28
static id_manager manager_
Definition: id.hpp:50
bool is_empty() const
Definition: id.hpp:33
static unit_id create_fake(size_t val)
Definition: id.hpp:36
void reset_fake()
Definition: id.cpp:56
size_t next_id_
Definition: id.hpp:48
static const size_t highest_bit
Definition: id.hpp:29