source: golgotha/src/golg/global_id.hh

Last change on this file was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 2.7 KB
RevLine 
[80]1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#ifndef G1_GLOBAL_ID_HH
10#define G1_GLOBAL_ID_HH
11
12#include "arch.hh"
13#include "g1_limits.hh"
14
15class g1_object_class;
16
17class g1_global_id_reset_notifier
18{
19public:
20  g1_global_id_reset_notifier *next;
21  static g1_global_id_reset_notifier *first;
22  g1_global_id_reset_notifier();
23  virtual void reset() = 0;
24  ~g1_global_id_reset_notifier();
25};
26
27class g1_global_id_manager_class
28{
29public:
30  enum
31  {
32    ID_INCREMENT=(1<<(G1_MAX_OBJECTS_BITS+1)),
33    ID_MASK=ID_INCREMENT-1,
34  };
35private:
36
37  w32 obj_id[G1_MAX_OBJECTS];
38  g1_object_class *obj[G1_MAX_OBJECTS];
39  w16 first_free;
40public:
41  g1_global_id_manager_class();
42
43  void init();
44  void claim_freespace();
45  void free_objects();
46
47  i4_bool check_id(w32 id) const { return id==obj_id[id&ID_MASK]; }
48
49  i4_bool preassigned(w32 id) const;
50  void assign(w32 id, g1_object_class *for_who);
51  w32 alloc(g1_object_class *for_who);
52  void free(w32 id);
53  g1_object_class *get(w32 id) { return obj[id&ID_MASK]; }
54  g1_object_class *checked_get(w32 id) { return check_id(id) ? get(id) : 0; }
55
56  w32 invalid_id() { return 0; }
57
58  class remapper
59  {
60  protected:
61    friend g1_global_id_manager_class;
62    g1_global_id_manager_class *gid;
63    w32 *map;
64    // remapping functions
65    remapper(g1_global_id_manager_class *gid);
66    ~remapper();
67  public:
68    w32 &operator[](w32 id) { return map[id&ID_MASK]; }
69  };
70  remapper* alloc_remapping() { return new remapper(this); }
71  void free_remapping(remapper* mapper) { delete mapper; }
72
73  void debug(w32 flag_pass=0xffffffff);
74};
75
76extern g1_global_id_manager_class g1_global_id;
77class g1_loader_class;
78class g1_saver_class;
79
80// a convient way to access objects through global id's
81class g1_id_ref
82{
83public:
84  w32 id;
85  g1_object_class *get() const { return g1_global_id.checked_get(id); }
86  i4_bool valid() { return g1_global_id.check_id(id); }
87  g1_id_ref() { id=0; }
88  g1_id_ref(w32 id) : id(id) {}
89  g1_id_ref(g1_object_class *o);
90  g1_object_class *operator->() const { return get(); }
91  g1_id_ref& operator=(const g1_id_ref &r) { id=r.id;  return *this; }
92  g1_id_ref& operator=(w32 _id) { id=_id;  return *this; }
93  g1_id_ref& operator=(g1_object_class *o);
94  void save(g1_saver_class *fp);
95  void load(g1_loader_class *fp);
96};
97
98#endif
Note: See TracBrowser for help on using the repository browser.