Skip to content

Commit 8b3a94f

Browse files
AtkinsSJlinusg
authored andcommitted
LibCards+Games+GamesSettings: Return ErrorOr from CardStack::push()
Very few of these calls can propagate their errors yet, but one step at a time. :^)
1 parent 83687f8 commit 8b3a94f

File tree

6 files changed

+32
-30
lines changed

6 files changed

+32
-30
lines changed

Userland/Applications/GamesSettings/CardSettingsWidget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class Preview final : public Cards::CardGame {
3939
TRY(preview->add_stack(point, Cards::CardStack::Type::Normal));
4040

4141
for (size_t i = 0; i < Cards::Card::card_count; ++i)
42-
preview->stack_at_location(0).push(TRY(Cards::Card::try_create(Cards::Suit::Diamonds, static_cast<Cards::Rank>(i))));
43-
preview->stack_at_location(1).push(TRY(Cards::Card::try_create(Cards::Suit::Spades, Cards::Rank::Ace)));
44-
preview->stack_at_location(2).push(TRY(Cards::Card::try_create(Cards::Suit::Hearts, Cards::Rank::Queen)));
45-
preview->stack_at_location(3).push(TRY(Cards::Card::try_create(Cards::Suit::Clubs, Cards::Rank::Jack)));
42+
TRY(preview->stack_at_location(0).push(TRY(Cards::Card::try_create(Cards::Suit::Diamonds, static_cast<Cards::Rank>(i)))));
43+
TRY(preview->stack_at_location(1).push(TRY(Cards::Card::try_create(Cards::Suit::Spades, Cards::Rank::Ace))));
44+
TRY(preview->stack_at_location(2).push(TRY(Cards::Card::try_create(Cards::Suit::Hearts, Cards::Rank::Queen))));
45+
TRY(preview->stack_at_location(3).push(TRY(Cards::Card::try_create(Cards::Suit::Clubs, Cards::Rank::Jack))));
4646

4747
preview->stack_at_location(0).peek().set_upside_down(true);
4848
preview->stack_at_location(2).set_highlighted(true);

Userland/Games/Solitaire/Game.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ void Game::timer_event(Core::TimerEvent&)
7070
if (current_pile.count() < m_new_game_animation_pile) {
7171
auto card = m_new_deck.take_last();
7272
card->set_upside_down(true);
73-
current_pile.push(card);
73+
current_pile.push(card).release_value_but_fixme_should_propagate_errors();
7474
} else {
75-
current_pile.push(m_new_deck.take_last());
75+
current_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
7676
++m_new_game_animation_pile;
7777
}
7878

@@ -81,7 +81,7 @@ void Game::timer_event(Core::TimerEvent&)
8181
if (m_new_game_animation_pile == piles.size()) {
8282
auto& stock_pile = stack_at_location(Stock);
8383
while (!m_new_deck.is_empty())
84-
stock_pile.push(m_new_deck.take_last());
84+
stock_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
8585

8686
update(stock_pile.bounding_box());
8787

@@ -404,13 +404,13 @@ void Game::draw_cards()
404404
NonnullRefPtrVector<Card> moved_cards;
405405
while (!play.is_empty()) {
406406
auto card = play.pop();
407-
stock.push(card);
407+
stock.push(card).release_value_but_fixme_should_propagate_errors();
408408
moved_cards.prepend(card);
409409
}
410410

411411
while (!waste.is_empty()) {
412412
auto card = waste.pop();
413-
stock.push(card);
413+
stock.push(card).release_value_but_fixme_should_propagate_errors();
414414
moved_cards.prepend(card);
415415
}
416416

@@ -424,7 +424,7 @@ void Game::draw_cards()
424424
update(stock.bounding_box());
425425
} else {
426426
auto play_bounding_box = play.bounding_box();
427-
play.take_all(waste);
427+
play.take_all(waste).release_value_but_fixme_should_propagate_errors();
428428

429429
size_t cards_to_draw = 0;
430430
switch (m_mode) {
@@ -445,7 +445,7 @@ void Game::draw_cards()
445445
for (size_t i = 0; (i < cards_to_draw) && !stock.is_empty(); ++i) {
446446
auto card = stock.pop();
447447
cards_drawn.prepend(card);
448-
play.push(move(card));
448+
play.push(move(card)).release_value_but_fixme_should_propagate_errors();
449449
}
450450

451451
remember_move_for_undo(stock, play, cards_drawn);
@@ -466,7 +466,7 @@ void Game::pop_waste_to_play_stack()
466466
if (play.is_empty() && !waste.is_empty()) {
467467
auto card = waste.pop();
468468
moving_cards().append(card);
469-
play.push(move(card));
469+
play.push(move(card)).release_value_but_fixme_should_propagate_errors();
470470
}
471471
}
472472

@@ -490,7 +490,7 @@ bool Game::attempt_to_move_card_to_foundations(CardStack& from)
490490
auto card = from.pop();
491491

492492
mark_intersecting_stacks_dirty(card);
493-
foundation.push(card);
493+
foundation.push(card).release_value_but_fixme_should_propagate_errors();
494494

495495
NonnullRefPtrVector<Card> moved_card;
496496
moved_card.append(card);
@@ -612,12 +612,12 @@ void Game::perform_undo()
612612
if (m_last_move.from->type() == CardStack::Type::Play && m_mode == Mode::SingleCardDraw) {
613613
auto& waste = stack_at_location(Waste);
614614
if (!m_last_move.from->is_empty())
615-
waste.push(m_last_move.from->pop());
615+
waste.push(m_last_move.from->pop()).release_value_but_fixme_should_propagate_errors();
616616
}
617617

618618
for (auto& to_intersect : m_last_move.cards) {
619619
mark_intersecting_stacks_dirty(to_intersect);
620-
m_last_move.from->push(to_intersect);
620+
m_last_move.from->push(to_intersect).release_value_but_fixme_should_propagate_errors();
621621
(void)m_last_move.to->pop();
622622
}
623623

@@ -632,7 +632,7 @@ void Game::perform_undo()
632632
}
633633
}
634634
for (auto& card : cards_popped) {
635-
play.push(card);
635+
play.push(card).release_value_but_fixme_should_propagate_errors();
636636
}
637637
}
638638

Userland/Games/Spider/Game.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void Game::perform_undo()
9595
for (size_t i = 0; i < m_last_move.card_count; i++)
9696
cards.append(m_last_move.to->pop());
9797
for (ssize_t i = m_last_move.card_count - 1; i >= 0; i--)
98-
m_last_move.from->push(cards[i]);
98+
m_last_move.from->push(cards[i]).release_value_but_fixme_should_propagate_errors();
9999

100100
update_score(-1);
101101

@@ -162,7 +162,7 @@ void Game::detect_full_stacks()
162162
auto original_current_rect = current_pile.bounding_box();
163163

164164
for (size_t j = 0; j < Card::card_count; j++) {
165-
completed_stack.push(current_pile.pop());
165+
completed_stack.push(current_pile.pop()).release_value_but_fixme_should_propagate_errors();
166166
}
167167

168168
update(original_current_rect);
@@ -384,9 +384,9 @@ void Game::timer_event(Core::TimerEvent&)
384384
if (current_pile.count() < (cards_to_draw - 1)) {
385385
auto card = m_new_deck.take_last();
386386
card->set_upside_down(true);
387-
current_pile.push(card);
387+
current_pile.push(card).release_value_but_fixme_should_propagate_errors();
388388
} else {
389-
current_pile.push(m_new_deck.take_last());
389+
current_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
390390
++m_new_game_animation_pile;
391391
}
392392

@@ -397,7 +397,7 @@ void Game::timer_event(Core::TimerEvent&)
397397

398398
auto& stock_pile = stack_at_location(Stock);
399399
while (!m_new_deck.is_empty())
400-
stock_pile.push(m_new_deck.take_last());
400+
stock_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
401401

402402
update(stock_pile.bounding_box());
403403

@@ -414,7 +414,7 @@ void Game::timer_event(Core::TimerEvent&)
414414
auto& current_pile = stack_at_location(piles.at(m_draw_animation_pile));
415415
auto card = stock_pile.pop();
416416
card->set_upside_down(false);
417-
current_pile.push(card);
417+
current_pile.push(card).release_value_but_fixme_should_propagate_errors();
418418
update(current_pile.bounding_box());
419419
++m_draw_animation_pile;
420420

Userland/Libraries/LibCards/CardGame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void CardGame::drop_cards_on_stack(Cards::CardStack& stack, CardStack::MovementR
8888
VERIFY(stack.is_allowed_to_push(m_moving_cards.at(0), m_moving_cards.size(), movement_rule));
8989
for (auto& to_intersect : moving_cards()) {
9090
mark_intersecting_stacks_dirty(to_intersect);
91-
stack.push(to_intersect);
91+
stack.push(to_intersect).release_value_but_fixme_should_propagate_errors();
9292
(void)moving_cards_source_stack()->pop();
9393
}
9494

Userland/Libraries/LibCards/CardStack.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ bool CardStack::make_top_card_visible()
290290
return false;
291291
}
292292

293-
void CardStack::push(NonnullRefPtr<Card> card)
293+
ErrorOr<void> CardStack::push(NonnullRefPtr<Card> card)
294294
{
295295
auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last();
296296

@@ -306,9 +306,10 @@ void CardStack::push(NonnullRefPtr<Card> card)
306306

307307
card->set_position(top_most_position);
308308

309-
m_stack.append(card);
310-
m_stack_positions.append(top_most_position);
309+
TRY(m_stack.try_append(card));
310+
TRY(m_stack_positions.try_append(top_most_position));
311311
calculate_bounding_box();
312+
return {};
312313
}
313314

314315
NonnullRefPtr<Card> CardStack::pop()
@@ -323,15 +324,16 @@ NonnullRefPtr<Card> CardStack::pop()
323324
return card;
324325
}
325326

326-
void CardStack::take_all(CardStack& stack)
327+
ErrorOr<void> CardStack::take_all(CardStack& stack)
327328
{
328329
while (!m_stack.is_empty()) {
329330
auto card = m_stack.take_first();
330331
m_stack_positions.take_first();
331-
stack.push(move(card));
332+
TRY(stack.push(move(card)));
332333
}
333334

334335
calculate_bounding_box();
336+
return {};
335337
}
336338

337339
void CardStack::calculate_bounding_box()

Userland/Libraries/LibCards/CardStack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class CardStack final : public RefCounted<CardStack> {
4343

4444
bool make_top_card_visible(); // Returns true if the card was flipped.
4545

46-
void push(NonnullRefPtr<Card> card);
46+
ErrorOr<void> push(NonnullRefPtr<Card>);
4747
NonnullRefPtr<Card> pop();
48-
void take_all(CardStack&);
48+
ErrorOr<void> take_all(CardStack&);
4949
void rebound_cards();
5050

5151
bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const;

0 commit comments

Comments
 (0)