Skip to content

Commit

Permalink
operator ++, --, ()
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBrumbler committed Sep 3, 2022
1 parent a604074 commit 2315039
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
40 changes: 39 additions & 1 deletion shared/utils/accessor-wrapper-types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ namespace bs_hook {
// We cannot EASILY solve this using wrapper types, because we could be holding a value type as T.
// This makes things tricky and we should come back to this when we are confident we can handle this case correctly.


template<internal::NTTPString name, class T, bool get, bool set>
/// @brief Represents a InstanceProperty on a wrapper type. Forwards to calling the get/set methods where applicable.
struct InstanceProperty;
Expand All @@ -89,6 +88,9 @@ namespace bs_hook {
return this->operator T();
}

template<typename... Targs>
decltype(auto) operator ()(Targs&&... args) { return this->operator T()(std::forward<Targs...>(args...)); }

private:
void* instance;
};
Expand All @@ -102,6 +104,7 @@ namespace bs_hook {
if (!res) throw PropertyException(std::string("Failed to set instance property: ") + name.data.data());
return *this;
}

private:
void* instance;
};
Expand Down Expand Up @@ -140,6 +143,14 @@ auto& operator op##=(const U& rhs) { \
return this->operator T();
}

template<typename... Targs>
decltype(auto) operator ()(Targs&&... args) { return this->operator T()(std::forward<Targs...>(args...)); }

auto& operator ++() { return this->operator=(++this->operator T()); }
auto& operator ++(int) { return this->operator=(this->operator T()++); }
auto& operator --() { return this->operator=(--this->operator T()); }
auto& operator --(int) { return this->operator=(this->operator T()--); }

/* These operators forward to the ones on the underlying type */
BINARY_OPERATOR_OP_EQ_PROP(+);
BINARY_OPERATOR_OP_EQ_PROP(-);
Expand Down Expand Up @@ -181,6 +192,9 @@ auto& operator op##=(const U& rhs) { \
inline auto v() const {
return this->operator T();
}

template<typename... Targs>
decltype(auto) operator ()(Targs&&... args) { return this->operator T()(std::forward<Targs...>(args...)); }
};

template<class T, internal::NTTPString name, auto klass_resolver>
Expand Down Expand Up @@ -224,6 +238,14 @@ auto& operator op##=(const U& rhs) { \
return this->operator T();
}

template<typename... Targs>
decltype(auto) operator ()(Targs&&... args) { return this->operator T()(std::forward<Targs...>(args...)); }

auto& operator ++() { return this->operator=(++this->operator T()); }
auto& operator ++(int) { return this->operator=(this->operator T()++); }
auto& operator --() { return this->operator=(--this->operator T()); }
auto& operator --(int) { return this->operator=(this->operator T()--); }

BINARY_OPERATOR_OP_EQ_PROP(+);
BINARY_OPERATOR_OP_EQ_PROP(-);
BINARY_OPERATOR_OP_EQ_PROP(*);
Expand Down Expand Up @@ -275,6 +297,9 @@ auto& operator op##=(const U& rhs) { \
return this->operator Ref();
}

template<typename... Targs>
decltype(auto) operator ()(Targs&&... args) { return this->operator Ref()(std::forward<Targs...>(args...)); }

private:
void* instance;
};
Expand Down Expand Up @@ -307,6 +332,11 @@ auto& operator op##=(const U& rhs) { \
return *this;
}

auto& operator ++() { return this->operator=(++this->operator Ref()); }
auto& operator ++(int) { return this->operator=(this->operator Ref()++); }
auto& operator --() { return this->operator=(--this->operator Ref()); }
auto& operator --(int) { return this->operator=(this->operator Ref()--); }

BINARY_OPERATOR_OP_EQ_FIELD(+);
BINARY_OPERATOR_OP_EQ_FIELD(-);
BINARY_OPERATOR_OP_EQ_FIELD(*);
Expand Down Expand Up @@ -355,6 +385,9 @@ auto& operator op##=(const U& rhs) { \
inline auto v() const {
return this->operator T();
}

template<typename... Targs>
decltype(auto) operator ()(Targs&&... args) { return this->operator T()(std::forward<Targs...>(args...)); }
};

template<class T, internal::NTTPString name, auto klass_resolver>
Expand All @@ -367,6 +400,11 @@ auto& operator op##=(const U& rhs) { \
return *this;
}

auto& operator ++() { return this->operator=(++this->operator T()); }
auto& operator ++(int) { return this->operator=(this->operator T()++); }
auto& operator --() { return this->operator=(--this->operator T()); }
auto& operator --(int) { return this->operator=(this->operator T()--); }

BINARY_OPERATOR_OP_EQ_STATIC_FIELD(+);
BINARY_OPERATOR_OP_EQ_STATIC_FIELD(-);
BINARY_OPERATOR_OP_EQ_STATIC_FIELD(*);
Expand Down
9 changes: 9 additions & 0 deletions src/tests/accessor-wrapper-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <iostream>


struct Delegate {
void operator()(int a) { a += 5;}
};

struct Color {
float r, g, b, a;
Color& operator +=(const Color& rhs) {
Expand All @@ -25,6 +29,7 @@ struct Underlying {
int b;
int c;
Color d;
Delegate e;
};

struct Test;
Expand Down Expand Up @@ -56,6 +61,7 @@ struct Test {
bs_hook::InstanceProperty<"B", Color, true, true> colorProp{instance};
bs_hook::AssignableInstanceField<int, 0x8> c{instance};
bs_hook::AssignableInstanceField<Color, 0x16> mycol{instance};
bs_hook::AssignableInstanceField<Delegate, 0x20> delegate{instance};
static inline bs_hook::AssignableStaticField<int, "staticA", &::il2cpp_utils::il2cpp_type_check::il2cpp_no_arg_class<Test>::get> staticA;
static inline bs_hook::StaticField<int, "staticA2", &::il2cpp_utils::il2cpp_type_check::il2cpp_no_arg_class<Test>::get> staticA2;
static inline bs_hook::StaticProperty<int, "staticProp", true, true, &::il2cpp_utils::il2cpp_type_check::il2cpp_no_arg_class<Test>::get> staticB;
Expand All @@ -69,6 +75,8 @@ int main() {
t.B = 0x1234;
std::cout << std::endl;
t.c = 123;
t.c++;
t.c--;
std::cout << "A: " << t.A << " B: " << t.B << " c: " << t.c << std::endl;
t.assign(new Underlying());
std::cout << "A: " << t.A << " B: " << t.B << " c: " << t.c << std::endl;
Expand All @@ -81,6 +89,7 @@ int main() {
t.mycol += Color{1, 2, 3, 4};
t.colorProp->g = -5.0f;
t.colorProp += Color{1, 2, 3, 4};
t.delegate(4);
// Test::staticA2 = 123; // INVALID!
// Test::staticProp2 = 123; // INVALID!
// Test::staticB = Test::staticProp3; // INVALID!
Expand Down

0 comments on commit 2315039

Please sign in to comment.