From bacb645939397c8f4f070e46093ae764df20e34c Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Thu, 4 Nov 2010 08:03:27 +0100 Subject: [PATCH] Rewrite options handling in an object oriented fashion Big rewrite and about 100 lines removed. No functional change. Signed-off-by: Marco Costalba --- src/benchmark.cpp | 10 +- src/evaluate.cpp | 6 +- src/position.h | 12 -- src/search.cpp | 52 ++++---- src/timeman.cpp | 10 +- src/types.h | 4 + src/uci.cpp | 12 +- src/ucioption.cpp | 309 ++++++++++++++-------------------------------- src/ucioption.h | 50 ++++++-- 9 files changed, 183 insertions(+), 282 deletions(-) diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 15816bee..b42077b1 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -89,11 +89,11 @@ void benchmark(const string& commandLine) { cerr << "The number of threads must be between 1 and " << MAX_THREADS << endl; Application::exit_with_failure(); } - set_option_value("Hash", ttSize); - set_option_value("Threads", threads); - set_option_value("OwnBook", "false"); - set_option_value("Use Search Log", "true"); - set_option_value("Search Log Filename", "bench.txt"); + Options["Hash"].set_value(ttSize); + Options["Threads"].set_value(threads); + Options["OwnBook"].set_value("false"); + Options["Use Search Log"].set_value("true"); + Options["Search Log Filename"].set_value("bench.txt"); csVal >> val; csVal >> fileName; diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 7695b3bb..d349242f 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -423,7 +423,7 @@ void read_weights(Color us) { // If running in analysis mode, make sure we use symmetrical king safety. We do this // by replacing both Weights[kingDangerUs] and Weights[kingDangerThem] by their average. - if (get_option_value_bool("UCI_AnalyseMode")) + if (Options["UCI_AnalyseMode"].value()) Weights[kingDangerUs] = Weights[kingDangerThem] = (Weights[kingDangerUs] + Weights[kingDangerThem]) / 2; init_safety(); @@ -920,8 +920,8 @@ namespace { Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight) { // Scale option value from 100 to 256 - int mg = get_option_value_int(mgOpt) * 256 / 100; - int eg = get_option_value_int(egOpt) * 256 / 100; + int mg = Options[mgOpt].value() * 256 / 100; + int eg = Options[egOpt].value() * 256 / 100; return apply_weight(make_score(mg, eg), internalWeight); } diff --git a/src/position.h b/src/position.h index 4414519d..200dfdf9 100644 --- a/src/position.h +++ b/src/position.h @@ -21,18 +21,6 @@ #if !defined(POSITION_H_INCLUDED) #define POSITION_H_INCLUDED -// Disable some silly and noisy warning from MSVC compiler -#if defined(_MSC_VER) - -// Forcing value to bool 'true' or 'false' (performance warning) -#pragma warning(disable: 4800) - -// Conditional expression is constant -#pragma warning(disable: 4127) - - -#endif - //// //// Includes //// diff --git a/src/search.cpp b/src/search.cpp index c5c8b839..a4795c6a 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -407,12 +407,12 @@ bool think(Position& pos, bool infinite, bool ponder, int time[], int increment[ UseTimeManagement = !ExactMaxTime && !MaxDepth && !MaxNodes && !InfiniteSearch; // Look for a book move, only during games, not tests - if (UseTimeManagement && get_option_value_bool("OwnBook")) + if (UseTimeManagement && Options["OwnBook"].value()) { - if (get_option_value_string("Book File") != OpeningBook.file_name()) - OpeningBook.open(get_option_value_string("Book File")); + if (Options["Book File"].value() != OpeningBook.file_name()) + OpeningBook.open(Options["Book File"].value()); - Move bookMove = OpeningBook.get_move(pos, get_option_value_bool("Best Book Move")); + Move bookMove = OpeningBook.get_move(pos, Options["Best Book Move"].value()); if (bookMove != MOVE_NONE) { if (PonderSearch) @@ -424,38 +424,38 @@ bool think(Position& pos, bool infinite, bool ponder, int time[], int increment[ } // Read UCI option values - TT.set_size(get_option_value_int("Hash")); - if (get_option_value_bool("Clear Hash")) + TT.set_size(Options["Hash"].value()); + if (Options["Clear Hash"].value()) { - set_option_value("Clear Hash", "false"); + Options["Clear Hash"].set_value("false"); TT.clear(); } - CheckExtension[1] = Depth(get_option_value_int("Check Extension (PV nodes)")); - CheckExtension[0] = Depth(get_option_value_int("Check Extension (non-PV nodes)")); - SingleEvasionExtension[1] = Depth(get_option_value_int("Single Evasion Extension (PV nodes)")); - SingleEvasionExtension[0] = Depth(get_option_value_int("Single Evasion Extension (non-PV nodes)")); - PawnPushTo7thExtension[1] = Depth(get_option_value_int("Pawn Push to 7th Extension (PV nodes)")); - PawnPushTo7thExtension[0] = Depth(get_option_value_int("Pawn Push to 7th Extension (non-PV nodes)")); - PassedPawnExtension[1] = Depth(get_option_value_int("Passed Pawn Extension (PV nodes)")); - PassedPawnExtension[0] = Depth(get_option_value_int("Passed Pawn Extension (non-PV nodes)")); - PawnEndgameExtension[1] = Depth(get_option_value_int("Pawn Endgame Extension (PV nodes)")); - PawnEndgameExtension[0] = Depth(get_option_value_int("Pawn Endgame Extension (non-PV nodes)")); - MateThreatExtension[1] = Depth(get_option_value_int("Mate Threat Extension (PV nodes)")); - MateThreatExtension[0] = Depth(get_option_value_int("Mate Threat Extension (non-PV nodes)")); - - MinimumSplitDepth = get_option_value_int("Minimum Split Depth") * ONE_PLY; - MaxThreadsPerSplitPoint = get_option_value_int("Maximum Number of Threads per Split Point"); - MultiPV = get_option_value_int("MultiPV"); - UseLogFile = get_option_value_bool("Use Search Log"); + CheckExtension[1] = Options["Check Extension (PV nodes)"].value(); + CheckExtension[0] = Options["Check Extension (non-PV nodes)"].value(); + SingleEvasionExtension[1] = Options["Single Evasion Extension (PV nodes)"].value(); + SingleEvasionExtension[0] = Options["Single Evasion Extension (non-PV nodes)"].value(); + PawnPushTo7thExtension[1] = Options["Pawn Push to 7th Extension (PV nodes)"].value(); + PawnPushTo7thExtension[0] = Options["Pawn Push to 7th Extension (non-PV nodes)"].value(); + PassedPawnExtension[1] = Options["Passed Pawn Extension (PV nodes)"].value(); + PassedPawnExtension[0] = Options["Passed Pawn Extension (non-PV nodes)"].value(); + PawnEndgameExtension[1] = Options["Pawn Endgame Extension (PV nodes)"].value(); + PawnEndgameExtension[0] = Options["Pawn Endgame Extension (non-PV nodes)"].value(); + MateThreatExtension[1] = Options["Mate Threat Extension (PV nodes)"].value(); + MateThreatExtension[0] = Options["Mate Threat Extension (non-PV nodes)"].value(); + + MinimumSplitDepth = Options["Minimum Split Depth"].value() * ONE_PLY; + MaxThreadsPerSplitPoint = Options["Maximum Number of Threads per Split Point"].value(); + MultiPV = Options["MultiPV"].value(); + UseLogFile = Options["Use Search Log"].value(); if (UseLogFile) - LogFile.open(get_option_value_string("Search Log Filename").c_str(), std::ios::out | std::ios::app); + LogFile.open(Options["Search Log Filename"].value().c_str(), std::ios::out | std::ios::app); read_weights(pos.side_to_move()); // Set the number of active threads - int newActiveThreads = get_option_value_int("Threads"); + int newActiveThreads = Options["Threads"].value(); if (newActiveThreads != ThreadsMgr.active_threads()) { ThreadsMgr.set_active_threads(newActiveThreads); diff --git a/src/timeman.cpp b/src/timeman.cpp index e05c4626..86e6729c 100644 --- a/src/timeman.cpp +++ b/src/timeman.cpp @@ -114,10 +114,10 @@ void TimeManager::init(int myTime, int myInc, int movesToGo, int currentPly) int hypMTG, hypMyTime, t1, t2; // Read uci parameters - int emergencyMoveHorizon = get_option_value_int("Emergency Move Horizon"); - int emergencyBaseTime = get_option_value_int("Emergency Base Time"); - int emergencyMoveTime = get_option_value_int("Emergency Move Time"); - int minThinkingTime = get_option_value_int("Minimum Thinking Time"); + int emergencyMoveHorizon = Options["Emergency Move Horizon"].value(); + int emergencyBaseTime = Options["Emergency Base Time"].value(); + int emergencyMoveTime = Options["Emergency Move Time"].value(); + int minThinkingTime = Options["Minimum Thinking Time"].value(); // Initialize to maximum values but unstablePVExtraTime that is reset unstablePVExtraTime = 0; @@ -137,7 +137,7 @@ void TimeManager::init(int myTime, int myInc, int movesToGo, int currentPly) maximumSearchTime = Min(maximumSearchTime, t2); } - if (get_option_value_bool("Ponder")) + if (Options["Ponder"].value()) optimumSearchTime += optimumSearchTime / 4; // Make sure that maxSearchTime is not over absoluteMaxSearchTime diff --git a/src/types.h b/src/types.h index f6d993d0..91563109 100644 --- a/src/types.h +++ b/src/types.h @@ -27,6 +27,10 @@ #else +// Disable some silly and noisy warning from MSVC compiler +#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false' +#pragma warning(disable: 4127) // Conditional expression is constant + typedef __int8 int8_t; typedef unsigned __int8 uint8_t; typedef __int16 int16; diff --git a/src/uci.cpp b/src/uci.cpp index c9ecbf23..a4964ecd 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -122,7 +122,7 @@ namespace { } else if (token == "ucinewgame") { - set_option_value("New Game", "true"); + Options["New Game"].set_value("true"); pos.from_fen(StartPositionFEN); } else if (token == "isready") @@ -233,16 +233,22 @@ namespace { while (uip >> token && token != "value") name += (" " + token); + if (Options.find(name) == Options.end()) + { + cout << "No such option: " << name << endl; + return; + } + if (token != "value" || !(uip >> value)) { - set_option_value(name, "true"); + Options[name].set_value("true"); return; } while (uip >> token) value += (" " + token); - set_option_value(name, value); + Options[name].set_value(value); } diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 2db1beeb..514d7b81 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -17,17 +17,8 @@ along with this program. If not, see . */ - -//// -//// Includes -//// - -#include -#include -#include -#include +#include #include -#include #include "misc.h" #include "thread.h" @@ -37,150 +28,68 @@ using std::string; using std::cout; using std::endl; -//// -//// Local definitions -//// - -namespace { - - enum OptionType { SPIN, COMBO, CHECK, STRING, BUTTON }; - - typedef std::vector StrVector; - - struct Option { - - string name, defaultValue, currentValue; - OptionType type; - size_t idx; - int minValue, maxValue; - StrVector comboValues; - - Option(); - Option(const char* defaultValue, OptionType = STRING); - Option(bool defaultValue, OptionType = CHECK); - Option(int defaultValue, int minValue, int maxValue); - - bool operator<(const Option& o) const { return idx < o.idx; } - }; - - typedef std::vector