mesos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nniel...@apache.org
Subject git commit: stringify() for flag values.
Date Wed, 04 Dec 2013 21:07:07 GMT
Updated Branches:
  refs/heads/master ede137656 -> 5048e151f


stringify() for flag values.

This patch adds a stringify member function to the Flag structure which
lets us iterate and fetch flag values. During flag initialization, the
toString methods are closed over the flag value and option references.
Flag values can't be closed over up front as flag values may be changed
by user code arbitrarily. So stringify takes in the owning flags
instance as a FlagsBase pointer: stringify(FlagsBase*).

From: Niklas Nielsen <nik@qni.dk>
Review: https://reviews.apache.org/r/15285


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

Branch: refs/heads/master
Commit: 5048e151f0aa9893e9fc3d108e1e0ebcdff7ff74
Parents: ede1376
Author: niklas <niklas@static.144-76-223-227.clients.your-server.de>
Authored: Wed Dec 4 21:03:56 2013 +0000
Committer: niklas <niklas@static.144-76-223-227.clients.your-server.de>
Committed: Wed Dec 4 21:03:56 2013 +0000

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/flags/flag.hpp |  1 +
 .../stout/include/stout/flags/flags.hpp         | 16 +++++
 .../stout/include/stout/flags/stringifier.hpp   | 67 ++++++++++++++++++++
 .../3rdparty/stout/tests/flags_tests.cpp        | 61 ++++++++++++++++++
 4 files changed, 145 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/5048e151/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flag.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flag.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flag.hpp
index d31c984..2ba739d 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flag.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flag.hpp
@@ -19,6 +19,7 @@ struct Flag
   std::string help;
   bool boolean;
   std::tr1::function<Try<Nothing>(FlagsBase*, const std::string&)> loader;
+  std::tr1::function<Option<std::string>(const FlagsBase&)> stringify;
 };
 
 } // namespace flags {

http://git-wip-us.apache.org/repos/asf/mesos/blob/5048e151/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 cfe996e..0d3c836 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/flags.hpp
@@ -23,6 +23,7 @@
 
 #include <stout/flags/flag.hpp>
 #include <stout/flags/loader.hpp>
+#include <stout/flags/stringifier.hpp>
 #include <stout/flags/parse.hpp>
 
 namespace flags {
@@ -72,6 +73,11 @@ public:
   const_iterator begin() const { return flags.begin(); }
   const_iterator end() const { return flags.end(); }
 
+  typedef std::map<std::string, Flag>::iterator iterator;
+
+  iterator begin() { return flags.begin(); }
+  iterator end() { return flags.end(); }
+
   template <typename T1, typename T2>
   void add(T1* t1,
            const std::string& name,
@@ -150,6 +156,7 @@ void FlagsBase::add(
           std::tr1::bind(&parse<T1>, std::tr1::placeholders::_1)),
       name,
       std::tr1::placeholders::_2); // Use _2 because ignore FlagsBase*.
+  flag.stringify = std::tr1::bind(&Stringifier<T1>, t1);
 
   // Update the help string to include the default value.
   flag.help += help.size() > 0 && help.find_last_of("\n\r") != help.size() - 1
@@ -179,6 +186,7 @@ void FlagsBase::add(
           std::tr1::bind(&parse<T>, std::tr1::placeholders::_1)),
       name,
       std::tr1::placeholders::_2); // Use _2 because ignore FlagsBase*.
+  flag.stringify = std::tr1::bind(&OptionStringifier<T>, option);
 
   FlagsBase::add(flag);
 }
@@ -212,6 +220,10 @@ void FlagsBase::add(
           std::tr1::bind(&parse<T1>, std::tr1::placeholders::_1)),
       name,
       std::tr1::placeholders::_2);
+  flag.stringify = std::tr1::bind(
+      &MemberStringifier<Flags, T1>,
+      std::tr1::placeholders::_1,
+      t1);
 
   // Update the help string to include the default value.
   flag.help += help.size() > 0 && help.find_last_of("\n\r") != help.size() - 1
@@ -249,6 +261,10 @@ void FlagsBase::add(
           std::tr1::bind(&parse<T>, std::tr1::placeholders::_1)),
       name,
       std::tr1::placeholders::_2);
+  flag.stringify = std::tr1::bind(
+      &OptionMemberStringifier<Flags, T>,
+      std::tr1::placeholders::_1,
+      option);
 
   add(flag);
 }

http://git-wip-us.apache.org/repos/asf/mesos/blob/5048e151/3rdparty/libprocess/3rdparty/stout/include/stout/flags/stringifier.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/flags/stringifier.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/stringifier.hpp
new file mode 100644
index 0000000..669218c
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/flags/stringifier.hpp
@@ -0,0 +1,67 @@
+#ifndef __STOUT_FLAGS_STRINGIFIER_HPP__
+#define __STOUT_FLAGS_STRINGIFIER_HPP__
+
+#include <string>
+
+#include <tr1/functional>
+
+#include <stout/error.hpp>
+#include <stout/nothing.hpp>
+#include <stout/option.hpp>
+#include <stout/try.hpp>
+
+#include <stout/flags/parse.hpp>
+
+namespace flags {
+
+// Forward declaration.
+class FlagsBase;
+
+template<typename T>
+static Option<std::string> Stringifier(T* value)
+{
+  return stringify(*value);
+}
+
+
+template<typename T>
+static Option<std::string> OptionStringifier(Option<T>* value)
+{
+  if (value->isSome()) {
+    return stringify(value->get());
+  }
+  return None();
+}
+
+
+template<typename F, typename T>
+static Option<std::string> MemberStringifier(
+    const FlagsBase& base,
+    T F::*flag)
+{
+  const F* f = dynamic_cast<const F*>(&base);
+  if (f != NULL) {
+    return stringify(f->*flag);
+  }
+  return None();
+}
+
+
+template<typename F, typename T>
+static Option<std::string> OptionMemberStringifier(
+    const FlagsBase& base,
+    Option<T> F::*flag)
+{
+  const F* f = dynamic_cast<const F*>(&base);
+  if (f != NULL) {
+    const Option<T>& v = f->*flag;
+    if (v.isSome()) {
+      return stringify(v.get());
+    }
+  }
+  return None();
+}
+
+} // namespace flags {
+
+#endif // __STOUT_FLAGS_STRINGIFIER_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/5048e151/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 9af2da1..9d48c16 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/flags_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/flags_tests.cpp
@@ -5,6 +5,7 @@
 
 #include <stout/duration.hpp>
 #include <stout/flags.hpp>
+#include <stout/foreach.hpp>
 #include <stout/gtest.hpp>
 #include <stout/none.hpp>
 #include <stout/nothing.hpp>
@@ -227,6 +228,66 @@ TEST(FlagsTest, LoadFromCommandLineWithNonFlags)
 }
 
 
+TEST(FlagsTest, Stringification)
+{
+  TestFlags flags;
+
+  Duration name6;
+
+  flags.add(&name6,
+            "name6",
+            "Also set name6",
+            Milliseconds(42));
+
+  Option<bool> name7;
+
+  flags.add(&name7,
+            "name7",
+            "Optional name7");
+
+  Option<bool> name8;
+
+  flags.add(&name8,
+            "name8",
+            "Optional name8");
+
+  std::map<std::string, Option<std::string> > values;
+
+  values["name2"] = Some("43");
+  values["no-name4"] = None();
+  values["name5"] = None();
+
+  flags.load(values);
+
+  foreachpair(const std::string& name, const Flag& flag, flags) {
+    Option<std::string> value = flag.stringify(flags);
+    if (name == "name1") {
+      ASSERT_SOME(value);
+      EXPECT_EQ("ben folds", value.get());
+    } else if (name == "name2") {
+      ASSERT_SOME(value);
+      EXPECT_EQ("43", value.get());
+    } else if (name == "name3") {
+      ASSERT_SOME(value);
+      EXPECT_EQ("false", value.get());
+    } else if (name == "name4") {
+      ASSERT_SOME(value);
+      EXPECT_EQ("false", value.get());
+    } else if (name == "name5") {
+      ASSERT_SOME(value);
+      EXPECT_EQ("true", value.get());
+    } else if (name == "name6") {
+      ASSERT_SOME(value);
+      EXPECT_EQ("42ms", value.get());
+    } else if (name == "name7") {
+      ASSERT_NONE(value);
+    } else if (name == "name8") {
+      ASSERT_NONE(value);
+    }
+  }
+}
+
+
 TEST(FlagsTest, DuplicatesFromEnvironment)
 {
   TestFlags flags;


Mime
View raw message