CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FwdRef.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_FwdRef_h
2 #define DataFormats_Common_FwdRef_h
3 
4 /*----------------------------------------------------------------------
5 
6 FwdRef: A template for a interproduct reference to a member of a product.
7 
8 ----------------------------------------------------------------------*/
84 /*----------------------------------------------------------------------
85 // This defines the public interface to the class FwdRef<C, T, F>.
86 // C is the collection type.
87 // T (default C::value_type) is the type of an element in the collection.
88 //
89 // ProductID productID is the product ID of the collection.
90 // key_type itemKey is the key of the element in the collection.
91 // C::value_type *itemPtr is a C++ pointer to the element
92 // FwdRef<C, T, F> const& ref is another FwdRef<C, T, F>
93 
94 // Constructors
95  FwdRef(); // Default constructor
96  FwdRef(FwdRef<C, T> const& ref); // Copy constructor (default, not explicitly specified)
97 
98  FwdRef(Ref<C,T,F> const & fwdRef, Ref<C,T,F> const & backRef);
99 
100 // Destructor
101  virtual ~FwdRef() {}
102 
103  // Operators and methods
104  FwdRef<C, T>& operator=(FwdRef<C, T> const&); // assignment (default, not explicitly specified)
105  T const& operator*() const; // dereference
106  T const* const operator->() const; // member dereference
107  bool operator==(FwdRef<C, T> const& ref) const; // equality
108  bool operator!=(FwdRef<C, T> const& ref) const; // inequality
109  bool operator<(FwdRef<C, T> const& ref) const; // ordering
110  bool isNonnull() const; // true if an object is referenced
111  bool isNull() const; // equivalent to !isNonnull()
112  bool operator!() const; // equivalent to !isNonnull()
113  ----------------------------------------------------------------------*/
114 
117 
118 namespace edm {
119 
120  template <typename C,
121  typename T = typename refhelper::ValueTrait<C>::value,
122  typename F = typename refhelper::FindTrait<C, T>::value>
123  class FwdRef {
124 
125 
126  public:
128  typedef C product_type;
129  typedef T value_type;
130  typedef T const element_type; //used for generic programming
131  typedef F finder_type;
132  typedef typename boost::binary_traits<F>::second_argument_type argument_type;
136 
138  FwdRef() : ref_(), backRef_() {}
139 
142  Ref<C,T,F> const & backRef) :
143  ref_(ref), backRef_(backRef) {}
144 
146  ~FwdRef() {}
147 
149  T const&
150  operator*() const;
151 
153  T const*
154  operator->() const;
155 
157  T const* get() const {
158  if ( ref_.isNonnull() ) {
159  return ref_.get();
160  } else if ( backRef_.isNonnull() ) {
161  return backRef_.get();
162  } else {
163  return 0;
164  }
165  }
166 
168  bool isNull() const {return !isNonnull(); }
169 
171  //bool isNonnull() const {return id().isValid(); }
172  bool isNonnull() const { return ref_.isNonnull() || backRef_.isNonnull(); }
173 
175  bool operator!() const {return isNull();}
176 
177  Ref<C,T,F> const & ref() const { return ref_; }
178  Ref<C,T,F> const & backRef() const { return backRef_; }
179 
182  if ( ref_.productGetter() ) return ref_.productGetter();
183  else return backRef_.productGetter();
184  }
185 
187  // Accessor must get the product if necessary
188  C const* product() const;
189 
191  ProductID id() const {return ref_.isNonnull() ? ref_.id() : backRef_.id();}
192 
193 
195  key_type key() const {return ref_.isNonnull() ? ref_.key() : backRef_.key() ;}
196 
197  bool hasProductCache() const {return ref_.hasProductCache() || backRef_.hasProductCache();}
198 
201  bool isAvailable() const {return ref_.isAvailable() || backRef_.isAvailable();}
202 
204  bool isTransient() const {return ref_.isTransient();}
205 
206  //Used by ROOT storage
208 
209  private:
210  Ref<C,T,F> ref_;
211  Ref<C,T,F> backRef_;
212  };
213 }
214 
215 #include "DataFormats/Common/interface/RefProd.h"
216 
217 namespace edm {
218 
219 
221  // Accessor must get the product if necessary
222  template <typename C, typename T, typename F>
223  inline
224  C const*
226  return ref_.isNonnull() && ref_.isAvailable() ?
227  ref_.product() :
228  backRef_.product();
229  }
230 
232  template <typename C, typename T, typename F>
233  inline
234  T const&
236  return ref_.isNonnull() && ref_.isAvailable() ?
237  ref_.operator*() :
238  backRef_.operator*();
239  }
240 
242  template <typename C, typename T, typename F>
243  inline
244  T const*
246  return ref_.isNonnull() && ref_.isAvailable() ?
247  ref_.operator->() :
248  backRef_.operator->();
249  }
250 
251 
254  template <typename C, typename T, typename F>
255  inline
256  bool
257  operator==(FwdRef<C, T, F> const& lhs, FwdRef<C, T, F> const& rhs) {
258  return
259  (lhs.ref() == rhs.ref() ) &&
260  (lhs.backRef() == rhs.backRef() )
261  ;
262  }
263 
266  template <typename C, typename T, typename F>
267  inline
268  bool
269  operator==(Ref<C, T, F> const& lhs, FwdRef<C, T, F> const& rhs) {
270  return
271  (lhs == rhs.ref() ) ||
272  (lhs == rhs.backRef() )
273  ;
274  }
275 
278  template <typename C, typename T, typename F>
279  inline
280  bool
281  operator==(FwdRef<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
282  return
283  (lhs.ref() == rhs ) ||
284  (lhs.backRef() == rhs )
285  ;
286  }
287 
288 
289 
290  template <typename C, typename T, typename F>
291  inline
292  bool
293  operator!=(FwdRef<C, T, F> const& lhs, FwdRef<C, T, F> const& rhs) {
294  return !(lhs == rhs);
295  }
296 
297  template <typename C, typename T, typename F>
298  inline
299  bool
300  operator!=(Ref<C, T, F> const& lhs, FwdRef<C, T, F> const& rhs) {
301  return !(lhs == rhs);
302  }
303 
304  template <typename C, typename T, typename F>
305  inline
306  bool
307  operator!=(FwdRef<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
308  return !(lhs == rhs);
309  }
310 
313  template <typename C, typename T, typename F>
314  inline
315  bool
316  operator<(FwdRef<C, T, F> const& lhs, FwdRef<C, T, F> const& rhs) {
317  return (lhs.ref() < rhs.ref() );
318  }
319 
320  template <typename C, typename T, typename F>
321  inline
322  bool
323  operator<(Ref<C, T, F> const& lhs, FwdRef<C, T, F> const& rhs) {
324  return (lhs < rhs.ref() );
325  }
326 
327  template <typename C, typename T, typename F>
328  inline
329  bool
330  operator<(FwdRef<C, T, F> const& lhs, Ref<C, T, F> const& rhs) {
331  return (lhs.ref() < rhs );
332  }
333 
334 }
335 
336 
337 #endif
type
Definition: HCALResponse.h:22
C product_type
for export
Definition: FwdRef.h:128
bool operator!=(debugging_allocator< X > const &, debugging_allocator< Y > const &)
#define CMS_CLASS_VERSION(_version_)
bool operator!() const
Checks for null.
Definition: FwdRef.h:175
T const element_type
Definition: FwdRef.h:130
bool operator==(debugging_allocator< X > const &, debugging_allocator< Y > const &)
key_type key() const
Accessor for product key.
Definition: FwdRef.h:195
EDProductGetter const * productGetter() const
Accessor for product getter.
Definition: FwdRef.h:181
bool isNull() const
Checks for null.
Definition: FwdRef.h:168
T const * operator->() const
Member dereference operator.
Definition: FwdRef.h:245
~FwdRef()
Destructor.
Definition: FwdRef.h:146
bool hasProductCache() const
Definition: FwdRef.h:197
bool isNonnull() const
Checks for non-null.
Definition: FwdRef.h:172
FwdRef()
Default constructor needed for reading from persistent store. Not for direct use. ...
Definition: FwdRef.h:138
T value_type
Definition: FwdRef.h:129
Ref< C, T, F > backRef_
Definition: FwdRef.h:211
Ref< C, T, F > ref_
Definition: FwdRef.h:210
FwdRef(Ref< C, T, F > const &ref, Ref< C, T, F > const &backRef)
General purpose constructor from 2 refs (forward and backward.
Definition: FwdRef.h:141
#define private
Definition: FWFileEntry.h:18
bool isTransient() const
Checks if this ref is transient (i.e. not persistable).
Definition: FwdRef.h:204
Ref< C, T, F > const & backRef() const
Definition: FwdRef.h:178
Ref< C, T, F > const & ref() const
Definition: FwdRef.h:177
long double T
F finder_type
Definition: FwdRef.h:131
C const * product() const
Accessor for product collection.
Definition: FwdRef.h:225
boost::remove_cv< typename boost::remove_reference< argument_type >::type >::type key_type
Definition: FwdRef.h:133
bool isAvailable() const
Definition: FwdRef.h:201
boost::binary_traits< F >::second_argument_type argument_type
Definition: FwdRef.h:132
ProductID id() const
Accessor for product ID.
Definition: FwdRef.h:191
T const & operator*() const
Dereference operator.
Definition: FwdRef.h:235