Przeglądaj źródła

LibCards+Games+GamesSettings: Return ErrorOr from CardStack::push()

Very few of these calls can propagate their errors yet, but one step at
a time. :^)
Sam Atkins 2 lat temu
rodzic
commit
8b3a94ffbc

+ 4 - 4
Userland/Applications/GamesSettings/CardSettingsWidget.cpp

@@ -39,10 +39,10 @@ public:
         TRY(preview->add_stack(point, Cards::CardStack::Type::Normal));
 
         for (size_t i = 0; i < Cards::Card::card_count; ++i)
-            preview->stack_at_location(0).push(TRY(Cards::Card::try_create(Cards::Suit::Diamonds, static_cast<Cards::Rank>(i))));
-        preview->stack_at_location(1).push(TRY(Cards::Card::try_create(Cards::Suit::Spades, Cards::Rank::Ace)));
-        preview->stack_at_location(2).push(TRY(Cards::Card::try_create(Cards::Suit::Hearts, Cards::Rank::Queen)));
-        preview->stack_at_location(3).push(TRY(Cards::Card::try_create(Cards::Suit::Clubs, Cards::Rank::Jack)));
+            TRY(preview->stack_at_location(0).push(TRY(Cards::Card::try_create(Cards::Suit::Diamonds, static_cast<Cards::Rank>(i)))));
+        TRY(preview->stack_at_location(1).push(TRY(Cards::Card::try_create(Cards::Suit::Spades, Cards::Rank::Ace))));
+        TRY(preview->stack_at_location(2).push(TRY(Cards::Card::try_create(Cards::Suit::Hearts, Cards::Rank::Queen))));
+        TRY(preview->stack_at_location(3).push(TRY(Cards::Card::try_create(Cards::Suit::Clubs, Cards::Rank::Jack))));
 
         preview->stack_at_location(0).peek().set_upside_down(true);
         preview->stack_at_location(2).set_highlighted(true);

+ 12 - 12
Userland/Games/Solitaire/Game.cpp

@@ -70,9 +70,9 @@ void Game::timer_event(Core::TimerEvent&)
             if (current_pile.count() < m_new_game_animation_pile) {
                 auto card = m_new_deck.take_last();
                 card->set_upside_down(true);
-                current_pile.push(card);
+                current_pile.push(card).release_value_but_fixme_should_propagate_errors();
             } else {
-                current_pile.push(m_new_deck.take_last());
+                current_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
                 ++m_new_game_animation_pile;
             }
 
@@ -81,7 +81,7 @@ void Game::timer_event(Core::TimerEvent&)
             if (m_new_game_animation_pile == piles.size()) {
                 auto& stock_pile = stack_at_location(Stock);
                 while (!m_new_deck.is_empty())
-                    stock_pile.push(m_new_deck.take_last());
+                    stock_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
 
                 update(stock_pile.bounding_box());
 
@@ -404,13 +404,13 @@ void Game::draw_cards()
         NonnullRefPtrVector<Card> moved_cards;
         while (!play.is_empty()) {
             auto card = play.pop();
-            stock.push(card);
+            stock.push(card).release_value_but_fixme_should_propagate_errors();
             moved_cards.prepend(card);
         }
 
         while (!waste.is_empty()) {
             auto card = waste.pop();
-            stock.push(card);
+            stock.push(card).release_value_but_fixme_should_propagate_errors();
             moved_cards.prepend(card);
         }
 
@@ -424,7 +424,7 @@ void Game::draw_cards()
         update(stock.bounding_box());
     } else {
         auto play_bounding_box = play.bounding_box();
-        play.take_all(waste);
+        play.take_all(waste).release_value_but_fixme_should_propagate_errors();
 
         size_t cards_to_draw = 0;
         switch (m_mode) {
@@ -445,7 +445,7 @@ void Game::draw_cards()
         for (size_t i = 0; (i < cards_to_draw) && !stock.is_empty(); ++i) {
             auto card = stock.pop();
             cards_drawn.prepend(card);
-            play.push(move(card));
+            play.push(move(card)).release_value_but_fixme_should_propagate_errors();
         }
 
         remember_move_for_undo(stock, play, cards_drawn);
@@ -466,7 +466,7 @@ void Game::pop_waste_to_play_stack()
     if (play.is_empty() && !waste.is_empty()) {
         auto card = waste.pop();
         moving_cards().append(card);
-        play.push(move(card));
+        play.push(move(card)).release_value_but_fixme_should_propagate_errors();
     }
 }
 
@@ -490,7 +490,7 @@ bool Game::attempt_to_move_card_to_foundations(CardStack& from)
             auto card = from.pop();
 
             mark_intersecting_stacks_dirty(card);
-            foundation.push(card);
+            foundation.push(card).release_value_but_fixme_should_propagate_errors();
 
             NonnullRefPtrVector<Card> moved_card;
             moved_card.append(card);
@@ -612,12 +612,12 @@ void Game::perform_undo()
     if (m_last_move.from->type() == CardStack::Type::Play && m_mode == Mode::SingleCardDraw) {
         auto& waste = stack_at_location(Waste);
         if (!m_last_move.from->is_empty())
-            waste.push(m_last_move.from->pop());
+            waste.push(m_last_move.from->pop()).release_value_but_fixme_should_propagate_errors();
     }
 
     for (auto& to_intersect : m_last_move.cards) {
         mark_intersecting_stacks_dirty(to_intersect);
-        m_last_move.from->push(to_intersect);
+        m_last_move.from->push(to_intersect).release_value_but_fixme_should_propagate_errors();
         (void)m_last_move.to->pop();
     }
 
@@ -632,7 +632,7 @@ void Game::perform_undo()
             }
         }
         for (auto& card : cards_popped) {
-            play.push(card);
+            play.push(card).release_value_but_fixme_should_propagate_errors();
         }
     }
 

+ 6 - 6
Userland/Games/Spider/Game.cpp

@@ -95,7 +95,7 @@ void Game::perform_undo()
     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--)
-        m_last_move.from->push(cards[i]);
+        m_last_move.from->push(cards[i]).release_value_but_fixme_should_propagate_errors();
 
     update_score(-1);
 
@@ -162,7 +162,7 @@ void Game::detect_full_stacks()
                 auto original_current_rect = current_pile.bounding_box();
 
                 for (size_t j = 0; j < Card::card_count; j++) {
-                    completed_stack.push(current_pile.pop());
+                    completed_stack.push(current_pile.pop()).release_value_but_fixme_should_propagate_errors();
                 }
 
                 update(original_current_rect);
@@ -384,9 +384,9 @@ void Game::timer_event(Core::TimerEvent&)
             if (current_pile.count() < (cards_to_draw - 1)) {
                 auto card = m_new_deck.take_last();
                 card->set_upside_down(true);
-                current_pile.push(card);
+                current_pile.push(card).release_value_but_fixme_should_propagate_errors();
             } else {
-                current_pile.push(m_new_deck.take_last());
+                current_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
                 ++m_new_game_animation_pile;
             }
 
@@ -397,7 +397,7 @@ void Game::timer_event(Core::TimerEvent&)
 
                 auto& stock_pile = stack_at_location(Stock);
                 while (!m_new_deck.is_empty())
-                    stock_pile.push(m_new_deck.take_last());
+                    stock_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
 
                 update(stock_pile.bounding_box());
 
@@ -414,7 +414,7 @@ void Game::timer_event(Core::TimerEvent&)
             auto& current_pile = stack_at_location(piles.at(m_draw_animation_pile));
             auto card = stock_pile.pop();
             card->set_upside_down(false);
-            current_pile.push(card);
+            current_pile.push(card).release_value_but_fixme_should_propagate_errors();
             update(current_pile.bounding_box());
             ++m_draw_animation_pile;
 

+ 1 - 1
Userland/Libraries/LibCards/CardGame.cpp

@@ -88,7 +88,7 @@ void CardGame::drop_cards_on_stack(Cards::CardStack& stack, CardStack::MovementR
     VERIFY(stack.is_allowed_to_push(m_moving_cards.at(0), m_moving_cards.size(), movement_rule));
     for (auto& to_intersect : moving_cards()) {
         mark_intersecting_stacks_dirty(to_intersect);
-        stack.push(to_intersect);
+        stack.push(to_intersect).release_value_but_fixme_should_propagate_errors();
         (void)moving_cards_source_stack()->pop();
     }
 

+ 7 - 5
Userland/Libraries/LibCards/CardStack.cpp

@@ -290,7 +290,7 @@ bool CardStack::make_top_card_visible()
     return false;
 }
 
-void CardStack::push(NonnullRefPtr<Card> card)
+ErrorOr<void> CardStack::push(NonnullRefPtr<Card> card)
 {
     auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last();
 
@@ -306,9 +306,10 @@ void CardStack::push(NonnullRefPtr<Card> card)
 
     card->set_position(top_most_position);
 
-    m_stack.append(card);
-    m_stack_positions.append(top_most_position);
+    TRY(m_stack.try_append(card));
+    TRY(m_stack_positions.try_append(top_most_position));
     calculate_bounding_box();
+    return {};
 }
 
 NonnullRefPtr<Card> CardStack::pop()
@@ -323,15 +324,16 @@ NonnullRefPtr<Card> CardStack::pop()
     return card;
 }
 
-void CardStack::take_all(CardStack& stack)
+ErrorOr<void> CardStack::take_all(CardStack& stack)
 {
     while (!m_stack.is_empty()) {
         auto card = m_stack.take_first();
         m_stack_positions.take_first();
-        stack.push(move(card));
+        TRY(stack.push(move(card)));
     }
 
     calculate_bounding_box();
+    return {};
 }
 
 void CardStack::calculate_bounding_box()

+ 2 - 2
Userland/Libraries/LibCards/CardStack.h

@@ -43,9 +43,9 @@ public:
 
     bool make_top_card_visible(); // Returns true if the card was flipped.
 
-    void push(NonnullRefPtr<Card> card);
+    ErrorOr<void> push(NonnullRefPtr<Card>);
     NonnullRefPtr<Card> pop();
-    void take_all(CardStack&);
+    ErrorOr<void> take_all(CardStack&);
     void rebound_cards();
 
     bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const;