Skip to content

Commit

Permalink
Clean up some use of callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Dec 28, 2014
1 parent 3027e8f commit 6284425
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
16 changes: 14 additions & 2 deletions code/cpp/echomesh/component/LightingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void LightingWindow::saveSnapshotToFile(const string& name) {
}

void LightingWindow::setLights(const color::FColorList& colors) {
runOnMessageThread(&InstrumentGrid::setLights, instrumentGrid_, colors);
runOnMessageThread([=] () { instrumentGrid_->setLights(colors); });
}

LightingWindow* makeLightingWindow() {
Expand All @@ -44,12 +44,24 @@ LightingWindow* makeLightingWindow() {
return window.release();
}

void LightingWindow::closeButtonPressed() {
// LOG(INFO) << "closing!!";
}

void LightingWindow::moved() {
// LOG(INFO) << "Moved!";
}

void LightingWindow::resized() {
// LOG(INFO) << "Resized!!";
}

static void deleteWindow(LightingWindow* window) {
delete window;
}

void deleteLightingWindow(LightingWindow* window) {
runOnMessageThread(deleteWindow, window);
runOnMessageThread([=]() { delete window; });
}

} // namespace echomesh
6 changes: 3 additions & 3 deletions code/cpp/echomesh/component/LightingWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class LightingWindow : public DocumentWindow {
InstrumentGrid* grid() { return instrumentGrid_; }
void saveSnapshotToFile(const string&);

void closeButtonPressed() {}
virtual void moved() {}
void closeButtonPressed() override;
void moved() override;
void resized() override;
void setLights(const color::FColorList&);

private:
Expand All @@ -31,4 +32,3 @@ void deleteLightingWindow(LightingWindow*);


} // namespace echomesh

17 changes: 1 addition & 16 deletions code/cpp/echomesh/util/GetDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@ namespace audio {

bool equals(const OneMidiConfig&, const OneMidiConfig&);

class CallbackWrapper : public CallbackMessage {
public:
explicit CallbackWrapper(CallbackMessage* cb) : callback_(cb) {}

virtual void messageCallback() {
callback_->messageCallback();
}

private:
CallbackMessage* const callback_;

DISALLOW_COPY_ASSIGN_EMPTY_AND_LEAKS(CallbackWrapper);
};

template <typename DeviceClass>
class ConfigMidi : public CallbackMessage {
public:
Expand All @@ -36,7 +22,7 @@ class ConfigMidi : public CallbackMessage {
configAssigned_ = true;
config_ = config;

runOnMessageThread(&ConfigMidi<DeviceClass>::messageCallback, this);
runOnMessageThread([=] () { messageCallback(); });
}
}

Expand Down Expand Up @@ -85,4 +71,3 @@ class ConfigMidiOutput : public ConfigMidi<MidiOutput> {
} // namespace echomesh

#endif

23 changes: 7 additions & 16 deletions code/cpp/echomesh/util/RunOnMessageThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,17 @@ namespace echomesh {

typedef std::function<void()> VoidFunction;

template <class Callback>
class RunOnMessageThread : public CallbackMessage {
public:
RunOnMessageThread(VoidFunction f) : function_(f) {}
virtual void messageCallback() { function_(); }
VoidFunction const function_;
RunOnMessageThread(Callback f) : callback_(f) {}
virtual void messageCallback() { callback_(); }
Callback callback_;
};

inline void runOnMessageThread(VoidFunction f) {
(new RunOnMessageThread(f))->post();
}

template <typename F, typename X>
void runOnMessageThread(F f, X x) {
runOnMessageThread(std::bind(f, x));
}

template <typename F, typename X, typename Y>
void runOnMessageThread(F f, X x, Y y) {
runOnMessageThread(std::bind(f, x, y));
template <class Callback>
void runOnMessageThread(Callback cb) {
(new RunOnMessageThread<Callback>(cb))->post();
}

} // namespace echomesh

0 comments on commit 6284425

Please sign in to comment.