From 585f81b9436e137c3ed784d9a91efa9e6e792e03 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 23 Nov 2009 05:25:33 +0000 Subject: Refactored httpd server integration, split http support and server logic in two modules. Added functions to load component implementations packaged as dynamic libraries. Minor monad code cleanup, converted cast operators to separate functions. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@883249 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/kernel/monad.hpp | 137 ++++++++++++++++++++++++----------------- 1 file changed, 82 insertions(+), 55 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 98eb3799c0..1a45640c32 100644 --- a/sca-cpp/trunk/kernel/monad.hpp +++ b/sca-cpp/trunk/kernel/monad.hpp @@ -42,10 +42,6 @@ public: id(const V& v) : v(v) { } - operator const V() const { - return v; - } - const id& operator=(const id& m) { if(this == &m) return *this; @@ -65,16 +61,25 @@ public: private: const V v; + + template friend const X content(const id& m); }; /** * Write an identity monad to a stream. */ template std::ostream& operator<<(std::ostream& out, const id& m) { - out << (V)m; + out << content(m); return out; } +/** + * Returns the content of an identity monad. + */ +template const V content(const id& m) { + return m.v; +} + /** * Return an identity monad from a value. */ @@ -90,11 +95,11 @@ template const lambda(V)> unit() { * Bind a function to an identity monad. Pass the value in the monad to the function. */ template const id operator>>(const id& m, const lambda(V)>& f) { - return f(m); + return f(content(m)); } template const id operator>>(const id& m, const id (* const f)(const V)) { - return f(m); + return f(content(m)); } /** @@ -109,10 +114,6 @@ public: maybe() : hasv(false) { } - operator const V() const { - return v; - } - const maybe& operator=(const maybe& m) { if(this == &m) return *this; @@ -138,18 +139,19 @@ private: const bool hasv; V v; - template friend const bool hasValue(const maybe& m); + template friend const bool hasContent(const maybe& m); + template friend const X content(const maybe& m); }; /** * Write a maybe monad to a stream. */ template std::ostream& operator<<(std::ostream& out, const maybe& m) { - if (!hasValue(m)) { + if (!hasContent(m)) { out << "nothing"; return out; } - out << (V)m; + out << content(m); return out; } @@ -165,26 +167,33 @@ template const lambda(V)> just() { } /** - * Returns true if the monad contains a value. + * Returns true if a maybe monad contains a content. */ -template const bool hasValue(const maybe& m) { +template const bool hasContent(const maybe& m) { return m.hasv; } +/** + * Returns the content of a maybe monad. + */ +template const V content(const maybe& m) { + return m.v; +} + /** * Bind a function to a maybe monad. Passes the value in the monad to the function * if present, or does nothing if there's no value. */ template const maybe operator>>(const maybe& m, const lambda(V)>& f) { - if (!hasValue(m)) + if (!hasContent(m)) return m; - return f(m); + return f(content(m)); } template const maybe operator>>(const maybe& m, const maybe (* const f)(const V)) { - if (!hasValue(m)) + if (!hasContent(m)) return m; - return f(m); + return f(content(m)); } /** @@ -207,10 +216,6 @@ public: f = m.f; } - operator const V() const { - return v; - } - const failable& operator=(const failable& m) { if(this == &m) return *this; @@ -242,7 +247,8 @@ private: failable(const bool hasv, const F& f) : hasv(hasv), f(f) { } - template friend const bool hasValue(const failable& m); + 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); }; @@ -251,12 +257,11 @@ private: * Write a failable monad to a stream. */ template std::ostream& operator<<(std::ostream& out, const failable& m) { - if (!hasValue(m)) { + if (!hasContent(m)) { out << reason(m); return out; } - const V v = m; - out << v; + out << content(m); return out; } @@ -283,12 +288,19 @@ template const lambda(V)> failure() { } /** - * Returns true if the monad contains a value. + * Returns true if the monad contains a content. */ -template const bool hasValue(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) { + return m.v; +} + /** * Returns the reason for failure of a failable monad. */ @@ -302,24 +314,24 @@ template const F reason(const failable& m) { */ template const failable operator>>(const failable& m, const lambda(V)>& f) { - if (!hasValue(m)) + if (!hasContent(m)) return m; - return f(m); + return f(content(m)); } template const failable operator>>(const failable& m, const failable (* const f)(const V)) { - if (!hasValue(m)) + if (!hasContent(m)) return m; - return f(m); + return f(content(m)); } /** - * State + value pair data type used by the state monad. + * State + content pair data type used by the state monad. */ -template class svp { +template class scp { public: - svp(const S& s, const V& v) : s(s), v(v) { + scp(const S& s, const V& v) : s(s), v(v) { } operator const S() const { @@ -330,7 +342,7 @@ public: return v; } - const svp& operator=(const svp& p) { + const scp& operator=(const scp& p) { if(this == &p) return *this; s = p.s; @@ -338,11 +350,11 @@ public: return *this; } - const bool operator!=(const svp& p) const { + const bool operator!=(const scp& p) const { return !this->operator==(p); } - const bool operator==(const svp& p) const { + const bool operator==(const scp& p) const { if (this == &p) return true; return s == p.s && v == p.v; @@ -351,19 +363,34 @@ public: private: const S s; const V v; + + template friend const A scpstate(const scp& m); + template friend const B content(const scp& m); }; /** - * State monad. Used to represent the combination of a state and a value. - * To get the state in the monad, just cast it to the state type. - * To get the value in the monad, just cast it to the value type. + * Returns the state of a state-content pair. + */ +template const S scpstate(const scp& m) { + return m.s; +} + +/** + * Returns the content of a state-content pair. + */ +template const S content(const scp& m) { + return m.v; +} + +/** + * State monad. Used to represent the combination of a state and a content. */ template class state { public: - state(const lambda(S)>& f) : f(f) { + state(const lambda(S)>& f) : f(f) { } - const svp operator()(const S& s) const { + const scp operator()(const S& s) const { return f(s); } @@ -385,7 +412,7 @@ public: } private: - const lambda(S)> f; + const lambda(S)> f; }; /** @@ -399,14 +426,14 @@ template std::ostream& operator<<(std::ostream& out, con } /** - * Return a state monad carrying a result value. + * Return a state monad carrying a result content. */ template struct returnState { const V v; returnState(const V& v) : v(v) { } - const svp operator()(const S& s) const { - return svp(s, v); + const scp operator()(const S& s) const { + return scp(s, v); } }; @@ -416,16 +443,16 @@ template const state result(const V& v) { /** * Return a state monad with a transformer function. - * A transformer function takes a state and returns an svp pair carrying a value and a + * A transformer function takes a state and returns an scp pair carrying a content and a * new (transformed) state. */ -template const state transformer(const lambda(S)>& f) { +template const state transformer(const lambda(S)>& f) { return state(f); } /** - * Bind a function to a state monad. The function takes a value and returns a state - * monad carrying a return value. + * Bind a function to a state monad. The function takes a content and returns a state + * monad carrying a return content. */ template struct stateBind { const state st; @@ -434,8 +461,8 @@ template struct stateBind { stateBind(const state& st, const lambda(A)>& f) : st(st), f(f) { } - const svp operator()(const S& is) const { - const svp iscp = st(is); + const scp operator()(const S& is) const { + const scp iscp = st(is); const state m = f((A)iscp); return m((S)iscp); } -- cgit v1.2.3