@@ -295,24 +295,20 @@ namespace {
295295 return moduleHeaderInfoBuffer;
296296 }
297297
298+ // / State helper for object auto registration.
298299 enum class AutoReg : unsigned char {
299300 kNotInitialised = 0 ,
300301 kOn ,
301302 kOff ,
302303 };
303304
304- // //////////////////////////////////////////////////////////////////////////////
305- // / \brief Test if various objects (such as TH1-derived classes) should automatically register
306- // / themselves (ROOT 6 mode) or not (ROOT 7 mode).
307- // / A default can be set in a .rootrc using e.g. "Root.ObjectAutoRegistration: 1" or setting
308- // / the environment variable "ROOT_OBJECT_AUTO_REGISTRATION=0".
309- AutoReg &ObjectAutoRegistrationEnabledImpl ()
305+ // / Set up the default state for object auto registration by inspecting the environment.
306+ // / This state is used to initialise the auto-registration state for each thread that starts.
307+ AutoReg ObjectAutoRegistrationDefault ()
310308 {
311309 static constexpr auto rcName = " Root.ObjectAutoRegistration" ; // Update the docs if this is changed
312310 static constexpr auto envName = " ROOT_OBJECT_AUTO_REGISTRATION" ; // Update the docs if this is changed
313- thread_local static AutoReg tlsState = AutoReg::kNotInitialised ;
314-
315- static const AutoReg defaultState = []() {
311+ static const AutoReg defaultFromEnvironment = []() {
316312 AutoReg autoReg = AutoReg::kOn ; // ROOT 6 default
317313 std::stringstream infoMessage;
318314
@@ -356,10 +352,18 @@ namespace {
356352 return autoReg;
357353 }();
358354
359- if (tlsState == AutoReg::kNotInitialised ) {
360- assert (defaultState != AutoReg::kNotInitialised );
361- tlsState = defaultState;
362- }
355+ return defaultFromEnvironment;
356+ }
357+
358+ // //////////////////////////////////////////////////////////////////////////////
359+ // / \brief Test if various objects (such as TH1-derived classes) should automatically register
360+ // / themselves (ROOT 6 mode) or not (ROOT 7 mode).
361+ // / A default can be set in a .rootrc using e.g. "Root.ObjectAutoRegistration: 1" or setting
362+ // / the environment variable "ROOT_OBJECT_AUTO_REGISTRATION=0".
363+ AutoReg &ObjectAutoRegistrationEnabledImpl ()
364+ {
365+ thread_local static AutoReg tlsState = ObjectAutoRegistrationDefault ();
366+ assert (tlsState != AutoReg::kNotInitialised );
363367
364368 return tlsState;
365369 }
@@ -733,10 +737,20 @@ namespace Internal {
733737 // /
734738 // / ## Setting defaults
735739 // /
736- // / A default can be set in a .rootrc using e.g. `Root.ObjectAutoRegistration: 1` or setting
737- // / the environment variable `ROOT_OBJECT_AUTO_REGISTRATION=0`. Note that this default affects
738- // / all the threads that get started.
739- // / When a default is set using one of these methods, ROOT will notify with an Info message.
740+ // / A default can be set (in order of precedence):
741+ // / 1. Setting the environment variable `ROOT_OBJECT_AUTO_REGISTRATION=[01]`
742+ // / 2. Setting `Root.ObjectAutoRegistration: [01]` in a .rootrc file.
743+ // /
744+ // / To do this programmatically, one can use
745+ // / \code{.cpp}
746+ // / setenv("ROOT_OBJECT_AUTO_REGISTRATION", 1);
747+ // / // or using ROOT's TEnv:
748+ // / gEnv->SetValue("Root.ObjectAutoRegistration", 1);
749+ // / \endcode
750+ // / This has to be done *before* the first object with auto-registration is created. Once this is done,
751+ // / every thread starts with the same default. A running thread's behaviour can only be changed using
752+ // / Enable/DisableObjectAutoRegistration().
753+ // / When the default state is changed using the environment or .rootrc, ROOT issues a reminder.
740754 // /
741755 // / ## Difference to TH1::AddDirectoryStatus()
742756 // /
@@ -765,6 +779,7 @@ namespace Internal {
765779 assert (state != AutoReg::kNotInitialised );
766780 return state == AutoReg::kOn ;
767781 }
782+
768783 } // namespace Experimental
769784} // end of ROOT namespace
770785
0 commit comments