Closed Bug 1570395 Opened 5 years ago Closed 5 years ago

Local static smart pointers

Categories

(Core :: XPCOM, task, P1)

task

Tracking

()

RESOLVED FIXED
mozilla70
Tracking Status
firefox70 --- fixed

People

(Reporter: bugzilla, Assigned: bugzilla)

References

Details

Attachments

(1 file)

As discussed with Nathan last week on IRC, I'd like to write a smart pointer that can be used for C++11 "magic statics", such that we can initialize them like so:

void foo() {
  static StaticLocalAutoPtr<Bar> sInstance = []() -> Bar* {
    // Create an instance of Bar
  }();
  // ...
}

In bug 1569681 I added a new MOZ_STATIC_LOCAL_CLASS annotation for static analysis that forces a class to have a trivial destructor and only be instantiated as a static local, so we can now implement this without fear of being used incorrectly.

This patch adds smart pointers to be used for initializing objects as C++11
"magic statics", ie. as static local variables that are initialized in a
thread-safe manner.

Unlike Static{Auto,Ref}Ptr, they may have non-trivial constructors, though they
must still have trivial destructors to prevent emission of atexit calls.

The new classes use the new MOZ_STATIC_LOCAL_CLASS annotation which ensures
their instantiations are static locals and prevents non-trivial destructors.

Pushed by aklotz@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/0356a4d2ee45
Add StaticLocalAutoPtr and StaticLocalRefPtr to XPCOM; r=froydnj
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla70
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: