AlterOffice
AlterOffice 3.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #ifndef INCLUDED_RTL_INSTANCE_HXX
5 #define INCLUDED_RTL_INSTANCE_HXX
6 
7 #include "sal/config.h"
8 
9 #include <cstddef>
10 
12 #include "osl/getglobalmutex.hxx"
13 
14 namespace {
15 
249 template< typename Inst, typename InstCtor,
250  typename Guard, typename GuardCtor,
251  typename Data = int, typename DataCtor = int >
252 class rtl_Instance
253 {
254 public:
255  static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor)
256  {
257 #if defined _MSC_VER
258  static Inst * m_pInstance = NULL;
259 #endif // _MSC_VER
260  Inst * p = m_pInstance;
261  if (!p)
262  {
263  Guard aGuard(aGuardCtor());
264  p = m_pInstance;
265  if (!p)
266  {
267  p = aInstCtor();
269  m_pInstance = p;
270  }
271  }
272  else
273  {
275  }
276  return p;
277  }
278 
279  static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
280  DataCtor aDataCtor)
281  {
282 #if defined _MSC_VER
283  static Inst * m_pInstance = NULL;
284 #endif // _MSC_VER
285  Inst * p = m_pInstance;
286  if (!p)
287  {
288  Data aData(aDataCtor());
289  Guard aGuard(aGuardCtor());
290  p = m_pInstance;
291  if (!p)
292  {
293  p = aInstCtor(aData);
295  m_pInstance = p;
296  }
297  }
298  else
299  {
301  }
302  return p;
303  }
304 
305  static Inst * create(InstCtor aInstCtor, GuardCtor aGuardCtor,
306  const Data &rData)
307  {
308 #if defined _MSC_VER
309  static Inst * m_pInstance = 0;
310 #endif // _MSC_VER
311  Inst * p = m_pInstance;
312  if (!p)
313  {
314  Guard aGuard(aGuardCtor());
315  p = m_pInstance;
316  if (!p)
317  {
318  p = aInstCtor(rData);
320  m_pInstance = p;
321  }
322  }
323  else
324  {
326  }
327  return p;
328  }
329 
330 private:
331 #if !defined _MSC_VER
332  static Inst * m_pInstance;
333 #endif // _MSC_VER
334 };
335 
336 #if !defined _MSC_VER
337 template< typename Inst, typename InstCtor,
338  typename Guard, typename GuardCtor,
339  typename Data, typename DataCtor >
340 Inst *
341 rtl_Instance< Inst, InstCtor, Guard, GuardCtor, Data, DataCtor >::m_pInstance
342 = NULL;
343 #endif // _MSC_VER
344 
345 }
346 
347 namespace rtl {
348 
368 #if defined LIBO_INTERNAL_ONLY
369 template<typename T, typename Unique>
370 class Static {
371 public:
378  static T & get() {
379  static T instance;
380  return instance;
381  }
382 };
383 #else
384 template<typename T, typename Unique>
385 class Static {
386 public:
393  static T & get() {
394  return *rtl_Instance<
395  T, StaticInstance,
397  StaticInstance(), ::osl::GetGlobalMutex() );
398  }
399 private:
400  struct StaticInstance {
401  T * operator () () {
402  static T instance;
403  return &instance;
404  }
405  };
406 };
407 #endif
408 
428 #if defined LIBO_INTERNAL_ONLY
429 template<typename T, typename Data, typename Unique>
430 class StaticWithArg {
431 public:
438  static T & get(const Data& rData) {
439  static T instance(rData);
440  return instance;
441  }
442 
449  static T & get(Data& rData) {
450  static T instance(rData);
451  return instance;
452  }
453 };
454 #else
455 template<typename T, typename Data, typename Unique>
457 public:
464  static T & get(const Data& rData) {
465  return *rtl_Instance<
466  T, StaticInstanceWithArg,
468  Data >::create( StaticInstanceWithArg(),
470  rData );
471  }
472 
479  static T & get(Data& rData) {
480  return *rtl_Instance<
481  T, StaticInstanceWithArg,
483  Data >::create( StaticInstanceWithArg(),
485  rData );
486  }
487 private:
488  struct StaticInstanceWithArg {
489  T * operator () (const Data& rData) {
490  static T instance(rData);
491  return &instance;
492  }
493 
494  T * operator () (Data& rData) {
495  static T instance(rData);
496  return &instance;
497  }
498  };
499 };
500 #endif
501 
510 #if defined LIBO_INTERNAL_ONLY
511 template<typename T, typename InitAggregate>
512 class StaticAggregate {
513 public:
521  static T * get() {
522  static T *instance = InitAggregate()();
523  return instance;
524  }
525 };
526 #else
527 template<typename T, typename InitAggregate>
529 public:
536  static T * get() {
537  return rtl_Instance<
538  T, InitAggregate,
540  InitAggregate(), ::osl::GetGlobalMutex() );
541  }
542 };
543 #endif
544 
575 #if defined LIBO_INTERNAL_ONLY
576 template<typename T, typename InitData,
577  typename Unique = InitData, typename Data = T>
578 class StaticWithInit {
579 public:
586  static T & get() {
587  static T instance = InitData()();
588  return instance;
589  }
590 };
591 #else
592 template<typename T, typename InitData,
593  typename Unique = InitData, typename Data = T>
595 public:
602  static T & get() {
603  return *rtl_Instance<
604  T, StaticInstanceWithInit,
606  Data, InitData >::create( StaticInstanceWithInit(),
608  InitData() );
609  }
610 private:
611  struct StaticInstanceWithInit {
612  T * operator () ( Data d ) {
613  static T instance(d);
614  return &instance;
615  }
616  };
617 };
618 #endif
619 } // namespace rtl
620 
621 #endif // INCLUDED_RTL_INSTANCE_HXX
622 
623 /* 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:53
A helper functor for the rtl_Instance template.
Definition: getglobalmutex.hxx:15
Guard< Mutex > MutexGuard
Definition: mutex.hxx:235
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:456
Helper base class for a late-initialized (default-constructed) static variable, implementing the doub...
Definition: instance.hxx:385
Helper class for a late-initialized static aggregate, e.g.
Definition: instance.hxx:528
Helper base class for a late-initialized static variable, implementing the double-checked locking pat...
Definition: instance.hxx:594