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

📄 ch08.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT>61: };</TT><TT>62: 63: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 8.9. BankImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>  1: // BankImpl.cpp</TT><TT>  2: </TT><TT>  3: #include &quot;BankImpl.h&quot;</TT><TT>  4: </TT><TT>  5: #include &lt;time.h&gt;</TT><TT>  6: #include &lt;string.h&gt;</TT><TT>  7: #include &lt;iostream.h&gt;</TT><TT>  8: #include &lt;algorithm&gt;</TT><TT>  9: #include &lt;functional&gt;</TT><TT> 10: </TT><TT> 11: #include &quot;SavingsAccountImpl.h&quot;</TT><TT> 12: #include &quot;CheckingAccountImpl.h&quot;</TT><B><TT> 13: #include &quot;ATMCardImpl.h&quot;</TT></B><TT> 14: </TT><TT> 15: extern CORBA::BOA_var boa;</TT><TT> 16: </TT><TT> 17: // STL-derived unary function which returns TRUE if Accounts are</TT><TT> 18: // equal.</TT><TT> 19: class IsAccountEqual : public std::unary_function&lt;Account_ptr,</TT><TT> 20:         bool&gt; {</TT><TT> 21: public:</TT><TT> 22:     IsAccountEqual(argument_type account) { myAccount = account; }</TT><TT> 23:     result_type operator()(argument_type account) { return account-&gt;</TT><TT> 24:             _is_equivalent(myAccount) != 0; }</TT><TT> 25: private:</TT><TT> 26:     argument_type myAccount;</TT><TT> 27: };</TT><TT> 28: </TT><TT> 29: // Constructor.</TT><TT> 30: //</TT><TT> 31: // name - This Bank's name.</TT><TT> 32: BankImpl::BankImpl(const char* name) : _sk_Bank(name), myAccounts(),</TT><TT> 33:         myName(strdup(name)), myAddress(strdup(&quot;123 Elm Street, &quot;</TT><TT> 34:         &quot;Anyware USA 12345&quot;)), myNextAccountNumber(0) {</TT><TT> 35: </TT><TT> 36: }</TT><TT> 37: </TT><TT> 38: // Default constructor.</TT><TT> 39: BankImpl::BankImpl() : myAccounts(), myName(NULL), myAddress(NULL),</TT><TT> 40:         myNextAccountNumber(0) {</TT><TT> 41: </TT><TT> 42: }</TT><TT> 43: </TT><TT> 44: // Destructor.</TT><TT> 45: BankImpl::~BankImpl() {</TT><TT> 46: </TT><TT> 47:     cout &lt;&lt; &quot;Bank \&quot;&quot; &lt;&lt; name() &lt;&lt; &quot;\&quot; being destroyed.&quot; &lt;&lt; endl;</TT><TT> 48:     free(myName);</TT><TT> 49:     free(myAddress);</TT><TT> 50: }</TT><TT> 51: </TT><TT> 52: char* BankImpl::name() {</TT><TT> 53: </TT><TT> 54:     return CORBA::strdup(myName);</TT><TT> 55: }</TT><TT> 56: </TT><TT> 57: void BankImpl::name(const char* val) {</TT><TT> 58: </TT><TT> 59:     free(myName);</TT><TT> 60:     myName = strdup(val);</TT><TT> 61: }</TT><TT> 62: </TT><TT> 63: char* BankImpl::address() {</TT><TT> 64: </TT><TT> 65:     return CORBA::strdup(myAddress);</TT><TT> 66: }</TT><TT> 67: </TT><TT> 68: void BankImpl::address(const char* val) {</TT><TT> 69: </TT><TT> 70:     free(myAddress);</TT><TT> 71:     myAddress = strdup(val);</TT><TT> 72: }</TT><TT> 73: </TT><TT> 74: Account_ptr BankImpl::createAccount(Customer_ptr customer,</TT><TT> 75:         const char* accountType, CORBA::Float openingBalance) {</TT><TT> 76: </TT><TT> 77:     Account_ptr newAccount;</TT><TT> 78: </TT><TT> 79:     if (strcmp(accountType, &quot;savings&quot;) == 0) {</TT><TT> 80: </TT><TT> 81:         // Create a new SavingsAccountImpl object for the Account.</TT><TT> 82:         cout &lt;&lt; &quot;BankImpl: Creating new SavingsAccount for &quot;</TT><TT> 83:                 &quot;Customer &quot; &lt;&lt; customer-&gt;name() &lt;&lt; &quot;.&quot; &lt;&lt; endl;</TT><TT> 84:         newAccount = new SavingsAccountImpl(getNextAccountNumber(),</TT><TT> 85:                 getCurrentDate(), openingBalance, customer, 10.0);</TT><TT> 86:     } else if (strcmp(accountType, &quot;checking&quot;) == 0) {</TT><TT> 87: </TT><TT> 88:         // Create a new CheckingAccountImpl object for the Account.</TT><TT> 89:         cout &lt;&lt; &quot;BankImpl: Creating new CheckingAccount for &quot;</TT><TT> 90:                 &quot;Customer &quot; &lt;&lt; customer-&gt;name() &lt;&lt; &quot;.&quot; &lt;&lt; endl;</TT><TT> 91:         newAccount = new CheckingAccountImpl(getNextAccountNumber(),</TT><TT> 92:                 getCurrentDate(), openingBalance, customer);</TT><TT> 93:     } else {</TT><TT> 94: </TT><TT> 95:         // Invalid Account type; do nothing.</TT><TT> 96:         cout &lt;&lt; &quot;BankImpl: Customer &quot; &lt;&lt; customer-&gt;name() &lt;&lt;</TT><TT> 97:                 &quot; requested invalid Account type \&quot;&quot; &lt;&lt; accountType</TT><TT> 98:                 &lt;&lt; &quot;\&quot;.&quot; &lt;&lt; endl;</TT><TT> 99:         return Account::_nil();</TT><TT>100:     }</TT><TT>101: </TT><TT>102:     // Add the created Account at the end of the list and return it.</TT><TT>103:     ::boa-&gt;obj_is_ready(newAccount);</TT><TT>104:     myAccounts.push_back(Account::_duplicate(newAccount));</TT><TT>105:     return newAccount;</TT><TT>106: }</TT><TT>107: </TT><TT>108: void BankImpl::deleteAccount(Account_ptr account) throw</TT><TT>109:         (InvalidAccountException) {</TT><TT>110: </TT><TT>111:     std::vector&lt;Account_ptr&gt;::iterator first = myAccounts.begin();</TT><TT>112:     std::vector&lt;Account_ptr&gt;::iterator last = myAccounts.end();</TT><TT>113:     IsAccountEqual predicate(account);</TT><TT>114: </TT><TT>115:     std::vector&lt;Account_ptr&gt;::iterator matchedAccount = std::</TT><TT>116:             find_if(first, last, predicate);</TT><TT>117:     if (matchedAccount == last) {</TT><TT>118: </TT><TT>119:         // Invalid Account; throw an exception.</TT><TT>120:         cout &lt;&lt; &quot;BankImpl: Attempted to delete invalid Account.&quot; &lt;&lt;</TT><TT>121:                 endl;</TT><TT>122:         throw InvalidAccountException();</TT><TT>123:     }</TT><TT>124:     cout &lt;&lt; &quot;BankImpl: Deleting Account \&quot;&quot; &lt;&lt; account-&gt;</TT><TT>125:             accountNumber() &lt;&lt; &quot;\&quot;.&quot; &lt;&lt; endl;</TT><TT>126: </TT><TT>127:     // Delete the given Account.</TT><TT>128:     myAccounts.erase(matchedAccount);</TT><TT>129:     account-&gt;_release();</TT><TT>130: }</TT><TT>131: </TT><TT>132: AccountList* BankImpl::getAccounts() {</TT><TT>133: </TT><TT>134:     AccountList* list = new AccountList(myAccounts.size());</TT><TT>135:     CORBA::Long i;</TT><TT>136: </TT><TT>137:     for (i = 0; i &lt; myAccounts.size(); i++) {</TT><TT>138:         (*list)[i] = Account::_duplicate(myAccounts[i]);</TT><TT>139:     }</TT><TT>140: </TT><TT>141:     return list;</TT><TT>142: }</TT><TT>143: </TT><B><TT>144: ATMCard_ptr BankImpl::issueATMCard(CORBA::Short pin, Account_ptr</TT></B><B><TT>145:         account) throw (InvalidAccountException) {</TT></B><B><TT>146: </TT></B><B><TT>147:     // First check to see if the Account is with this Bank.</TT></B><B><TT>148:     std::vector&lt;Account_ptr&gt;::iterator first = myAccounts.begin();</TT></B><B><TT>149:     std::vector&lt;Account_ptr&gt;::iterator last = myAccounts.end();</TT></B><B><TT>150:     IsAccountEqual predicate(account);</TT></B><B><TT>151: </TT></B><B><TT>152:     std::vector&lt;Account_ptr&gt;::iterator matchedAccount = std::</TT></B><B><TT>153:             find_if(first, last, predicate);</TT></B><B><TT>154:     if (matchedAccount == last) {</TT></B><B><TT>155: </TT></B><B><TT>156:         // Invalid Account; throw an exception.</TT></B><B><TT>157:         throw InvalidAccountException();</TT></B><B><TT>158:     }</TT></B><B><TT>159: </TT></B><B><TT>160:     // If we got this far, the Account must exist with this Bank,</TT></B><B><TT>161:     // so we can proceed.</TT></B><B><TT>162:     ATMCard_ptr newCard = new ATMCardImpl(pin, account);</TT></B><B><TT>163: </TT></B><B><TT>164:     return ATMCard::_duplicate(newCard);</TT></B><B><TT>165: }</TT></B><TT>166: </TT><TT>167: // Return the next available account number. The result is returned</TT><TT>168: // in a static buffer.</TT><TT>169: char* BankImpl::getNextAccountNumber() {</TT><TT>170: </TT><TT>171:     static char accountNumber[16] = &quot;Account        &quot;;</TT><TT>172: </TT><TT>173:     sprintf(accountNumber + 7, &quot;%08u&quot;, myNextAccountNumber++);</TT><TT>174: </TT><TT>175:     return accountNumber;</TT><TT>176: }</TT><TT>177: </TT><TT>178: // Return the current date in the form &quot;Mmm DD YYYY&quot;. The result is</TT><TT>179: // returned in a static buffer.</TT><TT>180: char* BankImpl::getCurrentDate() {</TT><TT>181: </TT><TT>182:     static char currentDate[12] = &quot;           &quot;;</TT><TT>183: </TT><TT>184:     time_t ltime;</TT><TT>185:     time(&amp;ltime);</TT><TT>186:     char* ctimeResult = ctime(&amp;ltime);</TT><TT>187: </TT><TT>188:     memcpy(currentDate, ctimeResult + 4, 3);</TT><TT>189:     memcpy(currentDate + 4, ctimeResult + 8, 2);</TT><TT>190:     memcpy(currentDate + 7, ctimeResult + 20, 4);</TT><TT>191: </TT><TT>192:     return currentDate;193: }</TT> </FONT></PRE><P>The logic of <TT>ATMCardImpl</TT> (Listings 8.10 and 8.11) is very similar tothat in <TT>BankImpl</TT>; <TT>addAccount()</TT>, <TT>removeAccount()</TT>, and <TT>getAccounts()</TT>are implemented in exactly the same way as they are in <TT>BankImpl</TT> (with theexception that <TT>removeAccount()</TT> and <TT>addAccount()</TT> call <TT>isAuthorized()</TT>to determine whether the <TT>Account</TT> is in the list of <TT>Account</TT>s, ratherthan duplicate this functionality).<H4><FONT COLOR="#000077">Listing 8.10. ATMCardImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATMCardImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef ATMCardImpl_h</TT><TT> 4: #define ATMCardImpl_h</TT><TT> 5: </TT><TT> 6: #include &lt;vector&gt;</TT><TT> 7: </TT><TT> 8: #include &quot;../ATMCard_s.h&quot;</TT><TT> 9: </TT><TT>10: class ATMCardImpl : public _sk_ATMCard {</TT><TT>11: </TT><TT>12: public:</TT><TT>13:     // Constuctor.</TT><TT>14:     //</TT><TT>15:     // pin - the initial PIN to use for this ATMCard.</TT><TT>16:     // initialAccount - the Account for which this ATMCard is</TT><TT>17:     // initially authorized.</TT><TT>18:     ATMCardImpl(CORBA::Short pin, Account_ptr initialAccount);</TT><TT>19: </TT><TT>20:     // Destructor.</TT><TT>21:     ~ATMCardImpl();</TT><TT>22: </TT><TT>23:     // These methods are described in ATMCard.idl.</TT><TT>24:     virtual CORBA::Boolean isAuthorized(Account_ptr account);</TT><TT>25:     virtual CORBA::Short pin();</TT><TT>26:     virtual void pin(CORBA::Short val);</TT><TT>27:     virtual void removeAccount(Account_ptr account) throw</TT><TT>28:             (InvalidAccountException);</TT><TT>29:     virtual void addAccount(Account_ptr account) throw</TT><TT>30:             (InvalidAccountException);</TT><TT>31:     virtual AccountList* getAccounts();</TT><TT>32: </TT><TT>33: private:</TT><TT>34: </TT><TT>35:     // Default constructor.</TT><TT>36:     ATMCardImpl();</TT><TT>37: </TT><TT>38:     // This ATMCard's PIN.</TT><TT>39:     CORBA::Short myPIN;</TT><TT>40: </TT><TT>41:     // A list of Accounts on which this ATM is authorized.</TT><TT>42:     std::vector&lt;Account_ptr&gt; myAccounts;</TT><TT>43: };</TT><TT>44: 45: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 8.11. ATMCardImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>  1: // ATMCardImpl.cpp</TT><TT>  2: </TT><TT>  3: #include &quot;ATMCardImpl.h&quot;</TT><TT>  4: </TT><TT>  5: #include &lt;iostream.h&gt;</TT><TT>  6: #include &lt;algorithm&gt;</TT><TT>  7: #include &lt;functional&gt;</TT><TT>  8: </TT><TT>  9: // STL-derived unary function which returns TRUE if Accounts are</TT><TT> 10: // equal.</TT><TT> 11: class IsAccountEqual : public std::unary_function&lt;Account_ptr,</TT><TT> 12:         bool&gt; {</TT><TT> 13: public:</TT><TT> 14:     IsAccountEqual(argument_type account) { myAccount = account; }</TT><TT> 15:     result_type operator()(argument_type account) { return account-&gt;</TT><TT> 16:             _is_equivalent(myAccount) != 0; }</TT><TT> 17: private:</TT><TT> 18:     argument_type myAccount;</TT><TT> 19: };</TT><TT> 20: </TT><TT> 21: // Constuctor.</TT><TT> 22: //</TT><TT> 23: // pin - the initial PIN to use for this ATMCard.</TT><TT> 24: // initialAccount - the Account for which this ATMCard is</TT><TT> 25: // initially authorized.</TT><TT> 26: ATMCardImpl::ATMCardImpl(CORBA::Short pin, Account_ptr</TT><TT> 27:         initialAccount) : myPIN(pin), myAccounts() {</TT><TT> 28: </TT><TT> 29:     // Add the Account to the authorized Account list.</TT><TT> 30:     myAccounts.push_back(Account::_duplicate(initialAccount));</TT><TT> 31: }</TT><TT> 32: </TT><TT> 33: // Default constructor.</TT><TT> 34: ATMCardImpl::ATMCardImpl() : myPIN(0), myAccounts() {</TT><TT> 35: </TT><TT> 36: }</TT>

⌨️ 快捷键说明

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