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