validatedobject.htm

来自「The goal of this library is to make ODBC」· HTM 代码 · 共 381 行 · 第 1/2 页

HTM
381
字号
<html>


<head>
<style>
CODE {COLOR: #990000;}
.code{COLOR: #990000}
.codeComment{COLOR: #008000}
.codeHighlight{BACKGROUND-COLOR: #FFFF00}
.codeFileName{FONT-WEIGHT: bold;}
</style>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="Author" content="Mike Gradman">
<meta name="KeyWords"
content="DTL, Oracle, ODBC, database API, C++, Template Library">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<!--
  -- Copyright 2000
  -- Michael Gradman & Corwin Joy
  --
  -- Permission to use, copy, modify, distribute and sell this software
  -- and its documentation for any purpose is hereby granted without fee,
  -- provided that the above copyright notice appears in all copies and
  -- that both that copyright notice and this permission notice appear
  -- in supporting documentation.  Corwin Joy & Michael Gradman make no
  -- representations about the suitability of this software for any
  -- purpose.  It is provided "as is" without express or implied warranty.
  --
  --
  -- Copyright (c) 1996-1999
  -- Silicon Graphics Computer Systems, Inc.
  --
  -- Permission to use, copy, modify, distribute and sell this software
  -- and its documentation for any purpose is hereby granted without fee,
  -- provided that the above copyright notice appears in all copies and
  -- that both that copyright notice and this permission notice appear
  -- in supporting documentation.  Silicon Graphics makes no
  -- representations about the suitability of this software for any
  -- purpose.  It is provided "as is" without express or implied warranty.
  --
  -- Copyright (c) 1994
  -- Hewlett-Packard Company
  --
  -- Permission to use, copy, modify, distribute and sell this software
  -- and its documentation for any purpose is hereby granted without fee,
  -- provided that the above copyright notice appears in all copies and
  -- that both that copyright notice and this permission notice appear
  -- in supporting documentation.  Hewlett-Packard Company makes no
  -- representations about the suitability of this software for any
  -- purpose.  It is provided "as is" without express or implied warranty.
  --
  -->
<!-- Generated by htmldoc -->
<title>ValidatedObject</title>
</head>

<body bgcolor="#FFFFFF" text="#000000" link="#0000EE"
vlink="#551A8B" alink="#FF0000">

<p><font size="6" face="Bookman Old Style"><em><strong><u>dtl</u></strong></em></font></p>

<p><img src="stat.gif" width="6" height="6"> <!--end header--> <br>
</p>
<h1>ValidatedObject</h1>



















<table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
        <td><img src="utilities.gif" width="194" height="38"></td>
        <td align="right"><img src="type.gif" width="194"
        height="39"></td>
    </tr>
    <tr>
        <td valign="top"><b>Category</b>: utilities</td>
        <td align="right" valign="top"><b>Component type</b>:
        type</td>
    </tr>
</table>
<h3>Description</h3>

<p><font size="2" face="Courier New">ValidatedObject's </font>maintain
state information that allows them to determine whether they are
in a valid state or have been corrupted. Once an operation is
performed in one of these self-validating classes that detects
that the object is no longer in a valid state, the operation will
throw a <tt>ValidityException. </tt>To get validation facilties
for an object, simply derive from <font size="2"
face="Courier New">ValidatedObject. </font>You can choose whether
a method needs to validate the object or not by calling <font
size="2" face="Courier New">validate()</font>at the beginning of
the method if you wish to validate the object's state. To define
the critieria used to define whether the object is in a valid
state or not, override the <font size="2" face="Courier New">valid()
</font>method. Often, in your <font size="2" face="Courier New">valid()
</font>method, you may wish to test whether the object is already
valid to save some work as no further testing is required if the
object is found to already be invalid. Unless you override the <font
size="2" face="Courier New">revalidate() </font>method, once an
object becomes invalid, it remains invalid permanently. Your <font
size="2" face="Courier New">revalidate() </font>method tries to
reconcile the state of the object, doing whatever processing or
cleanup it needs to return the object to a valid state. This
reconciling of the object can either always succeed, sometimes
succeed (and either throwing <tt>ValidityException </tt>or doing
something else in case of failure), or never succeed (also
achievable just by not overriding <font size="2"
face="Courier New">revalidate(), </font>which will throw <tt>ValidityException</tt>).</p>

<h3>Definition</h3>

<p>Defined in the <font size="2" face="Courier New">validate.h</font><font
size="1" face="Courier New"> </font>header file. </p>

<h3>Example: A validated BankAccount object</h3>

<p><code>class MoneyAccount : public
ValidatedObject<br>
{ <br>
private:<br>
&nbsp;&nbsp;&nbsp;<span class="codeComment">// if less than 0 money left, we're broke --&gt;
object corrupted!</span><br>
&nbsp;&nbsp;&nbsp;<span class="codeComment">// (must then see MoneyAccount manager to
revalidate() or go to jail:P)</span><br>
&nbsp;&nbsp;&nbsp;<span class="codeComment">// (in this example though, once you're in
debt, your account is frozen!)</span><br>
&nbsp;&nbsp;&nbsp;double balance;<br>
public:<br>
&nbsp;&nbsp;&nbsp;MoneyAccount(unsigned double initialDeposit) :
ValidatedObject(), balance(initialDeposit) { } <br>
<br>
&nbsp;&nbsp;&nbsp;<span class="codeComment">// should be able to see balance even if
account is frozen!</span><br>
&nbsp;&nbsp;&nbsp;double GetBalance() { return balance; }<br>
<br>
&nbsp;&nbsp;&nbsp;<span class="codeComment">// account only valid if not in debt!</span><br>
&nbsp;&nbsp;&nbsp;virtual bool valid()<br>
&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">// if we know the object is
invalid from a previous error,</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="codeComment">// object is still invalid (in
this case, account still frozen)</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!ValidatedObject::valid())<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return
false;<br>
<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span class="codeComment">// freeze account by invalidating
object if in debt</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (balance &lt; 0)}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invalidate();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return
false;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return
true;<br>
&nbsp;&nbsp;&nbsp;}<br>
<br>
&nbsp;&nbsp;&nbsp;<span class="codeComment">// these operations work only if the account is
not frozen</span><br>
&nbsp;&nbsp;&nbsp;void deposit(unsigned double amt)<br>
&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate(); <span class="codeComment">// call to
validate will throw if object not valid</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;balance += amt;<br>
&nbsp;&nbsp;&nbsp;}<br>
<br>
&nbsp;&nbsp;&nbsp;double withdraw(unsigned double amt)<br>
&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate(); <span class="codeComment">// call to
validate will throw if object not valid</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<span class="codeComment">// can withdraw up to how much we

⌨️ 快捷键说明

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