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  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #if !defined INCLUDED_RTL_INSTANCE_HXX
30 #define INCLUDED_RTL_INSTANCE_HXX
31 
33 #include "osl/getglobalmutex.hxx"
34 
35 namespace {
36 
270 template< typename Inst, typename InstCtor,
271  typename Guard, typename GuardCtor,
272  typename Data = int, typename DataCtor = int >
273 class rtl_Instance
274 {
275 public:
276  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor)
277  {
278 #if defined _MSC_VER
279  static Inst * m_pInstance = 0;
280 #endif // _MSC_VER
281  Inst * p = m_pInstance;
282  if (!p)
283  {
284  Guard aGuard(aGuardCtor());
285  p = m_pInstance;
286  if (!p)
287  {
288  p = aInstCtor();
290  m_pInstance = p;
291  }
292  }
293  else
294  {
296  }
297  return p;
298  }
299 
300  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
301  DataCtor aDataCtor)
302  {
303 #if defined _MSC_VER
304  static Inst * m_pInstance = 0;
305 #endif // _MSC_VER
306  Inst * p = m_pInstance;
307  if (!p)
308  {
309  Data aData(aDataCtor());
310  Guard aGuard(aGuardCtor());
311  p = m_pInstance;
312  if (!p)
313  {
314  p = aInstCtor(aData);
316  m_pInstance = p;
317  }
318  }
319  else
320  {
322  }
323  return p;
324  }
325 
326  static inline Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
327  const Data &rData)
328  {
329 #if defined _MSC_VER
330  static Inst * m_pInstance = 0;
331 #endif // _MSC_VER
332  Inst * p = m_pInstance;
333  if (!p)
334  {
335  Guard aGuard(aGuardCtor());
336  p = m_pInstance;
337  if (!p)
338  {
339  p = aInstCtor(rData);
341  m_pInstance = p;
342  }
343  }
344  else
345  {
347  }
348  return p;
349  }
350 
351 private:
352 #if !defined _MSC_VER
353  static Inst * m_pInstance;
354 #endif // _MSC_VER
355 };
356 
357 #if !defined _MSC_VER
358 template< typename Inst, typename InstCtor,
359  typename Guard, typename GuardCtor,
360  typename Data, typename DataCtor >
361 Inst *
362 rtl_Instance< Inst, InstCtor, Guard, GuardCtor, Data, DataCtor >::m_pInstance
363 = 0;
364 #endif // _MSC_VER
365 
366 }
367 
368 namespace rtl {
369 
389 #if defined HAVE_THREADSAFE_STATICS
390 template<typename T, typename Unique>
391 class Static {
392 public:
399  static T & get() {
400  static T instance;
401  return instance;
402  }
403 };
404 #else
405 template<typename T, typename Unique>
406 class Static {
407 public:
414  static T & get() {
415  return *rtl_Instance<
416  T, StaticInstance,
418  StaticInstance(), ::osl::GetGlobalMutex() );
419  }
420 private:
421  struct StaticInstance {
422  T * operator () () {
423  static T instance;
424  return &instance;
425  }
426  };
427 };
428 #endif
429 
449 #if defined HAVE_THREADSAFE_STATICS
450 template<typename T, typename Data, typename Unique>
451 class StaticWithArg {
452 public:
459  static T & get(const Data& rData) {
460  static T instance(rData);
461  return instance;
462  }
463 
470  static T & get(Data& rData) {
471  static T instance(rData);
472  return instance;
473  }
474 };
475 #else
476 template<typename T, typename Data, typename Unique>
478 public:
485  static T & get(const Data& rData) {
486  return *rtl_Instance<
487  T, StaticInstanceWithArg,
489  Data >::create( StaticInstanceWithArg(),
491  rData );
492  }
493 
500  static T & get(Data& rData) {
501  return *rtl_Instance<
502  T, StaticInstanceWithArg,
504  Data >::create( StaticInstanceWithArg(),
506  rData );
507  }
508 private:
509  struct StaticInstanceWithArg {
510  T * operator () (const Data& rData) {
511  static T instance(rData);
512  return &instance;
513  }
514 
515  T * operator () (Data& rData) {
516  static T instance(rData);
517  return &instance;
518  }
519  };
520 };
521 #endif
522 
531 #if defined HAVE_THREADSAFE_STATICS
532 template<typename T, typename InitAggregate>
533 class StaticAggregate {
534 public:
542  static T * get() {
543  static T *instance = InitAggregate()();
544  return instance;
545  }
546 };
547 #else
548 template<typename T, typename InitAggregate>
550 public:
557  static T * get() {
558  return rtl_Instance<
559  T, InitAggregate,
561  InitAggregate(), ::osl::GetGlobalMutex() );
562  }
563 };
564 #endif
565 
596 #if defined HAVE_THREADSAFE_STATICS
597 template<typename T, typename InitData,
598  typename Unique = InitData, typename Data = T>
599 class StaticWithInit {
600 public:
607  static T & get() {
608  static T instance = InitData()();
609  return instance;
610  }
611 };
612 #else
613 template<typename T, typename InitData,
614  typename Unique = InitData, typename Data = T>
616 public:
623  static T & get() {
624  return *rtl_Instance<
625  T, StaticInstanceWithInit,
627  Data, InitData >::create( StaticInstanceWithInit(),
629  InitData() );
630  }
631 private:
632  struct StaticInstanceWithInit {
633  T * operator () ( Data d ) {
634  static T instance(d);
635  return &instance;
636  }
637  };
638 };
639 #endif
640 } // namespace rtl
641 
642 #endif // INCLUDED_RTL_INSTANCE_HXX
643 
644 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */