Skip to content

Commit

Permalink
Use std::bind in tape recorders
Browse files Browse the repository at this point in the history
  • Loading branch information
captainurist committed Oct 13, 2023
1 parent cc11a0b commit 97b1078
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
46 changes: 18 additions & 28 deletions test/Testing/Game/CharacterTapeRecorder.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
#include "CharacterTapeRecorder.h"

#include "Engine/Party.h"
#include <functional>

template<class Member, class... Args>
static auto bind(Member member, Args... args) {
return [member, ...args = std::move(args)] (const Character &character) {
return std::invoke(member, character, args...);
};
}
#include "Engine/Party.h"

template<class T, class Member, class... Args>
static auto bindAs(Member member, Args... args) {
return [base = bind(member, std::move(args)...)] (const Character &character) {
return static_cast<T>(base(character));
};
}
using namespace std::placeholders; // NOLINT

CharacterTapeRecorder::CharacterTapeRecorder(TestController *controller) : _controller(controller) {
assert(controller);
Expand All @@ -25,61 +15,61 @@ std::span<Character> CharacterTapeRecorder::characters() {
}

TestTape<int64_t> CharacterTapeRecorder::experience(int characterIndex) {
return custom(characterIndex, bindAs<int64_t>(&Character::experience));
return custom(characterIndex, std::bind<int64_t>(&Character::experience, _1));
}

TestMultiTape<int64_t> CharacterTapeRecorder::experiences() {
return custom(bindAs<int64_t>(&Character::experience));
return custom(std::bind<int64_t>(&Character::experience, _1));
}

TestTape<CharacterExpressionID> CharacterTapeRecorder::expression(int characterIndex) {
return custom(characterIndex, bind(&Character::expression));
return custom(characterIndex, std::bind(&Character::expression, _1));
}

TestMultiTape<CharacterExpressionID> CharacterTapeRecorder::expressions() {
return custom(bind(&Character::expression));
return custom(std::bind(&Character::expression, _1));
}

TestTape<int> CharacterTapeRecorder::hp(int characterIndex) {
return custom(characterIndex, bind(&Character::health));
return custom(characterIndex, std::bind(&Character::health, _1));
}

TestMultiTape<int> CharacterTapeRecorder::hps() {
return custom(bind(&Character::health));
return custom(std::bind(&Character::health, _1));
}

TestTape<int> CharacterTapeRecorder::mp(int characterIndex) {
return custom(characterIndex, bind(&Character::mana));
return custom(characterIndex, std::bind(&Character::mana, _1));
}

TestMultiTape<int> CharacterTapeRecorder::mps() {
return custom(bind(&Character::mana));
return custom(std::bind(&Character::mana, _1));
}

TestTape<int> CharacterTapeRecorder::ac(int characterIndex) {
return custom(characterIndex, bind(&Character::GetActualAC));
return custom(characterIndex, std::bind(&Character::GetActualAC, _1));
}

TestMultiTape<int> CharacterTapeRecorder::acs() {
return custom(bind(&Character::GetActualAC));
return custom(std::bind(&Character::GetActualAC, _1));
}

TestTape<int> CharacterTapeRecorder::level(int characterIndex) {
return custom(characterIndex, bind(&Character::GetActualLevel));
return custom(characterIndex, std::bind(&Character::GetActualLevel, _1));
}

TestMultiTape<int> CharacterTapeRecorder::levels() {
return custom(bind(&Character::GetActualLevel));
return custom(std::bind(&Character::GetActualLevel, _1));
}

TestMultiTape<int> CharacterTapeRecorder::skillLevels(CharacterSkillType skill) {
return custom(bind(&Character::actualSkillLevel, skill));
return custom(std::bind(&Character::actualSkillLevel, _1, skill));
}

TestMultiTape<Condition> CharacterTapeRecorder::conditions() {
return custom(bind(&Character::GetMajorConditionIdx));
return custom(std::bind(&Character::GetMajorConditionIdx, _1));
}

TestMultiTape<int> CharacterTapeRecorder::resistances(CharacterAttributeType resistance) {
return custom(bind(&Character::GetActualResistance, resistance));
return custom(std::bind(&Character::GetActualResistance, _1, resistance));
}
4 changes: 2 additions & 2 deletions test/Testing/Game/CharacterTapeRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CharacterTapeRecorder {
public:
explicit CharacterTapeRecorder(TestController *controller);

template<class Callback, class T = std::invoke_result_t<Callback, const Character &>>
template<class Callback, class T = std::decay_t<std::invoke_result_t<Callback, const Character &>>>
TestMultiTape<T> custom(Callback callback) {
return _controller->recordTape([callback = std::move(callback)] {
AccessibleVector<T> result;
Expand All @@ -22,7 +22,7 @@ class CharacterTapeRecorder {
});
}

template<class Callback, class T = std::invoke_result_t<Callback, const Character &>>
template<class Callback, class T = std::decay_t<std::invoke_result_t<Callback, const Character &>>>
TestTape<T> custom(int characterIndex, Callback callback) {
return _controller->recordTape([characterIndex, callback = std::move(callback)] {
return callback(characters()[characterIndex]);
Expand Down
2 changes: 1 addition & 1 deletion test/Testing/Game/CommonTapeRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CommonTapeRecorder {
* @param callback Callback that will calculate the values to store on a tape.
* @return Tape object.
*/
template<class Callback, class T = std::invoke_result_t<Callback>>
template<class Callback, class T = std::decay_t<std::invoke_result_t<Callback>>>
TestTape<T> custom(Callback callback) {
return _controller->recordTape(std::move(callback));
}
Expand Down

0 comments on commit 97b1078

Please sign in to comment.