📄 c-wfc4.html
字号:
cout << "dynamic_cast threw an exception ... caught here!" << endl; return FALSE; } } /************************************************************************* * * objectTypeShow - ascertain the type of an object * * Use dynamic type checking to determine the type of an object. * * RETURNS : N/A */ LOCAL void objectTypeShow (object_t* pObject) { cout << "Attempting to ascertain type of object at " << "0x" << hex << (int) pObject << endl; if (isRed (*pObject)) { cout << "red." << endl; } else if (isBlue (*pObject)) { cout << "blue." << endl; } else if (isGreen (*pObject)) { cout << "green." << endl; } } /************************************************************************* * * objectTypeShowByName - ascertain the type of a registered object * * Lookup 'objectName' in the global object registry and * print the type of the associated object. * * RETURNS : N/A */ void objectTypeShowByName ( char* objectName ) { cout << "Looking up object '" << objectName << "'" << endl; object_t *pObject = objectRegistryGet ().lookup (objectName); if (pObject != NULL) { objectTypeShow (pObject); } else { cout << "No such object in the Object Registry." << endl; } } /************************************************************************* * * objectRegistryShow - show contents of global object registry * * RETURNS : N/A */ void objectRegistryShow () { cout << "Showing Object Registry ..." << endl; objectRegistryGet ().list (); } /************************************************************************* * * classRegistryShow - show contents of global class registry * * RETURNS : N/A */</a></b><dd> <b><a name="84650">void classRegistryShow () { cout << "Showing Class Registry ..." << endl; classRegistryGet ().list (); }</a></b></pre></dl><dl class="margin"><dd><hr class="Line"></dl><dl class="margin"><dd><pre class="Code"><b><a name="84652">/* factory.h - class declarations for the object factory */ /* Copyright 1993-1998 Wind River Systems, Inc. */ /* modification history -------------------- 01a,05oct98,sn wrote */ #include <vxWorks.h> #include <iostream.h> #include <string> #include <typeinfo> #include <map> /* * object_t hierarchy * * object_t * | * +------------+------------+ * | | | * red_t blue_t green_t * */ struct object_t { virtual void method () {} }; struct red_t : object_t { }; struct blue_t : object_t { }; struct green_t : object_t { };</a></b><dd> <b><a name="84653">/* * object_factory_t hierarchy * * * object_factory_t * | * +--------------------+--------------------+ * | | | * red_factory_t blue_factory_t green_factory_t */ struct object_factory_t { virtual object_t* create () = 0; }; struct red_factory_t : object_factory_t { red_t* create () { return new red_t; } }; struct blue_factory_t : object_factory_t { blue_t* create () { return new blue_t; } }; struct green_factory_t : object_factory_t { green_t* create () { return new green_t; } }; /* * registry_t<T> - a registry of objects of type T * * The registry maps user readable names to pointers to objects. * */ template <class T> class registry_t { private: typedef map <string, T*> map_t; map_t registry; public: void insert (string objectName, T* pObject); T* lookup (string objectName); void list (); }; /* object_registry_t - a registry of objects derived from object_t */ typedef registry_t <object_t> object_registry_t;</a></b><dd> <b><a name="84654">/* class_registry_t - a registry of object factories ('classes') */ class class_registry_t : public registry_t <object_factory_t> { public: object_t* create (string className) ; }; /* * template method definitions * * It is common to put template method definitions in header * files so that they may be instantiated whenever necessary. * */ /************************************************************************* * * registry_t<T>::insert - register an object * * Register object pointed to by pObject under 'objectName'. * * RETURNS : N/A */ template <class T> void registry_t<T>::insert ( string objectName, T* pObject ) { registry [objectName] = pObject; } /************************************************************************* * * registry_t<T>::lookup - lookup an object by name * * Lookup 'objectName' in this registry and return a pointer * to the corresponding object. * * RETURNS : a pointer to an object or NULL */ template <class T> T* registry_t<T>::lookup ( string objectName ) { return registry [objectName]; }</a></b><dd> <b><a name="84655">/************************************************************************* * * registry_t<T>::list - list objects in this registry * * RETURNS : N/A */ template <class T> void registry_t<T>::list () { cout << "Name \t" << "Address" << endl; cout << "====================================================" << endl; for (map_t::iterator i = registry.begin (); i != registry.end (); ++i) { cout << i -> first << " \t" << "0x" << hex << (int) i -> second << endl; } } /* function declarations */ /* objectCreate - create an object of a given type */ object_t* objectCreate (char* className, char* objectName); /* objectTypeShowByName - ascertain the type of a registered object */ void objectTypeShowByName (char* objectName); /* objectRegistryShow - show contents of global object registry */ void objectRegistryShow (); /* classRegistryShow - show contents of global class registry */ void classRegistryShow ();</a></b></pre></dl></dl><a name="foot"><hr></a><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="GuideIX.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="c-wfc.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="c-wfc3.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-wfc5.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p></body></html><!---by WRS Documentation (), Wind River Systems, Inc. conversion tool: Quadralay WebWorks Publisher 4.0.11 template: CSS Template, Jan 1998 - Jefro --->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -