LibreOffice
LibreOffice 5.3 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
instance.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_RTL_INSTANCE_HXX
21 #define INCLUDED_RTL_INSTANCE_HXX
22 
23 #include <sal/config.h>
24 
25 #include <cstddef>
26 
28 #if ! HAVE_THREADSAFE_STATICS
29 #include <osl/getglobalmutex.hxx>
30 #endif
31 
32 namespace {
33 
267 template< typename Inst, typename InstCtor,
268  typename Guard, typename GuardCtor,
269  typename Data = int, typename DataCtor = int >
270 class rtl_Instance
271 {
272 public:
273  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor)
274  {
275 #if defined _MSC_VER
276  static Inst * m_pInstance = 0;
277 #endif // _MSC_VER
278  Inst * p = m_pInstance;
279  if (!p)
280  {
281  Guard aGuard(aGuardCtor());
282  p = m_pInstance;
283  if (!p)
284  {
285  p = aInstCtor();
287  m_pInstance = p;
288  }
289  }
290  else
291  {
293  }
294  return p;
295  }
296 
297  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
298  DataCtor aDataCtor)
299  {
300 #if defined _MSC_VER
301  static Inst * m_pInstance = 0;
302 #endif // _MSC_VER
303  Inst * p = m_pInstance;
304  if (!p)
305  {
306  Data aData(aDataCtor());
307  Guard aGuard(aGuardCtor());
308  p = m_pInstance;
309  if (!p)
310  {
311  p = aInstCtor(aData);
313  m_pInstance = p;
314  }
315  }
316  else
317  {
319  }
320  return p;
321  }
322 
323  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
324  const Data &rData)
325  {
326 #if defined _MSC_VER
327  static Inst * m_pInstance = 0;
328 #endif // _MSC_VER
329  Inst * p = m_pInstance;
330  if (!p)
331  {
332  Guard aGuard(aGuardCtor());
333  p = m_pInstance;
334  if (!p)
335  {
336  p = aInstCtor(rData);
338  m_pInstance = p;
339  }
340  }
341  else
342  {
344  }
345  return p;
346  }
347 
348 private:
349 #if !defined _MSC_VER
350  static Inst * m_pInstance;
351 #endif // _MSC_VER
352 };
353 
354 #if !defined _MSC_VER
355 template< typename Inst, typename InstCtor,
356  typename Guard, typename GuardCtor,
357  typename Data, typename DataCtor >
358 Inst *
359 rtl_Instance< Inst, InstCtor, Guard, GuardCtor, Data, DataCtor >::m_pInstance
360 = NULL;
361 #endif // _MSC_VER
362 
363 }
364 
365 namespace rtl {
366 
386 #if HAVE_THREADSAFE_STATICS
387 template<typename T, typename Unique>
388 class Static {
389 public:
396  static T & get() {
397  static T instance;
398  return instance;
399  }
400 };
401 #else
402 template<typename T, typename Unique>
403 class Static {
404 public:
411  static T & get() {
412  return *rtl_Instance<
413  T, StaticInstance,
415  StaticInstance(), ::osl::GetGlobalMutex() );
416  }
417 private:
418  struct StaticInstance {
419  T * operator () () {
420  static T instance;
421  return &instance;
422  }
423  };
424 };
425 #endif
426 
446 #if HAVE_THREADSAFE_STATICS
447 template<typename T, typename Data, typename Unique>
448 class StaticWithArg {
449 public:
456  static T & get(const Data& rData) {
457  static T instance(rData);
458  return instance;
459  }
460 
467  static T & get(Data& rData) {
468  static T instance(rData);
469  return instance;
470  }
471 };
472 #else
473 template<typename T, typename Data, typename Unique>
475 public:
482  static T & get(const Data& rData) {
483  return *rtl_Instance<
484  T, StaticInstanceWithArg,
486  Data >::create( StaticInstanceWithArg(),
488  rData );
489  }
490 
497  static T & get(Data& rData) {
498  return *rtl_Instance<
499  T, StaticInstanceWithArg,
501  Data >::create( StaticInstanceWithArg(),
503  rData );
504  }
505 private:
506  struct StaticInstanceWithArg {
507  T * operator () (const Data& rData) {
508  static T instance(rData);
509  return &instance;
510  }
511 
512  T * operator () (Data& rData) {
513  static T instance(rData);
514  return &instance;
515  }
516  };
517 };
518 #endif
519 
528 #if HAVE_THREADSAFE_STATICS
529 template<typename T, typename InitAggregate>
530 class StaticAggregate {
531 public:
539  static T * get() {
540  static T *instance = InitAggregate()();
541  return instance;
542  }
543 };
544 #else
545 template<typename T, typename InitAggregate>
547 public:
554  static T * get() {
555  return rtl_Instance<
556  T, InitAggregate,
558  InitAggregate(), ::osl::GetGlobalMutex() );
559  }
560 };
561 #endif
562 
593 #if HAVE_THREADSAFE_STATICS
594 template<typename T, typename InitData,
595  typename Unique = InitData, typename Data = T>
596 class StaticWithInit {
597 public:
604  static T & get() {
605  static T instance = InitData()();
606  return instance;
607  }
608 };
609 #else
610 template<typename T, typename InitData,
611  typename Unique = InitData, typename Data = T>
613 public:
620  static T & get() {
621  return *rtl_Instance<
622  T, StaticInstanceWithInit,
624  Data, InitData >::create( StaticInstanceWithInit(),
626  InitData() );
627  }
628 private:
629  struct StaticInstanceWithInit {
630  T * operator () ( Data d ) {
631  static T instance(d);
632  return &instance;
633  }
634  };
635 };
636 #endif
637 } // namespace rtl
638 
639 #endif // INCLUDED_RTL_INSTANCE_HXX
640 
641 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:31
Helper class for a late-initialized static aggregate, e.g.
Definition: instance.hxx:546
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:403
Helper base class for a late-initialized static variable, implementing the double-checked locking pat...
Definition: instance.hxx:612
Guard< Mutex > MutexGuard
Definition: mutex.hxx:224
#define OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER()
A platform specific macro needed to make double-checked locking work.
Definition: doublecheckedlocking.h:67
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:474