factory.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:4k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* factory.h - class declarations for the object factory */
  2. /* Copyright 1993-1998 Wind River Systems, Inc. */
  3. /*
  4. modification history
  5. --------------------
  6. 01a,05oct98,sn   wrote
  7. */
  8. #include <vxWorks.h>
  9. #include <iostream.h>
  10. #include <string>
  11. #include <typeinfo>
  12. #include <map>
  13. /* 
  14.  * object_t hierarchy
  15.  *
  16.  *             object_t
  17.  *                 |
  18.  *    +------------+------------+
  19.  *    |            |            |
  20.  *  red_t        blue_t       green_t
  21.  *
  22.  */
  23. struct object_t
  24.     {
  25.     virtual void method () {}
  26.     };
  27. struct red_t : object_t
  28.     {
  29.     };
  30. struct blue_t : object_t
  31.     {
  32.     };
  33. struct green_t : object_t
  34.     {
  35.     };
  36. /*
  37.  * object_factory_t hierarchy
  38.  *
  39.  *  
  40.  *                    object_factory_t
  41.  *                         |
  42.  *    +--------------------+--------------------+
  43.  *    |                    |                    |
  44.  *  red_factory_t      blue_factory_t     green_factory_t
  45.  */
  46. struct object_factory_t
  47.     {
  48.     virtual object_t* create () = 0;
  49.     };
  50. struct red_factory_t : object_factory_t
  51.     {
  52.     red_t* create () { return new red_t; }
  53.     };
  54. struct blue_factory_t : object_factory_t
  55.     {
  56.     blue_t* create () { return new blue_t; }
  57.     };
  58. struct green_factory_t : object_factory_t
  59.     {
  60.     green_t* create () { return new green_t; }
  61.     };
  62. /*
  63.  * registry_t<T> - a registry of objects of type T
  64.  *
  65.  * The registry maps user readable names to pointers to objects.
  66.  *
  67.  */
  68. template <class T> class registry_t
  69.     {
  70. private:
  71.     typedef map <string, T*> map_t;
  72.     map_t registry;
  73. public:
  74.     void insert (string objectName, T* pObject);
  75.     T* lookup (string objectName);
  76.     void list ();
  77.     };
  78. /* object_registry_t - a registry of objects derived from object_t */
  79. typedef registry_t <object_t> object_registry_t;
  80. /* class_registry_t - a registry of object factories ('classes') */
  81. class class_registry_t : public registry_t <object_factory_t>
  82.     {
  83. public:
  84.     object_t* create (string className) ;
  85.     };
  86.     
  87. /*
  88.  * template method definitions 
  89.  *
  90.  * It is common to put template method definitions in header
  91.  * files so that they may be instantiated whenever necessary.
  92.  *
  93.  */
  94. /******************************************************************************
  95. *
  96. * registry_t<T>::insert - register an object
  97. *
  98. * Register object pointed to by pObject under 'objectName'.
  99. *
  100. * RETURNS : N/A
  101. */
  102. template <class T> 
  103. void registry_t<T>::insert 
  104.     (
  105.     string objectName,
  106.     T* pObject
  107.     ) 
  108.     { 
  109.     registry [objectName] = pObject; 
  110.     }
  111. /******************************************************************************
  112. *
  113. * registry_t<T>::lookup - lookup an object by name
  114. *
  115. * Lookup 'objectName' in this registry and return a pointer
  116. * to the corresponding object. 
  117. *
  118. * RETURNS : a pointer to an object or NULL
  119. */
  120. template <class T> 
  121. T* registry_t<T>::lookup 
  122.     (
  123.     string objectName
  124.     ) 
  125.     {
  126.     return registry [objectName]; 
  127.     }
  128. /******************************************************************************
  129. *
  130. * registry_t<T>::list - list objects in this registry 
  131. *
  132. * RETURNS : N/A
  133. */
  134. template <class T>
  135. void registry_t<T>::list () 
  136.     {
  137.     cout << "Name t" << "Address" << endl;
  138.     cout << "=========================================================" << endl;
  139.     for (map_t::iterator i = registry.begin (); 
  140.          i != registry.end (); ++i) 
  141.         {
  142.         cout << i -> first << " t" 
  143.              << "0x" << hex << (int) i -> second << endl;
  144.         }
  145.     }
  146. /* function declarations */
  147. /* objectCreate - create an object of a given type */
  148. object_t* objectCreate (char* className, char* objectName);
  149. /* objectTypeShowByName - ascertain the type of a registered object */
  150. void objectTypeShowByName (char* objectName);
  151. /* objectRegistryShow - show contents of global object registry */
  152. void objectRegistryShow ();
  153. /* classRegistryShow - show contents of global class registry */
  154. void classRegistryShow ();