LibreOffice
LibreOffice 7.1 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
25#include <cstddef>
26
29
30namespace {
31
265template< typename Inst, typename InstCtor,
266 typename Guard, typename GuardCtor,
267 typename Data = int, typename DataCtor = int >
268class rtl_Instance
269{
270public:
271 static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor)
272 {
273#if defined _MSC_VER
274 static Inst * m_pInstance = 0;
275#endif // _MSC_VER
276 Inst * p = m_pInstance;
277 if (!p)
278 {
279 Guard aGuard(aGuardCtor());
280 p = m_pInstance;
281 if (!p)
282 {
283 p = aInstCtor();
285 m_pInstance = p;
286 }
287 }
288 else
289 {
291 }
292 return p;
293 }
294
295 static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
296 DataCtor aDataCtor)
297 {
298#if defined _MSC_VER
299 static Inst * m_pInstance = 0;
300#endif // _MSC_VER
301 Inst * p = m_pInstance;
302 if (!p)
303 {
304 Data aData(aDataCtor());
305 Guard aGuard(aGuardCtor());
306 p = m_pInstance;
307 if (!p)
308 {
309 p = aInstCtor(aData);
311 m_pInstance = p;
312 }
313 }
314 else
315 {
317 }
318 return p;
319 }
320
321 static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
322 const Data &rData)
323 {
324#if defined _MSC_VER
325 static Inst * m_pInstance = 0;
326#endif // _MSC_VER
327 Inst * p = m_pInstance;
328 if (!p)
329 {
330 Guard aGuard(aGuardCtor());
331 p = m_pInstance;
332 if (!p)
333 {
334 p = aInstCtor(rData);
336 m_pInstance = p;
337 }
338 }
339 else
340 {
342 }
343 return p;
344 }
345
346private:
347#if !defined _MSC_VER
348 static Inst * m_pInstance;
349#endif // _MSC_VER
350};
351
352#if !defined _MSC_VER
353template< typename Inst, typename InstCtor,
354 typename Guard, typename GuardCtor,
355 typename Data, typename DataCtor >
356Inst *
357rtl_Instance< Inst, InstCtor, Guard, GuardCtor, Data, DataCtor >::m_pInstance
358= NULL;
359#endif // _MSC_VER
360
361}
362
363namespace rtl {
364
384#if defined LIBO_INTERNAL_ONLY
385template<typename T, typename Unique>
386class Static {
387public:
394 static T & get() {
395 static T instance;
396 return instance;
397 }
398};
399#else
400template<typename T, typename Unique>
401class Static {
402public:
409 static T & get() {
410 return *rtl_Instance<
411 T, StaticInstance,
413 StaticInstance(), ::osl::GetGlobalMutex() );
414 }
415private:
416 struct StaticInstance {
417 T * operator () () {
418 static T instance;
419 return &instance;
420 }
421 };
422};
423#endif
424
444#if defined LIBO_INTERNAL_ONLY
445template<typename T, typename Data, typename Unique>
446class StaticWithArg {
447public:
454 static T & get(const Data& rData) {
455 static T instance(rData);
456 return instance;
457 }
458
465 static T & get(Data& rData) {
466 static T instance(rData);
467 return instance;
468 }
469};
470#else
471template<typename T, typename Data, typename Unique>
473public:
480 static T & get(const Data& rData) {
481 return *rtl_Instance<
482 T, StaticInstanceWithArg,
484 Data >::create( StaticInstanceWithArg(),
486 rData );
487 }
488
495 static T & get(Data& rData) {
496 return *rtl_Instance<
497 T, StaticInstanceWithArg,
499 Data >::create( StaticInstanceWithArg(),
501 rData );
502 }
503private:
504 struct StaticInstanceWithArg {
505 T * operator () (const Data& rData) {
506 static T instance(rData);
507 return &instance;
508 }
509
510 T * operator () (Data& rData) {
511 static T instance(rData);
512 return &instance;
513 }
514 };
515};
516#endif
517
526#if defined LIBO_INTERNAL_ONLY
527template<typename T, typename InitAggregate>
528class StaticAggregate {
529public:
537 static T * get() {
538 static T *instance = InitAggregate()();
539 return instance;
540 }
541};
542#else
543template<typename T, typename InitAggregate>
545public:
552 static T * get() {
553 return rtl_Instance<
554 T, InitAggregate,
556 InitAggregate(), ::osl::GetGlobalMutex() );
557 }
558};
559#endif
591#if defined LIBO_INTERNAL_ONLY
592template<typename T, typename InitData,
593 typename Unique = InitData, typename Data = T>
594class StaticWithInit {
595public:
602 static T & get() {
603 static T instance = InitData()();
604 return instance;
605 }
606};
607#else
608template<typename T, typename InitData,
609 typename Unique = InitData, typename Data = T>
611public:
618 static T & get() {
619 return *rtl_Instance<
620 T, StaticInstanceWithInit,
622 Data, InitData >::create( StaticInstanceWithInit(),
624 InitData() );
625 }
626private:
627 struct StaticInstanceWithInit {
628 T * operator () ( Data d ) {
629 static T instance(d);
630 return &instance;
631 }
632 };
633};
634#endif
635} // namespace rtl
636
637#endif // INCLUDED_RTL_INSTANCE_HXX
638
639/* 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:69
Definition: unotype.hxx:43
Guard< Mutex > MutexGuard
Definition: mutex.hxx:246
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:32
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:401
static T & get()
Gets the static.
Definition: instance.hxx:409
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:472
static T & get(Data &rData)
Gets the static.
Definition: instance.hxx:495
static T & get(const Data &rData)
Gets the static.
Definition: instance.hxx:480
Helper class for a late-initialized static aggregate, e.g.
Definition: instance.hxx:544
static T * get()
Gets the static aggregate, late-initializing.
Definition: instance.hxx:552
Helper base class for a late-initialized static variable, implementing the double-checked locking pat...
Definition: instance.hxx:610
static T & get()
Gets the static.
Definition: instance.hxx:618