Changeset 724564


Ignore:
Timestamp:
Jan 4, 2011, 5:15:14 PM (15 years ago)
Author:
Frederik Heber <heber@…>
Children:
1afcbe
Parents:
746ff1
git-author:
Frederik Heber <heber@…> (01/04/11 15:57:33)
git-committer:
Frederik Heber <heber@…> (01/04/11 17:15:14)
Message:

Factory know has an additional type table and stubs have been refactored.

  • CommonStub.?pp contains basic classes with a int counter inside.
  • Library version is now 2:1:0, API is 1.0.3.
Files:
1 added
10 edited
1 moved

Legend:

Unmodified
Added
Removed
  • configure.ac

    r746ff1 r724564  
    33
    44AC_PREREQ([2.65])
    5 AC_INIT([CodePatterns], [1.0.2], [heber@ins.uni-bonn.de], [codepatterns], [http://trac.ins.uni-bonn.de/projects/CodePatterns/])
     5AC_INIT([CodePatterns], [1.0.3], [heber@ins.uni-bonn.de], [codepatterns], [http://trac.ins.uni-bonn.de/projects/CodePatterns/])
    66AC_CONFIG_AUX_DIR(config)
    77AC_CONFIG_SRCDIR([src/Patterns/Singleton_impl.hpp])
     
    2525# refer to the libtool manual, section "Updating library version information":
    2626# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
    27 AC_SUBST([CODEPATTERNS_SO_VERSION], [2:0:0])
    28 AC_SUBST([CODEPATTERNS_API_VERSION], [1.0.2])
     27AC_SUBST([CODEPATTERNS_SO_VERSION], [2:1:0])
     28AC_SUBST([CODEPATTERNS_API_VERSION], [1.0.3])
    2929
    3030# Checks for libraries.
  • src/Patterns/Factory.hpp

    r746ff1 r724564  
    1515
    1616#include <map>
     17#include <typeinfo>
     18
     19#include "Assert.hpp"
    1720
    1821#include "Creator.hpp"
     
    285288  }
    286289
     290  /** Getter for desired type of product.
     291   *
     292   * @param type_info object of the desired type
     293   * @return reference to copy of current type product or NULL if type mismatch
     294   */
     295  T* getProduct(const std::type_info &instance_type_info) const
     296  {
     297    ASSERT(types.count(instance_type_info.name()) != 0,
     298        "Factory<"+toString(typeid(T).name())+">::getProduct() - type info name "+instance_type_info.name()+" is not registered.");
     299    return PrototypeTable[ types[instance_type_info.name()] ]->create();
     300  }
    287301
    288302  /** Getter for current type of product.
     
    330344  void FillEnumTable();
    331345
     346  typedef std::map<
     347      std::string,
     348      TypeList
     349      > TypeMap;
    332350  typedef std::map<
    333351      std::string,
     
    344362
    345363  static TypeList currenttype;
     364  static TypeMap types;
    346365  static EnumMap enums;
    347366  static InstanceTable PrototypeTable;
     
    350369
    351370template <class T> typename Factory<T>::TypeList Factory<T>::currenttype = (typename Factory<T>::TypeList)0;
     371template <class T> typename Factory<T>::TypeMap Factory<T>::types;
    352372template <class T> typename Factory<T>::EnumMap Factory<T>::enums;
    353373template <class T> typename Factory<T>::NameMap Factory<T>::names;
  • src/Patterns/Factory_impl.hpp

    r746ff1 r724564  
    1616#include <boost/preprocessor/punctuation/comma.hpp>
    1717#include <boost/preprocessor/punctuation/comma_if.hpp>
     18#include <boost/preprocessor/punctuation/paren.hpp>
    1819#include <boost/preprocessor/seq/elem.hpp>
    1920#include <boost/preprocessor/seq/size.hpp>
     
    5253#endif
    5354
    54 /** Functions that allows to print a given seq of elements in the way of
    55  *  std::map from strings to enums.
    56  *
    57  * e.g. let "seq" be defined as
    58  * #define seq (one)(two)(three)(four)
    59  *
    60  * then we use
    61  * #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_string_enum_map(seqsize, n, seq, EnumMap, name_space)
    62  * #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_SEQ_SIZE(seq)-1 )
    63  * #include BOOST_PP_LOCAL_ITERATE()
    64  *
    65  * which expands by the preprocessor to:
    66  * EnumMap["one"] = test:: one;
    67  * EnumMap["two"] = test:: two;
    68  * EnumMap["three"] = test:: three;
    69  * EnumMap["four"] = test:: four;
    70 
    71  */
    72 #define seqitems_as_string_enum_map(z,n,seq_with_elements, map, name_space) \
    73   map [BOOST_PP_STRINGIZE( \
    74   BOOST_PP_SEQ_ELEM(n, seq_with_elements) \
    75   )] = name_space BOOST_PP_SEQ_ELEM(n, seq_with_elements) \
    76   ;
    7755
    7856/** Functions that allows to print a given seq of elements in the way of
     
    9270 * EnumMap[three] = test::three;
    9371 * EnumMap[four] = test::four;
    94 
     72 *
    9573 */
    9674#define seqitems_as_enum_key_map(z,n,seq_with_elements, map, keytype, name_space, suffix) \
     
    9977  ] = keytype< name_space BOOST_PP_SEQ_ELEM(n, seq_with_elements) suffix > ();
    10078
     79/** Functions that allows to print a given seq of elements in the way of
     80 *  std::map from strings to enums.
     81 *
     82 * e.g. let "seq" be defined as
     83 * #define seq (one)(two)(three)(four)
     84 *
     85 * then we use
     86 * #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_value_map(seqsize, n, seq, EnumMap, typid BOOST_PP_LPAREN() super:: , BOOST_PP_RPAREN().name() , test::)
     87 * #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_SEQ_SIZE(seq)-1 )
     88 * #include BOOST_PP_LOCAL_ITERATE()
     89 *
     90 * which expands by the preprocessor to:
     91 * EnumMap[ typeid ( super:: one ) .name() ] = test::one;
     92 * EnumMap[ typeid ( super:: two ) .name() ] = test::two;
     93 * EnumMap[ typeid ( super:: three ) .name() ] = test::three;
     94 * EnumMap[ typeid ( super:: four ) .name() ] = test::four;
     95 *
     96 * or we use
     97 * #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_value_map(seqsize, n, seq, EnumMap, BOOST_PP_STRINGIZE BOOST_PP_LPAREN() , BOOST_PP_RPAREN() , test::)
     98 * #define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_SEQ_SIZE(seq)-1 )
     99 * #include BOOST_PP_LOCAL_ITERATE()
     100 *
     101 * which expands by the preprocessor to:
     102 * EnumMap[ "one" ] = test:: one;
     103 * EnumMap[ "two" ] = test:: two;
     104 * EnumMap[ "three" ] = test:: three;
     105 * EnumMap[ "four" ] = test:: four;
     106 */
     107#define seqitems_as_enum_value_map(z,n,seq_with_elements, map, type_specifier, type_suffix, enum_name_space) \
     108  map [ type_specifier  \
     109  BOOST_PP_SEQ_ELEM(n, seq_with_elements) \
     110  type_suffix \
     111  ] = enum_name_space BOOST_PP_SEQ_ELEM(n, seq_with_elements);
     112
    101113
    102114template <>
     
    104116{
    105117  // insert all known (enum, string) keys
    106 #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_string_enum_map(~, n, type_seq, enums, FactoryTypeList<Abstract_Interface_Class>::)
     118#define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_value_map(~, n, type_seq, enums, BOOST_PP_STRINGIZE BOOST_PP_LPAREN() , BOOST_PP_RPAREN() , FactoryTypeList<Abstract_Interface_Class>::)
     119#define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_SEQ_SIZE(type_seq)-1 )
     120#include BOOST_PP_LOCAL_ITERATE()
     121
     122  // insert all known (type, enum) keys
     123#define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_value_map(~, n, type_seq, types, typeid BOOST_PP_LPAREN() type_name_space , type_name_space_suffix BOOST_PP_RPAREN() .name(), FactoryTypeList<Abstract_Interface_Class>::)
    107124#define BOOST_PP_LOCAL_LIMITS  (0, BOOST_PP_SEQ_SIZE(type_seq)-1 )
    108125#include BOOST_PP_LOCAL_ITERATE()
  • src/Patterns/unittests/CreatorUnitTest.cpp

    r746ff1 r724564  
    2525
    2626#include "Creator.hpp"
     27#include "stubs/CommonStub.hpp"
    2728#include "stubs/CreatorStub.hpp"
    2829
  • src/Patterns/unittests/FactoryUnitTest.cpp

    r746ff1 r724564  
    2222#include "FactoryUnitTest.hpp"
    2323
     24#include "stubs/CommonStub.hpp"
    2425#include "stubs/FactoryStub.hpp"
    2526
     
    6869void FactoryTest::setUp()
    6970{
    70   FactoryStub::getInstance();
     71  rndA =
     72      FactoryStub::getInstance().
     73      PrototypeTable[FactoryStub::Aclass]->create();
     74  rndB =
     75      FactoryStub::getInstance().
     76      PrototypeTable[FactoryStub::Bclass]->create();
    7177}
    7278
    7379void FactoryTest::tearDown()
    7480{
     81  delete rndA;
     82  delete rndB;
    7583  FactoryStub::purgeInstance();
    7684}
     
    93101  }
    94102
    95   // check one of the distributions in the table
    96   ICreatorStub * rnd =
    97       FactoryStub::getInstance().
    98       PrototypeTable[FactoryStub::Aclass]->create();
     103  // check distributions in the table
    99104  CPPUNIT_ASSERT_EQUAL(
    100105      std::string(typeid(teststubs::Aclass).name()),
    101       rnd->name()
     106      rndA->name()
    102107  );
    103   delete rnd;
     108  CPPUNIT_ASSERT_EQUAL(
     109      std::string(typeid(teststubs::Bclass).name()),
     110      rndB->name()
     111  );
    104112}
     113
     114
     115void FactoryTest::getProductEnumTest()
     116{
     117  ICreatorStub * rndA_1 =
     118      FactoryStub::getInstance().getProduct(FactoryStub::Aclass);
     119  CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
     120  delete rndA_1;
     121}
     122
     123void FactoryTest::getProductNameTest()
     124{
     125  ICreatorStub * rndA_1 =
     126      FactoryStub::getInstance().getProduct(std::string("Aclass"));
     127  CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
     128  delete rndA_1;
     129}
     130
     131void FactoryTest::getProductTypeTest()
     132{
     133  ICreatorStub * rndA_1 =
     134      FactoryStub::getInstance().getProduct( typeid(teststubs::Aclass) );
     135  CPPUNIT_ASSERT( typeid(*rndA) == typeid(*rndA_1) );
     136  delete rndA_1;
     137}
  • src/Patterns/unittests/FactoryUnitTest.hpp

    r746ff1 r724564  
    1414#endif
    1515
     16class ICreatorStub;
    1617
    1718#include <cppunit/extensions/HelperMacros.h>
     
    2122  CPPUNIT_TEST_SUITE( FactoryTest );
    2223  CPPUNIT_TEST ( DistributionTest );
     24  CPPUNIT_TEST ( getProductEnumTest );
     25  CPPUNIT_TEST ( getProductNameTest );
     26  CPPUNIT_TEST ( getProductTypeTest );
    2327  CPPUNIT_TEST_SUITE_END();
    2428
     
    2832
    2933  void DistributionTest();
     34  void getProductEnumTest();
     35  void getProductNameTest();
     36  void getProductTypeTest();
    3037
    3138private:
     39  ICreatorStub * rndA;
     40  ICreatorStub * rndB;
    3241};
    3342
  • src/Patterns/unittests/Makefile.am

    r746ff1 r724564  
    3737        CreatorUnitTest.hpp \
    3838        ../Creator.hpp \
    39         stubs/CreatorStub.hpp \
    40         stubs/CreatorStub.cpp
     39        stubs/CommonStub.cpp \
     40        stubs/CommonStub.hpp \
     41        stubs/CreatorStub.hpp
    4142CreatorUnitTest_LDADD = $(TESTLIBS)
    4243
     
    4445        FactoryUnitTest.cpp \
    4546        FactoryUnitTest.hpp \
     47        stubs/CommonStub.cpp \
     48        stubs/CommonStub.hpp \
    4649        stubs/CreatorStub.hpp \
    47         stubs/CreatorStub.cpp \
    4850        stubs/FactoryStub.hpp \
    4951        stubs/FactoryStub.cpp \
  • src/Patterns/unittests/stubs/CommonStub.cpp

    r746ff1 r724564  
    77
    88/*
    9  * CreatorStub.cpp
     9 * CommonStub.cpp
    1010 *
    1111 *  Created on: Jan 4, 2011
     
    1313 */
    1414
     15// include config.h
     16#ifdef HAVE_CONFIG_H
     17#include <config.h>
     18#endif
     19
     20#include "CommonStub.hpp"
     21
    1522namespace teststubs {
    16   class Aclass {};
    17   class Bclass {};
     23  Aclass::Aclass() :
     24    counter(0)
     25  {};
     26
     27  Aclass::~Aclass() {};
     28
     29  Bclass::Bclass() :
     30    counter(256)
     31  {};
     32
     33  Bclass::~Bclass() {};
    1834};
  • src/Patterns/unittests/stubs/CreatorStub.hpp

    r746ff1 r724564  
    88#ifndef CREATORSTUB_HPP_
    99#define CREATORSTUB_HPP_
     10
     11// include config.h
     12#ifdef HAVE_CONFIG_H
     13#include <config.h>
     14#endif
    1015
    1116#include <typeinfo>
     
    1924  virtual int getcount() = 0;
    2025  virtual std::string name() = 0;
    21 };
    22 
    23 namespace teststubs {
    24   class Aclass
    25   {
    26   public:
    27     Aclass() :
    28       counter(0)
    29     {};
    30     ~Aclass() {};
    31 
    32     int counter;
    33   };
    34 
    35   class Bclass
    36   {
    37   public:
    38     Bclass() :
    39       counter(256)
    40     {};
    41     ~Bclass() {};
    42 
    43     int counter;
    44   };
    4526};
    4627
  • src/Patterns/unittests/stubs/FactoryStub.cpp

    r746ff1 r724564  
    1313 */
    1414
     15// include config.h
     16#ifdef HAVE_CONFIG_H
     17#include <config.h>
     18#endif
     19
    1520#include "Singleton_impl.hpp"
    1621
     22#include "CommonStub.hpp"
    1723#include "FactoryStub.hpp"
    1824
  • src/Patterns/unittests/stubs/FactoryStub.hpp

    r746ff1 r724564  
    99#define FACTORYSTUB_HPP_
    1010
     11// include config.h
     12#ifdef HAVE_CONFIG_H
     13#include <config.h>
     14#endif
     15
    1116#include "CreatorStub.hpp"
    1217
    13 // has to be appear BEFORE Factory.hpp is included!
     18// triple has to be appear BEFORE Factory.hpp is included!
    1419#include "FactoryStub.def"
    1520#include "FactoryTypeList.hpp"
     21#include "FactoryStub.undef"
     22
    1623#include "Factory.hpp"
    1724
     
    2734  virtual ~FactoryStub();
    2835};
    29 #include "FactoryStub.undef"
    3036
    3137#endif /* FACTORYSTUB_HPP_ */
Note: See TracChangeset for help on using the changeset viewer.