OEOnce

template<class T>
struct OEOnce

This class is used to do thread-safe one time initialization of an object as a function local static. It is intended to imitate the behavior of the C++11 “magic statics” feature. The default constructor of the class T is guaranteed to be called once, and only once, and all other threads will block until initialization is complete.

The following code snippet demonstrates how to use OEOnce to cache an initialized OESubSearch object to be used by subsequent function invocations:

class PhenylSubSearch
{
  OESubSearch _subsrch;
public:
  PhenylSubSearch() : _subsrch("c1ccccc1") { }
  const OESubSearch &GetSubSearch() const { return _subsrch; }
};

bool HasPhenyl(const OEMolBase &mol)
{
  static OEOnce<PhenylSubSearch> phenylSubSrch = OE_ONCE_INIT;
  return phenylSubSrch->GetSubSearch().SingleMatch(mol);
}

Warning

The thread safety of the object T is the responsibility of the user. OEOnce will only ensure that there is only one of them initialized for the whole lifetime of the process and that its destructor is called during process exit.

operator*

T &operator*()

Dereference that OEOnce object, returning a reference to the singleton T object. The reference will only be returned once at least one thread has fully initialized the object.

operator->

T *operator->()

Allows the OEOnce object to behave like a pointer to T. The pointer will only be returned once at least one thread has fully initialized the object.

operator T *

operator T *()

Allows the OEOnce object to be cast to a pointer of T. The pointer will only be returned once at least one thread has fully initialized the object.

GetValue

T *GetValue()

Returns the pointer to a singleton object initialized in a thread safe way. The pointer will only be returned once at least one thread has fully initialized the object.