September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,38 +0,0 @@
class Singleton
{
public:
static Singleton* Instance()
{
// We need to ensure that we don't accidentally create two Singletons
HANDLE hMutex = CreateMutex(NULL, FALSE, "MySingletonMutex");
WaitForSingleObject(hMutex, INFINITE);
// Create the instance of the class.
// Since it's a static variable, if the class has already been created,
// It won't be created again.
static Singleton myInstance;
// Release our mutex so that other application threads can use this function
ReleaseMutex( hMutex );
// Free the handle
CloseHandle( hMutex );
// Return a pointer to our mutex instance.
return &myInstance;
}
// Any other public methods
protected:
Singleton()
{
// Constructor code goes here.
}
~Singleton()
{
// Destructor code goes here.
}
// And any other protected methods.
}

View file

@ -1,27 +0,0 @@
class Singleton
{
public:
static Singleton* Instance()
{
// Since it's a static variable, if the class has already been created,
// It won't be created again.
static Singleton myInstance;
// Return a pointer to our mutex instance.
return &myInstance;
}
// Any other public methods
protected:
Singleton()
{
// Constructor code goes here.
}
~Singleton()
{
// Destructor code goes here.
}
// And any other protected methods.
}

View file

@ -1,36 +0,0 @@
class Singleton
{
public:
static Singleton & Instance()
{
// Since it's a static variable, if the class has already been created,
// It won't be created again.
// And it **is** thread-safe in C++11.
static Singleton myInstance;
// Return a reference to our instance.
return myInstance;
}
// delete copy and move constructors and assign operators
Singleton(Singleton const&) = delete; // Copy construct
Singleton(Singleton&&) = delete; // Move construct
Singleton& operator=(Singleton const&) = delete; // Copy assign
Singleton& operator=(Singleton &&) = delete; // Move assign
// Any other public methods
protected:
Singleton()
{
// Constructor code goes here.
}
~Singleton()
{
// Destructor code goes here.
}
// And any other protected methods.
}

View file

@ -0,0 +1,99 @@
#include <stdexcept>
template <typename Self>
class singleton
{
protected:
static Self*
sentry;
public:
static Self&
instance()
{
return *sentry;
}
singleton()
{
if(sentry)
throw std::logic_error("Error: attempt to instantiate a singleton over a pre-existing one!");
sentry = (Self*)this;
}
virtual ~singleton()
{
if(sentry == this)
sentry = 0;
}
};
template <typename Self>
Self*
singleton<Self>::sentry = 0;
/*
Example usage:
*/
#include <iostream>
#include <string>
using namespace
std;
class controller : public singleton<controller>
{
public:
controller(string const& name)
: name(name)
{
trace("begin");
}
~controller()
{
trace("end");
}
void
work()
{
trace("doing stuff");
}
void
trace(string const& message)
{
cout << name << ": " << message << endl;
}
string
name;
};
int
main()
{
controller*
first = new controller("first");
controller::instance().work();
delete first;
/*
No problem, our first controller no longer exists...
*/
controller
second("second");
controller::instance().work();
try
{
/*
Never happens...
*/
controller
goner("goner");
controller::instance().work();
}
catch(exception const& error)
{
cout << error.what() << endl;
}
controller::instance().work();
/*
Never happens (and depending on your system this may or may not print a helpful message!)
*/
controller
goner("goner");
controller::instance().work();
}

View file

@ -1,4 +1,4 @@
#symbol singleton =
class singleton =
{
// ...
}.

View file

@ -1,8 +1,8 @@
#class $Singleton
class $Singleton
{
#field theField.
object theField.
// ...
}
#static singleton = $Singleton new.
static singleton = $Singleton new.

View file

@ -0,0 +1,9 @@
// version 1.1.2
object Singleton {
fun speak() = println("I am a singleton")
}
fun main(args: Array<String>) {
Singleton.speak()
}

View file

@ -0,0 +1,32 @@
a = .singleton~new
b = .singleton~new
a~foo = "Rick"
if a~foo \== b~foo then say "A and B are not the same object"
::class singleton
-- initialization method for the class
::method init class
expose singleton
-- mark this as unallocated. We could also just allocate
-- the singleton now, but better practice is probably wait
-- until it is requested
singleton = .nil
-- override the new method. Since this is a guarded
-- method by default, this is thread safe
::method new class
expose singleton
-- first request? Do the real creation now
if singleton == .nil then do
-- forward to the super class. We use this form of
-- FORWARD rather than explicit call ~new:super because
-- this takes care of any arguments passed to NEW as well.
forward class(super) continue
singleton = result
end
return singleton
-- an attribute that can be used to demonstrate this really is
a singleton.
::attribute foo

View file

@ -0,0 +1,11 @@
class SingletonClass {
static let sharedInstance = SingletonClass()
///Override the init method and make it private
private override init(){
// User can do additional manipulations here.
}
}
// Usage
let sharedObject = SingletonClass.sharedInstance

View file

@ -0,0 +1,4 @@
class [static] Borg{ var v }
b1 := Borg; b2 := Borg();
b1 == b2 //--> True
b1.v=123; b2.v.println(); //--> 123