⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-wfc4.html

📁 vxworks相关论文
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><link rel="STYLESHEET" type="text/css" href="wrs.css"><title>    C++ Development   </title></head><body bgcolor="FFFFFF"><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><font face="Helvetica, sans-serif" class="sans"><h3 class="H2"><i><a name="84638">5.4  &nbsp;&nbsp;Example</a></i></h3></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84642"> </a><a href="c-wfc4.html#84646">Example&nbsp;5-2</a> exercises various C++ features including the Standard Template Library, user defined templates, Run-Time Type Identification, and exception handling. To try it out, create a project containing <b class="file">factory.cpp</b> and <b class="file">factory.h</b> and build and download <b class="file">linkedObjs.o</b>. At the shell type:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84643"></b><tt class="output">-&gt;</tt><b> testFactory</a></b></pre></dl><dd><p class="Body"><a name="84644"> </a>Full documentation on what you should except to see is given in the source code.</p></dl></dl><h4 class="EntityTitle"><a name="84646"><font face="Helvetica, sans-serif" size="-1" class="sans">Example 5-2:&nbsp;&nbsp;Code Factory Example</font></a></h4><dl class="margin"><dl class="margin"><dd><pre class="Code"><b><a name="84647">/* factory.cpp - implements an object factory */  /* Copyright 1993-1998 Wind River Systems, Inc. */  /* modification history -------------------- 01a,05oct98,sn   wrote */  /* DESCRIPTION  We implement an "object factory". The first step is to give classes human-readable names by registering them with a  "global class registry". Then objects of a named type may be  created and registered with an "object registry". </a></b><dd> <b><a name="84648">This gives us an opportunity to exercise various C++ features:  * Standard Template Library     A "map" is used as the basis for the various registries. * User defined templates     The class registry and object registry are both based on a      generic registry type. * Run Time Type Checking     We provide a function to determine the type of a registered      object using "dynamic_cast". * Exception Handling     If we attempt to cast to the "wrong" type we have to handle a      C++ exception.  We provide a C interface to facilitate easy testing from the Wind Shell.  Here is an example test run (you can run this whole test through the  function testFactory()):  -&gt; classRegistryShow Showing Class Registry ... Name    Address ========================================================= blue_t  0x6b1c7a0 green_t 0x6b1c790 red_t   0x6b1c7b0  -&gt; objectRegistryShow Showing Object Registry ... Name    Address =========================================================  -&gt; objectCreate "green_t", "bob" Creating an object called 'bob' of type 'green_t'  -&gt; objectCreate "red_t", "bill" Creating an object called 'bill' of type 'red_t'  -&gt; objectRegistryShow Showing Object Registry ... Name    Address ========================================================= bill    0x6b1abf8 bob     0x6b1ac18  -&gt; objectTypeShowByName "bob" Looking up object 'bob' Attempting to ascertain type of object at 0x6b1ac18 Attempting a dynamic_cast to red_t ... dynamic_cast threw an exception ... caught here! Attempting a dynamic_cast to blue_t ... dynamic_cast threw an exception ... caught here! Attempting a dynamic_cast to green_t ... Cast to green_t succeeded! green.  */  /* includes */ #include "factory.h"  /* locals */  /* pointer to the global class registry */ LOCAL class_registry_t* pClassRegistry;  /* pointer to the global object registry */  LOCAL object_registry_t* pObjectRegistry;  /************************************************************************* * * testFactory - an example test run * */ void testFactory () {     cout &lt;&lt; "classRegistryShow ()" &lt;&lt; endl;     classRegistryShow ();     cout &lt;&lt; "objectRegistryShow ()" &lt;&lt; endl;     objectRegistryShow ();     cout &lt;&lt; "objectCreate (\"green_t\", \"bob\")" &lt;&lt; endl;     objectCreate ("green_t", "bob");     cout &lt;&lt; "objectCreate (\"red_t\", \"bill\")" &lt;&lt; endl;     objectCreate ("red_t", "bill");     cout &lt;&lt; "objectRegistryShow ()" &lt;&lt; endl;     objectRegistryShow ();     cout &lt;&lt; "objectTypeShowByName (\"bob\")" &lt;&lt; endl;     objectTypeShowByName ("bob"); }  /************************************************************************* * * class_registry_t::create - create an object of type className * * Look up 'className' in this registry. If it exists then * create an object of this type by using the registered class factory; * otherwise return NULL.   * * RETURNS : pointer to new object of type className or NULL. */  object_t* class_registry_t::create      (     string className     )      {      object_factory_t* pFactory = lookup (className);     if (pFactory != NULL)         {         return lookup (className)-&gt; create ();          }     else         {         cout &lt;&lt; "No such class in Class Registry. " &lt;&lt; endl;         return NULL;         }     }  /************************************************************************* * * classRegistryGet - get a reference to the global class registry *  * Create and populate a new class registry if necessary. * * RETURNS : a reference to the global class registry */  LOCAL class_registry_t&amp; classRegistryGet ()     {     if (pClassRegistry == NULL)         {         pClassRegistry = new class_registry_t;         pClassRegistry -&gt; insert ("red_t", new red_factory_t);         pClassRegistry -&gt; insert ("blue_t", new blue_factory_t);         pClassRegistry -&gt; insert ("green_t", new green_factory_t);         }     return *pClassRegistry;     }  /************************************************************************* * * objectRegistryGet - get a reference to the global object registry * * Create a new object registry if necessary. * * RETURNS : a reference to the global object registry */  LOCAL object_registry_t&amp; objectRegistryGet ()     {     if (pObjectRegistry == NULL)         {         pObjectRegistry = new object_registry_t;         }     return *pObjectRegistry;     }  /************************************************************************* * * objectCreate - create an object of a given type * * Use the class factory registered in the global class registry * under 'className' to create a new object. Register this object * in the global object registry under 'object name'. * * RETURNS : object of type className */  object_t* objectCreate      (     char* className,      char* objectName     )     {     cout &lt;&lt; "Creating an object called '" &lt;&lt; objectName &lt;&lt; "'"           &lt;&lt; " of type '" &lt;&lt; className &lt;&lt; "'" &lt;&lt; endl;     object_t* pObject = classRegistryGet().create (className);     if (pObject != NULL)         {         objectRegistryGet().insert(objectName, pObject);         }     else         {         cout &lt;&lt; "Could not create object. Sorry. " &lt;&lt; endl;         }     return pObject;     }  /************************************************************************* * * isRed    * isBlue   - is anObject a reference to an object of the specified type? * isGreen  * * Try a dynamic_cast. If this succeeds then return TRUE. If it fails * then catch the resulting exception and return FALSE. * * RETURNS : TRUE or FALSE */  /* isRed */  LOCAL BOOL isRed (object_t&amp; anObject)     {     try         {         cout &lt;&lt; "Attempting a dynamic_cast to red_t ..." &lt;&lt; endl;         dynamic_cast&lt;red_t&amp;&gt; (anObject);         cout &lt;&lt; "Cast to red_t succeeded!" &lt;&lt; endl;         return TRUE;         }     catch (exception)         {         cout &lt;&lt; "dynamic_cast threw an exception ... caught here!" &lt;&lt; endl;         return FALSE;         }         }  /* isBlue */</a></b><dd> <b><a name="84649">LOCAL BOOL isBlue (object_t&amp; anObject)     {     try         {         cout &lt;&lt; "Attempting a dynamic_cast to blue_t ..." &lt;&lt; endl;         dynamic_cast&lt;blue_t&amp;&gt; (anObject);         cout &lt;&lt; "Cast to blue_t succeeded!" &lt;&lt; endl;         return TRUE;         }     catch (exception)         {         cout &lt;&lt; "dynamic_cast threw an exception ... caught here!" &lt;&lt; endl;         return FALSE;         }         }  /* isGreen */  LOCAL BOOL isGreen (object_t&amp; anObject)     {     try         {         cout &lt;&lt; "Attempting a dynamic_cast to green_t ..." &lt;&lt; endl;         dynamic_cast&lt;green_t&amp;&gt; (anObject);         cout &lt;&lt; "Cast to green_t succeeded!" &lt;&lt; endl;         return TRUE;         }     catch (exception)         { 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -