Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET Apache Qpid Documentation
qpid/sys/ExceptionHolder.h
Go to the documentation of this file.
00001 #ifndef QPID_EXCEPTIONHOLDER_H
00002 #define QPID_EXCEPTIONHOLDER_H
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  * 
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  * 
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
00022  *
00023  */
00024 
00025 #include <boost/shared_ptr.hpp>
00026 
00027 
00028 namespace qpid {
00029 namespace sys {
00030 
00031 struct Raisable {
00032     virtual ~Raisable() {};
00033     virtual void raise() const=0;
00034     virtual std::string what() const=0;
00035 };
00036 
00041 class ExceptionHolder : public Raisable {
00042   public:
00043     ExceptionHolder() {}
00044     // Use default copy & assign.
00045     
00047     template <class Ex> ExceptionHolder(Ex* ex) { wrap(ex); }
00048     template <class Ex> ExceptionHolder(const boost::shared_ptr<Ex>& ex) { wrap(ex.release()); }
00049 
00050     template <class Ex> ExceptionHolder& operator=(Ex* ex) { wrap(ex); return *this; }
00051     template <class Ex> ExceptionHolder& operator=(boost::shared_ptr<Ex> ex) { wrap(ex.release()); return *this; }
00052         
00053     void raise() const { if (wrapper.get()) wrapper->raise() ; }
00054     std::string what() const { return wrapper.get() ? wrapper->what() : std::string(); }
00055     bool empty() const { return !wrapper.get(); }
00056     operator bool() const { return !empty(); }
00057     void reset() { wrapper.reset(); }
00058 
00059   private:
00060     template <class Ex> struct Wrapper : public Raisable {
00061         Wrapper(Ex* ptr) : exception(ptr) {}
00062         void raise() const { throw *exception; }
00063         std::string what() const { return exception->what(); }
00064         boost::shared_ptr<Ex> exception;
00065     };
00066     template <class Ex> void wrap(Ex* ex) { wrapper.reset(new Wrapper<Ex>(ex)); }
00067     boost::shared_ptr<Raisable> wrapper;
00068 };
00069     
00070 
00071 }} // namespace qpid::sys
00072 
00073 
00074 #endif  

Qpid C++ API Reference
Generated on Fri Aug 5 2011 13:38:46 for Qpid C++ Client API by doxygen 1.7.4