Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
projects:singleton_template [2015/11/15] – [Q & A] terenceprojects:singleton_template [2016/07/05] (current) – July 5, 2016 terence
Line 1: Line 1:
 ====== Singleton Template (C++) ====== ====== Singleton Template (C++) ======
 +
 +//An inheritance-based way to make singleton classes in C++//
 +
  
 ===== License ===== ===== License =====
Line 5: Line 8:
   * **Author:** [[mailto:tjgrant@tatewake.com|Terence J. Grant]]   * **Author:** [[mailto:tjgrant@tatewake.com|Terence J. Grant]]
   * **License:** [[http://opensource.org/licenses/MIT|MIT License]]   * **License:** [[http://opensource.org/licenses/MIT|MIT License]]
-  * **Last Update:** 2015-11-15+  * **Last Update:** 2015-12-21
   * **Donate:** [[:donate|Your donations are appreciated!]]   * **Donate:** [[:donate|Your donations are appreciated!]]
  
Line 14: Line 17:
 After downloading, make sure to follow the [[#how_to_use|how to use]] instructions below; they're worth reading. After downloading, make sure to follow the [[#how_to_use|how to use]] instructions below; they're worth reading.
  
-FIXME+  * Latest version: [[https://github.com/tatewake/singleton-template|Singleton Template(C++) version 2015-12-21 on GitHub]]
  
 ===== About ===== ===== About =====
  
-FIXME+The Singleton Template is a [[wp>Template_(C%2B%2B)|C++ template class]] that allows you to define your own singleton classes as inheriting from a single base class.
  
 +This provides a few benefits over other techniques:
 +
 +  * Consistent method interface among all singletons (all singletons will have a **''::getInstance()''** method)
 +  * Never having to copy / paste code for singleton creation (which is good if, for instance, best practices change)
 +
 +The singleton creation method in **''::getInstance()''** is via the "Meyers Singleton" technique, described in the book [[http://www.amazon.com/gp/product/0321334876/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0321334876&linkCode=as2&tag=terjgra-20&linkId=FWGRI7VTSLU2WGVB|Effective C++ by Scott Meyers]]. So essentially, this class is a "Meyers Singleton C++ template class."
 ===== How to Use ===== ===== How to Use =====
  
Line 28: Line 37:
  
 <code c++> <code c++>
-#include "singleton.h"+#include "tSingleton.h"
 </code> </code>
  
   * Create your own singleton class, ensuring that:   * Create your own singleton class, ensuring that:
  
-  - You inherit from **''tjgrant::singleton<MyClass>''**, where **''MyClass''** is the name of your class+  - You inherit from **''tSingleton<MyClass>''**, where **''MyClass''** is the name of your class
   - Your **constructor**'s access method is **''protected''**   - Your **constructor**'s access method is **''protected''**
   - Your **destructor** is **''virtual''**   - Your **destructor** is **''virtual''**
Line 40: Line 49:
 <code c++> <code c++>
 class MyClass class MyClass
-: public tjgrant::singleton<MyClass>+: public tSingleton<MyClass>
 { {
 protected: protected:
Line 53: Line 62:
     //Methods go here     //Methods go here
  
-    friend class tjgrant::singleton<MyClass>;+    friend class tSingleton<MyClass>;
 }; };
 </code> </code>
Line 100: Line 109:
 class MyClass class MyClass
 : public MyBase, : public MyBase,
-  public tjgrant::singleton<MyClass>+  public tSingleton<MyClass>
 </code> </code>
  
-=== Thread Safety ===+=== Thread Safety===
  
   * **Q:** Is this library [[wp>Thread_safety|thread safe]]? //(Advanced: Won't apply to most users.)//   * **Q:** Is this library [[wp>Thread_safety|thread safe]]? //(Advanced: Won't apply to most users.)//
Line 110: Line 119:
     * After the first time, **''::getInstance''** only reads memory, so this should be safe on any thread     * After the first time, **''::getInstance''** only reads memory, so this should be safe on any thread
   * //Note: That said, I would suggest only accessing **''::getInstance''** on the main thread whenever possible//   * //Note: That said, I would suggest only accessing **''::getInstance''** on the main thread whenever possible//
- 
-=== Personal Beliefs === 
- 
-  * **Q:** Singletons / Templates / Multiple Inheritance is horrible and you shouldn't use it! 
-  * **A:** I disagree. Tools are tools, you're free to use them **or not use them** as you wish. 
-  * //Note: If you really do feel this way, realize that these are your "personal beliefs" and I have little interest in discussing them.// 
  
 If you've found this project useful, [[:donate|please donate today!]] :-) If you've found this project useful, [[:donate|please donate today!]] :-)
 ===== History ===== ===== History =====
  
-**November 152015**+**July 52016**
  
 +  * Removed the namespace, renamed class from "singleton" to "tSingleton" like it originally was
 +
 +**December 21, 2015**
 +
 +  * Added GitHub link to download section
   * First public release   * First public release
  
 +**November 15, 2015**
 +
 +  * Created project page
Print/export
QR Code
QR Code Singleton Template (C++) (generated for current page)