BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
IfdKey.h
Go to the documentation of this file.
1#ifndef IfdKey_HH
2#define IfdKey_HH
3//--------------------------------------------------------------------------
4// File and Version Information:
5// $Id: IfdKey.h,v 1.1.1.1 2005/04/21 01:18:05 zhangy Exp $
6//
7// Description:
8// IfdKey is a Key, i.e., something that knows operator== and,
9// maybe someday, operator<. Subclasses, like IfdIntKey and IfdStrKey
10// allow keys based on integer and string values. Another derivative,
11// IfdTypeKey<T> serves as mapping between C++ static classes and
12// Keys. Finally, IfdKey wroks with its derivative, IfdCompositeKey,
13// to form a composite pattern that allows lists, trees, etc. of
14// keys to be formed and yet have an op== defined on it.
15//
16// Usage: Keys do not know op=, i.e., assignemnt, and I have supressed
17// the copy ctor. You should pass them by reference or poiner. There
18// is a clone() method, so a consumer can effective copy a key. Note
19// that this means that the caller retains ownership of the key, and
20// the "consumer" that takes a copy *owns* the copy.
21//
22// Author List:
23// Ed Frank University of Pennsylvania
24//
25// History:
26// Ed Frank 17 Nov 96 Creation of first version
27//
28// Bugs:
29//
30//------------------------------------------------------------------------
31
32//
33// Reading IntIfdKey.hh after this is helpful. Then tackle CompositeIfdKey.hh
34//
35
36
37#include <assert.h>
38#include <stdlib.h>
39class HepString;
40#if !(defined(__GNUC__) && (__GNUC__ < 3) && (__GNUC_MINOR__ < 95)) // BABAR_IOSTREAMS_MIGRATION
41#include <iosfwd>
42#else // BABAR_IOSTREAMS_MIGRATION
43class ostream;
44#endif // BABAR_IOSTREAMS_MIGRATION
45class IfdDictKey;
46//template<class T> class TypeKey;
47
48class IfdKey {
49public:
50 virtual ~IfdKey();
51
52 virtual int operator==( const IfdKey & ) const = 0;
53 inline int operator!=( const IfdKey &k ) const;
54
55
56 virtual void add( const IfdKey& );
57
58 inline int cardinality(void) const;
59 // For Composite keys, == # of added elements. For Non-Composites, == 0;
60
61#if !(defined(__GNUC__) && (__GNUC__ < 3) && (__GNUC_MINOR__ < 95)) // BABAR_IOSTREAMS_MIGRATION
62 virtual void print( std::ostream &o ) const = 0;
63#else // BABAR_IOSTREAMS_MIGRATION
64 virtual void print( ostream &o ) const = 0;
65#endif // BABAR_IOSTREAMS_MIGRATION
66#if !(defined(__GNUC__) && (__GNUC__ < 3) && (__GNUC_MINOR__ < 95)) // BABAR_IOSTREAMS_MIGRATION
67 friend std::ostream& operator<<( std::ostream& o, const IfdKey & k );
68#else // BABAR_IOSTREAMS_MIGRATION
69 friend ostream& operator<<( ostream& o, const IfdKey & k );
70#endif // BABAR_IOSTREAMS_MIGRATION
71
72 virtual IfdKey* clone( void ) const = 0;
73 // Clone makes an exact copy of an object. It is crucial that
74 // derivatives implement this in a way that places the created
75 // object on the heap.
76
77 virtual unsigned int hash( void ) const
78 { return _hashVal;}
79
80 unsigned int _hashVal; //$$ public (ick) for now. clean later.
81
82 static unsigned int nHashBuckets( void )
83 { return _nHashBuckets; }
84
85protected:
86 enum { _nHashBuckets = 1031 }; // .33 msec/ev, not opt
87
90
91 IfdKey( keyKind kind );
92 // IfdKey is a pure interface. Prevent creation of them. Only
93 // derivatives can be created and this ctor requires them to
94 // define their keykind at ctor time. This could have been
95 // a virtual getKeyKind, but that affected performance. See below.
96
97
98 inline IfdKey::keyKind getKeyKind( void ) const { return _myKeyKind; }
99 // IfdKeys are things that understand operator==. We have made
100 // this machinery rather than a simple overloaded global op==
101 // because we wanted CompositeKey's, i.e., IfdKeys with subfields.
102 // The enum above allows the subfields to be of various kinds,
103 // each of which understands operator==. Thus we can have
104 // a composite key in which the first field is a TypeKey and the
105 // second is a string. Below is an anonymous
106 // union that we use to store the actual token being compared.
107 // The enum allows us to access the union correctly. If we
108 // were to put these data into the derivative classes, we
109 // would end up having to cast the argument of operator==(IfdKey &k)
110 // to a specific type before we could do the ==. This seems
111 // cleaner. This is slightly gross, but (1) we really do gain
112 // something (2) Once we've handled int,string, and TypeKeys,
113 // we are basically done, i.e., we don't expect much extension
114 // here.
115
117 // Originally, getKeyKind was virtual. But that was too slow. It
118 // is reasonable to consider this a difference in value rather than
119 // difference in behavior, so I've made getKeyKind a non-virtual
120 // accessor to _myKeyKind and we set _myKeyKind in the ctors.
121
123 // See accessor.
124
125 union {
127 unsigned int uintVal;
128 char* strVal;
129 };
130
131
132 friend class IfdIntKey;
133 friend class IfdStrKey;
134 friend class IfdTypeKeyIFace;
135 friend class IfdCompositeKey;
137 // We need to declare these classes as friends because, for
138 // example, IntKey::op==( IfdKey& k) must get at k.intVal, which
139 // is protected. Even though all of the IfdKey derivatives inherit
140 // from IfdKey, the rule is that you can only get at protected
141 // data in your *own* class, so IntKey can not get at IfdKey::intVal,
142 // only IntKey::intVal.
143
144 friend unsigned ifdKeyHash(const IfdDictKey& k);
145
146private:
147 virtual void make_vtab( void ) const;
148
149 // Copy ctor and assignment op.
150 // Not sure what to do about these. I think they may not
151 // make any sense, e.g., what if someone has IfdIntKey ik, IfdStrKey sk,
152 // and tries to do ik=sk? That is nonsense. It may be OK to do
153 // a copy ctor and implement it via clone; however, I will just turn
154 // off the copy ctor for now too. If someone needs it, ask me
155 // and I'll think more about it.
156 //
157
158 IfdKey( const IfdKey &) { ::abort(); }
159 IfdKey& operator=( IfdKey &) { if ( this != 0 ) ::abort(); return *this; }
160};
161
162
163//*****************************************************************************
164inline
165int
167//*****************************************************************************
168 return _myCardinality;
169}
170
171//*****************************************************************************
172//inline
173//IfdKey::keyKind
174//IfdKey::getKeyKind( void ) const {
175//*****************************************************************************
176// return _myKeyKind;
177//}
178
179//*****************************************************************************
180inline
181int
182IfdKey::operator != ( const IfdKey &k ) const {
183//*****************************************************************************
184
185 return ! ( *this == k );
186}
187
188
189#endif /* IFDKEY_HH */
Definition: IfdKey.h:48
keyKind _myKeyKind
Definition: IfdKey.h:116
int _myCardinality
Definition: IfdKey.h:122
friend class IfdCompositeKey
Definition: IfdKey.h:135
friend std::ostream & operator<<(std::ostream &o, const IfdKey &k)
virtual IfdKey * clone(void) const =0
@ _nHashBuckets
Definition: IfdKey.h:86
virtual int operator==(const IfdKey &) const =0
virtual ~IfdKey()
Definition: IfdKey.cxx:29
unsigned int uintVal
Definition: IfdKey.h:127
static unsigned int nHashBuckets(void)
Definition: IfdKey.h:82
unsigned int _hashVal
Definition: IfdKey.h:80
friend class IfdTypeKeyIFace
Definition: IfdKey.h:134
virtual void add(const IfdKey &)
Definition: IfdKey.cxx:34
int intVal
Definition: IfdKey.h:126
friend class BdbOdfIfdTypeKeyIFace
Definition: IfdKey.h:136
friend unsigned ifdKeyHash(const IfdDictKey &k)
virtual unsigned int hash(void) const
Definition: IfdKey.h:77
keyKind
Definition: IfdKey.h:88
@ typeKey
Definition: IfdKey.h:89
@ intKey
Definition: IfdKey.h:88
@ odfTypeKey
Definition: IfdKey.h:89
@ compositeKey
Definition: IfdKey.h:88
@ strKey
Definition: IfdKey.h:88
char * strVal
Definition: IfdKey.h:128
IfdKey::keyKind getKeyKind(void) const
Definition: IfdKey.h:98
int operator!=(const IfdKey &k) const
Definition: IfdKey.h:182
virtual void print(std::ostream &o) const =0
int cardinality(void) const
Definition: IfdKey.h:166
std::ostream ostream
Definition: bpkt_streams.h:41