Skip to content

ScopedAccess classes are unwieldy #2

Description

@reuk

To use a ScopedAccess object, you have to know its full type, which is pretty awkward to spell out, especially inside template classes:

using ScopedReader = typename farbot::RealtimeMutatable<Data<T>>::template ScopedAccess<false>;

The client has to remember what true and false mean, which isn't obvious. An enum class IsRealtime { no, yes }; instead of a plain bool would help greatly here, as would inner using declarations for ScopedReader and ScopedWriter.

Even better, though, would be to have a [[nodiscard]] auto scopedRead() const noexcept member function directly on the RealtimeMutatable class, so that the user can just do something like this.

// no need to utter the inner typename at all!
const auto reader = realtimeMutatable.scopedRead();
std::cout << *reader << '\n'; // or whatever other slow thing we're doing on the non-realtime thread

In C++17, guaranteed copy elision should mean that you can return one of these non-copyable ScopedAccess objects from a function like this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions