Changeset 31:278018886120 in rrlib_util


Ignore:
Timestamp:
19.08.2011 12:13:46 (8 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Message:

Added ReturnNullptr policy and default creator functions

Files:
2 added
4 edited
1 moved

Legend:

Unmodified
Added
Removed
  • patterns/factory.h

    r17 r31  
    4242// Internal includes with "" 
    4343//---------------------------------------------------------------------- 
    44 #include "rrlib/util/patterns/factory/policies/error/DefaultError.h" 
     44#include "rrlib/util/patterns/factory/policies/error/ThrowException.h" 
     45#include "rrlib/util/patterns/factory/policies/error/ReturnNullptr.h" 
    4546 
     47#include "rrlib/util/patterns/factory/functions.h" 
    4648#include "rrlib/util/patterns/factory/tFactory.h" 
    4749#include "rrlib/util/patterns/factory/tCloneFactory.h" 
  • patterns/factory/policies/error/ThrowException.h

    r17 r31  
    2020// 
    2121//---------------------------------------------------------------------- 
    22 /*!\file    DefaultError.h 
     22/*!\file    ThrowException.h 
    2323 * 
    2424 * \author  Tobias Foehst 
     
    2626 * \date    2011-01-07 
    2727 * 
    28  * \brief Contains DefaultError 
     28 * \brief Contains ThrowException 
    2929 * 
    30  * \b DefaultError 
     30 * \b ThrowException 
    3131 * 
    3232 */ 
    3333//---------------------------------------------------------------------- 
    34 #ifndef __rrlib__util__patterns__factory__policies__error__DefaultError_h__ 
    35 #define __rrlib__util__patterns__factory__policies__error__DefaultError_h__ 
     34#ifndef __rrlib__util__patterns__factory__policies__error__ThrowException_h__ 
     35#define __rrlib__util__patterns__factory__policies__error__ThrowException_h__ 
    3636 
    3737//---------------------------------------------------------------------- 
     
    3939//---------------------------------------------------------------------- 
    4040#include <stdexcept> 
     41#include <string> 
    4142 
    4243//---------------------------------------------------------------------- 
     
    7172 */ 
    7273template <typename TAbstractProduct, typename TIdentifier> 
    73 struct DefaultError 
     74struct ThrowException 
    7475{ 
    7576  class tFactoryException : public std::runtime_error 
  • patterns/factory/tCloneFactory.h

    r17 r31  
    7373typename TAbstractProduct, 
    7474typename TProductCreator = tFunctor<TAbstractProduct *, const TAbstractProduct &>, 
    75 template <typename TAbstractProduct, typename TIdentifier> class TErrorPolicy = factory::DefaultError 
     75template <typename TAbstractProduct, typename TIdentifier> class TUnknownKeyPolicy = factory::ThrowException 
    7676> 
    7777class tCloneFactory 
     
    100100  } 
    101101 
     102  template <typename TProduct> 
     103  const bool Register(const tTypeInfoWrapper &id) 
     104  { 
     105    return this->Register(id, &factory::DefaultCopyCloner<TProduct>); 
     106  } 
     107 
    102108  const bool Unregister(const tTypeInfoWrapper &id) 
    103109  { 
     
    118124      return (it->second)(model); 
    119125    } 
    120     return TErrorPolicy<TAbstractProduct, tTypeInfoWrapper>::OnUnknownType(id); 
     126    return TUnknownKeyPolicy<TAbstractProduct, tTypeInfoWrapper>::OnUnknownType(id); 
    121127  } 
    122128 
  • patterns/factory/tFactory.h

    r17 r31  
    7373typename TIdentifier, 
    7474typename TProductCreator = tFunctor<TAbstractProduct *>, 
    75 template <typename TAbstractProduct, typename TIdentifier> class TErrorPolicy = factory::DefaultError 
     75template <typename TAbstractProduct, typename TIdentifier> class TUnknownKeyPolicy = factory::ThrowException 
    7676> 
    7777class tFactory 
     
    8888  } 
    8989 
     90  template <typename TProduct> 
     91  const bool Register(const TIdentifier &id) 
     92  { 
     93    return this->Register(id, &factory::DefaultNewCreator<TProduct>); 
     94  } 
     95 
    9096  const bool Unregister(const TIdentifier &id) 
    9197  { 
     
    100106      return (it->second)(); 
    101107    } 
    102     return TErrorPolicy<TAbstractProduct, TIdentifier>::OnUnknownType(id); 
     108    return TUnknownKeyPolicy<TAbstractProduct, TIdentifier>::OnUnknownType(id); 
    103109  } 
    104110 
  • test/test_factory_pattern.cpp

    r17 r31  
    102102}; 
    103103 
    104 tLine *CreateLine() 
    105 { 
    106   return new tLine; 
    107 } 
    108 tRectangle *CreateRectangle() 
    109 { 
    110   return new tRectangle; 
    111 } 
    112 tCircle *CreateCircle() 
    113 { 
    114   return new tCircle; 
    115 } 
    116 tEllipse *CreateEllipse() 
    117 { 
    118   return new tEllipse; 
    119 } 
    120104 
    121105tLine *CloneLine(const tShape &) 
     
    142126  std::cout << "Testing factory" << std::endl; 
    143127 
    144   tShapeFactory::GetInstance().Register("line", &CreateLine); 
    145   tShapeFactory::GetInstance().Register("rectangle", &CreateRectangle); 
    146   tShapeFactory::GetInstance().Register("circle", &CreateCircle); 
    147   tShapeFactory::GetInstance().Register("ellipse", &CreateEllipse); 
     128  tShapeFactory::GetInstance().Register<tLine>("line"); 
     129  tShapeFactory::GetInstance().Register<tRectangle>("line"); 
     130  tShapeFactory::GetInstance().Register<tRectangle>("rectangle"); 
     131  tShapeFactory::GetInstance().Register<tCircle>("circle"); 
     132  tShapeFactory::GetInstance().Register<tEllipse>("ellipse"); 
    148133 
    149134  tShape *line = tShapeFactory::GetInstance().Create("line"); 
Note: See TracChangeset for help on using the changeset viewer.