Created
August 17, 2020 22:57
-
-
Save misyltoad/499e64283b7e78d963d683b3dfaf70eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```cpp | |
// Sorry, I don't know which. | |
#define WIN32_MEAN_AND_LEAN | |
#define WIN32_LEAN_AND_MEAN | |
enum class FizzBuzzState : uint32_t | |
{ | |
Fizz = 0b01, | |
Buzz = 0b10, | |
FizzBuzz = Fizz | Buzz | |
}; | |
constexpr ostream& operator<<(ostream& os, const FizzBuzzState& state) noexcept | |
{ | |
switch (state) | |
{ | |
case Fizz: os << "Fizz"; break; | |
case Buzz: os << "Buzz"; break; | |
case FizzBuzz: os << "FizzBuzz"; break; | |
} | |
return os; | |
} | |
class FizzBuzzerPrinter | |
{ | |
public: | |
FizzBuzzerPrinter(FizzBuzzState state) noexcept | |
: m_fizzBuzzState(state) | |
{ | |
std::cout << state << std::endl; | |
} | |
[[no_discard]] FizzBuzzState GetFizzBuzzState() const noexcept | |
{ | |
return m_fizzBuzzState; | |
} | |
private: | |
FizzBuzzState m_fizzBuzzState; | |
}; | |
template <typename FizzBuzzer, typename T> | |
class FizzBuzz | |
{ | |
public: | |
template <typename... Args> | |
FizzBuzz(const T& value, Args... args) noexcept | |
: m_fizzBuzzer(CalculateFizzBuzzState(value), args...) {} | |
[[no_discard]] static constexpr FizzBuzzState CalculateFizzBuzzState(const T& value, const FizzBuzzState& state) noexcept | |
{ | |
const bool fizz = value % 3; | |
const bool buzz = value % 5; | |
return static_cast<FizzBuzzState>(static_cast<uint32_t>(fizz) | static_cast<uint32_t>(buzz) << 1u)); | |
} | |
FizzBuzzer consexpr operator FizzBuzzer() noexcept const { return m_fizzBuzzer; } | |
private: | |
FizzBuzzer m_fizzBuzzer; | |
}; | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great code