00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #ifndef CH_H
00013 #define CH_H
00014
00015 #include "Candidate/CandHandle.h"
00016
00017 template <class CandType>
00018 class CH : public CandHandle
00019 {
00020
00021 public:
00022 enum ErrorType {DereferenceNil};
00023
00024 CH();
00025 CH(CandBase *cb);
00026 CH(const CandHandle &ch);
00027
00028 template<class T>
00029 CH(const CH<T> &ch);
00030
00031 template<class T>
00032 operator CH<T>();
00033
00034 const CandType* operator->() const;
00035 const CandType& operator*() const;
00036
00037
00038
00039 CH<CandType> &operator=(const CandHandle &rhs);
00040
00041 template<class T>
00042 CH<CandType> &operator=(const CH<T> &rhs);
00043
00044 virtual CH<CandType> *DupHandle() const;
00045 bool IsValid() const;
00046
00047 const CandType& RCmd() const;
00048 CandType& WCmd();
00049
00050 ClassDef(CH,1)
00051 };
00052
00053 #include <iostream>
00054 using namespace std;
00055
00056 #include "TBuffer.h"
00057 #include "TClass.h"
00058
00059 #include "MessageService/MsgService.h"
00060
00061
00062 template <class CandType>
00063 CH<CandType>::CH() :
00064 CandHandle(0)
00065 {
00066 }
00067
00068
00069 template <class CandType>
00070 CH<CandType>::CH(CandBase *cb) :
00071 CandHandle(dynamic_cast<CandType*>(cb))
00072 {
00073 }
00074
00075
00076 template <class CandType>
00077 CH<CandType>::CH(const CandHandle &ch)
00078 {
00079 *this = const_cast<CandHandle &>(ch);
00080 }
00081
00082
00083 template <class CandType>
00084 CH<CandType> &CH<CandType>::operator=(const CandHandle &rhs)
00085 {
00086 CandHandle *chlhs = dynamic_cast<CandHandle*>(this);
00087 const CandHandle *chrhs = dynamic_cast<const CandHandle*>(&rhs);
00088 const CandType *refCrhs =
00089 dynamic_cast<const CandType*>(chrhs->GetCandBase());
00090
00091 if (refCrhs) {
00092 if (refCrhs == chlhs->GetCandBase()) {
00093 return *this;
00094 }
00095 else {
00096 CandHandle chnew(const_cast<CandType*>(refCrhs));
00097 *chlhs = chnew;
00098 return *this;
00099 }
00100 }
00101 else {
00102 CandHandle chnew((CandBase*)0);
00103 *chlhs = chnew;
00104 return *this;
00105 }
00106 }
00107
00108
00109 template<class CandType>
00110 CH<CandType> *CH<CandType>::DupHandle() const
00111 {
00112 return (new CH<CandType>(*this));
00113 }
00114
00115
00116 template<class CandType>
00117 bool CH<CandType>::IsValid() const
00118 {
00119 return dynamic_cast<const CandType*>(GetCandBase());
00120 }
00121
00122
00123 template <class CandType>
00124 const CandType *CH<CandType>::operator->() const
00125 {
00126 const CandType* cb = dynamic_cast<const CandType*>(GetCandBase());
00127 return cb;
00128 }
00129
00130
00131 template <class CandType>
00132 const CandType &CH<CandType>::operator*() const
00133 {
00134 const CandType* cb = dynamic_cast<const CandType*>(GetCandBase());
00135 return *cb;
00136 }
00137
00138
00139 template<class CandType>
00140 const CandType &CH<CandType>::RCmd() const
00141 {
00142 const CandType *cb = dynamic_cast<const CandType*>(GetCandBase());
00143 if (cb == 0) {
00144
00145
00146 throw CH::DereferenceNil;
00147 }
00148 return *cb;
00149 }
00150
00151
00152 template<class CandType>
00153 CandType &CH<CandType>::WCmd()
00154 {
00155 CandType *cb = dynamic_cast<CandType*>(GetOwnedCandBase());
00156 if (cb == 0) {
00157
00158
00159 throw CH::DereferenceNil;
00160 }
00161 return *cb;
00162 }
00163
00164 #ifndef __CINT__ // Templated functions don't work with rootcint
00165
00166
00167 template <class CandType>
00168 template<class T>
00169 CH<CandType>::CH(const CH<T> &ch)
00170 {
00171 *this = const_cast<CH<T>&>(ch);
00172 }
00173
00174
00175 template <class CandType>
00176 template<class T>
00177 CH<CandType>::operator CH<T>()
00178 {
00179 return CH<T>(GetCandBase());
00180 }
00181
00182
00183 template <class CandType>
00184 template<class T>
00185 CH<CandType> &CH<CandType>::operator=(const CH<T> &rhs)
00186 {
00187 CandHandle *chlhs = dynamic_cast<CandHandle*>(this);
00188 const CandHandle *chrhs = dynamic_cast<const CandHandle*>(&rhs);
00189 const CandType *refCrhs =
00190 dynamic_cast<const CandType*>(chrhs->GetCandBase());
00191
00192 if (refCrhs) {
00193 if (refCrhs == chlhs->GetCandBase()) {
00194 return *this;
00195 }
00196 else {
00197 CandHandle chnew(const_cast<CandType*>(refCrhs));
00198 *chlhs = chnew;
00199 return *this;
00200 }
00201 }
00202 else {
00203 CandHandle chnew((CandBase*)0);
00204 *chlhs = chnew;
00205 return *this;
00206 }
00207 }
00208
00209 #endif // __CINT__
00210
00211 #endif // CH_H