source: golgotha/src/golg/reference.hh @ 80

Last change on this file since 80 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
Line 
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 REF_MAN_HH
10#define REF_MAN_HH
11
12#include "arch.hh"
13
14class g1_object_class;
15class g1_saver_class;
16class g1_loader_class;
17
18
19// this structure is used to hold a linked list of objects needing notification
20// that an object is being deleted.  This is so if a missle is tracking a tank
21// and the tank blows up before the missle gets there, the missle can be notified
22// the object is being deleted and it can decide to track something else, detonate,
23// or go in a strait line.
24
25// references save notifiee and ref as indexes into an object array
26// because these objects may not exsist on load, they are kept as indexes until
27// the whole level is loaded, so you cannot use a reference directly after load()
28// but by the time your think function is called your reference will be valid
29
30class g1_reference_class
31{
32protected:
33  friend class g1_remove_manager_class;
34  friend class g1_saver_class;
35  friend class g1_loader_class;
36  g1_object_class *ref;
37
38  void remove_ref();
39
40  g1_reference_class() : ref(0), next(0) {}
41  g1_reference_class(g1_object_class *object_being_referenced)
42  { reference_object(object_being_referenced); }
43
44  void reference_object(g1_object_class *object_being_referenced);
45public:
46  g1_reference_class *next;
47 
48  void save(g1_saver_class *fp);
49  void load(g1_loader_class *fp);
50
51  i4_bool valid() const { return ref!=0; }
52
53  i4_bool operator==(const g1_reference_class &a) const { return ref == a.ref; }
54  i4_bool operator!=(const g1_reference_class &a) const { return ref != a.ref; }
55
56  ~g1_reference_class();
57};
58
59template <class T>
60class g1_typed_reference_class : public g1_reference_class
61{
62public:
63  T *get() const { return (T *)ref; }
64  T& operator*() const { return *((T *)ref); }
65  T* operator->() const { return (T *)ref; }
66
67  g1_typed_reference_class<T>() : g1_reference_class() {}
68  g1_typed_reference_class<T>(const g1_typed_reference_class<T> &r) : g1_reference_class(r.ref) {}
69
70  g1_typed_reference_class<T>& operator=(const g1_typed_reference_class<T> &r)
71  {
72    g1_reference_class::reference_object(r.ref);
73    return *this;
74  }
75
76  g1_typed_reference_class<T>& operator=(const T *ref)
77  {
78    g1_reference_class::reference_object((g1_object_class *)ref);
79    return *this;
80  }
81};
82
83#endif
84
85
86
Note: See TracBrowser for help on using the repository browser.