Giter Site home page Giter Site logo

jordiexvision / ofxsingleton Goto Github PK

View Code? Open in Web Editor NEW

This project forked from elliotwoods/ofxsingleton

0.0 2.0 0.0 26 KB

Singleton pattern for classes (doesn't need to be with openFrameworks, but we're laying it out like it is)

Makefile 46.34% C++ 52.06% Objective-C 1.60%

ofxsingleton's Introduction

ofxSingleton

Introduction

Utilities for using the singleton pattern with your own classes.

Singletons are generally classes which have 1 instance shared across the application. You can read more about what singletons are and why you might want to use them at [http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C].

Some people consider singletons to be 'bad design' or otherwise a design element which is often used in a lazy way (Google even disallows them in their own code), you can read about this debate at [http://stackoverflow.com/questions/11831/singletons-good-design-or-a-crutch].

Usage

Designing signleton classes

In your class declaration (.h), inherit from ofxSingleton::Singleton<YourClassType>, e.g.:

class MyClassType : public ofxSingleton::Singleton<MyClassType> {
	//...	
};

In your class definition (.cpp), define the symbol MyClassType::singletonStore, e.g.:

#include "MyClassType.h"

MyClassType::SingletonStore MyClassType::singletonStore;

// rest of class definition

Using singleton classes

Generally whenever you want to use the singleton of YourClassType, you simply use YourClassType::X() which returns a non-const reference to the singleton. You don't need to worry about creating or clearing the singleton, this happens automatically for you.

Register

ofxSingleton also provides a class ofxSingleton::Register (which is itself a singleton). This register keeps track of all singletons and can be useful for synchronising singletons between plugins and applications.

Notes

  • Since some logic must be run each time you call MySingletonClass::X(), you might want to keep a local const reference if you plan on calling it many times in the same function.
  • Instead of ofxSingleton::Singleton, you also have the option to instead inherit from ofxSingleton::UnmanagedSingleton which has fewer features and does not put the singleton into the singleton registry. Note that the line you must put into the class definition is different and looks like template<> std::shared_ptr<MyClassType> UnmanagedSingleton<MyClassType>::singleton;

Sharing singletons between dynamically loaded libraries

The general pattern for this follows:

  • All shared singletons are instantiated by the application (singletons cannot be shared between libraries if they are not in the application itself)
  • Each library and the application has an ofxSingleton::Register

License

MIT License.

ofxsingleton's People

Contributors

elliotwoods avatar satoruhiga avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.