1 //-< REFERENCE.H >---------------------------------------------------*--------*
2 // FastDB                    Version 1.0         (c) 1999  GARRET    *     ?  *
3 // (Main Memory Database Management System)                          *   /\|  *
4 //                                                                   *  /  \  *
5 //                          Created:     20-Nov-98    K.A. Knizhnik  * / [] \ *
6 //                          Last update: 15-Feb-99    K.A. Knizhnik  * GARRET *
7 //-------------------------------------------------------------------*--------*
8 // Database table field reference type
9 //-------------------------------------------------------------------*--------*
10 
11 #ifndef __REFERENCE_H__
12 #define __REFERENCE_H__
13 
14 BEGIN_FASTDB_NAMESPACE
15 
16 /**
17  * Base class for all references
18  */
19 class FASTDB_DLL_ENTRY dbAnyReference {
20     friend class dbAnyCursor;
21     friend class dbDatabase;
22     friend class dbFieldDescriptor;
23   protected:
24     oid_t       oid;
25 
26   public:
27     dbAnyReference(oid_t oid = 0) {
28         this->oid = oid;
29     }
30     /**
31      * Get object idetifier
32      * @return object identifier of referenced object
33      */
getOid()34     oid_t getOid() const {
35         return oid;
36     }
37 
38     /**
39      * Operator for comparision of two references of the same type
40      */
41     bool operator > (dbAnyReference const& ref) const {
42         return oid > ref.oid;
43     }
44 
45     /**
46      * Operator for comparision of two references of the same type
47      */
48     bool operator < (dbAnyReference const& ref) const {
49         return oid < ref.oid;
50     }
51 
52     /**
53      * Check whether reference is null
54      */
isNull(dbAnyReference const & ref)55     friend bool isNull(dbAnyReference const& ref) {
56         return ref.oid == 0;
57     }
58 
59     /**
60      * Check whether reference is null
61      */
isNull()62     bool isNull() const { return oid == 0; }
63 
dbDescribeComponents(dbFieldDescriptor * fd)64     dbFieldDescriptor* dbDescribeComponents(dbFieldDescriptor* fd) {
65         fd->type = fd->appType = dbField::tpReference;
66         fd->refTable = NULL;
67         fd->dbsSize = fd->alignment = sizeof(oid_t);
68         return NULL;
69     }
70 };
71 
72 /**
73  * Class for null references
74  */
75 class FASTDB_DLL_ENTRY dbNullReference {};
76 
77 /**
78  * Single instance of  dbNullReference class used to pass or asign null reference
79  */
80 extern FASTDB_DLL_ENTRY dbNullReference null;
81 
82 #if (defined(_MSC_VER) && (_MSC_VER+0 < 1200 || _MSC_VER >= 1310)) || defined(__MWERKS__)
83 //
84 // Visual C++ prior to 5.0 version (with applied Service Pack 3)
85 // didn't support lazy template instantiation. As far as VC has bug
86 // with treating local function prototypes, we have to use friend function.
87 //
88 template<class T>
89 extern dbTableDescriptor* dbGetTableDescriptor(T*);
90 #endif
91 
92 
93 /**
94  * Template for reference parameterized by class of referenced records.
95  */
96 template<class T>
97 class dbReference : public dbAnyReference {
98   public:
99     /**
100      * Internal method used to describe reference
101      */
dbDescribeComponents(dbFieldDescriptor * fd)102     dbFieldDescriptor* dbDescribeComponents(dbFieldDescriptor* fd) {
103         fd->type = fd->appType = dbField::tpReference;
104 #if defined(_MSC_VER) && (_MSC_VER+0 < 1200 || _MSC_VER >= 1310) || defined(__MWERKS__)
105         fd->refTable = dbGetTableDescriptor((T*)0);
106 #else
107 #if GNUC_BEFORE(2,96) || defined(__VACPP_MULTI__) || defined(__IBMCPP__)
108         extern dbTableDescriptor* dbGetTableDescriptor(T*);
109         fd->refTable = dbGetTableDescriptor((T*)0);
110 #else
111         fd->refTable = &T::dbDescriptor;
112 #endif
113 #endif
114         fd->dbsSize = fd->alignment = sizeof(oid_t);
115         return NULL;
116     }
117 
118     /**
119      * Assignment operator
120      * @param ref assigned reference of the same type
121      * @return this reference
122      */
123     dbReference& operator = (dbReference const& ref) {
124         oid = ref.oid;
125         return *this;
126     }
127 
128     /**
129      * Operator for assigning null to reference
130      * @return this reference
131      */
132     dbReference& operator = (dbNullReference const&) {
133         oid = 0;
134         return *this;
135     }
136 
137     /**
138      * Unsafe assignment operator. Assign any refernce.
139      * @param ref assigned reference. If it is not of the same type - result is unpredicted.
140      * @return this reference
141      */
unsafeAssign(dbAnyReference const & ref)142     dbReference<T>& unsafeAssign(dbAnyReference const& ref) {
143         oid = ref.getOid();
144         return *this;
145     }
146 
147     /**
148      * Operator for comparision of two references of the same type
149      */
150     bool operator == (dbReference const& ref) const {
151         return oid == ref.oid;
152     }
153 
154     /**
155      * Operator for comparision of two references of the same type
156      */
157     bool operator > (dbReference const& ref) const {
158         return oid > ref.oid;
159     }
160 
161     /**
162      * Operator for comparision of two references of the same type
163      */
164     bool operator < (dbReference const& ref) const {
165         return oid < ref.oid;
166     }
167 
168     /**
169      * Operator for comparision of two references of the same type
170      */
171     bool operator != (dbReference const& ref) const {
172         return oid != ref.oid;
173     }
174 
175     /**
176      * Operator for cecking if reference is null
177      */
178     bool operator == (dbNullReference const&) const {
179         return oid == 0;
180     }
181 
182     /**
183      * Operator for cecking if reference is not null
184      */
185     bool operator != (dbNullReference const&) const {
186         return oid != 0;
187     }
188 
189     /**
190      * Constructor of null reference
191      */
dbReference(dbNullReference const &)192     dbReference(dbNullReference const&) : dbAnyReference(0) {}
193 
194     /**
195      * Copy constructor from reference of the same type
196      */
dbReference(dbReference const & ref)197     dbReference(dbReference const& ref) : dbAnyReference(ref.oid) {}
198 
199     /**
200      * Construstor of reference from OID. Use this constructor with care, because
201      * FastDB is not able to check if asigned OID belongs to the required table.
202      * If you assign OID of record friom some other table and then use this reference, results will
203      * be unpredicted (even database corruption is possible)
204      */
dbAnyReference(oid)205     dbReference(oid_t oid=0) : dbAnyReference(oid) {}
206 };
207 
208 END_FASTDB_NAMESPACE
209 
210 #endif
211 
212 
213 
214 
215 
216