qpid/sys/ExceptionHolder.h Source File

Apache Qpid - AMQP Messaging for Java JMS, C++, Python, Ruby, and .NET | Apache Qpid Documentation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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& operator=(Ex* ex) { wrap(ex); return *this; }
    00049
    00050     void raise() const { if (wrapper.get()) wrapper->raise() ; }
    00051     std::string what() const { return wrapper.get() ? wrapper->what() : std::string(); }
    00052     bool empty() const { return !wrapper.get(); }
    00053     operator bool() const { return !empty(); }
    00054     void reset() { wrapper.reset(); }
    00055
    00056   private:
    00057     template <class Ex> struct Wrapper : public Raisable {
    00058         Wrapper(Ex* ptr) : exception(ptr) {}
    00059         void raise() const { throw *exception; }
    00060         std::string what() const { return exception->what(); }
    00061         boost::shared_ptr<Ex> exception;
    00062     };
    00063     template <class Ex> void wrap(Ex* ex) { wrapper.reset(new Wrapper<Ex>(ex)); }
    00064     boost::shared_ptr<Raisable> wrapper;
    00065 };
    00066
    00067
    00068 }} // namespace qpid::sys
    00069
    00070
    00071 #endif