Skip to content

Instantly share code, notes, and snippets.

View jeffersonfr's full-sized avatar
💭
... reading

Jeff Ferr jeffersonfr

💭
... reading
  • Joao Pessoa - PB
View GitHub Profile
@jeffersonfr
jeffersonfr / jstate.cpp
Last active February 2, 2024 13:45
Implementing MutableState from Kotlin/MVVM
#include <functional>
template <typename T> struct MutableState;
template <typename T> struct State {
State(MutableState<T> &state) : mState{state} {}
virtual void observe(std::function<void(T const &)> callback) {
mState.mCallback = callback;
}
@jeffersonfr
jeffersonfr / jinject.cpp
Last active February 16, 2023 12:22
Dependency injection
#include <iostream>
#include <type_traits>
#include <tuple>
#include <memory>
template <typename T, typename ...Args>
T inject(Args ...args) {
if constexpr (!std::is_pointer_v<T>) {
return T{std::forward<Args>(args)...};
} else {
@jeffersonfr
jeffersonfr / jdefer.cpp
Last active February 16, 2023 01:36
GoLang's defer in C++
#include <iostream>
#include <type_traits>
#include <functional>
struct DeferObject {
DeferObject() = default;
~DeferObject() {
mCallback();
}