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 2015/05/26 18:56:26 UTC

[1/3] mesos git commit: Use special constructor for Option from Some.

Repository: mesos
Updated Branches:
  refs/heads/master 3baa60965 -> a99ed23b6


Use special constructor for Option<T> from Some<T>.

This is to ensure that we handle Option<Option<T>> correctly.

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


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

Branch: refs/heads/master
Commit: d9484def01e5e928b82632c2a4d2bba291a676b2
Parents: 3baa609
Author: Joris Van Remoortere <jo...@gmail.com>
Authored: Tue May 26 09:42:23 2015 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Tue May 26 09:42:24 2015 -0700

----------------------------------------------------------------------
 3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/d9484def/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 ea79b50..8aa59f1 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/option.hpp
@@ -45,7 +45,7 @@ public:
   Option(const None& none) : state(NONE) {}
 
   template <typename U>
-  Option(const _Some<U>& some) : Option(some.t) {}
+  Option(const _Some<U>& some) : state(SOME), t(some.t) {}
 
   Option(const Option<T>& that) : state(that.state)
   {


[3/3] mesos git commit: Refactor Result leveraging Try> to remove dynamic allocation.

Posted by be...@apache.org.
Refactor Result<T> leveraging Try<Option<T>> to remove dynamic allocation.

Aggregate a Try<Option<T>> to leverage the RAII pattern around
unrestricted union. Added some comments to Result<T>.

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


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

Branch: refs/heads/master
Commit: a99ed23b6e050064c5bbb4491ea67d68bf64fa97
Parents: 1847977
Author: Joris Van Remoortere <jo...@gmail.com>
Authored: Tue May 26 09:55:27 2015 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Tue May 26 09:55:27 2015 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/result.hpp     | 106 +++++++++----------
 1 file changed, 48 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/a99ed23b/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 96b0f36..3d20614 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/result.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/result.hpp
@@ -26,109 +26,99 @@
 #include <stout/some.hpp>
 #include <stout/try.hpp>
 
+// This class is equivalent to Try<Option<T>> and can represent only
+// one of these states at a time:
+//   1) A value of T.
+//   2) No value of T.
+//   3) An error state, with a corresponding error string.
+// Calling 'isSome' will return true if it stores a value, in which
+// case calling 'get' will return a constant reference to the T
+// stored. Calling 'isNone' returns true if no value is stored and
+// there is no error. Calling 'isError' will return true if it stores
+// an error, in which case calling 'error' will return the error
+// string.
 template <typename T>
 class Result
 {
 public:
   static Result<T> none()
   {
-    return Result<T>(NONE);
+    return Result<T>(None());
   }
 
   static Result<T> some(const T& t)
   {
-    return Result<T>(SOME, new T(t));
+    return Result<T>(t);
   }
 
   static Result<T> error(const std::string& message)
   {
-    return Result<T>(ERROR, NULL, message);
+    return Result<T>(Error(message));
   }
 
   Result(const T& _t)
-    : state(SOME), t(new T(_t)) {}
+    : data(Some(_t)) {}
 
   template <typename U>
   Result(const U& u)
-    : state(SOME), t(new T(u)) {}
+    : data(Some(u)) {}
 
   Result(const Option<T>& option)
-    : state(option.isSome() ? SOME : NONE),
-      t(option.isSome() ? new T(option.get()) : NULL) {}
+    : data(option.isSome() ?
+           Try<Option<T>>(Some(option.get())) :
+           Try<Option<T>>(None())) {}
+
+  Result(const Try<T>& _try)
+    : data(_try.isSome() ?
+           Try<Option<T>>(Some(_try.get())) :
+           Try<Option<T>>(Error(_try.error()))) {}
 
   Result(const None& none)
-    : state(NONE), t(NULL) {}
+    : data(none) {}
 
   template <typename U>
   Result(const _Some<U>& some)
-    : state(SOME), t(new T(some.t)) {}
+    : data(some) {}
 
   Result(const Error& error)
-    : state(ERROR), t(NULL), message(error.message) {}
+    : data(error) {}
 
   Result(const ErrnoError& error)
-    : state(ERROR), t(NULL), message(error.message) {}
-
-  Result(const Result<T>& that)
-    : state(that.state),
-      t(that.t == NULL ? NULL : new T(*that.t)),
-      message(that.message) {}
-
-  Result(const Try<T>& _try)
-    : state(_try.isSome() ? SOME : ERROR),
-      t(_try.isSome() ? new T(_try.get()) : NULL),
-      message(_try.isSome() ? "" : _try.error()) {}
-
-  ~Result()
-  {
-    delete t;
-  }
-
-  Result<T>& operator = (const Result<T>& that)
-  {
-    if (this != &that) {
-      delete t;
-      state = that.state;
-      t = (that.t == NULL ? NULL : new T(*that.t));
-      message = that.message;
-    }
+    : data(error) {}
 
-    return *this;
-  }
+  // We don't need to implement these because we are leveraging
+  // Try<Option<T>>.
+  Result(const Result<T>& that) = default;
+  ~Result() = default;
+  Result<T>& operator = (const Result<T>& that) = default;
 
-  bool isSome() const { return state == SOME; }
-  bool isNone() const { return state == NONE; }
-  bool isError() const { return state == ERROR; }
+  // 'isSome', 'isNone', and 'isError' are mutually exclusive. They
+  // correspond to the underlying unioned state of the Option and Try.
+  bool isSome() const { return data.isSome() && data.get().isSome(); }
+  bool isNone() const { return data.isSome() && data.get().isNone(); }
+  bool isError() const { return data.isError(); }
 
   const T& get() const
   {
-    if (state != SOME) {
+    if (!isSome()) {
       std::string errorMessage = "Result::get() but state == ";
-      if (state == ERROR) {
-        errorMessage += "ERROR: " + message;
-      } else if (state == NONE) {
+      if (isError()) {
+        errorMessage += "ERROR: " + data.error();
+      } else if (isNone()) {
         errorMessage += "NONE";
       }
       ABORT(errorMessage);
     }
-    return *t;
+    return data.get().get();
   }
 
-  const std::string& error() const { assert(state == ERROR); return message; }
+  const std::string& error() const { assert(isError()); return data.error(); }
 
 private:
-  enum State {
-    SOME,
-    NONE,
-    ERROR
-  };
-
-  Result(State _state, T* _t = NULL, const std::string& _message = "")
-    : state(_state), t(_t), message(_message) {}
-
-  State state;
-  T* t;
-  std::string message;
+  // We leverage Try<Option<T>> to avoid dynamic allocation of T. This
+  // means we can take advantage of all the RAII features of 'Try' and
+  // makes the implementation of this class much simpler!
+  Try<Option<T>> data;
 };
 
 #endif // __STOUT_RESULT_HPP__


[2/3] mesos git commit: Refactor Try leveraging Option to remove dynamic allocation.

Posted by be...@apache.org.
Refactor Try<T> leveraging Option<T> to remove dynamic allocation.

Aggregate an Option<T> to leverage the RAII pattern around
unrestricted union. Added some comments to Try<T>.

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


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

Branch: refs/heads/master
Commit: 18479776e65c31f14c06d77f2b830768b566b034
Parents: d9484de
Author: Joris Van Remoortere <jo...@gmail.com>
Authored: Tue May 26 09:50:31 2015 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Tue May 26 09:50:31 2015 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/try.hpp        | 91 ++++++++------------
 .../3rdparty/stout/tests/some_tests.cpp         |  3 +
 2 files changed, 40 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/18479776/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 8150b70..bf1540b 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/try.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/try.hpp
@@ -21,92 +21,75 @@
 
 #include <stout/abort.hpp>
 #include <stout/error.hpp>
-
+#include <stout/option.hpp>
+#include <stout/some.hpp>
+
+// This class can represent only one of these states at a time:
+//   1) A value of T.
+//   2) An error state, with a corresponding error string.
+// Calling 'isSome' will return true if it stores a value, in which
+// case calling 'get' will return a constant reference to the T
+// stored. Calling 'isError' will return true if it stores an error,
+// in which case calling 'error' will return the error string.
 template <typename T>
 class Try
 {
 public:
   static Try<T> some(const T& t)
   {
-    return Try<T>(SOME, new T(t));
+    return Try<T>(t);
   }
 
   static Try<T> error(const std::string& message)
   {
-    return Try<T>(ERROR, NULL, message);
+    return Try<T>(Error(message));
   }
 
-  Try(const T& _t)
-    : state(SOME), t(new T(_t)) {}
+  Try(const T& t)
+    : data(Some(t)) {}
 
   template <typename U>
   Try(const U& u)
-    : state(SOME), t(new T(u)) {}
+    : data(Some(u)) {}
 
   Try(const Error& error)
-    : state(ERROR), t(NULL), message(error.message) {}
+    : message(error.message) {}
 
   Try(const ErrnoError& error)
-    : state(ERROR), t(NULL), message(error.message) {}
-
-  Try(const Try<T>& that)
-  {
-    state = that.state;
-    if (that.t != NULL) {
-      t = new T(*that.t);
-    } else {
-      t = NULL;
-    }
-    message = that.message;
-  }
+    : message(error.message) {}
 
   // TODO(bmahler): Add move constructor.
 
-  ~Try()
-  {
-    delete t;
-  }
-
-  Try<T>& operator = (const Try<T>& that)
-  {
-    if (this != &that) {
-      delete t;
-      state = that.state;
-      if (that.t != NULL) {
-        t = new T(*that.t);
-      } else {
-        t = NULL;
-      }
-      message = that.message;
-    }
+  // We don't need to implement these because we are leveraging
+  // Option<T>.
+  Try(const Try<T>& that) = default;
+  ~Try() = default;
+  Try<T>& operator = (const Try<T>& that) = default;
 
-    return *this;
-  }
-
-  bool isSome() const { return state == SOME; }
-  bool isError() const { return state == ERROR; }
+  // 'isSome' and 'isError' are mutually exclusive. They correspond
+  // to the underlying state of the Option.
+  bool isSome() const { return data.isSome(); }
+  bool isError() const { return data.isNone(); }
 
   const T& get() const
   {
-    if (state != SOME) {
+    if (!data.isSome()) {
       ABORT("Try::get() but state == ERROR: " + message);
     }
-    return *t;
+    return data.get();
   }
 
-  const std::string& error() const { assert(state == ERROR); return message; }
+  const std::string& error() const { assert(data.isNone()); return message; }
 
 private:
-  enum State {
-    SOME,
-    ERROR
-  };
-
-  Try(State _state, T* _t = NULL, const std::string& _message = "")
-    : state(_state), t(_t), message(_message) {}
-
-  State state;
-  T* t;
+  // We leverage Option<T> to avoid dynamic allocation of T. This
+  // means that the storage for T will be included in this object
+  // (Try<T>). Since Option<T> keeps track of whether a value is
+  // stored, we just ask it when we want to know whether we are
+  // storing a value or an error. Another advantage of leveraging
+  // Option<T> is that it takes care of all the manual construction
+  // and destruction. This makes the code for Try<T> really simple!
+  Option<T> data;
   std::string message;
 };
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/18479776/3rdparty/libprocess/3rdparty/stout/tests/some_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/some_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/some_tests.cpp
index 4041dc4..1e5f449 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/some_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/some_tests.cpp
@@ -24,6 +24,9 @@ TEST(Stout, Some)
   ASSERT_SOME(t1);
   EXPECT_SOME(t1.get());
   EXPECT_EQ(42, t1.get().get());
+  t1 = None();
+  ASSERT_SOME(t1);
+  EXPECT_NONE(t1.get());
 
   Try<Result<int> > t2 = Some(42);
   ASSERT_SOME(t2);