Changeset 97f035 for molecuilder/src


Ignore:
Timestamp:
Mar 11, 2010, 3:17:29 PM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
Children:
1e0785
Parents:
e9f2e1
Message:

Small improvements to singleton pattern

Location:
molecuilder/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/Patterns/Singleton.hpp

    re9f2e1 r97f035  
    3232    void reset(T* _content);
    3333    void reset();
    34     ptr_t& operator=(ptr_t& rhs);
     34    ptr_t& operator=(const ptr_t& rhs);
    3535  private:
    36     T* content;
     36    mutable T* content;
    3737  };
    3838
     
    8585  static void setInstance(T*);
    8686protected:
     87  // constructor accessible by subclasses
     88  Singleton();
    8789
    8890private:
     91  // private copy constructor to avoid unintended copying
     92  Singleton(const Singleton&);
     93
    8994  static boost::recursive_mutex instanceLock;
    9095  static ptr_t theInstance;
  • molecuilder/src/Patterns/Singleton_impl.hpp

    re9f2e1 r97f035  
    7777}
    7878
     79template<class T, bool _may_create>
     80Singleton<T,_may_create>::Singleton(){/* empty */}
     81
     82// private copy constructor to avoid unintended copying
     83template <class T, bool _may_create>
     84Singleton<T,_may_create>::Singleton(const Singleton<T,_may_create>&){
     85  assert(0 && "Copy constructor of singleton template called");
     86}
     87
    7988/**
    8089 * This define allows simple instantiation of the necessary singleton functions
     
    121130
    122131template <class T,bool _may_create>
    123 typename Singleton<T,_may_create>::ptr_t& Singleton<T,_may_create>::ptr_t::operator=(typename Singleton<T,_may_create>::ptr_t& rhs){
     132typename Singleton<T,_may_create>::ptr_t& Singleton<T,_may_create>::ptr_t::operator=(const typename Singleton<T,_may_create>::ptr_t& rhs){
    124133  if(&rhs!=this){
    125134    delete content;
  • molecuilder/src/unittests/SingletonTest.cpp

    re9f2e1 r97f035  
    2929    count1++;
    3030  }
     31  // explicit copy constructor to catch if this is ever called
     32  SingletonStub1(const SingletonStub1&){
     33    CPPUNIT_FAIL    ( "Copy constructor of Singleton called" );
     34  }
    3135  virtual ~SingletonStub1(){
    3236    count2++;
     
    4751  SingletonStub2(){
    4852    count1++;
     53  }
     54  // explicit copy constructor to catch if thsi is ever called
     55  SingletonStub2(const SingletonStub2&){
     56    CPPUNIT_FAIL    ( "Copy constructor of Singleton called" );
    4957  }
    5058  virtual ~SingletonStub2(){
Note: See TracChangeset for help on using the changeset viewer.