Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 121 lines (99 sloc) 4.631 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/*
factory.h: Template class for generating an object based on string-specified class.

Copyright 2011 David Simon <david.mike.simon@gmail.com>

This file is part of Orbit Ribbon.

Orbit Ribbon is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Orbit Ribbon is distributed in the hope that it will be awesome,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Orbit Ribbon. If not, see http://www.gnu.org/licenses/
*/

#ifndef ORBIT_RIBBON_FACTORY_H
#define ORBIT_RIBBON_FACTORY_H

#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
#include <map>
#include <memory>
#include <utility>
#include <typeinfo>

#include "except.h"

template<class BaseType, class SourceType> class FactorySpecBase {
  public:
    typedef BaseType base_type;
    typedef SourceType source_type;
    
    virtual std::string extract_name(const SourceType& source) { return typeid(source).name(); }
};

template<class FactorySpec> class Generator {
  public:
    virtual boost::shared_ptr<typename FactorySpec::base_type> create(const typename FactorySpec::source_type& source) =0;
};

template<class FactorySpec, class C> class AutoGenerator : public Generator<FactorySpec> {
  public:
    virtual boost::shared_ptr<typename FactorySpec::base_type> create(const typename FactorySpec::source_type& source) {
      return boost::shared_ptr<typename FactorySpec::base_type>(new C(source));
    }
};

template<class FactorySpec, class C, class S> class AutoCastingGenerator : public Generator<FactorySpec> {
  public:
    virtual boost::shared_ptr<typename FactorySpec::base_type> create(const typename FactorySpec::source_type& source) {
      return boost::shared_ptr<typename FactorySpec::base_type>(new C(static_cast<const S&>(source)));
    }
};

template<class FactorySpec> class Factory {
  public:
    // The entry with the key "" is special: it's the default, used if no more specific generator could be found
    typedef boost::shared_ptr<Generator<FactorySpec> > GeneratorPtr;
    typedef std::map<std::string, GeneratorPtr> GeneratorMap;
    GeneratorMap _generator_map;
    
    void register_class(const GeneratorPtr& ptr, const std::string& name = "") {
      typename GeneratorMap::iterator i = _generator_map.find(name);
      if (i != _generator_map.end()) {
        throw GameException("Attempted to register over existing factory for " + name);
      }
      _generator_map.insert(typename GeneratorMap::value_type(name, ptr));
    }
    
    boost::shared_ptr<typename FactorySpec::base_type> create(const typename FactorySpec::source_type& source) const {
      FactorySpec s;
      std::string name = s.extract_name(source);
      typename GeneratorMap::const_iterator i = _generator_map.find(name);
      if (i == _generator_map.end()) {
        i = _generator_map.find("");
      }
      
      if (i != _generator_map.end()) {
        return i->second->create(source);
      } else {
        throw GameException("No implementation for " + name + " under FactorySpec " + typeid(FactorySpec).name());
      }
    }
};

// Hack to avoid initialization order problems.
// We cannot create static instances of Factory and then use static instances of AutoRegistration to fill them out.
// We couldn't be sure the Factory would be initialized first if that happened.
// This function takes care of it; the factory is created as soon as it is needed, no sooner and no later.
template<class FactorySpec> Factory<FactorySpec>& get_factory() {
  static Factory<FactorySpec> factory;
  return factory;
}

template<class FactorySpec, class C> class AutoRegistration {
  public:
    AutoRegistration(const std::string& name = "") {
      boost::shared_ptr<Generator<FactorySpec> > generator(new AutoGenerator<FactorySpec, C>);
      get_factory<FactorySpec>().register_class(generator, name);
    }
};

template<class FactorySpec, class C> class AutoDefaultRegistration :
  public AutoRegistration<FactorySpec, C> {
};

template<class FactorySpec, class C, class S> class AutoRegistrationBySourceTypename {
  public:
    AutoRegistrationBySourceTypename() {
      boost::shared_ptr<Generator<FactorySpec> > generator(new AutoCastingGenerator<FactorySpec, C, S>);
      get_factory<FactorySpec>().register_class(generator, typeid(S).name());
    }
};

#endif
Something went wrong with that request. Please try again.