LibreOffice
LibreOffice 25.2 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
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();
289 m_pInstance = p;
290 }
291 }
292 else
293 {
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);
315 m_pInstance = p;
316 }
317 }
318 else
319 {
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);
340 m_pInstance = p;
341 }
342 }
343 else
344 {
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>
405class Static {
406public:
413 static T & get() {
414 return *rtl_Instance<
415 T, StaticInstance,
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>
477public:
484 static T & get(const Data& rData) {
485 return *rtl_Instance<
486 T, StaticInstanceWithArg,
488 Data >::create( StaticInstanceWithArg(),
490 rData );
491 }
492
499 static T & get(Data& rData) {
500 return *rtl_Instance<
501 T, StaticInstanceWithArg,
503 Data >::create( StaticInstanceWithArg(),
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>
549public:
556 static T * get() {
557 return rtl_Instance<
558 T, InitAggregate,
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>
615public:
622 static T & get() {
623 return *rtl_Instance<
624 T, StaticInstanceWithInit,
626 Data, InitData >::create( StaticInstanceWithInit(),
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: */
#define OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER()
A platform specific macro needed to make double-checked locking work.
Definition doublecheckedlocking.h:73
Guard< Mutex > MutexGuard
Definition mutex.hxx:273
Definition bootstrap.hxx:34
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition instance.hxx:405
static T & get()
Gets the static.
Definition instance.hxx:413
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition instance.hxx:476
static T & get(Data &rData)
Gets the static.
Definition instance.hxx:499
static T & get(const Data &rData)
Gets the static.
Definition instance.hxx:484
Helper class for a late-initialized static aggregate, e.g.
Definition instance.hxx:548
static T * get()
Gets the static aggregate, late-initializing.
Definition instance.hxx:556
Helper base class for a late-initialized static variable, implementing the double-checked locking pat...
Definition instance.hxx:614
static T & get()
Gets the static.
Definition instance.hxx:622
A helper functor for the rtl_Instance template.
Definition getglobalmutex.hxx:36