Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: warning about virtual method bypassing virtual dispatch #124

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/pages/extending-the-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,8 @@ int main() {
}
```

> [!IMPORTANT]
> If you do this you must call setFen after creating the board, otherwise the board won't use the overriden placePiece function.

If this was still not enough for you, think about adding the desired functionality back to master, in case
they are universal enough.
30 changes: 27 additions & 3 deletions include/chess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ THIS FILE IS AUTO GENERATED DO NOT CHANGE MANUALLY.

Source: https://github.com/Disservin/chess-library

VERSION: 0.6.68
VERSION: 0.6.69
*/

#ifndef CHESS_HPP
Expand Down Expand Up @@ -1776,7 +1776,7 @@ class Board {
explicit Board(std::string_view fen = constants::STARTPOS, bool chess960 = false) {
prev_states_.reserve(256);
chess960_ = chess960;
setFenInternal(fen);
setFenInternal<true>(fen);
}

virtual void setFen(std::string_view fen) { setFenInternal(fen); }
Expand Down Expand Up @@ -2794,8 +2794,25 @@ class Board {
bool chess960_ = false;

private:
void placePieceInternal(Piece piece, Square sq) {
assert(board_[sq.index()] == Piece::NONE);

auto type = piece.type();
auto color = piece.color();
auto index = sq.index();

assert(type != PieceType::NONE);
assert(color != Color::NONE);
assert(index >= 0 && index < 64);

pieces_bb_[type].set(index);
occ_bb_[color].set(index);
board_[index] = piece;
}

/// @brief [Internal Usage]
/// @param fen
template <bool ctor = false>
void setFenInternal(std::string_view fen) {
original_fen_ = fen;

Expand Down Expand Up @@ -2854,7 +2871,14 @@ class Board {
square -= 16;
} else {
auto p = Piece(std::string_view(&curr, 1));
placePiece(p, square);

// prevent warnings about virtual method bypassing virtual dispatch
if constexpr (ctor) {
placePieceInternal(p, Square(square));
} else {
placePiece(p, square);
}

key_ ^= Zobrist::piece(p, Square(square));
++square;
}
Expand Down
28 changes: 26 additions & 2 deletions src/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Board {
explicit Board(std::string_view fen = constants::STARTPOS, bool chess960 = false) {
prev_states_.reserve(256);
chess960_ = chess960;
setFenInternal(fen);
setFenInternal<true>(fen);
}

virtual void setFen(std::string_view fen) { setFenInternal(fen); }
Expand Down Expand Up @@ -1139,8 +1139,25 @@ class Board {
bool chess960_ = false;

private:
void placePieceInternal(Piece piece, Square sq) {
assert(board_[sq.index()] == Piece::NONE);

auto type = piece.type();
auto color = piece.color();
auto index = sq.index();

assert(type != PieceType::NONE);
assert(color != Color::NONE);
assert(index >= 0 && index < 64);

pieces_bb_[type].set(index);
occ_bb_[color].set(index);
board_[index] = piece;
}

/// @brief [Internal Usage]
/// @param fen
template <bool ctor = false>
void setFenInternal(std::string_view fen) {
original_fen_ = fen;

Expand Down Expand Up @@ -1199,7 +1216,14 @@ class Board {
square -= 16;
} else {
auto p = Piece(std::string_view(&curr, 1));
placePiece(p, square);

// prevent warnings about virtual method bypassing virtual dispatch
if constexpr (ctor) {
placePieceInternal(p, Square(square));
} else {
placePiece(p, square);
}

key_ ^= Zobrist::piece(p, Square(square));
++square;
}
Expand Down
2 changes: 1 addition & 1 deletion src/include.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ THIS FILE IS AUTO GENERATED DO NOT CHANGE MANUALLY.

Source: https://github.com/Disservin/chess-library

VERSION: 0.6.68
VERSION: 0.6.69
*/

#ifndef CHESS_HPP
Expand Down