My Project
UDK 3.2.7 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 
24 #include "osl/getglobalmutex.hxx"
25 
26 namespace {
27 
261 template< typename Inst, typename InstCtor,
262  typename Guard, typename GuardCtor,
263  typename Data = int, typename DataCtor = int >
264 class rtl_Instance
265 {
266 public:
267  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor)
268  {
269 #if defined _MSC_VER
270  static Inst * m_pInstance = 0;
271 #endif // _MSC_VER
272  Inst * p = m_pInstance;
273  if (!p)
274  {
275  Guard aGuard(aGuardCtor());
276  p = m_pInstance;
277  if (!p)
278  {
279  p = aInstCtor();
281  m_pInstance = p;
282  }
283  }
284  else
285  {
287  }
288  return p;
289  }
290 
291  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
292  DataCtor aDataCtor)
293  {
294 #if defined _MSC_VER
295  static Inst * m_pInstance = 0;
296 #endif // _MSC_VER
297  Inst * p = m_pInstance;
298  if (!p)
299  {
300  Data aData(aDataCtor());
301  Guard aGuard(aGuardCtor());
302  p = m_pInstance;
303  if (!p)
304  {
305  p = aInstCtor(aData);
307  m_pInstance = p;
308  }
309  }
310  else
311  {
313  }
314  return p;
315  }
316 
317  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
318  const Data &rData)
319  {
320 #if defined _MSC_VER
321  static Inst * m_pInstance = 0;
322 #endif // _MSC_VER
323  Inst * p = m_pInstance;
324  if (!p)
325  {
326  Guard aGuard(aGuardCtor());
327  p = m_pInstance;
328  if (!p)
329  {
330  p = aInstCtor(rData);
332  m_pInstance = p;
333  }
334  }
335  else
336  {
338  }
339  return p;
340  }
341 
342 private:
343 #if !defined _MSC_VER
344  static Inst * m_pInstance;
345 #endif // _MSC_VER
346 };
347 
348 #if !defined _MSC_VER
349 template< typename Inst, typename InstCtor,
350  typename Guard, typename GuardCtor,
351  typename Data, typename DataCtor >
352 Inst *
353 rtl_Instance< Inst, InstCtor, Guard, GuardCtor, Data, DataCtor >::m_pInstance
354 = 0;
355 #endif // _MSC_VER
356 
357 }
358 
359 namespace rtl {
360 
380 #if defined HAVE_THREADSAFE_STATICS
381 template<typename T, typename Unique>
382 class Static {
383 public:
390  static T & get() {
391  static T instance;
392  return instance;
393  }
394 };
395 #else
396 template<typename T, typename Unique>
397 class Static {
398 public:
405  static T & get() {
406  return *rtl_Instance<
407  T, StaticInstance,
409  StaticInstance(), ::osl::GetGlobalMutex() );
410  }
411 private:
412  struct StaticInstance {
413  T * operator () () {
414  static T instance;
415  return &instance;
416  }
417  };
418 };
419 #endif
420 
440 #if defined HAVE_THREADSAFE_STATICS
441 template<typename T, typename Data, typename Unique>
442 class StaticWithArg {
443 public:
450  static T & get(const Data& rData) {
451  static T instance(rData);
452  return instance;
453  }
454 
461  static T & get(Data& rData) {
462  static T instance(rData);
463  return instance;
464  }
465 };
466 #else
467 template<typename T, typename Data, typename Unique>
469 public:
476  static T & get(const Data& rData) {
477  return *rtl_Instance<
478  T, StaticInstanceWithArg,
480  Data >::create( StaticInstanceWithArg(),
482  rData );
483  }
484 
491  static T & get(Data& rData) {
492  return *rtl_Instance<
493  T, StaticInstanceWithArg,
495  Data >::create( StaticInstanceWithArg(),
497  rData );
498  }
499 private:
500  struct StaticInstanceWithArg {
501  T * operator () (const Data& rData) {
502  static T instance(rData);
503  return &instance;
504  }
505 
506  T * operator () (Data& rData) {
507  static T instance(rData);
508  return &instance;
509  }
510  };
511 };
512 #endif
513 
522 #if defined HAVE_THREADSAFE_STATICS
523 template<typename T, typename InitAggregate>
524 class StaticAggregate {
525 public:
533  static T * get() {
534  static T *instance = InitAggregate()();
535  return instance;
536  }
537 };
538 #else
539 template<typename T, typename InitAggregate>
541 public:
548  static T * get() {
549  return rtl_Instance<
550  T, InitAggregate,
552  InitAggregate(), ::osl::GetGlobalMutex() );
553  }
554 };
555 #endif
556 
587 #if defined HAVE_THREADSAFE_STATICS
588 template<typename T, typename InitData,
589  typename Unique = InitData, typename Data = T>
590 class StaticWithInit {
591 public:
598  static T & get() {
599  static T instance = InitData()();
600  return instance;
601  }
602 };
603 #else
604 template<typename T, typename InitData,
605  typename Unique = InitData, typename Data = T>
607 public:
614  static T & get() {
615  return *rtl_Instance<
616  T, StaticInstanceWithInit,
618  Data, InitData >::create( StaticInstanceWithInit(),
620  InitData() );
621  }
622 private:
623  struct StaticInstanceWithInit {
624  T * operator () ( Data d ) {
625  static T instance(d);
626  return &instance;
627  }
628  };
629 };
630 #endif
631 } // namespace rtl
632 
633 #endif // INCLUDED_RTL_INSTANCE_HXX
634 
635 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */