1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 19:17:44 +00:00

Everywhere: Stop using NonnullRefPtrVector

This class had slightly confusing semantics and the added weirdness
doesn't seem worth it just so we can say "." instead of "->" when
iterating over a vector of NNRPs.

This patch replaces NonnullRefPtrVector<T> with Vector<NNRP<T>>.
This commit is contained in:
Andreas Kling 2023-03-06 14:17:01 +01:00
parent 104be6c8ac
commit 8a48246ed1
168 changed files with 1280 additions and 1280 deletions

View file

@ -197,7 +197,7 @@ void Game::setup(DeprecatedString player_name, int hand_number)
m_passing_button->set_focus(false);
}
NonnullRefPtrVector<Card> deck = Cards::create_standard_deck(Cards::Shuffle::Yes).release_value_but_fixme_should_propagate_errors();
Vector<NonnullRefPtr<Card>> deck = Cards::create_standard_deck(Cards::Shuffle::Yes).release_value_but_fixme_should_propagate_errors();
for (auto& player : m_players) {
player.hand.ensure_capacity(Card::card_count);
@ -220,7 +220,7 @@ void Game::setup(DeprecatedString player_name, int hand_number)
continue_game_after_delay();
}
void Game::start_animation(NonnullRefPtrVector<Card> cards, Gfx::IntPoint end, Function<void()> did_finish_callback, int initial_delay_ms, int steps)
void Game::start_animation(Vector<NonnullRefPtr<Card>> cards, Gfx::IntPoint end, Function<void()> did_finish_callback, int initial_delay_ms, int steps)
{
stop_animation();
@ -229,7 +229,7 @@ void Game::start_animation(NonnullRefPtrVector<Card> cards, Gfx::IntPoint end, F
m_animation_steps = steps;
m_animation_cards.clear_with_capacity();
for (auto& card : cards)
m_animation_cards.empend(card, card.position());
m_animation_cards.empend(card, card->position());
m_animation_did_finish = make<Function<void()>>(move(did_finish_callback));
m_animation_delay_timer = Core::Timer::create_single_shot(initial_delay_ms, [&] {
m_animation_playing = true;
@ -338,17 +338,17 @@ size_t Game::pick_card(Player& player)
return player.pick_lead_card(move(valid_card), move(prefer_card));
}
}
auto* high_card = &m_trick[0];
auto* high_card = m_trick[0].ptr();
for (auto& card : m_trick)
if (high_card->suit() == card.suit() && hearts_card_value(card) > hearts_card_value(*high_card))
high_card = &card;
if (high_card->suit() == card->suit() && hearts_card_value(card) > hearts_card_value(*high_card))
high_card = card;
if (high_card->suit() == Cards::Suit::Spades && hearts_card_value(*high_card) > CardValue::Queen)
RETURN_CARD_IF_VALID(player.pick_specific_card(Cards::Suit::Spades, CardValue::Queen));
auto card_has_points = [](Card& card) { return hearts_card_points(card) > 0; };
auto trick_has_points = m_trick.first_matching(card_has_points).has_value();
bool is_trailing_player = m_trick.size() == 3;
if (!trick_has_points && is_trailing_player) {
RETURN_CARD_IF_VALID(player.pick_low_points_high_value_card(m_trick[0].suit()));
RETURN_CARD_IF_VALID(player.pick_low_points_high_value_card(m_trick[0]->suit()));
if (is_first_trick)
return player.pick_low_points_high_value_card().value();
else {
@ -523,11 +523,11 @@ void Game::advance_game()
return;
}
auto leading_card_suit = m_trick[0].suit();
auto leading_card_suit = m_trick[0]->suit();
size_t taker_index = 0;
auto taker_value = hearts_card_value(m_trick[0]);
for (size_t i = 1; i < 4; i++) {
if (m_trick[i].suit() != leading_card_suit)
if (m_trick[i]->suit() != leading_card_suit)
continue;
if (hearts_card_value(m_trick[i]) <= taker_value)
continue;
@ -608,7 +608,7 @@ void Game::play_card(Player& player, size_t card_index)
VERIFY(m_leading_player);
size_t leading_player_index = player_index(*m_leading_player);
NonnullRefPtrVector<Card> cards;
Vector<NonnullRefPtr<Card>> cards;
cards.append(*card);
start_animation(
cards,
@ -661,7 +661,7 @@ bool Game::is_valid_play(Player& player, Card& card, DeprecatedString* explanati
}
// Player must follow suit unless they don't have any matching cards.
auto leading_card_suit = m_trick[0].suit();
auto leading_card_suit = m_trick[0]->suit();
if (leading_card_suit == card.suit())
return true;
auto has_matching_card = player.has_card_of_suit(leading_card_suit);
@ -818,13 +818,13 @@ void Game::select_cards_for_passing()
void Game::pass_cards()
{
NonnullRefPtrVector<Card> first_player_cards;
Vector<NonnullRefPtr<Card>> first_player_cards;
for (auto& card : m_cards_highlighted)
first_player_cards.append(*card);
clear_highlighted_cards();
VERIFY(first_player_cards.size() == 3);
NonnullRefPtrVector<Card> passed_cards[4];
Vector<NonnullRefPtr<Card>> passed_cards[4];
passed_cards[0] = first_player_cards;
passed_cards[1] = m_players[1].pick_cards_to_pass(passing_direction());
passed_cards[2] = m_players[2].pick_cards_to_pass(passing_direction());
@ -852,7 +852,7 @@ void Game::pass_cards()
for (auto& card : passed_cards[i]) {
m_players[destination_player_index].hand.append(card);
if constexpr (!HEARTS_DEBUG)
card.set_upside_down(destination_player_index != 0);
card->set_upside_down(destination_player_index != 0);
if (destination_player_index == 0)
highlight_card(card);
}
@ -911,7 +911,7 @@ void Game::paint_event(GUI::PaintEvent& event)
}
for (size_t i = 0; i < m_trick.size(); i++)
m_trick[i].paint(painter);
m_trick[i]->paint(painter);
}
void Game::dump_state() const

View file

@ -64,7 +64,7 @@ private:
void pass_cards();
PassingDirection passing_direction() const;
void start_animation(NonnullRefPtrVector<Card> cards, Gfx::IntPoint end, Function<void()> did_finish_callback, int initial_delay_ms, int steps = 30);
void start_animation(Vector<NonnullRefPtr<Card>> cards, Gfx::IntPoint end, Function<void()> did_finish_callback, int initial_delay_ms, int steps = 30);
void stop_animation();
virtual void paint_event(GUI::PaintEvent&) override;
@ -92,7 +92,7 @@ private:
HashTable<NonnullRefPtr<Card>> m_cards_highlighted;
Player m_players[4];
NonnullRefPtrVector<Card> m_trick;
Vector<NonnullRefPtr<Card>> m_trick;
Player* m_leading_player { nullptr };
u8 m_trick_number { 0 };
RefPtr<Core::Timer> m_delay_timer;

View file

@ -25,10 +25,10 @@ static bool compare_card_points_and_value(CardWithIndex& cwi1, CardWithIndex& cw
return false;
}
NonnullRefPtrVector<Card> Player::pick_cards_to_pass(PassingDirection)
Vector<NonnullRefPtr<Card>> Player::pick_cards_to_pass(PassingDirection)
{
auto sorted_hand = hand_sorted_by_fn(compare_card_value);
NonnullRefPtrVector<Card> cards;
Vector<NonnullRefPtr<Card>> cards;
cards.append(*sorted_hand[0].card);
cards.append(*sorted_hand[1].card);
cards.append(*sorted_hand[2].card);
@ -158,11 +158,11 @@ bool Player::has_card_of_suit(Cards::Suit suit)
return matching_card.has_value();
}
void Player::remove_cards(NonnullRefPtrVector<Card> const& cards)
void Player::remove_cards(Vector<NonnullRefPtr<Card>> const& cards)
{
for (auto& card : cards) {
hand.remove_first_matching([&card](auto& other_card) {
return other_card.ptr() == &card;
return other_card == card;
});
}
}

View file

@ -33,7 +33,7 @@ public:
{
}
NonnullRefPtrVector<Card> pick_cards_to_pass(PassingDirection);
Vector<NonnullRefPtr<Card>> pick_cards_to_pass(PassingDirection);
size_t pick_lead_card(Function<bool(Card&)>, Function<bool(Card&)>);
Optional<size_t> pick_low_points_high_value_card(Optional<Cards::Suit> suit = {});
Optional<size_t> pick_lower_value_card(Card& other_card);
@ -45,7 +45,7 @@ public:
Vector<CardWithIndex> hand_sorted_by_fn(bool (*)(CardWithIndex&, CardWithIndex&)) const;
void sort_hand() { quick_sort(hand, hearts_card_less); }
void remove_cards(NonnullRefPtrVector<Card> const& cards);
void remove_cards(Vector<NonnullRefPtr<Card>> const& cards);
Vector<RefPtr<Card>> hand;
Vector<RefPtr<Card>> cards_taken;

View file

@ -53,7 +53,7 @@ ErrorOr<NonnullRefPtr<Game>> Game::try_create()
"/res/emoji/U+1FAB1.png"sv,
};
NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps;
Vector<NonnullRefPtr<Gfx::Bitmap>> food_bitmaps;
TRY(food_bitmaps.try_ensure_capacity(food_bitmaps_files.size()));
for (auto file : food_bitmaps_files) {
@ -69,7 +69,7 @@ ErrorOr<NonnullRefPtr<Game>> Game::try_create()
return adopt_nonnull_ref_or_enomem(new (nothrow) Game(move(food_bitmaps)));
}
Game::Game(NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps)
Game::Game(Vector<NonnullRefPtr<Gfx::Bitmap>> food_bitmaps)
: m_food_bitmaps(move(food_bitmaps))
{
set_font(Gfx::FontDatabase::default_fixed_width_font().bold_variant());
@ -263,7 +263,7 @@ void Game::paint_event(GUI::PaintEvent& event)
painter.fill_rect(bottom_side, m_snake_base_color.darkened(0.55));
}
painter.draw_scaled_bitmap(cell_rect(m_fruit), m_food_bitmaps[m_fruit_type], m_food_bitmaps[m_fruit_type].rect());
painter.draw_scaled_bitmap(cell_rect(m_fruit), m_food_bitmaps[m_fruit_type], m_food_bitmaps[m_fruit_type]->rect());
}
void Game::game_over()

View file

@ -30,7 +30,7 @@ public:
Function<bool(u32)> on_score_update;
private:
explicit Game(NonnullRefPtrVector<Gfx::Bitmap> food_bitmaps);
explicit Game(Vector<NonnullRefPtr<Gfx::Bitmap>> food_bitmaps);
virtual void paint_event(GUI::PaintEvent&) override;
virtual void keydown_event(GUI::KeyEvent&) override;
@ -76,7 +76,7 @@ private:
unsigned m_score { 0 };
bool m_is_new_high_score { false };
NonnullRefPtrVector<Gfx::Bitmap> m_food_bitmaps;
Vector<NonnullRefPtr<Gfx::Bitmap>> m_food_bitmaps;
Gfx::Color m_snake_base_color { Color::Yellow };
};

View file

@ -172,7 +172,7 @@ void Game::setup(Mode mode)
on_game_end(GameOverReason::NewGame, m_score);
for (auto& stack : stacks())
stack.clear();
stack->clear();
m_new_deck.clear();
m_new_game_animation_pile = 0;
@ -256,14 +256,14 @@ void Game::mousedown_event(GUI::MouseEvent& event)
auto click_location = event.position();
for (auto& to_check : stacks()) {
if (to_check.type() == CardStack::Type::Waste)
if (to_check->type() == CardStack::Type::Waste)
continue;
if (to_check.bounding_box().contains(click_location)) {
if (to_check.type() == CardStack::Type::Stock) {
if (to_check->bounding_box().contains(click_location)) {
if (to_check->type() == CardStack::Type::Stock) {
draw_cards();
} else if (!to_check.is_empty()) {
auto& top_card = to_check.peek();
} else if (!to_check->is_empty()) {
auto& top_card = to_check->peek();
if (top_card.is_upside_down()) {
if (top_card.rect().contains(click_location)) {
@ -356,8 +356,8 @@ void Game::mousemove_event(GUI::MouseEvent& event)
for (auto& to_intersect : moving_cards()) {
mark_intersecting_stacks_dirty(to_intersect);
to_intersect.rect().translate_by(dx, dy);
update(to_intersect.rect());
to_intersect->rect().translate_by(dx, dy);
update(to_intersect->rect());
}
m_mouse_down_location = click_location;
@ -380,11 +380,11 @@ void Game::doubleclick_event(GUI::MouseEvent& event)
auto click_location = event.position();
for (auto& to_check : stacks()) {
if (to_check.type() != CardStack::Type::Normal && to_check.type() != CardStack::Type::Play)
if (to_check->type() != CardStack::Type::Normal && to_check->type() != CardStack::Type::Play)
continue;
if (to_check.bounding_box().contains(click_location) && !to_check.is_empty()) {
auto& top_card = to_check.peek();
if (to_check->bounding_box().contains(click_location) && !to_check->is_empty()) {
auto& top_card = to_check->peek();
if (!top_card.is_upside_down() && top_card.rect().contains(click_location))
attempt_to_move_card_to_foundations(to_check);
@ -418,7 +418,7 @@ void Game::draw_cards()
update(waste.bounding_box());
update(play.bounding_box());
NonnullRefPtrVector<Card> moved_cards;
Vector<NonnullRefPtr<Card>> moved_cards;
while (!play.is_empty()) {
auto card = play.pop();
stock.push(card).release_value_but_fixme_should_propagate_errors();
@ -458,7 +458,7 @@ void Game::draw_cards()
update(stock.bounding_box());
NonnullRefPtrVector<Card> cards_drawn;
Vector<NonnullRefPtr<Card>> cards_drawn;
for (size_t i = 0; (i < cards_to_draw) && !stock.is_empty(); ++i) {
auto card = stock.pop();
cards_drawn.prepend(card);
@ -509,7 +509,7 @@ bool Game::attempt_to_move_card_to_foundations(CardStack& from)
mark_intersecting_stacks_dirty(card);
foundation.push(card).release_value_but_fixme_should_propagate_errors();
NonnullRefPtrVector<Card> moved_card;
Vector<NonnullRefPtr<Card>> moved_card;
moved_card.append(card);
remember_move_for_undo(from, foundation, moved_card);
@ -538,7 +538,7 @@ void Game::auto_move_eligible_cards_to_foundations()
while (true) {
bool card_was_moved = false;
for (auto& to_check : stacks()) {
if (to_check.type() != CardStack::Type::Normal && to_check.type() != CardStack::Type::Play)
if (to_check->type() != CardStack::Type::Normal && to_check->type() != CardStack::Type::Play)
continue;
if (attempt_to_move_card_to_foundations(to_check))
@ -567,17 +567,17 @@ void Game::paint_event(GUI::PaintEvent& event)
if (is_moving_cards()) {
for (auto& card : moving_cards())
card.clear(painter, background_color);
card->clear(painter, background_color);
}
for (auto& stack : stacks()) {
stack.paint(painter, background_color);
stack->paint(painter, background_color);
}
if (is_moving_cards()) {
for (auto& card : moving_cards()) {
card.paint(painter);
card.save_old_position();
card->paint(painter);
card->save_old_position();
}
}
@ -585,14 +585,14 @@ void Game::paint_event(GUI::PaintEvent& event)
if (is_moving_cards()) {
check_for_game_over();
for (auto& card : moving_cards())
card.set_moving(false);
card->set_moving(false);
}
clear_moving_cards();
}
}
void Game::remember_move_for_undo(CardStack& from, CardStack& to, NonnullRefPtrVector<Card> moved_cards)
void Game::remember_move_for_undo(CardStack& from, CardStack& to, Vector<NonnullRefPtr<Card>> moved_cards)
{
m_last_move.type = LastMove::Type::MoveCards;
m_last_move.from = &from;
@ -604,7 +604,7 @@ void Game::remember_move_for_undo(CardStack& from, CardStack& to, NonnullRefPtrV
void Game::remember_flip_for_undo(Card& card)
{
NonnullRefPtrVector<Card> cards;
Vector<NonnullRefPtr<Card>> cards;
cards.append(card);
m_last_move.type = LastMove::Type::FlipCard;
m_last_move.cards = cards;
@ -618,7 +618,7 @@ void Game::perform_undo()
return;
if (m_last_move.type == LastMove::Type::FlipCard) {
m_last_move.cards.at(0).set_upside_down(true);
m_last_move.cards[0]->set_upside_down(true);
if (on_undo_availability_change)
on_undo_availability_change(false);
invalidate_layout();
@ -641,7 +641,7 @@ void Game::perform_undo()
if (m_last_move.from->type() == CardStack::Type::Stock) {
auto& waste = stack_at_location(Waste);
auto& play = stack_at_location(Play);
NonnullRefPtrVector<Card> cards_popped;
Vector<NonnullRefPtr<Card>> cards_popped;
for (size_t i = 0; i < m_last_move.cards.size(); i++) {
if (!waste.is_empty()) {
auto card = waste.pop();

View file

@ -130,7 +130,7 @@ private:
Type type { Type::Invalid };
CardStack* from { nullptr };
NonnullRefPtrVector<Card> cards;
Vector<NonnullRefPtr<Card>> cards;
CardStack* to { nullptr };
};
@ -173,7 +173,7 @@ private:
void score_move(CardStack& from, CardStack& to, bool inverse = false);
void score_flip(bool inverse = false);
void remember_move_for_undo(CardStack& from, CardStack& to, NonnullRefPtrVector<Card> moved_cards);
void remember_move_for_undo(CardStack& from, CardStack& to, Vector<NonnullRefPtr<Card>> moved_cards);
void remember_flip_for_undo(Card& card);
void update_score(int to_add);
void draw_cards();
@ -200,7 +200,7 @@ private:
Mode m_mode { Mode::SingleCardDraw };
LastMove m_last_move;
NonnullRefPtrVector<Card> m_new_deck;
Vector<NonnullRefPtr<Card>> m_new_deck;
Gfx::IntPoint m_mouse_down_location;
bool m_mouse_down { false };

View file

@ -50,7 +50,7 @@ void Game::setup(Mode mode)
on_game_end(GameOverReason::NewGame, m_score);
for (auto& stack : stacks())
stack.clear();
stack->clear();
m_new_game_animation_pile = 0;
@ -91,7 +91,7 @@ void Game::perform_undo()
if (!m_last_move.was_visible)
m_last_move.from->peek().set_upside_down(true);
NonnullRefPtrVector<Card> cards;
Vector<NonnullRefPtr<Card>> cards;
for (size_t i = 0; i < m_last_move.card_count; i++)
cards.append(m_last_move.to->pop());
for (ssize_t i = m_last_move.card_count - 1; i >= 0; i--)
@ -146,18 +146,18 @@ void Game::detect_full_stacks()
Color color;
for (size_t i = current_pile.stack().size(); i > 0; i--) {
auto& card = current_pile.stack().at(i - 1);
if (card.is_upside_down())
if (card->is_upside_down())
break;
if (!started) {
if (card.rank() != Cards::Rank::Ace)
if (card->rank() != Cards::Rank::Ace)
break;
started = true;
color = card.color();
} else if (to_underlying(card.rank()) != last_value + 1 || card.color() != color) {
color = card->color();
} else if (to_underlying(card->rank()) != last_value + 1 || card->color() != color) {
break;
} else if (card.rank() == Cards::Rank::King) {
} else if (card->rank() == Cards::Rank::King) {
// we have a full set
auto original_current_rect = current_pile.bounding_box();
@ -177,7 +177,7 @@ void Game::detect_full_stacks()
on_undo_availability_change(false);
}
last_value = to_underlying(card.rank());
last_value = to_underlying(card->rank());
}
}
@ -212,24 +212,24 @@ void Game::paint_event(GUI::PaintEvent& event)
if (is_moving_cards()) {
for (auto& card : moving_cards())
card.clear(painter, background_color);
card->clear(painter, background_color);
}
for (auto& stack : stacks()) {
stack.paint(painter, background_color);
stack->paint(painter, background_color);
}
if (is_moving_cards()) {
for (auto& card : moving_cards()) {
card.paint(painter);
card.save_old_position();
card->paint(painter);
card->save_old_position();
}
}
if (!m_mouse_down) {
if (is_moving_cards()) {
for (auto& card : moving_cards())
card.set_moving(false);
card->set_moving(false);
}
clear_moving_cards();
}
@ -255,15 +255,15 @@ void Game::mousedown_event(GUI::MouseEvent& event)
auto click_location = event.position();
for (auto& to_check : stacks()) {
if (to_check.type() == CardStack::Type::Waste)
if (to_check->type() == CardStack::Type::Waste)
continue;
if (to_check.bounding_box().contains(click_location)) {
if (to_check.type() == CardStack::Type::Stock) {
if (to_check->bounding_box().contains(click_location)) {
if (to_check->type() == CardStack::Type::Stock) {
start_timer_if_necessary();
draw_cards();
} else if (!to_check.is_empty()) {
auto& top_card = to_check.peek();
} else if (!to_check->is_empty()) {
auto& top_card = to_check->peek();
if (top_card.is_upside_down()) {
if (top_card.rect().contains(click_location)) {
@ -312,7 +312,7 @@ void Game::mouseup_event(GUI::MouseEvent& event)
if (stack == moving_cards_source_stack())
continue;
if (stack.is_allowed_to_push(moving_cards().at(0), moving_cards().size(), Cards::CardStack::MovementRule::Any) && !stack.is_empty()) {
if (stack->is_allowed_to_push(moving_cards().at(0), moving_cards().size(), Cards::CardStack::MovementRule::Any) && !stack->is_empty()) {
move_focused_cards(stack);
rebound = false;
@ -361,8 +361,8 @@ void Game::mousemove_event(GUI::MouseEvent& event)
for (auto& to_intersect : moving_cards()) {
mark_intersecting_stacks_dirty(to_intersect);
to_intersect.rect().translate_by(dx, dy);
update(to_intersect.rect());
to_intersect->rect().translate_by(dx, dy);
update(to_intersect->rect());
}
m_mouse_down_location = click_location;

View file

@ -104,7 +104,7 @@ private:
Mode m_mode { Mode::SingleSuit };
LastMove m_last_move;
NonnullRefPtrVector<Card> m_new_deck;
Vector<NonnullRefPtr<Card>> m_new_deck;
Gfx::IntPoint m_mouse_down_location;
bool m_mouse_down { false };