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 2013/12/02 17:57:04 UTC
[10/14] git commit: Added 'Some'.
Added 'Some'.
Review: https://reviews.apache.org/r/15884
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/3a51ff2e
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/3a51ff2e
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/3a51ff2e
Branch: refs/heads/master
Commit: 3a51ff2efc5a5333dd019f53fdfceec6783ea9d1
Parents: 009a26d
Author: Benjamin Hindman <be...@gmail.com>
Authored: Tue Nov 26 22:43:25 2013 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sun Dec 1 23:16:43 2013 -0800
----------------------------------------------------------------------
3rdparty/libprocess/3rdparty/stout/Makefile.am | 2 +
.../3rdparty/stout/include/stout/check.hpp | 5 +-
.../3rdparty/stout/include/stout/flags.hpp | 10 +--
.../stout/include/stout/flags/flags.hpp | 11 ++-
.../stout/include/stout/flags/loader.hpp | 5 +-
.../3rdparty/stout/include/stout/os.hpp | 2 +-
.../3rdparty/stout/include/stout/some.hpp | 73 ++++++++++++++++++++
.../3rdparty/stout/tests/flags_tests.cpp | 19 ++---
.../3rdparty/stout/tests/some_tests.cpp | 67 ++++++++++++++++++
9 files changed, 168 insertions(+), 26 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/3rdparty/libprocess/3rdparty/stout/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am
index cc9a120..e46e763 100644
--- a/3rdparty/libprocess/3rdparty/stout/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am
@@ -53,6 +53,7 @@ EXTRA_DIST = \
include/stout/protobuf.hpp \
include/stout/result.hpp \
include/stout/set.hpp \
+ include/stout/some.hpp \
include/stout/stopwatch.hpp \
include/stout/stringify.hpp \
include/stout/strings.hpp \
@@ -81,6 +82,7 @@ EXTRA_DIST = \
tests/protobuf_tests.pb.h \
tests/protobuf_tests.proto \
tests/set_tests.cpp \
+ tests/some_tests.cpp \
tests/strings_tests.cpp \
tests/thread_tests.cpp \
tests/uuid_tests.cpp
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/3rdparty/libprocess/3rdparty/stout/include/stout/check.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/check.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/check.hpp
index 0165d42..eb31841 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/check.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/check.hpp
@@ -10,6 +10,7 @@
#include <stout/none.hpp>
#include <stout/option.hpp>
#include <stout/result.hpp>
+#include <stout/some.hpp>
#include <stout/try.hpp>
// Provides a CHECK_SOME macro, akin to CHECK.
@@ -26,7 +27,7 @@ template <typename T>
Option<std::string> _check(const Option<T>& o)
{
if (o.isNone()) {
- return Option<std::string>::some("is NONE");
+ return Some("is NONE");
}
return None();
}
@@ -48,7 +49,7 @@ Option<std::string> _check(const Result<T>& r)
if (r.isError()) {
return r.error();
} else if (r.isNone()) {
- return Option<std::string>::some("is NONE");
+ return Some("is NONE");
}
return None();
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/3rdparty/libprocess/3rdparty/stout/include/stout/flags.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/flags.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/flags.hpp
index 0efd079..a70db19 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/flags.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/flags.hpp
@@ -28,11 +28,11 @@
// ...
//
// map<string, Option<string> > values;
-// values["no-debug"] = None(); // --no-debug
-// values["debug"] = None(); // --debug
-// values["debug"] = Option<string>::some("true"); // --debug=true
-// values["debug"] = Option<string>::some("false"); // --debug=false
-// values["name"] = Option<string>::some("frank"); // --name=frank
+// values["no-debug"] = None(); // --no-debug
+// values["debug"] = None(); // --debug
+// values["debug"] = Some("true"); // --debug=true
+// values["debug"] = Some("false"); // --debug=false
+// values["name"] = Some("frank"); // --name=frank
//
// MyFlags flags;
// flags.load(values);
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/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 fcaa4e4..cfe996e 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp
@@ -16,6 +16,7 @@
#include <stout/nothing.hpp>
#include <stout/option.hpp>
#include <stout/os.hpp>
+#include <stout/some.hpp>
#include <stout/stringify.hpp>
#include <stout/strings.hpp>
#include <stout/try.hpp>
@@ -289,7 +290,7 @@ inline std::map<std::string, Option<std::string> > FlagsBase::extract(
if (flags.count(name) > 0 ||
(name.find("no-") == 0 && flags.count(name.substr(3)) > 0)) {
std::string value = variable.substr(eq + 1);
- values[name] = Option<std::string>::some(value);
+ values[name] = Some(value);
}
}
}
@@ -361,11 +362,7 @@ inline Try<Nothing> FlagsBase::load(
bool unknowns,
bool duplicates)
{
- return load(Option<std::string>::some(prefix),
- argc,
- argv,
- unknowns,
- duplicates);
+ return load(Some(prefix), argc, argv, unknowns, duplicates);
}
@@ -436,7 +433,7 @@ inline Try<Nothing> FlagsBase::load(
for (iterator = _values.begin(); iterator != _values.end(); ++iterator) {
const std::string& name = iterator->first;
const std::string& value = iterator->second;
- values[name] = Option<std::string>::some(value);
+ values[name] = Some(value);
}
return load(values, unknowns);
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/3rdparty/libprocess/3rdparty/stout/include/stout/flags/loader.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/loader.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/loader.hpp
index e5eaf24..a6e0f58 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/loader.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/loader.hpp
@@ -8,6 +8,7 @@
#include <stout/error.hpp>
#include <stout/nothing.hpp>
#include <stout/option.hpp>
+#include <stout/some.hpp>
#include <stout/try.hpp>
#include <stout/flags/parse.hpp>
@@ -48,7 +49,7 @@ struct OptionLoader
{
Try<T> t = parse(value);
if (t.isSome()) {
- *flag = Option<T>::some(t.get());
+ *flag = Some(t.get());
} else {
return Error("Failed to load value '" + value + "': " + t.error());
}
@@ -95,7 +96,7 @@ struct OptionMemberLoader
if (f != NULL) {
Try<T> t = parse(value);
if (t.isSome()) {
- f->*flag = Option<T>::some(t.get());
+ f->*flag = Some(t.get());
} else {
return Error("Failed to load value '" + value + "': " + t.error());
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
index f6bbf5e..544cf8c 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
@@ -704,7 +704,7 @@ inline Try<std::string> hostname()
std::string hostname = hep->h_name;
delete[] temp;
- return Try<std::string>::some(hostname);
+ return hostname;
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/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
new file mode 100644
index 0000000..e2f56cc
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/some.hpp
@@ -0,0 +1,73 @@
+#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.
+
+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;
+};
+
+
+template <typename T>
+_Some<T> Some(T t)
+{
+ return _Some<T>(t);
+}
+
+#endif // __STOUT_SOME_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/3rdparty/libprocess/3rdparty/stout/tests/flags_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/flags_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/flags_tests.cpp
index 2780cc5..9af2da1 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/flags_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/flags_tests.cpp
@@ -10,6 +10,7 @@
#include <stout/nothing.hpp>
#include <stout/option.hpp>
#include <stout/os.hpp>
+#include <stout/some.hpp>
using namespace flags;
@@ -57,9 +58,9 @@ TEST(FlagsTest, Load)
std::map<std::string, Option<std::string> > values;
- values["name1"] = Option<std::string>::some("billy joel");
- values["name2"] = Option<std::string>::some("43");
- values["name3"] = Option<std::string>::some("false");
+ values["name1"] = Some("billy joel");
+ values["name2"] = Some("43");
+ values["name3"] = Some("false");
values["no-name4"] = None();
values["name5"] = None();
@@ -100,7 +101,7 @@ TEST(FlagsTest, Add)
std::map<std::string, Option<std::string> > values;
- values["name6"] = Option<std::string>::some("ben folds");
+ values["name6"] = Some("ben folds");
values["no-name7"] = None();
flags.load(values);
@@ -120,9 +121,9 @@ TEST(FlagsTest, Flags)
std::map<std::string, Option<std::string> > values;
- values["name1"] = Option<std::string>::some("billy joel");
- values["name2"] = Option<std::string>::some("43");
- values["name3"] = Option<std::string>::some("false");
+ values["name1"] = Some("billy joel");
+ values["name2"] = Some("43");
+ values["name3"] = Some("false");
values["no-name4"] = None();
values["name5"] = None();
@@ -369,8 +370,8 @@ TEST(FlagsTest, Duration)
std::map<std::string, Option<std::string> > values;
- values["name6"] = Option<std::string>::some("2mins");
- values["name7"] = Option<std::string>::some("3hrs");
+ values["name6"] = Some("2mins");
+ values["name7"] = Some("3hrs");
flags.load(values);
http://git-wip-us.apache.org/repos/asf/mesos/blob/3a51ff2e/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
new file mode 100644
index 0000000..4041dc4
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/tests/some_tests.cpp
@@ -0,0 +1,67 @@
+#include <gtest/gtest.h>
+
+#include <map>
+#include <string>
+
+#include <stout/gtest.hpp>
+#include <stout/none.hpp>
+#include <stout/option.hpp>
+#include <stout/result.hpp>
+#include <stout/some.hpp>
+#include <stout/try.hpp>
+
+TEST(Stout, Some)
+{
+ Option<int> o1 = Some(42);
+ EXPECT_SOME(o1);
+ EXPECT_EQ(42, o1.get());
+
+ Result<int> r1 = Some(42);
+ EXPECT_SOME(r1);
+ EXPECT_EQ(42, r1.get());
+
+ Try<Option<int> > t1 = Some(42);
+ ASSERT_SOME(t1);
+ EXPECT_SOME(t1.get());
+ EXPECT_EQ(42, t1.get().get());
+
+ Try<Result<int> > t2 = Some(42);
+ ASSERT_SOME(t2);
+ EXPECT_SOME(t2.get());
+ EXPECT_EQ(42, t2.get().get());
+
+ Option<Result<int> > o2 = Some(42);
+ ASSERT_SOME(o2);
+ EXPECT_SOME(o2.get());
+ EXPECT_EQ(42, o2.get().get());
+
+ Option<Result<int> > o3 = Some(Some(42));
+ ASSERT_SOME(o3);
+ EXPECT_SOME(o3.get());
+ EXPECT_EQ(42, o3.get().get());
+
+ Result<Option<int> > r2 = Some(42);
+ ASSERT_SOME(r2);
+ EXPECT_SOME(r2.get());
+ EXPECT_EQ(42, r2.get().get());
+
+ Result<Option<int> > r3 = Some(Some(42));
+ ASSERT_SOME(r3);
+ EXPECT_SOME(r3.get());
+ EXPECT_EQ(42, r3.get().get());
+
+ Option<std::string> o4 = Some("hello");
+ EXPECT_SOME(o4);
+ EXPECT_EQ("hello", o4.get());
+
+ Result<std::string> r4 = Some("world");
+ EXPECT_SOME(r4);
+ EXPECT_EQ("world", r4.get());
+
+ std::map<std::string, Option<std::string> > values;
+ values["no-debug"] = None();
+ values["debug"] = None();
+ values["debug"] = Some("true");
+ values["debug"] = Some("false");
+ values["name"] = Some("frank");
+}