references._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * $Id: references.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19 #include <macros.h>
20 #include <exception.h>
21 #include <database.h>
22 class references {
23 public:
24 
25 
26 
27 
28 
29  mapping mReferences;
30 
31  void require_save(string|void a, string|void b);
32 int get_object_class();
33 object get_environment();
34 string get_identifier();
35 
36 /**
37  * Initialize the reference storage.
38  *
39  */
40 private:
41  void init_references()
42 {
43  mReferences = ([ ]);
44 }
45 
46 public:
47 
48 
49 /**
50  * Add a reference to this object. The reference functionality is
51  * maintained by the server itself and shouldnt be called by other code.
52  *
53  * @param object ref - the reference.
54  * @see remove_reference
55  */
56 void add_reference(object ref)
57 {
58  mReferences[ref] = 1;
59  require_save(STORE_REFS);
60 }
61 
62 /**
63  * Remove a reference from this object. The function is for internal use.
64  * The references are maintained by the server itself.
65  *
66  * @param object ref - a reference to remove.
67  * @see add_reference
68  */
69 void remove_reference(object ref)
70 {
71  if ( CALLER->get_object_id() != ref->get_object_id() )
72  THROW("Removing reference by non-referencing object ("+
73  CALLER->get_object_id()+":"+ref->get_object_id()+
74  ")", E_ACCESS);
75  m_delete(mReferences, ref);
76  require_save(STORE_REFS);
77 }
78 
79 /**
80  * Get the mapping of references. The mapping is in the form
81  * ([ ref:1 ]) - a mapping is used for faster lookup.
82  *
83  * @see add_reference
84  */
85 mapping get_references()
86 {
87  return copy_value(mReferences);
88 }
89 
90 /**
91  * Get the array of all referencing objects.
92  *
93  * @see add_reference
94  */
95 array get_referencing()
96 {
97  return indices(mReferences);
98 }
99 
100 
101 /**
102  * Store the references in the database. Database calls this function.
103  *
104  * @return refereces.
105 private:
106  * @see restore_references
107  */
108 mixed
109 private:
110 store_references()
111 {
112  if (CALLER != _Database ) THROW("Caller is not Database !", E_ACCESS);
113  return ([ "References": mReferences, ]);
114 }
115 
116 public:
117 
118 /**
119  * The object is loaded and its references restored by the database.
120  *
121  * @param mixed data - the reference data.
122 private:
123  * @see store_references
124  */
125 private:
126 void restore_references(mixed data)
127 {
128  if (CALLER != _Database ) THROW("Caller is not Database !", E_ACCESS);
129  mReferences = data["References"];
130 }
131 
132 public:
133 
134 
135 };