From efc5fbd8e0d272064364d67b0f98a0b3d087630f Mon Sep 17 00:00:00 2001 From: Taylor Brandstetter Date: Fri, 8 Dec 2017 11:42:15 -0800 Subject: [PATCH] Adding style guidance for adding signals to pure interfaces. Based on discussion in the webrtc-core group. Note that NONE of our existing code does this (yet), but I plan to convert it over time when convenient. Bug: None Change-Id: Ie808181915ea24483e0fd8fbb06273351ebe661d Reviewed-on: https://webrtc-review.googlesource.com/8140 Reviewed-by: Karl Wiberg Commit-Queue: Taylor Brandstetter Cr-Commit-Position: refs/heads/master@{#21214} --- style-guide.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/style-guide.md b/style-guide.md index 1c81bb327f..ca9bad2fc6 100644 --- a/style-guide.md +++ b/style-guide.md @@ -36,6 +36,45 @@ instead of | use See [the source](webrtc/api/array_view.h) for more detailed docs. +### sigslot + +sigslot is a lightweight library that adds a signal/slot language +construct to C++, making it easy to implement the observer pattern +with minimal boilerplate code. + +When adding a signal to a pure interface, **prefer to add a pure +virtual method that returns a reference to a signal**: + +``` +sigslot::signal& SignalFoo() = 0; +``` + +As opposed to making it a public member variable, as a lot of legacy +code does: + +``` +sigslot::signal SignalFoo; +``` + +The virtual method approach has the advantage that it keeps the +interface stateless, and gives the subclass more flexibility in how it +implements the signal. It may: + +* Have its own signal as a member variable. +* Use a `sigslot::repeater`, to repeat a signal of another object: + + ``` + sigslot::repeater foo_; + /* ... */ + foo_.repeat(bar_.SignalFoo()); + ``` +* Just return another object's signal directly, if the other object's + lifetime is the same as its own. + + ``` + sigslot::signal& SignalFoo() { return bar_.SignalFoo(); } + ``` + ## **C** There’s a substantial chunk of legacy C code in WebRTC, and a lot of