From 775488340e7f4c5054157a56461f8e76c733089e Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Thu, 5 Jul 2012 11:52:11 +0100 Subject: [PATCH] More idiomatic signature for operator=() Return a reference instead of void so to enable chained assignments like "p = q = Position(...);" Suggested by Rein Halbersma. No functional change. Signed-off-by: Marco Costalba --- src/position.cpp | 4 +++- src/position.h | 2 +- src/ucioption.cpp | 6 ++++-- src/ucioption.h | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index 4be0a231..a425b59c 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -89,7 +89,7 @@ CheckInfo::CheckInfo(const Position& pos) { /// object do not depend on any external data so we detach state pointer from /// the source one. -void Position::operator=(const Position& pos) { +Position& Position::operator=(const Position& pos) { memcpy(this, &pos, sizeof(Position)); startState = *st; @@ -97,6 +97,8 @@ void Position::operator=(const Position& pos) { nodes = 0; assert(pos_is_ok()); + + return *this; } diff --git a/src/position.h b/src/position.h index 27a70aae..9528bb71 100644 --- a/src/position.h +++ b/src/position.h @@ -97,7 +97,7 @@ public: Position(const Position& p) { *this = p; } Position(const Position& p, Thread* t) { *this = p; thisThread = t; } Position(const std::string& f, bool c960, Thread* t) { from_fen(f, c960, t); } - void operator=(const Position&); + Position& operator=(const Position&); // Text input/output void from_fen(const std::string& fen, bool isChess960, Thread* th); diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 763db556..58f3dbc0 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -134,18 +134,20 @@ UCIOption::UCIOption(int v, int minv, int maxv, Fn* f) : type("spin"), min(minv) /// check for option's limits, but we could receive the new value directly from /// the user by console window, so let's check the bounds anyway. -void UCIOption::operator=(const string& v) { +UCIOption& UCIOption::operator=(const string& v) { assert(!type.empty()); if ( (type != "button" && v.empty()) || (type == "check" && v != "true" && v != "false") || (type == "spin" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max))) - return; + return *this; if (type != "button") currentValue = v; if (on_change) (*on_change)(*this); + + return *this; } diff --git a/src/ucioption.h b/src/ucioption.h index c57cf021..0be8c7e3 100644 --- a/src/ucioption.h +++ b/src/ucioption.h @@ -38,7 +38,7 @@ public: UCIOption(const char* v, Fn* = NULL); UCIOption(int v, int min, int max, Fn* = NULL); - void operator=(const std::string& v); + UCIOption& operator=(const std::string& v); operator int() const { assert(type == "check" || type == "spin"); -- 2.39.2