LibreOffice: rtl/instance.hxx Source File

LibreOffice LibreOffice 25.8 SDK C/C++ API Reference

Loading...
Searching...
No Matches
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/*
21 * This file is part of LibreOffice published API.
22 */
23
24#ifndef INCLUDED_RTL_INSTANCE_HXX
25#define INCLUDED_RTL_INSTANCE_HXX
26
27#include "sal/config.h"
28
29#include <cstddef>
30
31#include "osl/doublecheckedlocking.h"
32#include "osl/getglobalmutex.hxx"
33
34namespace {
35
269template< typename Inst, typename InstCtor,
270 typename Guard, typename GuardCtor,
271 typename Data = int, typename DataCtor = int >
272class rtl_Instance
273{
274public:
275 static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor)
276 {
277#if defined _MSC_VER
278 static Inst * m_pInstance = NULL;
279#endif // _MSC_VER
280 Inst * p = m_pInstance;
281 if (!p)
282 {
283 Guard aGuard(aGuardCtor());
284 p = m_pInstance;
285 if (!p)
286 {
287 p = aInstCtor();
288 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
289 m_pInstance = p;
290 }
291 }
292 else
293 {
294 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
295 }
296 return p;
297 }
298
299 static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
300 DataCtor aDataCtor)
301 {
302#if defined _MSC_VER
303 static Inst * m_pInstance = NULL;
304#endif // _MSC_VER
305 Inst * p = m_pInstance;
306 if (!p)
307 {
308 Data aData(aDataCtor());
309 Guard aGuard(aGuardCtor());
310 p = m_pInstance;
311 if (!p)
312 {
313 p = aInstCtor(aData);
314 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
315 m_pInstance = p;
316 }
317 }
318 else
319 {
320 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
321 }
322 return p;
323 }
324
325 static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
326 const Data &rData)
327 {
328#if defined _MSC_VER
329 static Inst * m_pInstance = 0;
330#endif // _MSC_VER
331 Inst * p = m_pInstance;
332 if (!p)
333 {
334 Guard aGuard(aGuardCtor());
335 p = m_pInstance;
336 if (!p)
337 {
338 p = aInstCtor(rData);
339 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
340 m_pInstance = p;
341 }
342 }
343 else
344 {
345 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
346 }
347 return p;
348 }
349
350private:
351#if !defined _MSC_VER
352 static Inst * m_pInstance;
353#endif // _MSC_VER
354};
355
356#if !defined _MSC_VER
357template< typename Inst, typename InstCtor,
358 typename Guard, typename GuardCtor,
359 typename Data, typename DataCtor >
360Inst *
361rtl_Instance< Inst, InstCtor, Guard, GuardCtor, Data, DataCtor >::m_pInstance
362= NULL;
363#endif // _MSC_VER
364
365}
366
367namespace rtl {
368
388#if defined LIBO_INTERNAL_ONLY
389template<typename T, typename Unique>
390class Static {
391public:
398 static T & get() {
399 static T instance;
400 return instance;
401 }
402};
403#else
404template<typename T, typename Unique>
405 class Static {
406public:
413 static T & get() {
414 return *rtl_Instance<
415 T, StaticInstance,
416 ::osl::MutexGuard, ::osl::GetGlobalMutex >::create(
417 StaticInstance(), ::osl::GetGlobalMutex() );
418 }
419private:
420 struct StaticInstance {
421 T * operator () () {
422 static T instance;
423 return &instance;
424 }
425 };
426};
427#endif
428
448#if defined LIBO_INTERNAL_ONLY
449template<typename T, typename Data, typename Unique>
450class StaticWithArg {
451public:
458 static T & get(const Data& rData) {
459 static T instance(rData);
460 return instance;
461 }
462
469 static T & get(Data& rData) {
470 static T instance(rData);
471 return instance;
472 }
473};
474#else
475template<typename T, typename Data, typename Unique>
476 class StaticWithArg {
477public:
484 static T & get(const Data& rData) {
485 return *rtl_Instance<
486 T, StaticInstanceWithArg,
487 ::osl::MutexGuard, ::osl::GetGlobalMutex,
488 Data >::create( StaticInstanceWithArg(),
489 ::osl::GetGlobalMutex(),
490 rData );
491 }
492
499 static T & get(Data& rData) {
500 return *rtl_Instance<
501 T, StaticInstanceWithArg,
502 ::osl::MutexGuard, ::osl::GetGlobalMutex,
503 Data >::create( StaticInstanceWithArg(),
504 ::osl::GetGlobalMutex(),
505 rData );
506 }
507private:
508 struct StaticInstanceWithArg {
509 T * operator () (const Data& rData) {
510 static T instance(rData);
511 return &instance;
512 }
513
514 T * operator () (Data& rData) {
515 static T instance(rData);
516 return &instance;
517 }
518 };
519};
520#endif
521
530#if defined LIBO_INTERNAL_ONLY
531template<typename T, typename InitAggregate>
532class StaticAggregate {
533public:
541 static T * get() {
542 static T *instance = InitAggregate()();
543 return instance;
544 }
545};
546#else
547template<typename T, typename InitAggregate>
548 class StaticAggregate {
549public:
556 static T * get() {
557 return rtl_Instance<
558 T, InitAggregate,
559 ::osl::MutexGuard, ::osl::GetGlobalMutex >::create(
560 InitAggregate(), ::osl::GetGlobalMutex() );
561 }
562};
563#endif
595#if defined LIBO_INTERNAL_ONLY
596template<typename T, typename InitData,
597 typename Unique = InitData, typename Data = T>
598class StaticWithInit {
599public:
606 static T & get() {
607 static T instance = InitData()();
608 return instance;
609 }
610};
611#else
612template<typename T, typename InitData,
613 typename Unique = InitData, typename Data = T>
614 class StaticWithInit {
615public:
622 static T & get() {
623 return *rtl_Instance<
624 T, StaticInstanceWithInit,
625 ::osl::MutexGuard, ::osl::GetGlobalMutex,
626 Data, InitData >::create( StaticInstanceWithInit(),
627 ::osl::GetGlobalMutex(),
628 InitData() );
629 }
630private:
631 struct StaticInstanceWithInit {
632 T * operator () ( Data d ) {
633 static T instance(d);
634 return &instance;
635 }
636 };
637};
638#endif
639} // namespace rtl
640
641#endif // INCLUDED_RTL_INSTANCE_HXX
642
643/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
config.h
getglobalmutex.hxx
doublecheckedlocking.h
OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER
#define OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER()
A platform specific macro needed to make double-checked locking work.
Definition doublecheckedlocking.h:73
osl::MutexGuard
Guard< Mutex > MutexGuard
Definition mutex.hxx:273
rtl
Definition bootstrap.hxx:34
rtl::Static
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition instance.hxx:405
rtl::Static::get
static T & get()
Gets the static.
Definition instance.hxx:413
rtl::StaticWithArg
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition instance.hxx:476
rtl::StaticWithArg::get
static T & get(Data &rData)
Gets the static.
Definition instance.hxx:499
rtl::StaticWithArg::get
static T & get(const Data &rData)
Gets the static.
Definition instance.hxx:484
rtl::StaticAggregate
Helper class for a late-initialized static aggregate, e.g.
Definition instance.hxx:548
rtl::StaticAggregate::get
static T * get()
Gets the static aggregate, late-initializing.
Definition instance.hxx:556
rtl::StaticWithInit
Helper base class for a late-initialized static variable, implementing the double-checked locking pat...
Definition instance.hxx:614
rtl::StaticWithInit::get
static T & get()
Gets the static.
Definition instance.hxx:622
osl::GetGlobalMutex
A helper functor for the rtl_Instance template.
Definition getglobalmutex.hxx:36