summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/kernel/monad.hpp
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2009-11-23 05:25:33 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2009-11-23 05:25:33 +0000
commit585f81b9436e137c3ed784d9a91efa9e6e792e03 (patch)
treef40f0179601320ace0b454698dc76aaeddfc6a5d /sca-cpp/trunk/kernel/monad.hpp
parent3c6b7d709c7197078f8261f8e3464b0f217c988e (diff)
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
Diffstat (limited to 'sca-cpp/trunk/kernel/monad.hpp')
-rw-r--r--sca-cpp/trunk/kernel/monad.hpp137
1 files changed, 82 insertions, 55 deletions
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<V>& operator=(const id<V>& m) {
if(this == &m)
return *this;
@@ -65,17 +61,26 @@ public:
private:
const V v;
+
+ template<typename X> friend const X content(const id<X>& m);
};
/**
* Write an identity monad to a stream.
*/
template<typename V> std::ostream& operator<<(std::ostream& out, const id<V>& m) {
- out << (V)m;
+ out << content(m);
return out;
}
/**
+ * Returns the content of an identity monad.
+ */
+template<typename V> const V content(const id<V>& m) {
+ return m.v;
+}
+
+/**
* Return an identity monad from a value.
*/
template<typename V> const id<V> mkunit(const V& v) {
@@ -90,11 +95,11 @@ template<typename V> const lambda<id<V>(V)> unit() {
* Bind a function to an identity monad. Pass the value in the monad to the function.
*/
template<typename R, typename V> const id<R> operator>>(const id<V>& m, const lambda<id<R>(V)>& f) {
- return f(m);
+ return f(content(m));
}
template<typename R, typename V> const id<R> operator>>(const id<V>& m, const id<R> (* 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<V>& operator=(const maybe<V>& m) {
if(this == &m)
return *this;
@@ -138,18 +139,19 @@ private:
const bool hasv;
V v;
- template<typename A> friend const bool hasValue(const maybe<A>& m);
+ template<typename X> friend const bool hasContent(const maybe<X>& m);
+ template<typename X> friend const X content(const maybe<X>& m);
};
/**
* Write a maybe monad to a stream.
*/
template<typename V> std::ostream& operator<<(std::ostream& out, const maybe<V>& m) {
- if (!hasValue(m)) {
+ if (!hasContent(m)) {
out << "nothing";
return out;
}
- out << (V)m;
+ out << content(m);
return out;
}
@@ -165,26 +167,33 @@ template<typename V> const lambda<maybe<V>(V)> just() {
}
/**
- * Returns true if the monad contains a value.
+ * Returns true if a maybe monad contains a content.
*/
-template<typename V> const bool hasValue(const maybe<V>& m) {
+template<typename V> const bool hasContent(const maybe<V>& m) {
return m.hasv;
}
/**
+ * Returns the content of a maybe monad.
+ */
+template<typename V> const V content(const maybe<V>& 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<typename R, typename V> const maybe<R> operator>>(const maybe<V>& m, const lambda<maybe<R>(V)>& f) {
- if (!hasValue(m))
+ if (!hasContent(m))
return m;
- return f(m);
+ return f(content(m));
}
template<typename R, typename V> const maybe<R> operator>>(const maybe<V>& m, const maybe<R> (* 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<V, F>& operator=(const failable<V, F>& m) {
if(this == &m)
return *this;
@@ -242,7 +247,8 @@ private:
failable(const bool hasv, const F& f) : hasv(hasv), f(f) {
}
- template<typename A, typename B> friend const bool hasValue(const failable<A, B>& m);
+ template<typename A, typename B> friend const bool hasContent(const failable<A, B>& m);
+ template<typename A, typename B> friend const A content(const failable<A, B>& m);
template<typename A, typename B> friend const B reason(const failable<A, B>& m);
template<typename A, typename B> friend const failable<A, B> mkfailure(const B& f);
};
@@ -251,12 +257,11 @@ private:
* Write a failable monad to a stream.
*/
template<typename V, typename F> std::ostream& operator<<(std::ostream& out, const failable<V, F>& m) {
- if (!hasValue(m)) {
+ if (!hasContent(m)) {
out << reason(m);
return out;
}
- const V v = m;
- out << v;
+ out << content(m);
return out;
}
@@ -283,13 +288,20 @@ template<typename V, typename F> const lambda<failable<V, F>(V)> failure() {
}
/**
- * Returns true if the monad contains a value.
+ * Returns true if the monad contains a content.
*/
-template<typename V, typename F> const bool hasValue(const failable<V, F>& m) {
+template<typename V, typename F> const bool hasContent(const failable<V, F>& m) {
return m.hasv;
}
/**
+ * Returns the content of a failable monad.
+ */
+template<typename V, typename F> const V content(const failable<V, F>& m) {
+ return m.v;
+}
+
+/**
* Returns the reason for failure of a failable monad.
*/
template<typename V, typename F> const F reason(const failable<V, F>& m) {
@@ -302,24 +314,24 @@ template<typename V, typename F> const F reason(const failable<V, F>& m) {
*/
template<typename R, typename FR, typename V, typename FV>
const failable<R, FR> operator>>(const failable<V, FV>& m, const lambda<failable<R, FR>(V)>& f) {
- if (!hasValue(m))
+ if (!hasContent(m))
return m;
- return f(m);
+ return f(content(m));
}
template<typename R, typename FR, typename V, typename FV>
const failable<R, FR> operator>>(const failable<V, FV>& m, const failable<R, FR> (* 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<typename S, typename V> class svp {
+template<typename S, typename V> 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<S, V>& operator=(const svp<S, V>& p) {
+ const scp<S, V>& operator=(const scp<S, V>& p) {
if(this == &p)
return *this;
s = p.s;
@@ -338,11 +350,11 @@ public:
return *this;
}
- const bool operator!=(const svp<S, V>& p) const {
+ const bool operator!=(const scp<S, V>& p) const {
return !this->operator==(p);
}
- const bool operator==(const svp<S, V>& p) const {
+ const bool operator==(const scp<S, V>& 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<typename A, typename B> friend const A scpstate(const scp<A, B>& m);
+ template<typename A, typename B> friend const B content(const scp<A, B>& 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<typename S, typename V> const S scpstate(const scp<S, V>& m) {
+ return m.s;
+}
+
+/**
+ * Returns the content of a state-content pair.
+ */
+template<typename S, typename V> const S content(const scp<S, V>& m) {
+ return m.v;
+}
+
+/**
+ * State monad. Used to represent the combination of a state and a content.
*/
template<typename S, typename V> class state {
public:
- state(const lambda<svp<S, V>(S)>& f) : f(f) {
+ state(const lambda<scp<S, V>(S)>& f) : f(f) {
}
- const svp<S, V> operator()(const S& s) const {
+ const scp<S, V> operator()(const S& s) const {
return f(s);
}
@@ -385,7 +412,7 @@ public:
}
private:
- const lambda<svp<S, V>(S)> f;
+ const lambda<scp<S, V>(S)> f;
};
/**
@@ -399,14 +426,14 @@ template<typename S, typename V> std::ostream& operator<<(std::ostream& out, con
}
/**
- * Return a state monad carrying a result value.
+ * Return a state monad carrying a result content.
*/
template<typename S, typename V> struct returnState {
const V v;
returnState(const V& v) : v(v) {
}
- const svp<S, V> operator()(const S& s) const {
- return svp<S, V>(s, v);
+ const scp<S, V> operator()(const S& s) const {
+ return scp<S, V>(s, v);
}
};
@@ -416,16 +443,16 @@ template<typename S, typename V> const state<S, V> 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<typename S, typename V> const state<S, V> transformer(const lambda<svp<S, V>(S)>& f) {
+template<typename S, typename V> const state<S, V> transformer(const lambda<scp<S, V>(S)>& f) {
return state<S, V>(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<typename S, typename A, typename B> struct stateBind {
const state<S, A> st;
@@ -434,8 +461,8 @@ template<typename S, typename A, typename B> struct stateBind {
stateBind(const state<S, A>& st, const lambda<state<S, B>(A)>& f) : st(st), f(f) {
}
- const svp<S, B> operator()(const S& is) const {
- const svp<S, A> iscp = st(is);
+ const scp<S, B> operator()(const S& is) const {
+ const scp<S, A> iscp = st(is);
const state<S, B> m = f((A)iscp);
return m((S)iscp);
}