]> git.sesse.net Git - stockfish/commitdiff
Promote OptionsMap to a class
authorMarco Costalba <mcostalba@gmail.com>
Thu, 14 Apr 2011 13:59:49 +0000 (15:59 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 16 Apr 2011 09:49:20 +0000 (10:49 +0100)
And add a bit of documentation too.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/main.cpp
src/uci.cpp
src/ucioption.cpp
src/ucioption.h

index 02f2b87d92f00a7046c335b7d42cfbb41bd5de57..4b716dded6ec859ca24e7afa67216dcdd4865e0d 100644 (file)
@@ -50,7 +50,6 @@ int main(int argc, char* argv[]) {
 
   // Startup initializations
   init_bitboards();
 
   // Startup initializations
   init_bitboards();
-  init_uci_options();
   Position::init_zobrist();
   Position::init_piece_square_tables();
   init_eval(1);
   Position::init_zobrist();
   Position::init_piece_square_tables();
   init_eval(1);
index 683aac6d8d07a282428a9564f57152115fc7b569..8b90c22707574d1b41b21b53e11d74016309a504 100644 (file)
@@ -78,7 +78,7 @@ bool execute_uci_command(const string& cmd) {
   else if (token == "uci")
       cout << "id name " << engine_name()
            << "\nid author " << engine_authors()
   else if (token == "uci")
       cout << "id name " << engine_name()
            << "\nid author " << engine_authors()
-           << "\n" << options_to_uci()
+           << "\n" << Options.print_all()
            << "\nuciok" << endl;
 
   else if (token == "ucinewgame")
            << "\nuciok" << endl;
 
   else if (token == "ucinewgame")
index 97cbe1a64684da2cddbcc86561bd19a7c1ff6063..89e470585e0f0f8e3bf6073d80967895380981a3 100644 (file)
@@ -60,53 +60,55 @@ static string stringify(const T& v) {
 }
 
 
 }
 
 
-/// init_uci_options() initializes the UCI options to their hard coded default
+/// OptionsMap c'tor initializes the UCI options to their hard coded default
 /// values and initializes the default value of "Threads" and "Minimum Split Depth"
 /// parameters according to the number of CPU cores.
 
 /// values and initializes the default value of "Threads" and "Minimum Split Depth"
 /// parameters according to the number of CPU cores.
 
-void init_uci_options() {
-
-  Options["Use Search Log"] = Option(false);
-  Options["Search Log Filename"] = Option("SearchLog.txt");
-  Options["Book File"] = Option("book.bin");
-  Options["Best Book Move"] = Option(false);
-  Options["Mobility (Middle Game)"] = Option(100, 0, 200);
-  Options["Mobility (Endgame)"] = Option(100, 0, 200);
-  Options["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
-  Options["Pawn Structure (Endgame)"] = Option(100, 0, 200);
-  Options["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
-  Options["Passed Pawns (Endgame)"] = Option(100, 0, 200);
-  Options["Space"] = Option(100, 0, 200);
-  Options["Aggressiveness"] = Option(100, 0, 200);
-  Options["Cowardice"] = Option(100, 0, 200);
-  Options["Check Extension (PV nodes)"] = Option(2, 0, 2);
-  Options["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
-  Options["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
-  Options["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
-  Options["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
-  Options["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
-  Options["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
-  Options["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
-  Options["Minimum Split Depth"] = Option(4, 4, 7);
-  Options["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
-  Options["Threads"] = Option(1, 1, MAX_THREADS);
-  Options["Use Sleeping Threads"] = Option(true);
-  Options["Hash"] = Option(32, 4, 8192);
-  Options["Clear Hash"] = Option(false, "button");
-  Options["Ponder"] = Option(true);
-  Options["OwnBook"] = Option(true);
-  Options["MultiPV"] = Option(1, 1, 500);
-  Options["Skill level"] = Option(20, 0, 20);
-  Options["Emergency Move Horizon"] = Option(40, 0, 50);
-  Options["Emergency Base Time"] = Option(200, 0, 30000);
-  Options["Emergency Move Time"] = Option(70, 0, 5000);
-  Options["Minimum Thinking Time"] = Option(20, 0, 5000);
-  Options["UCI_Chess960"] = Option(false);
-  Options["UCI_AnalyseMode"] = Option(false);
+OptionsMap::OptionsMap() {
+
+  OptionsMap& o = *this;
+
+  o["Use Search Log"] = Option(false);
+  o["Search Log Filename"] = Option("SearchLog.txt");
+  o["Book File"] = Option("book.bin");
+  o["Best Book Move"] = Option(false);
+  o["Mobility (Middle Game)"] = Option(100, 0, 200);
+  o["Mobility (Endgame)"] = Option(100, 0, 200);
+  o["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
+  o["Pawn Structure (Endgame)"] = Option(100, 0, 200);
+  o["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
+  o["Passed Pawns (Endgame)"] = Option(100, 0, 200);
+  o["Space"] = Option(100, 0, 200);
+  o["Aggressiveness"] = Option(100, 0, 200);
+  o["Cowardice"] = Option(100, 0, 200);
+  o["Check Extension (PV nodes)"] = Option(2, 0, 2);
+  o["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
+  o["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
+  o["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
+  o["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
+  o["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
+  o["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
+  o["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
+  o["Minimum Split Depth"] = Option(4, 4, 7);
+  o["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
+  o["Threads"] = Option(1, 1, MAX_THREADS);
+  o["Use Sleeping Threads"] = Option(true);
+  o["Hash"] = Option(32, 4, 8192);
+  o["Clear Hash"] = Option(false, "button");
+  o["Ponder"] = Option(true);
+  o["OwnBook"] = Option(true);
+  o["MultiPV"] = Option(1, 1, 500);
+  o["Skill level"] = Option(20, 0, 20);
+  o["Emergency Move Horizon"] = Option(40, 0, 50);
+  o["Emergency Base Time"] = Option(200, 0, 30000);
+  o["Emergency Move Time"] = Option(70, 0, 5000);
+  o["Minimum Thinking Time"] = Option(20, 0, 5000);
+  o["UCI_Chess960"] = Option(false);
+  o["UCI_AnalyseMode"] = Option(false);
 
   // Set some SMP parameters accordingly to the detected CPU count
 
   // Set some SMP parameters accordingly to the detected CPU count
-  Option& thr = Options["Threads"];
-  Option& msd = Options["Minimum Split Depth"];
+  Option& thr = o["Threads"];
+  Option& msd = o["Minimum Split Depth"];
 
   thr.defaultValue = thr.currentValue = stringify(cpu_count());
 
 
   thr.defaultValue = thr.currentValue = stringify(cpu_count());
 
@@ -115,15 +117,15 @@ void init_uci_options() {
 }
 
 
 }
 
 
-/// options_to_uci() returns a string with all the UCI options in chronological
+/// OptionsMap::print_all() returns a string with all the UCI options in chronological
 /// insertion order (the idx field) and in the format defined by the UCI protocol.
 
 /// insertion order (the idx field) and in the format defined by the UCI protocol.
 
-string options_to_uci() {
+string OptionsMap::print_all() const {
 
   std::stringstream s;
 
 
   std::stringstream s;
 
-  for (size_t i = 0; i <= Options.size(); i++)
-      for (OptionsMap::const_iterator it = Options.begin(); it != Options.end(); ++it)
+  for (size_t i = 0; i <= size(); i++)
+      for (OptionsMap::const_iterator it = begin(); it != end(); ++it)
           if (it->second.idx == i)
           {
               const Option& o = it->second;
           if (it->second.idx == i)
           {
               const Option& o = it->second;
@@ -143,13 +145,13 @@ string options_to_uci() {
 
 /// Option class c'tors
 
 
 /// Option class c'tors
 
-Option::Option(const char* def) : type("string"), idx(Options.size()), minValue(0), maxValue(0)
+Option::Option(const char* def) : type("string"), minValue(0), maxValue(0), idx(Options.size())
 { defaultValue = currentValue = def; }
 
 { defaultValue = currentValue = def; }
 
-Option::Option(bool def, string t) : type(t), idx(Options.size()), minValue(0), maxValue(0)
+Option::Option(bool def, string t) : type(t), minValue(0), maxValue(0), idx(Options.size())
 { defaultValue = currentValue = (def ? "true" : "false"); }
 
 { defaultValue = currentValue = (def ? "true" : "false"); }
 
-Option::Option(int def, int minv, int maxv) : type("spin"), idx(Options.size()), minValue(minv), maxValue(maxv)
+Option::Option(int def, int minv, int maxv) : type("spin"), minValue(minv), maxValue(maxv), idx(Options.size())
 { defaultValue = currentValue = stringify(def); }
 
 
 { defaultValue = currentValue = stringify(def); }
 
 
@@ -164,8 +166,7 @@ void Option::set_value(const string& v) {
   if (v.empty())
       return;
 
   if (v.empty())
       return;
 
-  if (   (type == "check" || type == "button")
-      != (v == "true" || v == "false"))
+  if ((type == "check" || type == "button") != (v == "true" || v == "false"))
       return;
 
   if (type == "spin")
       return;
 
   if (type == "spin")
index b9c16cd913dab970780fc38f04966f209c388103..a5da2e440b768e50f3fa11f50700e11983f3b2e0 100644 (file)
@@ -27,7 +27,7 @@
 
 class Option {
 public:
 
 class Option {
 public:
-  Option() {} // To allow insertion in a std::map
+  Option() {} // To be used in a std::map
   Option(const char* defaultValue);
   Option(bool defaultValue, std::string type = "check");
   Option(int defaultValue, int minValue, int maxValue);
   Option(const char* defaultValue);
   Option(bool defaultValue, std::string type = "check");
   Option(int defaultValue, int minValue, int maxValue);
@@ -36,16 +36,33 @@ public:
   template<typename T> T value() const;
 
 private:
   template<typename T> T value() const;
 
 private:
-  friend void init_uci_options();
-  friend std::string options_to_uci();
+  friend class OptionsMap;
 
   std::string defaultValue, currentValue, type;
 
   std::string defaultValue, currentValue, type;
-  size_t idx;
   int minValue, maxValue;
   int minValue, maxValue;
+  size_t idx;
+};
+
+
+/// Custom comparator because UCI options should not be case sensitive
+struct CaseInsensitiveLess {
+  bool operator() (const std::string&, const std::string&) const;
+};
+
+
+/// Our options container is actually a map with a customized c'tor
+class OptionsMap : public std::map<std::string, Option, CaseInsensitiveLess> {
+public:
+  OptionsMap();
+  std::string print_all() const;
 };
 
 };
 
+extern OptionsMap Options;
+
+
+/// Option::value() definition and specializations
 template<typename T>
 template<typename T>
-inline T Option::value() const {
+T Option::value() const {
 
   assert(type == "spin");
   return T(atoi(currentValue.c_str()));
 
   assert(type == "spin");
   return T(atoi(currentValue.c_str()));
@@ -65,16 +82,4 @@ inline bool Option::value<bool>() const {
   return currentValue == "true";
 }
 
   return currentValue == "true";
 }
 
-
-// Custom comparator because UCI options should not be case sensitive
-struct CaseInsensitiveLess {
-  bool operator() (const std::string&, const std::string&) const;
-};
-
-typedef std::map<std::string, Option, CaseInsensitiveLess> OptionsMap;
-
-extern OptionsMap Options;
-extern void init_uci_options();
-extern std::string options_to_uci();
-
 #endif // !defined(UCIOPTION_H_INCLUDED)
 #endif // !defined(UCIOPTION_H_INCLUDED)