From f278315081b24b59bf73e9613e552e3519200a71 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 28 May 2012 04:39:18 +0000 Subject: Improve error reporting with a reason code. Improve debug and audit logging. Fix test scripts to cleanup state from previous builds and correctly report test errors. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1343138 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/kernel/monad.hpp | 102 +++++++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 35 deletions(-) (limited to 'sca-cpp/trunk/kernel/monad.hpp') diff --git a/sca-cpp/trunk/kernel/monad.hpp b/sca-cpp/trunk/kernel/monad.hpp index b1bef7996c..657a809c1c 100644 --- a/sca-cpp/trunk/kernel/monad.hpp +++ b/sca-cpp/trunk/kernel/monad.hpp @@ -203,59 +203,63 @@ template const maybe operator>>(const maybe& m, co * To get the value in the monad, just cast it to the value type. * To get the failure in the monad, cast it to the failure type. */ -template class failable { +template class failable { public: - failable() : hasv(false) { + failable() : hasv(false), c(-1) { } - failable(const V& v) : hasv(true), v(v) { + failable(const V& v) : hasv(true), v(v), c(-1) { } - failable(const failable& m) : hasv(m.hasv), v(m.v), f(m.f) { + failable(const failable& m) : hasv(m.hasv), v(m.v), f(m.f), c(m.c) { } - const failable& operator=(const failable& m) { + const failable& operator=(const failable& m) { if (&m == this) return *this; hasv = m.hasv; v = m.v; f = m.f; + c = m.c; return *this; } - const bool operator!=(const failable& m) const { + const bool operator!=(const failable& m) const { return !this->operator==(m); } - const bool operator==(const failable& m) const { + const bool operator==(const failable& m) const { if (this == &m) return true; if (!hasv) - return !m.hasv && f == m.f; + return !m.hasv && f == m.f && c == m.c; return m.hasv && v == m.v; } private: - failable(const bool hasv, const F& f) : hasv(hasv), f(f) { + failable(const bool hasv, const F& f, const C& c) : hasv(hasv), f(f), c(c) { } - template friend const bool hasContent(const failable& m); - template friend const A content(const failable& m); - template friend const B reason(const failable& m); - template friend const failable mkfailure(const B& f, const bool log); - template friend const failable mkfailure(); + template friend const bool hasContent(const failable& m); + template friend const A content(const failable& m); + template friend const B reason(const failable& m); + template friend const R rcode(const failable& m); + template friend const failable mkfailure(const B& f, const R& c, const bool log); + template friend const failable mkfailure(const B& f, const int c, const bool log); + template friend const failable mkfailure(); bool hasv; V v; F f; + C c; }; /** * Write a failable monad to a stream. */ -template ostream& operator<<(ostream& out, const failable& m) { +template ostream& operator<<(ostream& out, const failable& m) { if (!hasContent(m)) { - out << reason(m); + out << reason(m) << " : " << rcode(m); return out; } out << content(m); @@ -265,18 +269,18 @@ template ostream& operator<<(ostream& out, const failabl /** * Returns a failable monad with a success value in it. */ -template const failable mksuccess(const V& v) { - return failable(v); +template const failable mksuccess(const V& v) { + return failable(v); } -template const lambda(const V)> success() { - return mksuccess; +template const lambda(const V)> success() { + return mksuccess; } /** * Returns a failable monad with a failure in it. */ -template const failable mkfailure(const F& f, const bool log = true) { +template const failable mkfailure(const F& f, const C& c, const bool log = true) { #ifdef WANT_MAINTAINER_LOG if (!log) debug(f, "failable::mkfailure"); @@ -285,57 +289,85 @@ template const failable mkfailure(const F& f, cons ostringstream os; os << f; if (length(str(os)) != 0) - cfailure << "failable::mkfailure" << ": " << f << endl; + cfailure << "failable::mkfailure" << ": " << f << " : " << c << endl; } - return failable(false, f); + return failable(false, f, c); } -template const failable mkfailure(const char* f, const bool log = true) { - return mkfailure(string(f), log); +template const failable mkfailure(const F& f, const int c = -1, const bool log = true) { +#ifdef WANT_MAINTAINER_LOG + if (!log) + debug(f, c, "failable::mkfailure"); +#endif + if (log) { + ostringstream os; + os << f; + if (length(str(os)) != 0) + cfailure << "failable::mkfailure: " << str(os) << " : " << c << endl; + } + return failable(false, f, c); +} + +template const failable mkfailure(const char* f, const int c = -1, const bool log = true) { + return mkfailure(string(f), c, log); } template const failable mkfailure() { - return failable(false, string()); + return failable(false, string(), -1); +} + +template const lambda(const V)> failure() { + return mkfailure; } -template const lambda(const V)> failure() { - return mkfailure; +/** + * Convert a failable of a given type to a failable of another type. + */ +template const failable mkfailure(const failable& f, const bool log = true) { + return mkfailure(reason(f), rcode(f), log); } /** * Returns true if the monad contains a content. */ -template const bool hasContent(const failable& m) { +template const bool hasContent(const failable& m) { return m.hasv; } /** * Returns the content of a failable monad. */ -template const V content(const failable& m) { +template const V content(const failable& m) { return m.v; } /** * Returns the reason for failure of a failable monad. */ -template const F reason(const failable& m) { +template const F reason(const failable& m) { return m.f; } +/** + * Returns the reason code for failure of a failable monad. + */ +template const C rcode(const failable& m) { + return m.c; +} + /** * Bind a function to a failable monad. Passes the success value in the monad to the function * if present, or does nothing if there's no value and a failure instead. */ -template -const failable operator>>(const failable& m, const lambda(const V)>& f) { +template +const failable operator>>(const failable& m, const lambda(const V)>& f) { if (!hasContent(m)) return m; return f(content(m)); } -template -const failable operator>>(const failable& m, const failable (* const f)(const V)) { +template +const failable operator>>(const failable& m, const failable (* const f)(const V)) { if (!hasContent(m)) return m; return f(content(m)); -- cgit v1.2.3