LibreOffice
LibreOffice 4.3 SDK C/C++ API Reference
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 
26 #include <osl/getglobalmutex.hxx>
27 
28 namespace {
29 
263 template< typename Inst, typename InstCtor,
264  typename Guard, typename GuardCtor,
265  typename Data = int, typename DataCtor = int >
266 class rtl_Instance
267 {
268 public:
269  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor)
270  {
271 #if defined _MSC_VER
272  static Inst * m_pInstance = 0;
273 #endif // _MSC_VER
274  Inst * p = m_pInstance;
275  if (!p)
276  {
277  Guard aGuard(aGuardCtor());
278  p = m_pInstance;
279  if (!p)
280  {
281  p = aInstCtor();
283  m_pInstance = p;
284  }
285  }
286  else
287  {
289  }
290  return p;
291  }
292 
293  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
294  DataCtor aDataCtor)
295  {
296 #if defined _MSC_VER
297  static Inst * m_pInstance = 0;
298 #endif // _MSC_VER
299  Inst * p = m_pInstance;
300  if (!p)
301  {
302  Data aData(aDataCtor());
303  Guard aGuard(aGuardCtor());
304  p = m_pInstance;
305  if (!p)
306  {
307  p = aInstCtor(aData);
309  m_pInstance = p;
310  }
311  }
312  else
313  {
315  }
316  return p;
317  }
318 
319  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
320  const Data &rData)
321  {
322 #if defined _MSC_VER
323  static Inst * m_pInstance = 0;
324 #endif // _MSC_VER
325  Inst * p = m_pInstance;
326  if (!p)
327  {
328  Guard aGuard(aGuardCtor());
329  p = m_pInstance;
330  if (!p)
331  {
332  p = aInstCtor(rData);
334  m_pInstance = p;
335  }
336  }
337  else
338  {
340  }
341  return p;
342  }
343 
344 private:
345 #if !defined _MSC_VER
346  static Inst * m_pInstance;
347 #endif // _MSC_VER
348 };
349 
350 #if !defined _MSC_VER
351 template< typename Inst, typename InstCtor,
352  typename Guard, typename GuardCtor,
353  typename Data, typename DataCtor >
354 Inst *
355 rtl_Instance< Inst, InstCtor, Guard, GuardCtor, Data, DataCtor >::m_pInstance
356 = 0;
357 #endif // _MSC_VER
358 
359 }
360 
361 namespace rtl {
362 
382 #if HAVE_THREADSAFE_STATICS
383 template<typename T, typename Unique>
384 class Static {
385 public:
392  static T & get() {
393  static T instance;
394  return instance;
395  }
396 };
397 #else
398 template<typename T, typename Unique>
399 class Static {
400 public:
407  static T & get() {
408  return *rtl_Instance<
409  T, StaticInstance,
411  StaticInstance(), ::osl::GetGlobalMutex() );
412  }
413 private:
414  struct StaticInstance {
415  T * operator () () {
416  static T instance;
417  return &instance;
418  }
419  };
420 };
421 #endif
422 
442 #if HAVE_THREADSAFE_STATICS
443 template<typename T, typename Data, typename Unique>
444 class StaticWithArg {
445 public:
452  static T & get(const Data& rData) {
453  static T instance(rData);
454  return instance;
455  }
456 
463  static T & get(Data& rData) {
464  static T instance(rData);
465  return instance;
466  }
467 };
468 #else
469 template<typename T, typename Data, typename Unique>
471 public:
478  static T & get(const Data& rData) {
479  return *rtl_Instance<
480  T, StaticInstanceWithArg,
482  Data >::create( StaticInstanceWithArg(),
484  rData );
485  }
486 
493  static T & get(Data& rData) {
494  return *rtl_Instance<
495  T, StaticInstanceWithArg,
497  Data >::create( StaticInstanceWithArg(),
499  rData );
500  }
501 private:
502  struct StaticInstanceWithArg {
503  T * operator () (const Data& rData) {
504  static T instance(rData);
505  return &instance;
506  }
507 
508  T * operator () (Data& rData) {
509  static T instance(rData);
510  return &instance;
511  }
512  };
513 };
514 #endif
515 
524 #if HAVE_THREADSAFE_STATICS
525 template<typename T, typename InitAggregate>
526 class StaticAggregate {
527 public:
535  static T * get() {
536  static T *instance = InitAggregate()();
537  return instance;
538  }
539 };
540 #else
541 template<typename T, typename InitAggregate>
543 public:
550  static T * get() {
551  return rtl_Instance<
552  T, InitAggregate,
554  InitAggregate(), ::osl::GetGlobalMutex() );
555  }
556 };
557 #endif
558 
589 #if HAVE_THREADSAFE_STATICS
590 template<typename T, typename InitData,
591  typename Unique = InitData, typename Data = T>
592 class StaticWithInit {
593 public:
600  static T & get() {
601  static T instance = InitData()();
602  return instance;
603  }
604 };
605 #else
606 template<typename T, typename InitData,
607  typename Unique = InitData, typename Data = T>
609 public:
616  static T & get() {
617  return *rtl_Instance<
618  T, StaticInstanceWithInit,
620  Data, InitData >::create( StaticInstanceWithInit(),
622  InitData() );
623  }
624 private:
625  struct StaticInstanceWithInit {
626  T * operator () ( Data d ) {
627  static T instance(d);
628  return &instance;
629  }
630  };
631 };
632 #endif
633 } // namespace rtl
634 
635 #endif // INCLUDED_RTL_INSTANCE_HXX
636 
637 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Helper base class for a late-initialized static variable, implementing the double-checked locking pat...
Definition: instance.hxx:608
Definition: bootstrap.hxx:24
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:399
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:470
Helper class for a late-initialized static aggregate, e.g.
Definition: instance.hxx:542
Guard< Mutex > MutexGuard
Definition: mutex.hxx:238
#define OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER()
A platform specific macro needed to make double-checked locking work.
Definition: doublecheckedlocking.h:67
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:31