]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Import C++11 branch
[stockfish] / src / ucioption.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <algorithm>
21 #include <cassert>
22 #include <sstream>
23
24 #include "misc.h"
25 #include "thread.h"
26 #include "tt.h"
27 #include "uci.h"
28
29 using std::string;
30
31 UCI::OptionsMap Options; // Global object
32
33 namespace UCI {
34
35 /// 'On change' actions, triggered by an option's value change
36 void on_clear_hash(const Option&) { TT.clear(); }
37 void on_hash_size(const Option& o) { TT.resize(o); }
38 void on_logger(const Option& o) { start_logger(o); }
39 void on_threads(const Option&) { Threads.read_uci_options(); }
40
41
42 /// Our case insensitive less() function as required by UCI protocol
43 bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
44
45   return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
46          [](char c1, char c2) { return tolower(c1) < tolower(c2); });
47 }
48
49
50 /// init() initializes the UCI options to their hard-coded default values
51
52 void init(OptionsMap& o) {
53
54   const int MaxHashMB = Is64Bit ? 1024 * 1024 : 2048;
55
56   o["Write Debug Log"]       << Option(false, on_logger);
57   o["Contempt"]              << Option(0, -100, 100);
58   o["Min Split Depth"]       << Option(0, 0, 12, on_threads);
59   o["Threads"]               << Option(1, 1, MAX_THREADS, on_threads);
60   o["Hash"]                  << Option(16, 1, MaxHashMB, on_hash_size);
61   o["Clear Hash"]            << Option(on_clear_hash);
62   o["Ponder"]                << Option(true);
63   o["MultiPV"]               << Option(1, 1, 500);
64   o["Skill Level"]           << Option(20, 0, 20);
65   o["Move Overhead"]         << Option(30, 0, 5000);
66   o["Minimum Thinking Time"] << Option(20, 0, 5000);
67   o["Slow Mover"]            << Option(80, 10, 1000);
68   o["UCI_Chess960"]          << Option(false);
69 }
70
71
72 /// operator<<() is used to print all the options default values in chronological
73 /// insertion order (the idx field) and in the format defined by the UCI protocol.
74
75 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
76
77   for (size_t idx = 0; idx < om.size(); ++idx)
78       for (auto it : om)
79           if (it.second.idx == idx)
80           {
81               const Option& o = it.second;
82               os << "\noption name " << it.first << " type " << o.type;
83
84               if (o.type != "button")
85                   os << " default " << o.defaultValue;
86
87               if (o.type == "spin")
88                   os << " min " << o.min << " max " << o.max;
89
90               break;
91           }
92
93   return os;
94 }
95
96
97 /// Option class constructors and conversion operators
98
99 Option::Option(const char* v, OnChange f) : type("string"), min(0), max(0), on_change(f)
100 { defaultValue = currentValue = v; }
101
102 Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), on_change(f)
103 { defaultValue = currentValue = (v ? "true" : "false"); }
104
105 Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
106 {}
107
108 Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
109 { defaultValue = currentValue = std::to_string(v); }
110
111 Option::operator int() const {
112   assert(type == "check" || type == "spin");
113   return (type == "spin" ? stoi(currentValue) : currentValue == "true");
114 }
115
116 Option::operator std::string() const {
117   assert(type == "string");
118   return currentValue;
119 }
120
121
122 /// operator<<() inits options and assigns idx in the correct printing order
123
124 void Option::operator<<(const Option& o) {
125
126   static size_t insert_order = 0;
127
128   *this = o;
129   idx = insert_order++;
130 }
131
132
133 /// operator=() updates currentValue and triggers on_change() action. It's up to
134 /// the GUI to check for option's limits, but we could receive the new value from
135 /// the user by console window, so let's check the bounds anyway.
136
137 Option& Option::operator=(const string& v) {
138
139   assert(!type.empty());
140
141   if (   (type != "button" && v.empty())
142       || (type == "check" && v != "true" && v != "false")
143       || (type == "spin" && (stoi(v) < min || stoi(v) > max)))
144       return *this;
145
146   if (type != "button")
147       currentValue = v;
148
149   if (on_change)
150       on_change(*this);
151
152   return *this;
153 }
154
155 } // namespace UCI