You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2014/01/25 01:14:46 UTC

[5/7] git commit: Refactored implicit conversions of Some, None, and Error.

Refactored implicit conversions of Some, None, and Error.

Review: https://reviews.apache.org/r/17089


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/48e663e3
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/48e663e3
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/48e663e3

Branch: refs/heads/master
Commit: 48e663e3614006ab0a3cb1a772dcad2c201c0028
Parents: b8a0dd3
Author: Benjamin Hindman <be...@gmail.com>
Authored: Fri Jan 17 15:25:09 2014 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Fri Jan 24 16:14:01 2014 -0800

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/error.hpp      | 55 ++++------------
 .../stout/include/stout/flags/flags.hpp         | 18 ------
 .../3rdparty/stout/include/stout/none.hpp       | 63 ++++--------------
 .../3rdparty/stout/include/stout/option.hpp     | 10 +++
 .../3rdparty/stout/include/stout/result.hpp     | 23 ++++++-
 .../3rdparty/stout/include/stout/some.hpp       | 67 ++++----------------
 .../3rdparty/stout/include/stout/try.hpp        | 14 +++-
 7 files changed, 82 insertions(+), 168 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/48e663e3/3rdparty/libprocess/3rdparty/stout/include/stout/error.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/error.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/error.hpp
index b08ae50..12ba1ca 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/error.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/error.hpp
@@ -19,55 +19,24 @@
 
 #include <string>
 
-#include "result.hpp"
-#include "try.hpp"
-
-// An "error" type that is implicitly convertible to a Try<T> or
-// Result<T> for any T (effectively "syntactic sugar" to make code
-// more readable). The implementation uses cast operators to perform
-// the conversions instead of adding constructors to Try/Result
-// directly. One could imagine revisiting that decision for C++11
-// because the use of rvalue reference could eliminate some
-// unnecessary copies. However, performance is not critical since
-// Error should not get called very often in practice (if so, it's
-// probably being used for things that aren't really errors or there
-// is a more serious problem during execution).
+// A useful type that can be used to represent a Try that has
+// failed. You can also use 'ErrnoError' to append the error message
+// associated with the current 'errno' to your own error message.
+//
+// Examples:
+//
+//   Result<int> result = Error("uninitialized");
+//   Try<std::string> = Error("uninitialized");
+//
+//   void foo(Try<std::string> t) {}
+//
+//   foo(Error("some error here"));
 
 class Error
 {
 public:
   explicit Error(const std::string& _message) : message(_message) {}
 
-  template <typename T>
-  operator Try<T> () const
-  {
-    return Try<T>::error(message);
-  }
-
-  // Give the compiler some help for nested Try<T>. For example,
-  // enable converting Error to an Option<Try<T>>. Note that this will
-  // bind to the innermost Try<T>.
-  template <template <typename> class S, typename T>
-  operator S<Try<T> > () const
-  {
-    return S<Try<T> >(Try<T>::error(message));
-  }
-
-  template <typename T>
-  operator Result<T> () const
-  {
-    return Result<T>::error(message);
-  }
-
-  // Give the compiler some help for nested Result<T>. For example,
-  // enable converting Error to an Option<Result<T>>. Note that this
-  // will bind to the innermost Result<T>.
-  template <template <typename> class S, typename T>
-  operator S<Result<T> > () const
-  {
-    return S<Result<T> >(Result<T>::error(message));
-  }
-
   const std::string message;
 };
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/48e663e3/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp
index e95ced4..883926b 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp
@@ -62,13 +62,6 @@ public:
       bool unknowns = false,
       bool duplicates = false);
 
-  Try<Nothing> load(
-      const std::string& prefix,
-      int argc,
-      char** argv,
-      bool unknowns = false,
-      bool duplicates = false);
-
   virtual Try<Nothing> load(
       const std::map<std::string, Option<std::string> >& values,
       bool unknowns = false);
@@ -384,17 +377,6 @@ inline Try<Nothing> FlagsBase::load(
 
 
 inline Try<Nothing> FlagsBase::load(
-    const std::string& prefix,
-    int argc,
-    char** argv,
-    bool unknowns,
-    bool duplicates)
-{
-  return load(Some(prefix), argc, argv, unknowns, duplicates);
-}
-
-
-inline Try<Nothing> FlagsBase::load(
     const std::map<std::string, Option<std::string> >& values,
     bool unknowns)
 {

http://git-wip-us.apache.org/repos/asf/mesos/blob/48e663e3/3rdparty/libprocess/3rdparty/stout/include/stout/none.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/none.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/none.hpp
index 43bc2a5..ce94fef 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/none.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/none.hpp
@@ -14,56 +14,17 @@
 #ifndef __STOUT_NONE_HPP__
 #define __STOUT_NONE_HPP__
 
-#include "option.hpp"
-#include "result.hpp"
-
-// A "none" type that is implicitly convertible to an Option<T> and
-// Result<T> for any T (effectively "syntactic sugar" to make code
-// more readable). The implementation uses cast operators to perform
-// the conversions instead of adding constructors to Option/Result
-// directly. Performance shouldn't be an issue given that an instance
-// of None has no virtual functions and no fields.
-
-class None
-{
-public:
-  template <typename T>
-  operator Option<T> () const
-  {
-    return Option<T>::none();
-  }
-
-  // Give the compiler some help for nested Option<T>. For example,
-  // enable converting None to a Try<Option<T>>. Note that this will
-  // bind to the innermost Option<T>.
-  template <template <typename> class S, typename T>
-  operator S<Option<T> > () const
-  {
-    return S<Option<T> >(Option<T>::none());
-  }
-
-  template <typename T>
-  operator Result<T> () const
-  {
-    return Result<T>::none();
-  }
-
-  // Give the compiler some help for nested Result<T>. For example,
-  // enable converting None to a Try<Result<T>>. Note that this will
-  // bind to the innermost Result<T>.
-  template <template <typename> class S, typename T>
-  operator S<Result<T> > () const
-  {
-    return S<Result<T> >(Result<T>::none());
-  }
-
-  // Give the compiler some more help to disambiguate the above cast
-  // operators from Result<Option<T>>.
-  template <typename T>
-  operator Result<Option<T> > () const
-  {
-    return Result<Option<T> >::none();
-  }
-};
+// A useful type that can be used to represent an Option or Result.
+//
+// Examples:
+//
+//   Result<int> result = None();
+//   Option<std::string> = None();
+//
+//   void foo(Option<int> o = None()) {}
+//
+//   foo(None());
+
+struct None {};
 
 #endif // __STOUT_NONE_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/48e663e3/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
index 432a12b..259c463 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
@@ -18,7 +18,9 @@
 
 #include <algorithm>
 
+#include <stout/none.hpp>
 #include <stout/result.hpp>
+#include <stout/some.hpp>
 
 template <typename T>
 class Option
@@ -38,6 +40,14 @@ public:
 
   Option(const T& _t) : state(SOME), t(new T(_t)) {}
 
+  template <typename U>
+  Option(const U& u) : state(SOME), t(new T(u)) {}
+
+  Option(const None& none) : state(NONE), t(NULL) {}
+
+  template <typename U>
+  Option(const _Some<U>& some) : state(SOME), t(new T(some.t)) {}
+
   Option(const Option<T>& that)
   {
     state = that.state;

http://git-wip-us.apache.org/repos/asf/mesos/blob/48e663e3/3rdparty/libprocess/3rdparty/stout/include/stout/result.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/result.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/result.hpp
index cf2b243..ab15f01 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/result.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/result.hpp
@@ -20,6 +20,9 @@
 #include <iostream>
 #include <string>
 
+#include <stout/error.hpp>
+#include <stout/none.hpp>
+#include <stout/some.hpp>
 
 template <typename T>
 class Result
@@ -40,7 +43,25 @@ public:
     return Result<T>(ERROR, NULL, message);
   }
 
-  Result(const T& _t) : state(SOME), t(new T(_t)) {}
+  Result(const T& _t)
+    : state(SOME), t(new T(_t)) {}
+
+  template <typename U>
+  Result(const U& u)
+    : state(SOME), t(new T(u)) {}
+
+  Result(const None& none)
+    : state(NONE), t(NULL) {}
+
+  template <typename U>
+  Result(const _Some<U>& some)
+    : state(SOME), t(new T(some.t)) {}
+
+  Result(const Error& error)
+    : state(ERROR), t(NULL), message(error.message) {}
+
+  Result(const ErrnoError& error)
+    : state(ERROR), t(NULL), message(error.message) {}
 
   Result(const Result<T>& that)
   {

http://git-wip-us.apache.org/repos/asf/mesos/blob/48e663e3/3rdparty/libprocess/3rdparty/stout/include/stout/some.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/some.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/some.hpp
index 885a4ae..1a71ac4 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/some.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/some.hpp
@@ -14,65 +14,24 @@
 #ifndef __STOUT_SOME_HPP__
 #define __STOUT_SOME_HPP__
 
-#include <stout/option.hpp>
-#include <stout/result.hpp>
-
-// A "some" type that is implicitely convertable to an Option<T> and
-// Result<T> for any T (effectively "syntactic sugar" to make code
-// more readable). The implementation uses cast operators to perform
-// the conversions instead of adding constructors to Option/Result
-// directly. The extra copies involved here can be elided with C++11
-// rvalue references. Furthermore, since in most circumstances a Some
-// will not be needed (an Option<T> or Result<T> can be constructed
-// directly from the value) we don't worry about performance.
-
+// A useful type that can be used to represent an Option or Result.
+//
+// Examples:
+//
+//   Result<int> result = Some(42);
+//   Option<std::string> = Some("hello world");
+//
+//   void foo(Option<std::string> o) {}
+//
+//   foo(Some("hello world"));
+
+// NOTE: We use an intermediate type, _Some, so that one doesn't need
+// to explicitly define the template type when doing 'Some(value)'.
 template <typename T>
 struct _Some
 {
   _Some(T _t) : t(_t) {}
 
-  template <typename U>
-  operator Option<U> () const
-  {
-    return Option<U>::some(t);
-  }
-
-  // Give the compiler some help for nested Option<U>.
-  template <template <typename> class S, typename U>
-  operator S<Option<U> > () const
-  {
-    return S<Option<U> >(Option<U>::some(t));
-  }
-
-  template <typename U>
-  operator Result<U> () const
-  {
-    return Result<U>::some(t);
-  }
-
-  // Give the compiler some help for nested Result<U>.
-  template <template <typename> class S, typename U>
-  operator S<Result<U> > () const
-  {
-    return S<Result<U> >(Result<U>::some(t));
-  }
-
-  // Give the compiler some more help to disambiguate the above cast
-  // operators from Option<Result<U>>.
-  template <typename U>
-  operator Option<Result<U> > () const
-  {
-    return Option<Result<U> >::some(t);
-  }
-
-  // Give the compiler some more help to disambiguate the above cast
-  // operators from Result<Option<U>>.
-  template <typename U>
-  operator Result<Option<U> > () const
-  {
-    return Result<Option<U> >::some(t);
-  }
-
   const T t;
 };
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/48e663e3/3rdparty/libprocess/3rdparty/stout/include/stout/try.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/try.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/try.hpp
index b02d354..6a56c7d 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/try.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/try.hpp
@@ -20,6 +20,7 @@
 #include <iostream>
 #include <string>
 
+#include <stout/error.hpp>
 
 template <typename T>
 class Try
@@ -35,7 +36,18 @@ public:
     return Try<T>(ERROR, NULL, message);
   }
 
-  Try(const T& _t) : state(SOME), t(new T(_t)) {}
+  Try(const T& _t)
+    : state(SOME), t(new T(_t)) {}
+
+  template <typename U>
+  Try(const U& u)
+    : state(SOME), t(new T(u)) {}
+
+  Try(const Error& error)
+    : state(ERROR), t(NULL), message(error.message) {}
+
+  Try(const ErrnoError& error)
+    : state(ERROR), t(NULL), message(error.message) {}
 
   Try(const Try<T>& that)
   {