Skip to content

Instantly share code, notes, and snippets.

@nboutin
Last active February 18, 2023 17:00
Show Gist options
  • Select an option

  • Save nboutin/fb6f29916eb582e57037842e0ef557ea to your computer and use it in GitHub Desktop.

Select an option

Save nboutin/fb6f29916eb582e57037842e0ef557ea to your computer and use it in GitHub Desktop.

Revisions

  1. nboutin revised this gist Feb 18, 2023. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions raii_mutator_interface.cpp
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,16 @@
    // https://www.youtube.com/watch?v=nLSm3Haxz0I

    class MyWidget
    {
    void tinker_with(int amount);
    public:
    class Tinkerable;

    Tinkerable get_tinkerable(){
    return Tinkerable(*this);
    }
    };

    class MyWidget::Tinkerable
    {
    MyWidget& widget_;
  2. nboutin revised this gist Feb 18, 2023. 1 changed file with 12 additions and 3 deletions.
    15 changes: 12 additions & 3 deletions raii_mutator_interface.cpp
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // https://www.youtube.com/watch?v=nLSm3Haxz0I

    class MyWidget::Tinkerable
    {
    @@ -9,6 +10,14 @@ class MyWidget::Tinkerable
    : widget_(widget), lock_(widget_.mutex_) {}

    public:
    void tinker_with(int amount)

    };
    void tinker_with(int amount){
    widget_.thinker_with(amount);
    }
    };

    void tinker(MyWidget& widget)
    {
    auto tinkerable = widget.get_tinkerable();
    tinkerable.tinker_with(123);
    tinkerable.tinker_with(456);
    }
  3. nboutin created this gist Feb 18, 2023.
    14 changes: 14 additions & 0 deletions raii_mutator_interface.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@

    class MyWidget::Tinkerable
    {
    MyWidget& widget_;
    std::scoped_lock<std::mutex> lock_;
    friend MyWidget;

    explicit Tinkerable(MyWidget& widget)
    : widget_(widget), lock_(widget_.mutex_) {}

    public:
    void tinker_with(int amount)

    };