History: Complete implementation
Source of version: 3 (current)
Copy to clipboard
Here is a working example of Intermediate Tutorial 1, complete with multiple robots that can be animated, along with factory classes for generating objects, with a complete Singleton implementation that was obtained from this post [http://www.gamedev.net/community/forums/topic.asp?topic_id=301651] by cuzman in the GameDev.net forums.
All the classes are one big mess I know.
__singleton.h__
{CODE(wrap="1", colors="c++")}
//------------------------------------------------------
// file: singleton.h
// description: this is the template singleton class
//------------------------------------------------------
#ifndef IM_TUTORIAL1_SINGLETON_H__
#define IM_TUTORIAL1_SINGLETON_H__
namespace IntermediateTutorial1 {
// This class was found in a post in GameDev.net. I assume
// it is public domain, since it was posted in a public forum
// for the purpose of being improved upon and being
// utilized
//
// I modified it to create itself automatically in the
// getSingleton method instead of having a seperate
// initSingleton static method. Also added was the ability
// to get a ptr or a reference to the Singleton
//
// The original author's name is cozman
//
// Original Post:
// http://www.gamedev.net/community/forums/topic.asp?topic_id=301651
//
// Singleton solution w/ auto_ptr?
//
// cozman GDNet+ Member since: 8/7/2001 From: Cary, United States
//
// Posted - 2/17/2005 11:12:04 PM
// I was toying around with creating a generic Singleton class.
// I was wondering if any of you could comment on this idea:
//
// #include <cassert>
// #include <memory>
// #include <iostream>
//
// template<class T>
// class Singleton
// {
// public:
// static void initSingleton()
// {
// assert(instance_.get() == 0);
//
// instance_ = std::auto_ptr<T>(new T);
// }
//
// static T& getSingleton()
// {
// assert(instance_.get() != 0);
//
// return *instance_;
// }
//
// static void destroySingleton()
// {
// assert(instance_.get() != 0);
//
// instance_.reset();
// }
//
// virtual ~Singleton()=0;
//
// private:
//
// static std::auto_ptr<T> instance_;
// };
//
// template<class T>
// std::auto_ptr<T> Singleton<T>::instance_(0);
//
// template<class T>
// Singleton<T>::~Singleton() { }
//
// class Something : public Singleton<Something>
// {
// friend class Singleton<Something>;
// friend class std::auto_ptr<Something>;
// private:
// Something() { std::cout << "created" << std::endl; }
// ~Something() { std::cout << "destroyed" << std::endl; }
// };
//
//
// From basic testing it works fine, you can't leak memory,
// but you can also control the order of destruction if it
// is important.
//
// Does anyone have any suggestions?
/**
Template class for creating single-instance global classes.
*/
template<class T>
class Singleton
{
public:
// static void initSingleton()
// {
// assert(instance_.get() == 0);
//
// instance_ = std::auto_ptr<T>(new T);
// }
static T& getSingleton()
{
if(instance_.get() == 0)
instance_ = std::auto_ptr<T>(new T);
return *(instance_.get());
}
static T* getSingletonPtr()
{
return &(getSingleton());
}
static void destroySingleton()
{
assert(instance_.get() != 0);
instance_.reset();
}
virtual ~Singleton()=0;
private:
static std::auto_ptr<T> instance_;
};
template<class T>
std::auto_ptr<T> Singleton<T>::instance_(0);
template<class T>
Singleton<T>::~Singleton() { }
// implementation details
//class Something : public Singleton<Something>
//{
// friend class Singleton<Something>;
// friend class std::auto_ptr<Something>;
//private:
// Something() { std::cout << "created" << std::endl; }
// ~Something() { std::cout << "destroyed" << std::endl; }
//};
}
#endif
{CODE}
__im_tutorial1.h__
{CODE(wrap="1", colors="c++")}
#ifndef IM_TUTORIAL2_H__
# define IM_TURORIAL2_H__
# define CREATOR_CLASS(x) friend class x
#endif
{CODE}
((Go on)) to im_tutorial1.cpp source code