Skip to content

Commit

Permalink
Added a bit of documentation and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ubald committed Oct 13, 2017
1 parent 2513bf4 commit 90542c7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
12 changes: 11 additions & 1 deletion psychic-ui/Div.hpp
Expand Up @@ -74,9 +74,19 @@ namespace psychic_ui {

public:
Div();

/**
* Just as a safety for now, delete the copy constructor
* because I don't see a reason why we would need it.
*/
Div(const Div &) = delete;
~Div();

~Div() override;

/**
* Just as a safety for now, delete the copy constructor
* because I don't see a reason why we would need it.
*/
Div &operator=(const Div &) = delete;

/**
Expand Down
11 changes: 4 additions & 7 deletions psychic-ui/Window.cpp
@@ -1,6 +1,8 @@
#include <iostream>
#include "GrBackendSurface.h"
#include "Window.hpp"
#include <SkSurface.h>


namespace psychic_ui {

Expand Down Expand Up @@ -84,9 +86,7 @@ namespace psychic_ui {
throw std::runtime_error("Skia surface requested without a context");
}

if (_sk_surface) {
delete _sk_surface;
}
delete _sk_surface;

GrGLFramebufferInfo framebufferInfo{};
framebufferInfo.fFBOID = 0; // assume default framebuffer
Expand All @@ -98,6 +98,7 @@ namespace psychic_ui {
kSkia8888_GrPixelConfig,
framebufferInfo
);

_sk_surface = SkSurface::MakeFromBackendRenderTarget(
_sk_context,
backendRenderTarget,
Expand Down Expand Up @@ -517,8 +518,4 @@ namespace psychic_ui {

// endregion

// region Event Callbacks

// endregion

}
2 changes: 0 additions & 2 deletions psychic-ui/applications/SDL2Application.cpp
Expand Up @@ -139,8 +139,6 @@ namespace psychic_ui {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
}

//glEnable(GL_MULTISAMPLE);

// WINDOW FLAGS

uint32_t windowFlags = 0;
Expand Down
2 changes: 1 addition & 1 deletion psychic-ui/signals/Observer.hpp
Expand Up @@ -24,7 +24,7 @@ namespace psychic_ui {
public:
Observer() = default;

virtual ~Observer() {
~Observer() {
for (auto &slot: slots) {
slot->disconnect();
}
Expand Down
43 changes: 41 additions & 2 deletions psychic-ui/signals/Slot.hpp
Expand Up @@ -12,26 +12,65 @@ namespace psychic_ui {
*/
class SlotBase {
public:
/**
* Disconnects this slot, removing it from the signal
*/
virtual void disconnect() = 0;
};

/**
* Subscription for a signal, this is used to wrap a callback in a shared/weak pointer.
* Subscription for a signal
* This is used in the Signal class to keep a list of callbacks to execute
* when the signal is emitted and also to hold as a shared_ptr in an Observer
* derived class in order to automatically clean subscriptions in scenarios where
* the Observer has a shorter life span than the Signal source. When a Signal
* source has a shorter life, there is no need to hold a pointer to the slot as
* the Signal will be destroyed first, thus destroying the slots and their callbacks.
*
* When keeping a pointer to a slot for manual disconnection, type the Slot template
* with the same type parameters as the Signal. For example, a `Signal<float, int>` will
* return a `shared_ptr<Slot<float, int>>` when subscribing.
*/
template<class... T>
class Slot : public SlotBase {
public:
/**
* Creates a slot from a signal and callback
* The pointer to the signal is used in order to be able to disconnect
* the slot from its `disconnect` method.
*
* You should not have to create slots manually, this is called
* by the Signal class when subscribing.
*
* @param signal Signal that this slot is attached to
* @param callback Callback to execute when the signal is emited
*/
explicit Slot(Signal<T...> *signal, std::function<void(T...)> callback) :
notify(callback),
_signal(signal) {}

/**
* Disconnect the slot
* This will unsubscribe from the signal, effectively removing this
* slot from the vector in the Signal instance.
*/
void disconnect() override {
_signal->unsubscribe(this);
_signal = nullptr;
}

std::function<void(T...)> notify;
/**
* Function to ba called when the signal is emitted.
* This should not be called manually, it is called by
* the Signal class
*/
std::function<void(T...)> notify{nullptr};

private:
/**
* Signal that owns this slot
* This pointer is used to allow disconnecting from the slot.
*/
Signal<T...> *_signal;
};

Expand Down

0 comments on commit 90542c7

Please sign in to comment.