intro3.htm
来自「ST20 Embedded Toolset R2.0.5用于开发基于ST20芯片」· HTM 代码 · 共 127 行
HTM
127 行
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Quadralay WebWorks Publisher Professional Edition 6.0.5">
<meta name="TEMPLATEBASE" content="book_html">
<meta name="LASTUPDATED" content="12/01/03 14:13:56">
<title>1.2 Classes and objects</title>
<STYLE TYPE="text/css">
<!--
span.Signal { text-transform: uppercase; font-family: Verdana }
-->
</STYLE>
</head>
<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
background="images/backgrnd.gif">
<p><img src="images/stlogo.gif" width="106" height="83" align="left"
alt="logo here!"> </p>
<table width="331" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td><a href="os20toc.htm"><img src="images/navtoc.gif" width="84" height="23"
border="0" alt="TOC"> </a></td>
<td><a href="intro2.htm"><img src="images/navprev.gif" width="80" height="23"
border="0" alt="PREV"> </a></td>
<td><a href="intro4.htm"><img src="images/navnext.gif" width="83" height="23"
border="0" alt="NEXT"> </a></td>
<td><a href="os20ix.htm"><img src="images/navidx.gif" width="84" height="23"
border="0" alt="INDEX"> </a></td>
</tr>
</table>
<p><br clear="all">
</p>
<hr align="left">
<blockquote>
<h2>
<a name="1204742"> </a><font color="#003366" face="Verdana, Arial, Helvetica, sans-serif">1.2 Classes and objects</font>
</h2><hr>
<p>
<a name="1204746"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">OS20 uses an object-oriented style of programming. This will be familiar to many people from C++, however it is useful to understand how this has been applied to OS20, and how it has been implemented in the C language.</font>
</p>
<p>
<a name="1204753"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">Each of the major services of OS20 is represented by a class, that is:</font>
</p>
<ul>
<p> <font size=2 face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204754"> </a>memory partitions,</font></p>
<p> <font size=2 face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204755"> </a>tasks,</font></p>
<p> <font size=2 face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204756"> </a>semaphores,</font></p>
<p> <font size=2 face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204757"> </a>message queues,</font></p>
<p> <font size=2 face="Verdana, Arial, Helvetica, sans-serif"><li ><a name="1204758"> </a>channels.</font></p>
</ul>
<p>
<a name="1204759"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">A class is a purely abstract concept, which describes a collection of data items and a list of operations which can be performed on it.</font>
</p>
<p>
<a name="1204760"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">An object represents a concrete instance of a particular class, and so consists of a data structure in memory which describes the current state of the object, together with information which describes how operations which are applied to that object affect it and the rest of the system.</font>
</p>
<p>
<a name="1204764"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">For many classes within OS20, there are different flavors. For example, the semaphore class has FIFO and priority flavors. When a particular object is created, the required flavor must be specified by using a qualifier on the object creation function, and that is then fixed for the lifetime of that object. All the operations specified by a particular class can be applied to all objects of that class, however, how they behave may depend on the flavor of that class. So the exact behavior of <font size=2 face=Courier><strong>semaphore_wait()</strong></font> depends on whether it is applied to a FIFO or priority semaphore object.</font>
</p>
<p>
<a name="1205938"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">Once an object has been created, all the data which represents that object is encapsulated within it. Functions are provided to modify or retrieve this data.</font>
</p>
<p>
<a name="1206494"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif"><b>Caution:</b></font>
<br>
<dl><dl> <dl>
<dt> <a name="1206505"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">The internal layout of any of the structure should not be referenced directly. This changes between implementations and releases, although the size of the structure does not change.</font>
</dl>
</dl></dl>
<p>
<a name="1205945"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">To provide this abstraction within OS20 using only standard C language features, most functions which operate on an object take the address of the object as their first parameter. This provides a level of type checking at compile time, for example, to ensure that a message queue operation is not applied to a semaphore. The only functions which are applied to an object, and which do not take the address of the object as a first parameter are those where the object in question can be inferred. For example, when an operation can only be applied to the current task, there is no need to specify its address.</font>
</p>
<h3>
<a name="1204776"> </a><font color="#003366" face="Verdana, Arial, Helvetica, sans-serif">1.2.1 Object lifetime</font>
</h3>
<p>
<a name="1204778"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">All objects can be created using one of two functions:</font>
</p>
<a name="1204779"> </a><font size=2 face=Courier><strong><font size=2 face=Courier><strong><em>class</em></strong></font>_create<br></strong></font>
<a name="1204780"> </a><font size=2 face=Courier><strong><font size=2 face=Courier><strong><em>class</em></strong></font>_init<br></strong></font>
<p>
<a name="1204781"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">Normally the <font size=2 face=Courier><strong><em>class</em></strong></font><font size=2 face=Courier><strong>_create</strong></font> version of the call can be used. This allocates whatever memory is required to store the object, and returns a pointer to the object which can then be used in all subsequent operations on that object.</font>
</p>
<p>
<a name="1204782"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">However, if it is necessary to build a system with no dynamic memory allocation features or to have more control over the memory which is allocated, then the <font size=2 face=Courier><strong><em>class</em></strong></font><font size=2 face=Courier><strong>_init</strong></font> calls can be used. This leaves memory allocation up to the user, allowing a completely static system to be created if required. For <font size=2 face=Courier><strong><em>class</em></strong></font><font size=2 face=Courier><strong>_init</strong></font> calls, the user must provide pointers to the data structures, and OS20 uses these data structures instead of allocating them itself.</font>
</p>
<p>
<a name="1204786"> </a><font size=2 face="Verdana, Arial, Helvetica, sans-serif">When using <font size=2 face=Courier><strong><em>class</em></strong></font><font size=2 face=Courier><strong>_create</strong></font> calls, the memory for the object structure is normally allocated from the system partition (the one exception to this is that <font size=2 face=Courier><strong>tdesc_t</strong></font> structures are allocated from the internal partition). Thus the partitions must be initialized before any <font size=2 face=Courier><strong><em>class</em></strong></font><font size=2 face=Courier><strong>_create</strong></font> calls are made. Normally this is done automatically as described in <a href="getst.htm#1204411"><font color="#0000ff" face="Verdana, Arial, Helvetica, sans-serif"><i>Chapter
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?