Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef PLAYA_HANDLE_HPP
00043 #define PLAYA_HANDLE_HPP
00044
00045 #include "PlayaDefs.hpp"
00046 #include "PlayaOut.hpp"
00047 #include "PlayaPrintable.hpp"
00048 #include "Teuchos_Describable.hpp"
00049 #include "PlayaHandleable.hpp"
00050 #include "PlayaExceptions.hpp"
00051 #include "PlayaObjectWithVerbosity.hpp"
00052 #include "Teuchos_RefCountPtr.hpp"
00053 #include "Teuchos_TypeNameTraits.hpp"
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #define HANDLE_CTORS(handle, contents) \
00068 \
00069 handle() : Playa::Handle<contents >() {;} \
00070 \
00071 handle(Playa::Handleable<contents >* rawPtr) : Playa::Handle<contents >(rawPtr) {;} \
00072 \
00073 handle(const Teuchos::RCP<contents >& smartPtr) : Playa::Handle<contents >(smartPtr){;}
00074
00075
00076
00077 namespace Playa
00078 {
00079 using Teuchos::RCP;
00080 using Teuchos::rcp;
00081
00082
00083
00084
00085 template <class X>
00086 class ConstHandleTraits
00087 {
00088 public:
00089 typedef X NonconstType;
00090 };
00091
00092
00093
00094
00095 template <class X>
00096 class ConstHandleTraits<const X>
00097 {
00098 public:
00099 typedef X NonconstType;
00100 };
00101
00102
00103
00104
00105
00106
00107
00108 template <class PointerType>
00109 class Handle
00110 {
00111 public:
00112
00113 Handle() : ptr_() {;}
00114
00115
00116 Handle(const RCP<PointerType>& _ptr) : ptr_(_ptr) {;}
00117
00118
00119 Handle(Handleable<PointerType>* rawPtr) : ptr_(rawPtr->getRcp()) {;}
00120
00121
00122 const RCP<PointerType>& ptr() const {return ptr_;}
00123
00124
00125 RCP<PointerType>& ptr() {return ptr_;}
00126
00127
00128
00129
00130
00131 void print(std::ostream& os) const ;
00132
00133
00134
00135
00136
00137
00138
00139
00140 std::string description() const ;
00141
00142
00143
00144 std::string fallbackDescription() const ;
00145
00146
00147 int verb() const ;
00148
00149
00150 void setVerb(int v);
00151
00152 private:
00153 RCP<PointerType> ptr_;
00154 };
00155
00156
00157 template <class PointerType> inline
00158 void Handle<PointerType>::print(std::ostream& os) const
00159 {
00160 const Printable* p = dynamic_cast<const Printable*>(ptr_.get());
00161 const Describable* d = dynamic_cast<const Describable*>(ptr_.get());
00162
00163 if (p!=0) p->print(os);
00164 else if (d!=0) os << d->description();
00165 else os << fallbackDescription();
00166 }
00167
00168
00169 template <class PointerType> inline
00170 std::string Handle<PointerType>::description() const
00171 {
00172 const Describable* d = dynamic_cast<const Describable*>(ptr_.get());
00173 TeuchosOStringStream oss;
00174
00175 if (d!=0) oss << d->description();
00176 else oss << fallbackDescription();
00177
00178 return oss.str();
00179 }
00180
00181 template <class PointerType> inline
00182 std::string Handle<PointerType>::fallbackDescription() const
00183 {
00184 typedef typename ConstHandleTraits<PointerType>::NonconstType NC;
00185 TeuchosOStringStream oss;
00186
00187 oss << "Handle[" << TypeNameTraits<NC>::name()
00188 << ", ptr=" << ptr_.get() << "]";
00189 return oss.str();
00190 }
00191
00192
00193
00194
00195 template <class PointerType> inline
00196 int Handle<PointerType>::verb() const
00197 {
00198 const ObjectWithVerbosity* v
00199 = dynamic_cast<const ObjectWithVerbosity*>(ptr_.get());
00200 if (v) return v->verb();
00201 return 0;
00202 }
00203
00204
00205
00206 template <class PointerType> inline
00207 void Handle<PointerType>::setVerb(int verbosity)
00208 {
00209 ObjectWithVerbosity* v
00210 = dynamic_cast<ObjectWithVerbosity*>(ptr_.get());
00211 if (v) v->setVerb(verbosity);
00212 TEUCHOS_TEST_FOR_EXCEPTION(v==0, RuntimeError,
00213 "attempt to set verbosity on a handle that doesn't wrap "
00214 "an ObjectWithVerbosity subtype");
00215 }
00216
00217 template <class PointerType> inline
00218 std::ostream& operator<<(std::ostream& os, const Playa::Handle<PointerType>& h)
00219 {
00220 h.print(os);
00221 return os;
00222 }
00223
00224 }
00225
00226 #define STREAM_OUT(handleType) \
00227 inline std::ostream& operator<<(std::ostream& os, const handleType& h) \
00228 {h.print(os); return os;}
00229
00230
00231
00232 #endif
00233