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 2011/06/05 07:47:23 UTC

svn commit: r1131906 - /incubator/mesos/trunk/src/option.hpp

Author: benh
Date: Sun Jun  5 05:47:23 2011
New Revision: 1131906

URL: http://svn.apache.org/viewvc?rev=1131906&view=rev
Log:
Using specialized templates to avoid RTTI typeid() calls to find out if a default parameter is of type bool.

Modified:
    incubator/mesos/trunk/src/option.hpp

Modified: incubator/mesos/trunk/src/option.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/option.hpp?rev=1131906&r1=1131905&r2=1131906&view=diff
==============================================================================
--- incubator/mesos/trunk/src/option.hpp (original)
+++ incubator/mesos/trunk/src/option.hpp Sun Jun  5 05:47:23 2011
@@ -6,7 +6,6 @@
 #include <fstream>
 #include <string>
 #include <map>
-#include <typeinfo>
 #include <glog/logging.h>
 #include "params.hpp"
 #include "foreach.hpp"
@@ -28,26 +27,18 @@ using boost::bad_lexical_cast;
  **/
 class ValidatorBase {
 public:
-  virtual bool isValid(const string& val) const = 0;
   virtual ValidatorBase* clone() const = 0;
   virtual bool isBool() const = 0;
-};
-
-
-/**
- * Validator that checks if a string can be cast to its templated type.
- **/
-template <class T>
-class Validator : public ValidatorBase {
-public:
-  Validator() {}
-
   /**
    * Checks if the provided string can be cast to a T.
    * @param val value associated with some option
    * @return true if val can be cast to a T, otherwise false.
    **/
-  virtual bool isValid(const string& val) const
+  virtual bool isValid(const string& val) const = 0;
+
+protected:
+  template <class T>
+  bool isValidInternal(const string& val) const
   {
     try {
       lexical_cast<T>(val);
@@ -57,17 +48,42 @@ public:
     }
     return true;
   }
+};
 
-  virtual ValidatorBase* clone() const
-  {
-    return new Validator<T>();
+
+/**
+ * Validator that checks if a string can be cast to its templated type.
+ **/
+template <typename T>
+class Validator : public ValidatorBase {
+public:
+  virtual bool isValid(const string& val) const { 
+    return isValidInternal<T>(val); 
   }
+  
+  virtual bool isBool() const { return false; }
 
-  virtual bool isBool() const
-  {
-    return (typeid(bool) == typeid(T));
+  virtual ValidatorBase* clone() const  { 
+    return new Validator<T>(); 
   }
+};
+
 
+/**
+ * Validator for bools that checks if a string can be cast to its templated type.
+ **/
+template <>
+class Validator <bool> : public ValidatorBase {
+public:
+  bool isValid(const string& val) const { 
+    return isValidInternal<bool>(val); 
+  }
+
+  bool isBool() const { return true; }
+
+  ValidatorBase* clone() const { 
+    return new Validator<bool>(); 
+  }
 };