1. Stewart Mackenzie
  2. cappuccino-docs

Source

cappuccino-docs / cappuccino docs / cappuccino.org / learn / documentation / _c_p_operation_8j_source.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!-- Mirrored from cappuccino.org/learn/documentation/_c_p_operation_8j_source.html by HTTrack Website Copier/3.x [XR&CO'2006], Mon, 20 Sep 2010 02:19:30 GMT -->
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Cappuccino API: Foundation/CPOperation.j Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.1 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
    </ul>
  </div>
<h1>Foundation/CPOperation.j</h1><a href="_c_p_operation_8j.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * CPOperation.j</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * Created by Johannes Fahrenkrug.</span>
<a name="l00005"></a>00005 <span class="comment"> * Copyright 2009, Springenwerk.</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * This library is free software; you can redistribute it and/or</span>
<a name="l00008"></a>00008 <span class="comment"> * modify it under the terms of the GNU Lesser General Public</span>
<a name="l00009"></a>00009 <span class="comment"> * License as published by the Free Software Foundation; either</span>
<a name="l00010"></a>00010 <span class="comment"> * version 2.1 of the License, or (at your option) any later version.</span>
<a name="l00011"></a>00011 <span class="comment"> *</span>
<a name="l00012"></a>00012 <span class="comment"> * This library is distributed in the hope that it will be useful,</span>
<a name="l00013"></a>00013 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00014"></a>00014 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</span>
<a name="l00015"></a>00015 <span class="comment"> * Lesser General Public License for more details.</span>
<a name="l00016"></a>00016 <span class="comment"> *</span>
<a name="l00017"></a>00017 <span class="comment"> * You should have received a copy of the GNU Lesser General Public</span>
<a name="l00018"></a>00018 <span class="comment"> * License along with this library; if not, write to the Free Software</span>
<a name="l00019"></a>00019 <span class="comment"> * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA</span>
<a name="l00020"></a>00020 <span class="comment"> */</span>
<a name="l00021"></a>00021 
<a name="l00022"></a>00022 @<span class="keyword">import</span> &lt;Foundation/<a class="code" href="class_c_p_object.html" title="The root class from which most classes are subclassed.">CPObject</a>.j&gt;
<a name="l00023"></a>00023 
<a name="l00029"></a><a class="code" href="_c_p_operation_8j.html#ada4ce52b734c180a32b88459e0ff6dc6">00029</a> <a class="code" href="_c_p_operation_8j.html#ada4ce52b734c180a32b88459e0ff6dc6">CPOperationQueuePriorityVeryLow</a> = -8;
<a name="l00030"></a>00030 
<a name="l00036"></a><a class="code" href="_c_p_operation_8j.html#a4ad5dc97582869bd0e1f3d5bf10ab3c0">00036</a> <a class="code" href="_c_p_operation_8j.html#a4ad5dc97582869bd0e1f3d5bf10ab3c0">CPOperationQueuePriorityLow</a> = -4;
<a name="l00037"></a>00037 
<a name="l00043"></a><a class="code" href="_c_p_operation_8j.html#a36ee93cb8aee90db5cd71ecd381d0008">00043</a> <a class="code" href="_c_p_operation_8j.html#a36ee93cb8aee90db5cd71ecd381d0008">CPOperationQueuePriorityNormal</a> = 0;
<a name="l00044"></a>00044 
<a name="l00050"></a><a class="code" href="_c_p_operation_8j.html#a981621b1bbdd4c385c787b2a7a27516b">00050</a> <a class="code" href="_c_p_operation_8j.html#a981621b1bbdd4c385c787b2a7a27516b">CPOperationQueuePriorityHigh</a> = 4;
<a name="l00051"></a>00051 
<a name="l00057"></a><a class="code" href="_c_p_operation_8j.html#a0199d107991e714a8047074f7bc0ddf2">00057</a> <a class="code" href="_c_p_operation_8j.html#a0199d107991e714a8047074f7bc0ddf2">CPOperationQueuePriorityVeryHigh</a> = 8;
<a name="l00058"></a>00058 
<a name="l00059"></a>00059 
<a name="l00066"></a><a class="code" href="class_c_p_operation.html">00066</a> <span class="keyword">@implementation </span><a class="code" href="class_c_p_operation.html" title="Represents an operation that can be run in an CPOperationQueue.">CPOperation</a> : <a class="code" href="class_c_p_object.html" title="The root class from which most classes are subclassed.">CPObject</a> 
<a name="l00067"></a>00067 {
<a name="l00068"></a>00068     <a class="code" href="class_c_p_array.html" title="A mutable array backed by a JavaScript Array.A mutable array class backed by a JavaScript...">CPArray</a> operations;
<a name="l00069"></a>00069     BOOL _cancelled;
<a name="l00070"></a>00070     BOOL _executing;
<a name="l00071"></a>00071     BOOL _finished;
<a name="l00072"></a>00072     BOOL _ready;
<a name="l00073"></a>00073     <span class="keywordtype">int</span> _queuePriority;
<a name="l00074"></a>00074     JSObject _completionFunction;
<a name="l00075"></a>00075     <a class="code" href="class_c_p_array.html" title="A mutable array backed by a JavaScript Array.A mutable array class backed by a JavaScript...">CPArray</a> _dependencies;
<a name="l00076"></a>00076 }
<a name="l00077"></a>00077 
<a name="l00078"></a>00078 
<a name="l00079"></a><a class="code" href="class_c_p_operation.html#a11eb4ee2cdba0a9a99f091e7e78cb83a">00079</a> - (void)main 
<a name="l00080"></a>00080 {
<a name="l00081"></a>00081     <span class="comment">// should be overridden in child class</span>
<a name="l00082"></a>00082 }
<a name="l00083"></a>00083 
<a name="l00084"></a><a class="code" href="class_c_p_operation.html#a322ca060f4ed8585911b9a94876e82e4">00084</a> - (id)init 
<a name="l00085"></a>00085 {
<a name="l00086"></a>00086     <span class="keywordflow">if</span> (<span class="keyword">self</span> = [super init]) 
<a name="l00087"></a>00087     {
<a name="l00088"></a>00088         _cancelled = NO;
<a name="l00089"></a>00089         _executing = NO;
<a name="l00090"></a>00090         _finished = NO;
<a name="l00091"></a>00091         _ready = YES;
<a name="l00092"></a>00092         _dependencies = [[<a class="code" href="class_c_p_array.html" title="A mutable array backed by a JavaScript Array.A mutable array class backed by a JavaScript...">CPArray</a> alloc] init];
<a name="l00093"></a>00093         _queuePriority = <a class="code" href="_c_p_operation_8j.html#a36ee93cb8aee90db5cd71ecd381d0008">CPOperationQueuePriorityNormal</a>;
<a name="l00094"></a>00094     }
<a name="l00095"></a>00095     <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00096"></a>00096 }
<a name="l00097"></a>00097 
<a name="l00101"></a><a class="code" href="class_c_p_operation.html#a32d1c8f784ad5a1d4ecd96776c0d9251">00101</a> - (void)start 
<a name="l00102"></a>00102 {
<a name="l00103"></a>00103     <span class="keywordflow">if</span> (!_cancelled) 
<a name="l00104"></a>00104     {
<a name="l00105"></a>00105         [<span class="keyword">self</span> willChangeValueForKey:@&quot;isExecuting&quot;];
<a name="l00106"></a>00106         _executing = YES;
<a name="l00107"></a>00107         [<span class="keyword">self</span> didChangeValueForKey:@&quot;isExecuting&quot;];
<a name="l00108"></a>00108         [<span class="keyword">self</span> <a class="code" href="class_c_p_operation.html#a11eb4ee2cdba0a9a99f091e7e78cb83a">main</a>];
<a name="l00109"></a>00109         <span class="keywordflow">if</span> (_completionFunction) 
<a name="l00110"></a>00110         {
<a name="l00111"></a>00111             _completionFunction();
<a name="l00112"></a>00112         }
<a name="l00113"></a>00113         [<span class="keyword">self</span> willChangeValueForKey:@&quot;isExecuting&quot;];
<a name="l00114"></a>00114         _executing = NO;
<a name="l00115"></a>00115         [<span class="keyword">self</span> didChangeValueForKey:@&quot;isExecuting&quot;];
<a name="l00116"></a>00116         [<span class="keyword">self</span> willChangeValueForKey:@&quot;isFinished&quot;];
<a name="l00117"></a>00117         _finished = YES;
<a name="l00118"></a>00118         [<span class="keyword">self</span> didChangeValueForKey:@&quot;isFinished&quot;];
<a name="l00119"></a>00119     }
<a name="l00120"></a>00120 }
<a name="l00121"></a>00121 
<a name="l00126"></a><a class="code" href="class_c_p_operation.html#acc6e97b52cda683a6808d2032944e5aa">00126</a> - (BOOL)isCancelled 
<a name="l00127"></a>00127 {
<a name="l00128"></a>00128     <span class="keywordflow">return</span> _cancelled;
<a name="l00129"></a>00129 }
<a name="l00130"></a>00130 
<a name="l00135"></a><a class="code" href="class_c_p_operation.html#a6d426b63f035cc69fad9afbad5dc07f7">00135</a> - (BOOL)isExecuting 
<a name="l00136"></a>00136 {
<a name="l00137"></a>00137     <span class="keywordflow">return</span> _executing;
<a name="l00138"></a>00138 }
<a name="l00139"></a>00139 
<a name="l00144"></a><a class="code" href="class_c_p_operation.html#a2f145b5f92f52fcd74edb12a0e860b4c">00144</a> - (BOOL)isFinished 
<a name="l00145"></a>00145 {
<a name="l00146"></a>00146     <span class="keywordflow">return</span> _finished;
<a name="l00147"></a>00147 }
<a name="l00148"></a>00148 
<a name="l00153"></a><a class="code" href="class_c_p_operation.html#aa3f87f45789fe7be7cab0e18195dded3">00153</a> - (BOOL)isConcurrent 
<a name="l00154"></a>00154 {
<a name="l00155"></a>00155     <span class="keywordflow">return</span> NO;
<a name="l00156"></a>00156 }
<a name="l00157"></a>00157 
<a name="l00162"></a><a class="code" href="class_c_p_operation.html#a8844004b6c48ea39f0c49e1db064c6f1">00162</a> - (BOOL)isReady 
<a name="l00163"></a>00163 {    
<a name="l00164"></a>00164     <span class="keywordflow">return</span> _ready;
<a name="l00165"></a>00165 }
<a name="l00166"></a>00166 
<a name="l00171"></a><a class="code" href="class_c_p_operation.html#a87953b24b797cefeb103b3862cab1fce">00171</a> - (JSObject)completionFunction 
<a name="l00172"></a>00172 {
<a name="l00173"></a>00173     <span class="keywordflow">return</span> _completionFunction;
<a name="l00174"></a>00174 }
<a name="l00175"></a>00175 
<a name="l00179"></a><a class="code" href="class_c_p_operation.html#a97db31f76973301e63084e359741bfed">00179</a> - (void)setCompletionFunction:(JSObject)aJavaScriptFunction 
<a name="l00180"></a>00180 {
<a name="l00181"></a>00181     _completionFunction = aJavaScriptFunction;
<a name="l00182"></a>00182 }
<a name="l00183"></a>00183 
<a name="l00188"></a><a class="code" href="class_c_p_operation.html#a326292add5785c11667ab2c4030a5262">00188</a> - (void)addDependency:(<a class="code" href="class_c_p_operation.html" title="Represents an operation that can be run in an CPOperationQueue.">CPOperation</a>)anOperation 
<a name="l00189"></a>00189 {
<a name="l00190"></a>00190     [<span class="keyword">self</span> willChangeValueForKey:@&quot;dependencies&quot;];
<a name="l00191"></a>00191     [anOperation addObserver:self
<a name="l00192"></a>00192                   forKeyPath:@&quot;isFinished&quot;
<a name="l00193"></a>00193                      options:(CPKeyValueObservingOptionNew)
<a name="l00194"></a>00194                      context:NULL];
<a name="l00195"></a>00195     [_dependencies addObject:anOperation];
<a name="l00196"></a>00196     [<span class="keyword">self</span> didChangeValueForKey:@&quot;dependencies&quot;];
<a name="l00197"></a>00197     [<span class="keyword">self</span> _updateIsReadyState];
<a name="l00198"></a>00198 }
<a name="l00199"></a>00199 
<a name="l00204"></a><a class="code" href="class_c_p_operation.html#a9ebbcd25a0fefe67a89bfb67dbe4bb57">00204</a> - (void)removeDependency:(<a class="code" href="class_c_p_operation.html" title="Represents an operation that can be run in an CPOperationQueue.">CPOperation</a>)anOperation 
<a name="l00205"></a>00205 {
<a name="l00206"></a>00206     [<span class="keyword">self</span> willChangeValueForKey:@&quot;dependencies&quot;];
<a name="l00207"></a>00207     [_dependencies removeObject:anOperation];
<a name="l00208"></a>00208     [anOperation removeObserver:self
<a name="l00209"></a>00209                      forKeyPath:@&quot;isFinished&quot;];                 
<a name="l00210"></a>00210     [<span class="keyword">self</span> didChangeValueForKey:@&quot;dependencies&quot;];
<a name="l00211"></a>00211     [<span class="keyword">self</span> _updateIsReadyState];
<a name="l00212"></a>00212 }
<a name="l00213"></a>00213 
<a name="l00218"></a><a class="code" href="class_c_p_operation.html#ad9b869ec3eb0393c2eb59981e7802fd2">00218</a> - (<a class="code" href="class_c_p_array.html" title="A mutable array backed by a JavaScript Array.A mutable array class backed by a JavaScript...">CPArray</a>)dependencies 
<a name="l00219"></a>00219 {
<a name="l00220"></a>00220     <span class="keywordflow">return</span> _dependencies;
<a name="l00221"></a>00221 }
<a name="l00222"></a>00222 
<a name="l00226"></a><a class="code" href="class_c_p_operation.html#aa23430979de94854de165981ce30cee3">00226</a> - (void)waitUntilFinished 
<a name="l00227"></a>00227 {
<a name="l00228"></a>00228 }
<a name="l00229"></a>00229 
<a name="l00233"></a><a class="code" href="class_c_p_operation.html#aab311e9fa7b919ed5b88c77cc595d29f">00233</a> - (void)cancel 
<a name="l00234"></a>00234 {
<a name="l00235"></a>00235     [<span class="keyword">self</span> willChangeValueForKey:@&quot;isCancelled&quot;];
<a name="l00236"></a>00236     _cancelled = YES;
<a name="l00237"></a>00237     [<span class="keyword">self</span> didChangeValueForKey:@&quot;isCancelled&quot;];
<a name="l00238"></a>00238 }
<a name="l00239"></a>00239 
<a name="l00244"></a><a class="code" href="class_c_p_operation.html#aa92c96d74c1892d688573abb75c3136b">00244</a> - (void)setQueuePriority:(<span class="keywordtype">int</span>)priority 
<a name="l00245"></a>00245 {
<a name="l00246"></a>00246     _queuePriority = priority;
<a name="l00247"></a>00247 }
<a name="l00248"></a>00248 
<a name="l00253"></a><a class="code" href="class_c_p_operation.html#aa513ea7c11e1dbfdf40dece3952ef0f6">00253</a> - (int)queuePriority 
<a name="l00254"></a>00254 {
<a name="l00255"></a>00255     <span class="keywordflow">return</span> _queuePriority;
<a name="l00256"></a>00256 }
<a name="l00257"></a>00257 
<a name="l00258"></a>00258 <span class="comment">// We need to observe the &quot;isFinished&quot; key of our dependent operations so we can update our own &quot;isReady&quot; state</span>
<a name="l00259"></a><a class="code" href="class_c_p_operation.html#a0f532e7788dc39ffbaa61edc97ceeabb">00259</a> - (void)observeValueForKeyPath:(<a class="code" href="class_c_p_string.html" title="An immutable string (collection of characters).">CPString</a>)keyPath
<a name="l00260"></a>00260                       ofObject:(<span class="keywordtype">id</span>)object
<a name="l00261"></a>00261                         change:(<a class="code" href="class_c_p_dictionary.html" title="A mutable key-value pair collection.">CPDictionary</a>)change
<a name="l00262"></a>00262                        context:(<span class="keywordtype">void</span>)context
<a name="l00263"></a>00263 {
<a name="l00264"></a>00264     <span class="keywordflow">if</span> (keyPath == <span class="stringliteral">@&quot;isFinished&quot;</span>) 
<a name="l00265"></a>00265     {
<a name="l00266"></a>00266         [<span class="keyword">self</span> _updateIsReadyState];
<a name="l00267"></a>00267     }
<a name="l00268"></a>00268 }
<a name="l00269"></a>00269 
<a name="l00270"></a>00270 - (void)_updateIsReadyState 
<a name="l00271"></a>00271 {
<a name="l00272"></a>00272     var newReady = YES;
<a name="l00273"></a>00273     <span class="keywordflow">if</span> (_dependencies &amp;&amp; [_dependencies count] &gt; 0) 
<a name="l00274"></a>00274     {
<a name="l00275"></a>00275         var i = 0;
<a name="l00276"></a>00276         <span class="keywordflow">for</span> (i = 0; i &lt; [_dependencies count]; i++) 
<a name="l00277"></a>00277         {
<a name="l00278"></a>00278             <span class="keywordflow">if</span> (![[_dependencies objectAtIndex:i] isFinished]) 
<a name="l00279"></a>00279             {
<a name="l00280"></a>00280                 newReady = NO;
<a name="l00281"></a>00281             }
<a name="l00282"></a>00282         }
<a name="l00283"></a>00283     }
<a name="l00284"></a>00284     
<a name="l00285"></a>00285     <span class="keywordflow">if</span> (newReady != _ready) 
<a name="l00286"></a>00286     {
<a name="l00287"></a>00287         [<span class="keyword">self</span> willChangeValueForKey:@&quot;isReady&quot;];
<a name="l00288"></a>00288         _ready = newReady;
<a name="l00289"></a>00289         [<span class="keyword">self</span> didChangeValueForKey:@&quot;isReady&quot;];
<a name="l00290"></a>00290     }
<a name="l00291"></a>00291 }
<a name="l00292"></a>00292 
<a name="l00293"></a>00293 <span class="keyword">@end</span>
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated on Tue Apr 6 10:32:57 2010 for Cappuccino API by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>
</body>

<!-- Mirrored from cappuccino.org/learn/documentation/_c_p_operation_8j_source.html by HTTrack Website Copier/3.x [XR&CO'2006], Mon, 20 Sep 2010 02:19:30 GMT -->
</html>