Skip to content

Commit

Permalink
Polyfill std::to_underlying and convert several static_casts
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbaumann committed Feb 27, 2025
1 parent c61e2fb commit 0b1141a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/mips/psyqo/advancedpad.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ SOFTWARE.
#include <EASTL/functional.h>
#include <stdint.h>

#include "psyqo/utility-polyfill.h"

namespace psyqo {

/**
Expand Down Expand Up @@ -129,7 +131,7 @@ class AdvancedPad {
* @param pad The pad to query.
* @return A boolean value indicating whether the pad is connected.
*/
bool isPadConnected(Pad pad) const { return m_padData[static_cast<unsigned>(pad)].connected == 0; }
bool isPadConnected(Pad pad) const { return m_padData[toUnderlying(pad)].connected == 0; }

/**
* @brief Returns the state of a button.
Expand All @@ -142,7 +144,7 @@ class AdvancedPad {
* @return A boolean value indicating whether the button is pressed.
*/
bool isButtonPressed(Pad pad, Button button) const {
return (m_padData[static_cast<unsigned>(pad)].buttons & (1 << button)) == 0;
return (m_padData[toUnderlying(pad)].buttons & (1 << button)) == 0;
}

/**
Expand All @@ -160,7 +162,7 @@ class AdvancedPad {
* @return The state of the Analog Input as an unsigned 8-bit value(0-255).
*/
uint8_t getAdc(Pad pad, unsigned int index) const {
const unsigned padIndex = static_cast<unsigned>(pad);
const unsigned padIndex = toUnderlying(pad);

return index <= 7 ? m_padData[padIndex].adc[index] : 0;
}
Expand All @@ -182,9 +184,7 @@ class AdvancedPad {
* @return The value of the halfword.
*/

uint16_t getHalfword(Pad pad, unsigned int index) const {
return m_padData[static_cast<unsigned>(pad)].packed[index % 4];
}
uint16_t getHalfword(Pad pad, unsigned int index) const { return m_padData[toUnderlying(pad)].packed[index % 4]; }

/**
* @brief Returns the type of the pad.
Expand All @@ -196,7 +196,7 @@ class AdvancedPad {
* @param pad The pad to query.
* @return The type of the pad.
*/
uint8_t getPadType(Pad pad) const { return m_padData[static_cast<unsigned>(pad)].padType; }
uint8_t getPadType(Pad pad) const { return m_padData[toUnderlying(pad)].padType; }

private:
enum Command : uint8_t {
Expand Down Expand Up @@ -251,25 +251,25 @@ class AdvancedPad {

// prefix increment operator
inline psyqo::AdvancedPad::Pad& operator++(psyqo::AdvancedPad::Pad& pad) {
return pad = static_cast<psyqo::AdvancedPad::Pad>((static_cast<unsigned>(pad) + 1) & 7);
return pad = static_cast<psyqo::AdvancedPad::Pad>((toUnderlying(pad) + 1) & 7);
}

// postfix increment operator
inline psyqo::AdvancedPad::Pad operator++(psyqo::AdvancedPad::Pad& pad, int) {
psyqo::AdvancedPad::Pad copy(pad);
pad = static_cast<psyqo::AdvancedPad::Pad>((static_cast<unsigned>(pad) + 1) & 7);
pad = static_cast<psyqo::AdvancedPad::Pad>((toUnderlying(pad) + 1) & 7);
return copy;
}

// prefix decrement operator
inline psyqo::AdvancedPad::Pad& operator--(psyqo::AdvancedPad::Pad& pad) {
return pad = static_cast<psyqo::AdvancedPad::Pad>((static_cast<unsigned>(pad) - 1) & 7);
return pad = static_cast<psyqo::AdvancedPad::Pad>((toUnderlying(pad) - 1) & 7);
}

// postfix decrement operator
inline psyqo::AdvancedPad::Pad operator--(psyqo::AdvancedPad::Pad& pad, int) {
psyqo::AdvancedPad::Pad copy(pad);
pad = static_cast<psyqo::AdvancedPad::Pad>((static_cast<unsigned>(pad) - 1) & 7);
pad = static_cast<psyqo::AdvancedPad::Pad>((toUnderlying(pad) - 1) & 7);
return copy;
}

Expand Down
3 changes: 2 additions & 1 deletion src/mips/psyqo/src/advancedpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SOFTWARE.
#include "psyqo/hardware/cpu.hh"
#include "psyqo/hardware/sio.hh"
#include "psyqo/kernel.hh"
#include "psyqo/utility-polyfill.h"

using namespace psyqo::Hardware;

Expand Down Expand Up @@ -121,7 +122,7 @@ uint8_t psyqo::AdvancedPad::outputMultitap(unsigned ticks) {
}

void psyqo::AdvancedPad::processChanges(Pad pad) {
const unsigned padIndex = static_cast<unsigned>(pad);
const unsigned padIndex = toUnderlying(pad);
bool padConnected = isPadConnected(pad);
bool wasConnected = m_connected[padIndex];
if (wasConnected && !padConnected) {
Expand Down
40 changes: 40 additions & 0 deletions src/mips/psyqo/utility-polyfill.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
MIT License
Copyright (c) 2025 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <concepts>
#include <type_traits>

namespace psyqo {

template <typename T>
concept Enum = std::is_enum_v<T>;

template <Enum E>
constexpr std::underlying_type_t<E> toUnderlying(E v) {
return static_cast<std::underlying_type_t<E>>(v);
}

} // namespace psyqo

0 comments on commit 0b1141a

Please sign in to comment.