AnyPtr.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *
00003  * Copyright (C) 2009-2011 Cassio Neri Moreira
00004  *
00005  * This file is part of the KeyValue library.
00006  *
00007  * The KeyValue library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as published
00009  * by the Free Software Foundation, either version 3 of the License, or (at
00010  * your option) any later version.
00011  *
00012  * The KeyValue library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00015  * Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with KeyValue. If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  * If you modify this library, or any covered work, by linking or combining
00021  * it with Excel (or a modified version of that program), containing parts
00022  * covered by the terms of End-User License Agreement for Microsoft
00023  * Software, the licensors of KeyValue grant you additional permission to
00024  * convey the resulting work.
00025  *
00026  **************************************************************************/
00027 
00034 #ifndef KEYVALUE_UTIL_ANY_PTR_H_
00035 #define KEYVALUE_UTIL_ANY_PTR_H_
00036 
00037 #include "keyvalue/extern/SharedPtr.h"
00038 #include "keyvalue/util/Paparazzo.h"
00039 
00040 namespace keyvalue {
00041 namespace util {
00042 
00056 class AnyPtr {
00057 
00058 public:
00059 
00063   AnyPtr();
00064 
00072   template <typename T>
00073   explicit AnyPtr(T* ptr);
00081   template <typename T>
00082   explicit AnyPtr(shared_ptr<T> ptr);
00083 
00093   template <typename T>
00094   operator shared_ptr<T>() const;
00095 
00102   operator bool() const;
00103 
00109   void
00110   swap(AnyPtr& other);
00111 
00112 private:
00113 
00117   template <typename T>
00118   void
00119   thrower() const;
00120 
00121   shared_ptr<void> ptr_;
00122   void (AnyPtr::*thr_)() const;
00123 
00124 }; // class AnyPtr
00125 
00137 template <typename T>
00138 shared_ptr<T>
00139 dynamic_pointer_cast(const AnyPtr& ptr);
00140 
00141 //--------------------------------------------------------------------------
00142 // AnyPtr()
00143 //--------------------------------------------------------------------------
00144 
00145 template <typename T>
00146 AnyPtr::AnyPtr(T* ptr) : ptr_(ptr), thr_(&AnyPtr::thrower<T>) {
00147 }
00148 
00149 template <typename T>
00150 AnyPtr::AnyPtr(shared_ptr<T> ptr) : ptr_(ptr), thr_(&AnyPtr::thrower<T>) {
00151 }
00152 
00153 //--------------------------------------------------------------------------
00154 // operator shared_ptr<T>()
00155 //--------------------------------------------------------------------------
00156 
00157 template <typename T>
00158 AnyPtr::operator shared_ptr<T>() const {
00159 
00160   if (ptr_) {
00161 
00162     try { (this->*thr_)(); }
00163 
00164     catch (T* p) {
00165       shared_ptr<T> result = static_pointer_cast<T>(ptr_);
00166       shared_ptr<Paparazzo>::change_px(result, p);
00167       return result;
00168     }
00169 
00170     catch (...) {}
00171   }
00172 
00173   return shared_ptr<T>();
00174 }
00175 
00176 //--------------------------------------------------------------------------
00177 // thrower()
00178 //--------------------------------------------------------------------------
00179 
00180 template <typename T>
00181 void
00182 AnyPtr::thrower() const {
00183   throw static_cast<T*>(ptr_.get());
00184 }
00185 
00186 //--------------------------------------------------------------------------
00187 // dynamic_pointer_cast()
00188 //--------------------------------------------------------------------------
00189 
00190 template <typename T>
00191 shared_ptr<T>
00192 dynamic_pointer_cast(const AnyPtr& ptr) {
00193   // The automatic conversion does the job.
00194   return ptr;
00195 }
00196 
00197 } // namespace util
00198 } // namespace keyvalue
00199 
00200 #endif // KEYVALUE_UTIL_ANY_PTR_H_