Skip to content

Commit

Permalink
EVENTS: Handle exceptions thrown in functions forced into the main th…
Browse files Browse the repository at this point in the history
…read
  • Loading branch information
DrMcCoy committed Jan 21, 2014
1 parent 5e84234 commit a2a1962
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/events/requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class RequestManager : public Common::Singleton<RequestManager>, public Common::
MainThreadCallerFunctor caller(boost::bind(&MainThreadFunctor<T>::operator(), f));

callInMainThread(caller);
if (!f.getError().isEmpty()) {
throw Common::Exception(f.getError());
}

return f.getReturnValue();
}
Expand Down
36 changes: 32 additions & 4 deletions src/events/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#ifndef EVENTS_TYPES_H
#define EVENTS_TYPES_H

#include <stdexcept>

#include "common/error.h"

#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>

Expand Down Expand Up @@ -67,24 +71,48 @@ template<typename T> struct MainThreadFunctor {
private:
boost::function<T ()> func;
boost::shared_ptr<T> retVal;
boost::shared_ptr<Common::Exception> error;

public:
MainThreadFunctor(const boost::function<T ()> &f) : func(f), retVal(new T) { }
void operator()() const { *retVal = func(); }
MainThreadFunctor(const boost::function<T ()> &f) : func(f), retVal(new T), error(new Common::Exception) { }
void operator()() const {
try {
*retVal = func();
} catch (Common::Exception &e) {
*error = e;
} catch (std::exception &e) {
*error = Common::Exception(e);
} catch (...) {
*error = Common::Exception("Unknown exception thrown in MainThreadFunctor");
}
}

T getReturnValue() const { return *retVal; }
const Common::Exception &getError() const { return *error; }
};

/** Template specialization for a MainThreadFunctor returning void. */
template<> struct MainThreadFunctor<void> {
private:
boost::function<void ()> func;
boost::shared_ptr<Common::Exception> error;

public:
MainThreadFunctor(const boost::function<void ()> &f) : func(f) { }
void operator()() const { func(); }
MainThreadFunctor(const boost::function<void ()> &f) : func(f), error(new Common::Exception) { }
void operator()() const {
try {
func();
} catch (Common::Exception &e) {
*error = e;
} catch (std::exception &e) {
*error = Common::Exception(e);
} catch (...) {
*error = Common::Exception("Unknown exception thrown in MainThreadFunctor");
}
}

void getReturnValue() const { (void) 0; }
const Common::Exception &getError() const { return *error; }
};

typedef boost::function<void ()> MainThreadCallerFunctor;
Expand Down

0 comments on commit a2a1962

Please sign in to comment.