<commit_before>#ifndef __OPTION_HPP__
#define __OPTION_HPP__

#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <typeinfo>
#include <glog/logging.h>
#include "params.hpp"
#include "foreach.hpp"

namespace nexus { namespace internal {
    
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::map;
using boost::lexical_cast;
using boost::bad_lexical_cast;


/**
 * Interface of a validator
 **/
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
  {
    try {
      lexical_cast<T>(val);
    }
    catch(const bad_lexical_cast& ex) {
      return false;
    }
    return true;
  }

  virtual ValidatorBase* clone() const
  {
    return new Validator<T>();
  }

  virtual bool isBool() const
  {
    return (typeid(bool) == typeid(T));
  }

};


/**
 * Registered option with help string and default value<commit_msg>Using specialized templates to avoid RTTI typeid() calls to find out if a default parameter is of type bool.<commit_after>#ifndef __OPTION_HPP__
#define __OPTION_HPP__

#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <glog/logging.h>
#include "params.hpp"
#include "foreach.hpp"

namespace nexus { namespace internal {
    
using std::string;
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::map;
using boost::lexical_cast;
using boost::bad_lexical_cast;


/**
 * Interface of a validator
 **/
class ValidatorBase {
public:
  virtual ValidatorBase* clone() const = 0;
  virtual bool isBool() const = 0;
  /**
   * 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 = 0;

protected:
  template <class T>
  bool isValidInternal(const string& val) const
  {
    try {
      lexical_cast<T>(val);
    }
    catch(const bad_lexical_cast& ex) {
      return false;
    }
    return true;
  }
};


/**
 * 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 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>(); 
  }
};


/**
 * Registered option with help string and default value
{'fraction_non_alphanumeric': 0.1110781990521327, 'fraction_numerical': 0.0017772511848341231, 'mean_word_length': 3.6579310344827585, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3253586', 'n_tokens_mistral': 1099, 'n_tokens_neox': 1066, 'n_words': 374}