Skip to content

Commit

Permalink
Added default constructor to Result
Browse files Browse the repository at this point in the history
Cleaned up code, changed private member variable naming convention from var_ to _var. Added better examples and updated README
  • Loading branch information
thebashpotato committed Aug 19, 2023
1 parent a311bff commit 3550942
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 254 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
- [Etl](#etl)
- [Integration](#integration)
- [Usage](#usage)
- [API](#api)
- [Maintainers](#maintainers)
- [Contributing](#contributing)
- [License](#license)
Expand All @@ -39,8 +38,8 @@ its a single header file, at roughly 500 lines of code. A nice and easy read wit
the Result<OkType, ErrType> attempts to be as close to the Rust langauges implementation as possible,
and may be just what your looking for to ditch those try/catch error handling blocks.

- There is also a [template specialiazation for std::unique_ptr](https://github.com/thebashpotato/extra-template-library/blob/f1dcd42141c26f4826283d84ec39f87d364be621/etl/include/etl.hpp#L457) that can be used as a starting point for you own specialization if
the generic Result<T, E> does not work for your type. This will most likely only be the case for move only types.
- One current catch with the Result<T, E> type is if you have a **move only type** you will need to hi-jack the etl namespace
and create a template specialization for it, but don't worry it's easy. I have provided an example [here](https://github.com/thebashpotato/extra-template-library/blob/main/etl/examples/moveonly) which you can copy and paste, (Just replace the name of the the class with your own).

2. [etl::EnumerationIterator<IteratorName, IteratorBegin, IteratorEnd>](https://github.com/thebashpotato/extra-template-library/blob/main/etl/tests/enum_iterable_test.cpp)

Expand Down Expand Up @@ -157,16 +156,13 @@ pkg-config etl --clfags
## Usage

[Please see the unit tests](extra-template-library/etl/tests) for bite size examples for each class.
[Please see](extra-template-library/etl/examples/blackjack.cpp) for an example blackjack program utilizing etl to solve real world problems.
[Please see](extra-template-library/etl/examples/blackjack) for an example blackjack program utilizing etl to solve real world problems.
[Please see](extra-template-library/etl/examples/moveonly) for an example for a Result<T, E> move only class template specialization.

```cpp
#include <etl.hpp>
```

## API

TODO

## Maintainers

[@thebashpotato](https://github.com/thebashpotato)
Expand Down
4 changes: 2 additions & 2 deletions dev-scripts/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
# Build in release mode, modify the flags accordingly.
"CMAKE_RELEASE": {
"name": "cmake",
"flags": "-D ETL_DEV_MODE=OFF -D CMAKE_CXX_COMPILER=clang++",
"flags": "-D ETL_DEV_MODE=OFF -D CMAKE_CXX_COMPILER=clang++-14",
},
# Build in develop mode, modify the flags accordingly.
"CMAKE_DEVELOP": {
"name": "cmake",
"flags": "-D ETL_DEV_MODE=ON -D CMAKE_CXX_COMPILER=clang++",
"flags": "-D ETL_DEV_MODE=ON -D CMAKE_CXX_COMPILER=clang++-14",
},
# customize formatting and clang tidy through the flags
"CLANG_FORMATTER": {"name": "clang-format", "flags": "-i"},
Expand Down
6 changes: 5 additions & 1 deletion etl/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
#
set(ScratchFile "${PROJECT_NAME}-scratch")
set(Blackjack "${PROJECT_NAME}-blackjack")
set(MoveOnly "${PROJECT_NAME}-moveonly")

add_executable(${ScratchFile} "${APP_EXAMPLES_SOURCE_DIR}/scratch.cpp" ${UTILS_SOURCE_FILES})
add_executable(${Blackjack} "${APP_EXAMPLES_SOURCE_DIR}/blackjack.cpp" ${UTILS_SOURCE_FILES})
add_executable(${Blackjack} "${APP_EXAMPLES_SOURCE_DIR}/blackjack/main.cpp" ${UTILS_SOURCE_FILES})
add_executable(${MoveOnly} "${APP_EXAMPLES_SOURCE_DIR}/moveonly/main.cpp" ${UTILS_SOURCE_FILES})

target_include_directories(${ScratchFile} PUBLIC ${APP_INCLUDE_DIR})
target_include_directories(${Blackjack} PUBLIC ${APP_INCLUDE_DIR})
target_include_directories(${MoveOnly} PUBLIC ${APP_INCLUDE_DIR})

target_link_libraries(${ScratchFile} PRIVATE etl_project_options etl_project_warnings)
target_link_libraries(${Blackjack} PRIVATE etl_project_options etl_project_warnings)
target_link_libraries(${MoveOnly} PRIVATE etl_project_options etl_project_warnings)
181 changes: 0 additions & 181 deletions etl/examples/blackjack.cpp

This file was deleted.

109 changes: 109 additions & 0 deletions etl/examples/blackjack/blackjack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/// The black jack code is from the youtube channel Dave's Garage.
/// Give him a like and subscribe, he makes great content.
///
/// @link Link to the video: https://youtu.be/b8V-WIjlScA
/// @link Original code: https://github.com/davepl/blackjack
///
/// In the video Dave does some manual iteration over a C style enum.
/// At the time stamp 8:30 he says "One unfortunate part about C++
/// is that it doesn't provide a way to get the lowest and highest values in an enumeration".
/// Which he is correct. Since this was a problem I had already solved for myself I thought
/// I would share my solution with him and the world (if anyone ever sees this).

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <etl.hpp>
#include <iostream>
#include <memory>
#include <optional>
#include <random>
#include <type_traits>
#include <vector>

using namespace etl;

namespace blackjack {
enum class Rank : std::uint16_t {
ACE = 1,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
};

enum class Suit : std::uint16_t {
HEARTS,
DIAMONDS,
CLUBS,
SPADES,
};

class Card {
private:
Rank _rank;
Suit _suit;

public:
Card(Rank rank, Suit suit) noexcept;

[[nodiscard]] auto getRank() const noexcept -> Rank;

[[nodiscard]] auto getSuite() const noexcept -> Suit;
};


class Deck {
public:
using UniqueCard = std::unique_ptr<Card>;

private:
/// @brief Here is where we use the etl generic enum iterator class
using RankIterator =
EnumerationIterator<Rank, Rank::ACE, Rank::KING>;
using SuitIterator =
EnumerationIterator<Suit, Suit::HEARTS, Suit::SPADES>;

private:
std::vector<UniqueCard> _cards;

public:
/// @brief Builds a 52 card deck of 13 ranks with 4 suits,
///
/// @details Uses the custom EnumerationIterator template class to showcase
/// the C++ ability to iterate over enums.
Deck() noexcept;

/// @brief How many cards are in the deck
[[nodiscard]] auto size() -> std::size_t;

/// @brief Uses a random number and The classic Mersenne Twister,
/// random number generator to shuffle the deck.
auto shuffleDeck() -> void;

/// @brief Draws a single card from the deck if it isn't emptpy.
///
/// @returns Result<UniqueCard, Error>
[[nodiscard]] auto drawCard() -> Result<UniqueCard, Error>;
};


class Player {
private:
std::vector<Deck::UniqueCard> _hand;

public:
auto addCard(Deck::UniqueCard &&card) noexcept;

[[nodiscard]] auto getHandValue() -> uint16_t;
};

}// namespace blackjack
Loading

0 comments on commit 3550942

Please sign in to comment.