1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
2 | %% |
---|
3 | %% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
4 | %% |
---|
5 | %% The contents of this file are covered under the licence agreement in the |
---|
6 | %% file "LICENCE" distributed with Cforall. |
---|
7 | %% |
---|
8 | %% user.tex -- |
---|
9 | %% |
---|
10 | %% Author : Peter A. Buhr |
---|
11 | %% Created On : Wed Apr 6 14:53:29 2016 |
---|
12 | %% Last Modified By : Peter A. Buhr |
---|
13 | %% Last Modified On : Fri Nov 8 08:22:25 2024 |
---|
14 | %% Update Count : 6107 |
---|
15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
16 | |
---|
17 | % requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended |
---|
18 | |
---|
19 | \documentclass[11pt]{article} |
---|
20 | |
---|
21 | \makeatletter |
---|
22 | \def\@maketitle{% |
---|
23 | \newpage |
---|
24 | \null |
---|
25 | % \vskip 2em% |
---|
26 | \begin{center}% |
---|
27 | \let \footnote \thanks |
---|
28 | {\LARGE\bf \@title \par}% |
---|
29 | \@ifundefined{@author}{} |
---|
30 | { |
---|
31 | \ifx\@empty\@author |
---|
32 | \else |
---|
33 | \vskip 1.5em% |
---|
34 | {\large |
---|
35 | \lineskip .5em% |
---|
36 | \begin{tabular}[t]{c}% |
---|
37 | \@author |
---|
38 | \end{tabular}% |
---|
39 | \par |
---|
40 | }% |
---|
41 | \fi |
---|
42 | }% |
---|
43 | \ifx\@empty\@date |
---|
44 | \else |
---|
45 | \vskip 1em% |
---|
46 | {\large \@date}% |
---|
47 | \fi |
---|
48 | \end{center}% |
---|
49 | \par |
---|
50 | % \vskip 1.5em |
---|
51 | }%maketitle |
---|
52 | \makeatother |
---|
53 | |
---|
54 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
55 | |
---|
56 | % Latex packages used in the document. |
---|
57 | %\usepackage[T1]{fontenc} % allow Latin1 (extended ASCII) characters |
---|
58 | %\usepackage{textcomp} |
---|
59 | %\usepackage[latin1]{inputenc} |
---|
60 | |
---|
61 | \usepackage{fullpage,times,comment} |
---|
62 | \usepackage{epic,eepic} |
---|
63 | \usepackage{upquote} % switch curled `'" to straight |
---|
64 | \usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt]{subfig} |
---|
65 | \renewcommand{\thesubfigure}{\alph{subfigure})} |
---|
66 | \usepackage{latexsym} % \Box glyph |
---|
67 | \usepackage{mathptmx} % better math font with "times" |
---|
68 | \usepackage[usenames]{color} |
---|
69 | \usepackage[pagewise]{lineno} |
---|
70 | \renewcommand{\linenumberfont}{\scriptsize\sffamily} |
---|
71 | \input{common} % common CFA document macros |
---|
72 | \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref} |
---|
73 | \usepackage{breakurl} |
---|
74 | |
---|
75 | \renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt} |
---|
76 | \newcommand{\uC}{$\mu$\CC} |
---|
77 | |
---|
78 | % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore |
---|
79 | % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR |
---|
80 | % AFTER HYPERREF. |
---|
81 | \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}} |
---|
82 | |
---|
83 | \setlength{\topmargin}{-0.45in} % move running title into header |
---|
84 | \setlength{\headsep}{0.25in} |
---|
85 | |
---|
86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
87 | |
---|
88 | \setlength{\gcolumnposn}{3in} |
---|
89 | \CFAStyle % use default CFA format-style |
---|
90 | \lstset{language=CFA} % CFA default lnaguage |
---|
91 | \lstnewenvironment{C++}[1][] % use C++ style |
---|
92 | {\lstset{language=C++,escapechar=\$,mathescape=false,moredelim=**[is][\protect\color{red}]{@}{@},#1}}{} |
---|
93 | \lstnewenvironment{uC++}[1][] |
---|
94 | {\lstset{language=uC++,escapechar=\$,mathescape=false,moredelim=**[is][\protect\color{red}]{@}{@},#1}}{} |
---|
95 | |
---|
96 | \newsavebox{\myboxA} |
---|
97 | \newsavebox{\myboxB} |
---|
98 | |
---|
99 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
100 | |
---|
101 | % Names used in the document. |
---|
102 | \newcommand{\Version}{\input{build/version}} |
---|
103 | \newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}} |
---|
104 | \newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}} |
---|
105 | \newcommand{\R}[1]{{\color{red}#1}} |
---|
106 | \newcommand{\RB}[1]{\Textbf{#1}} |
---|
107 | \newcommand{\B}[1]{{\Textbf[blue]{#1}}} |
---|
108 | \newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}} |
---|
109 | \newcommand{\Sp}{\R{\textvisiblespace}} |
---|
110 | \newcommand{\KWC}{K-W C\xspace} |
---|
111 | |
---|
112 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
113 | |
---|
114 | \setcounter{secnumdepth}{3} % number subsubsections |
---|
115 | \setcounter{tocdepth}{3} % subsubsections in table of contents |
---|
116 | \makeindex |
---|
117 | |
---|
118 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
119 | |
---|
120 | \title{\vspace*{-0.5in} |
---|
121 | \uC to \CFA Cheat Sheet} |
---|
122 | %\author{Peter A. Buhr} |
---|
123 | \date{} |
---|
124 | |
---|
125 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
126 | |
---|
127 | \begin{document} |
---|
128 | \pagestyle{headings} |
---|
129 | % changed after setting pagestyle |
---|
130 | \renewcommand{\sectionmark}[1]{\markboth{\thesection\quad #1}{\thesection\quad #1}} |
---|
131 | \renewcommand{\subsectionmark}[1]{\markboth{\thesubsection\quad #1}{\thesubsection\quad #1}} |
---|
132 | |
---|
133 | %\linenumbers % comment out to turn off line numbering |
---|
134 | |
---|
135 | \maketitle |
---|
136 | \vspace*{-0.55in} |
---|
137 | |
---|
138 | \section{Introduction} |
---|
139 | |
---|
140 | \CFA is NOT an object-oriented programming-language. |
---|
141 | \CFA uses parametric polymorphism and allows overloading of variables and routines: |
---|
142 | \begin{cfa} |
---|
143 | int i; char i; double i; $\C[2in]{// overload name i}$ |
---|
144 | int i(); double i(); char i(); |
---|
145 | i += 1; $\C{// int i}$ |
---|
146 | i += 1.0; $\C{// double i}$ |
---|
147 | i += 'a'; $\C{// char i}$ |
---|
148 | int j = i(); $\C{// int i()}$ |
---|
149 | double j = i(); $\C{// double i();}$ |
---|
150 | char j = i(); $\C{// char i()}\CRT$ |
---|
151 | \end{cfa} |
---|
152 | \CFA has rebindable references. |
---|
153 | \begin{cquote} |
---|
154 | \begin{tabular}{@{}l|l@{}} |
---|
155 | \multicolumn{2}{@{}l}{\lstinline{ int x = 1, y = 2, * p1x = &x, * p1y = &y, ** p2i = &p1x,}} \\ |
---|
156 | \multicolumn{2}{@{}l}{\lstinline{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ & r1x = x, & r1y = y, && r2i = r1x;}} \\ |
---|
157 | \begin{uC++} |
---|
158 | **p2i = 3; |
---|
159 | p2i = &p1y; |
---|
160 | **p2i = 3; |
---|
161 | p1x = p1y; |
---|
162 | **p2i = 4; |
---|
163 | p1x = @nullptr@; |
---|
164 | \end{uC++} |
---|
165 | & |
---|
166 | \begin{cfa} |
---|
167 | r2i = 3; $\C[1.0in]{// change x}$ |
---|
168 | &r2i = &r1y; $\C{// change p2i / r2i}$ |
---|
169 | r2i = 3; $\C{// change y}$ |
---|
170 | &r1x = &r1y; $\C{// change p1x / r1x}$ |
---|
171 | r2i = 4; $\C{// change y}$ |
---|
172 | &r1x = @0p@; $\C{// reset}\CRT$ |
---|
173 | \end{cfa} |
---|
174 | \end{tabular} |
---|
175 | \end{cquote} |
---|
176 | Non-rebindable reference (\CC reference) is a @const@ reference (@const@ pointer). |
---|
177 | \begin{cfa} |
---|
178 | int & @const@ cr = x; // must initialize, no null pointer |
---|
179 | int & @const@ & @const@ crcr = cr; // generalize |
---|
180 | \end{cfa} |
---|
181 | Aggregate qualification is reduced or eliminated by opening scopes using the @with@ clause. |
---|
182 | \begin{cfa} |
---|
183 | struct S { int i; int j; double m; }; // field i has same type in structures S and T |
---|
184 | struct T { int i; int k; int m; }; |
---|
185 | void foo( S s, T t ) @with( s, t )@ { // open structure scope s and t in parallel |
---|
186 | j + k; $\C[1.6in]{// unambiguous, s.j + t.k}$ |
---|
187 | m = 5.0; $\C{// unambiguous, s.m = 5.0}$ |
---|
188 | m = 1; $\C{// unambiguous, t.m = 1}$ |
---|
189 | int a = m; $\C{// unambiguous, a = t.m}$ |
---|
190 | double b = m; $\C{// unambiguous, b = s.m}$ |
---|
191 | int c = s.i + t.i; $\C{// unambiguous with qualification}$ |
---|
192 | (double)m; $\C{// unambiguous with cast s.m}\CRT$ |
---|
193 | } |
---|
194 | \end{cfa} |
---|
195 | \noindent |
---|
196 | In subsequent code examples, the left example is \uC and the right example is \CFA. |
---|
197 | |
---|
198 | |
---|
199 | \section{Stream I/O} |
---|
200 | |
---|
201 | \CFA output streams automatically separate values and insert a newline at the end of the print. |
---|
202 | \begin{cquote} |
---|
203 | \begin{tabular}{@{}l|l@{}} |
---|
204 | \begin{uC++} |
---|
205 | #include <@iostream@> |
---|
206 | using namespace std; |
---|
207 | int i; double d; char c; |
---|
208 | cin >> i >> d >> c; |
---|
209 | cout << i << ' ' << d << ' ' << c | endl; |
---|
210 | \end{uC++} |
---|
211 | & |
---|
212 | \begin{cfa} |
---|
213 | #include <@fstream.hfa@> |
---|
214 | |
---|
215 | int i; double d; char c; |
---|
216 | sin | i | d | c; |
---|
217 | sout | i | d | c |
---|
218 | \end{cfa} |
---|
219 | \end{tabular} |
---|
220 | \end{cquote} |
---|
221 | |
---|
222 | |
---|
223 | \section{Looping} |
---|
224 | |
---|
225 | \begin{cquote} |
---|
226 | \begin{tabular}{@{}l|l@{}} |
---|
227 | \begin{uC++} |
---|
228 | for ( @;;@ ) { ... } / while ( @true@ ) { ... } |
---|
229 | for ( int i = 0; i < @10@; i += 1 ) { ... } |
---|
230 | for ( int i = @5@; i < @15@; i += @2@ ) { ... } |
---|
231 | for ( int i = -1; i <@=@ 10; i += 3 ) { ... } |
---|
232 | for ( int i = 10; i > 0; i @-@= 1 ) { ... } |
---|
233 | \end{uC++} |
---|
234 | & |
---|
235 | \begin{cfa} |
---|
236 | for () { ... } / while () { ... } |
---|
237 | for ( @10@ ) { ... } / for ( i; @10@ ) { ... } |
---|
238 | for ( i; @5@ ~ @15@ ~ @2@ ) { ... } |
---|
239 | for ( i; -1 ~@=@ 10 ~ 3 ) { ... } |
---|
240 | for ( i; 0 @-@~ 10 ) { ... } |
---|
241 | \end{cfa} |
---|
242 | \\ |
---|
243 | \hline |
---|
244 | \begin{uC++} |
---|
245 | int i = 0 |
---|
246 | for ( i = 0; i < 10; i += 1 ) { ... } |
---|
247 | @if ( i == 10 )@ { ... } |
---|
248 | \end{uC++} |
---|
249 | & |
---|
250 | \begin{cfa} |
---|
251 | |
---|
252 | for ( i; 10 ) { ... } |
---|
253 | @else@ { ... } // i == 10 |
---|
254 | \end{cfa} |
---|
255 | \\ |
---|
256 | \hline |
---|
257 | \begin{uC++} |
---|
258 | @L1:@ for ( ;; ) { |
---|
259 | @L2:@ for ( ;; ) { |
---|
260 | ... if ( ... ) @break L1@; ... |
---|
261 | ... if ( ... ) @break L2@; ... |
---|
262 | } |
---|
263 | } |
---|
264 | \end{uC++} |
---|
265 | & |
---|
266 | \begin{cfa} |
---|
267 | @L1:@ for () { |
---|
268 | @L2:@ for () { |
---|
269 | ... if ( ... ) @break L1@; ... |
---|
270 | ... if ( ... ) @break L2@; ... |
---|
271 | } |
---|
272 | } |
---|
273 | \end{cfa} |
---|
274 | \end{tabular} |
---|
275 | \end{cquote} |
---|
276 | |
---|
277 | |
---|
278 | \section{Exception} |
---|
279 | |
---|
280 | Currently, \CFA uses macros @ExceptionDecl@ and @ExceptionInst@ to declare and instantiate an exception. |
---|
281 | \begin{cquote} |
---|
282 | \begin{tabular}{@{}l|ll@{}} |
---|
283 | \begin{uC++} |
---|
284 | |
---|
285 | @_Exception@ E { // local or global scope |
---|
286 | ... // exception fields |
---|
287 | }; |
---|
288 | try { |
---|
289 | ... |
---|
290 | if ( ... ) @_Resume@ E( /* initialization */ ); |
---|
291 | if ( ... ) @_Throw@ E( /* initialization */ ); |
---|
292 | ... |
---|
293 | } @_CatchResume@( E & ) { ... // reference |
---|
294 | } catch( E & ) { ... |
---|
295 | } catch( ... ) { ... // catch any |
---|
296 | } _Finally { ... |
---|
297 | } |
---|
298 | \end{uC++} |
---|
299 | & |
---|
300 | \begin{cfa} |
---|
301 | #include <Exception.hfa> |
---|
302 | @ExceptionDecl@( E, // must be global scope |
---|
303 | ... // exception fields |
---|
304 | ); |
---|
305 | try { |
---|
306 | ... |
---|
307 | if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ ); |
---|
308 | if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ ); |
---|
309 | ... |
---|
310 | } @catchResume@( E * ) { ... // pointer |
---|
311 | } catch( E * ) { ... |
---|
312 | } catch( exception_t * ) { ... // catch any |
---|
313 | } finally { ... |
---|
314 | } |
---|
315 | \end{cfa} |
---|
316 | \end{tabular} |
---|
317 | \end{cquote} |
---|
318 | |
---|
319 | |
---|
320 | \section{Non-local Exception} |
---|
321 | |
---|
322 | \begin{cquote} |
---|
323 | \begin{tabular}{@{}l|ll@{}} |
---|
324 | \begin{uC++} |
---|
325 | |
---|
326 | |
---|
327 | void main() { |
---|
328 | try { |
---|
329 | _Enable { |
---|
330 | ... suspend(); ... |
---|
331 | } |
---|
332 | } @_CatchResume@( E & ) { // reference |
---|
333 | ... |
---|
334 | } catch( E & ) { |
---|
335 | ... |
---|
336 | } |
---|
337 | } |
---|
338 | \end{uC++} |
---|
339 | & |
---|
340 | \begin{cfa} |
---|
341 | #define resumePoll( coroutine ) resume( coroutine ); checked_poll() |
---|
342 | #define suspendPoll suspend; checked_poll() |
---|
343 | void main() { |
---|
344 | try { |
---|
345 | enable_ehm(); |
---|
346 | ... suspendPoll ... |
---|
347 | disable_ehm(); |
---|
348 | } @catchResume@( E * ) { // pointer |
---|
349 | ... |
---|
350 | } catch( E & ) { |
---|
351 | ... |
---|
352 | } |
---|
353 | } |
---|
354 | \end{cfa} |
---|
355 | \end{tabular} |
---|
356 | \end{cquote} |
---|
357 | |
---|
358 | |
---|
359 | \section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}} |
---|
360 | |
---|
361 | \begin{cquote} |
---|
362 | \begin{tabular}{@{}l|l@{}} |
---|
363 | \begin{uC++} |
---|
364 | struct S { |
---|
365 | int i = 0; // cheat, implicit default constructor |
---|
366 | int setter( int j ) { int t = i; i = j; return t; } |
---|
367 | int getter() { return i; } |
---|
368 | }; |
---|
369 | S s; |
---|
370 | @s.@setter( 3 ); // object calls |
---|
371 | int k = @s.@getter(); |
---|
372 | \end{uC++} |
---|
373 | & |
---|
374 | \begin{cfa} |
---|
375 | struct S { int i; }; |
---|
376 | void ?{}( S & s ) { s.i = 0; } // explicit default constructor |
---|
377 | int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; } |
---|
378 | int getter( @S & s@ ) @with( s )@ { return i; } |
---|
379 | |
---|
380 | S s; |
---|
381 | setter( @s,@ 3 ); // normal calls |
---|
382 | int k = getter( @s@ ); |
---|
383 | \end{cfa} |
---|
384 | \end{tabular} |
---|
385 | \end{cquote} |
---|
386 | |
---|
387 | |
---|
388 | \section{Constructor / Destructor} |
---|
389 | |
---|
390 | \begin{cquote} |
---|
391 | \begin{tabular}{@{}l|l@{}} |
---|
392 | \begin{uC++} |
---|
393 | |
---|
394 | struct S { |
---|
395 | int i, j; |
---|
396 | @S@( int i, int j ) { S::i = i; S::j = j; } |
---|
397 | @~S@() {} |
---|
398 | }; |
---|
399 | S s = { 1, 2 }, s2{ 1, 2 }; |
---|
400 | S * s3 = new S{ 1, 2 }; |
---|
401 | S & s4 = *new S{ 1, 2 }; |
---|
402 | \end{uC++} |
---|
403 | & |
---|
404 | \begin{cfa} |
---|
405 | #include <stdlib.hfa> // malloc |
---|
406 | struct S { |
---|
407 | int i, j; |
---|
408 | }; |
---|
409 | void @?{}( S & s@, int i, int j ) { s.[i, j] = [i, j]; } |
---|
410 | void @^?{}( S & s@ ) {} |
---|
411 | S s = { 1, 2 }, s2{ 1, 2 }; |
---|
412 | S * s3 = &(*malloc()){ 1, 2 }; |
---|
413 | S & s4 = (*malloc()){ 1, 2 }; // fails |
---|
414 | \end{cfa} |
---|
415 | \end{tabular} |
---|
416 | \end{cquote} |
---|
417 | |
---|
418 | |
---|
419 | \section{String} |
---|
420 | |
---|
421 | \begin{cquote} |
---|
422 | \begin{tabular}{@{}l|l@{}} |
---|
423 | \multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\ |
---|
424 | \begin{uC++} |
---|
425 | s1 = "abcdefg"; |
---|
426 | s2 = s1; |
---|
427 | s1 += s2; |
---|
428 | s1 == s2; s1 != s2; |
---|
429 | s1 < s2; s1 <= s2; s1 > s2; s1 >= s2; |
---|
430 | s1.length(); |
---|
431 | s1[3]; |
---|
432 | s1.substr( 2 ); s1.substr( 2, 3 ); |
---|
433 | s1.replace( 2, 5, s2 ); |
---|
434 | s1.find( s2 ); s1.rfind( s2 ); |
---|
435 | s1.find_first_of( s2 ); s1.find_last_of( s2 ); |
---|
436 | s1.find_first_not_of( s2 ); s1.find_last_not_of( s2 ); |
---|
437 | getline( cin, s1 ); |
---|
438 | cout << s1 << endl; |
---|
439 | \end{uC++} |
---|
440 | & |
---|
441 | \begin{cfa} |
---|
442 | s1 = "abcdefg"; |
---|
443 | s2 = s1; |
---|
444 | s1 += s2; |
---|
445 | s1 == s2; s1 != s2; |
---|
446 | s1 < s2; s1 <= s2; s1 > s2; s1 >= s2; |
---|
447 | size( s1 ); |
---|
448 | s1[3]; |
---|
449 | s1( 2 ); s1( 2, 3 ); |
---|
450 | // replace( s1, 2, 5, s2 ); |
---|
451 | // find( s1, s2 ), rfind( s1, s2 ); |
---|
452 | // find_first_of( s2 ); find_last_of( s2 ); |
---|
453 | // find_first_not_of( s1, s2 ); find_last_not_of( s1, s2 ); |
---|
454 | sin | getline( s1 ); |
---|
455 | sout | s1; |
---|
456 | \end{cfa} |
---|
457 | \end{tabular} |
---|
458 | \end{cquote} |
---|
459 | |
---|
460 | |
---|
461 | \section{\texorpdfstring{\lstinline{uArray}}{uArray}} |
---|
462 | |
---|
463 | \begin{cquote} |
---|
464 | \begin{tabular}{@{}l|l@{}} |
---|
465 | \begin{uC++} |
---|
466 | #include <iostream> |
---|
467 | using namespace std; |
---|
468 | struct S { |
---|
469 | int i; |
---|
470 | S( int i ) { S::i = i; cout << "ctor " << S::i << endl; } |
---|
471 | ~S() { S::i = i; cout << "dtor " << S::i << endl; } |
---|
472 | }; |
---|
473 | int main() { |
---|
474 | enum { N = 5 }; |
---|
475 | @uArray( S, s, N );@ // no constructor calls |
---|
476 | for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // constructor calls |
---|
477 | for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl; |
---|
478 | } |
---|
479 | \end{uC++} |
---|
480 | & |
---|
481 | \begin{cfa} |
---|
482 | #include <fstream.hfa> |
---|
483 | #include <array.hfa> |
---|
484 | struct S { |
---|
485 | int i; |
---|
486 | }; |
---|
487 | void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; } |
---|
488 | void ^?{}( S & s ) { sout | "dtor" | s.i; } |
---|
489 | int main() { |
---|
490 | enum { N = 5 }; |
---|
491 | @array( S, N ) s = { delay_init };@ // no constructor calls |
---|
492 | for ( i; N ) @s[i]{ i }@; // constructor calls |
---|
493 | for ( i; N ) sout | s[i]@.@i; |
---|
494 | } |
---|
495 | \end{cfa} |
---|
496 | \end{tabular} |
---|
497 | \end{cquote} |
---|
498 | |
---|
499 | |
---|
500 | \section{Coroutines} |
---|
501 | |
---|
502 | \begin{cquote} |
---|
503 | \begin{tabular}{@{}l|ll@{}} |
---|
504 | \begin{uC++} |
---|
505 | |
---|
506 | @_Coroutine@ C { |
---|
507 | // private coroutine fields |
---|
508 | void main() { |
---|
509 | ... @suspend();@ ... |
---|
510 | ... @_Resume E( ... ) _At partner;@ |
---|
511 | ... @uThisCoroutine();@ ... |
---|
512 | |
---|
513 | } |
---|
514 | public: |
---|
515 | void mem( ... ) { |
---|
516 | ... @resume();@ ... |
---|
517 | } |
---|
518 | }; |
---|
519 | \end{uC++} |
---|
520 | & |
---|
521 | \begin{cfa} |
---|
522 | #include <$coroutine$.hfa> |
---|
523 | @coroutine@ C { |
---|
524 | // private coroutine fields |
---|
525 | |
---|
526 | }; |
---|
527 | void main( C & c ) { |
---|
528 | ... @suspend;@ ... // keyword not routine |
---|
529 | ... @resumeAt( partner, ExceptionInst( E, ... ) );@ |
---|
530 | ... @active_coroutine();@ ... |
---|
531 | } |
---|
532 | void mem( C & c, ... ) { |
---|
533 | ... @resume( c );@ ... |
---|
534 | } |
---|
535 | \end{cfa} |
---|
536 | \\ |
---|
537 | \multicolumn{2}{@{}l@{}}{\lstinline{C c;}} |
---|
538 | \end{tabular} |
---|
539 | \end{cquote} |
---|
540 | |
---|
541 | |
---|
542 | \section{\lstinline{COBEGIN}/\lstinline{COFOR}} |
---|
543 | |
---|
544 | \begin{cquote} |
---|
545 | \begin{tabular}{@{}l|ll@{}} |
---|
546 | \begin{uC++} |
---|
547 | |
---|
548 | #include <uCobegin.h> |
---|
549 | int main() { |
---|
550 | @COBEGIN@ |
---|
551 | BEGIN osacquire( cout ) << "A" << endl; END |
---|
552 | BEGIN osacquire( cout ) << "B" << endl; END |
---|
553 | BEGIN osacquire( cout ) << "C" << endl; END |
---|
554 | BEGIN osacquire( cout ) << "D" << endl; END |
---|
555 | BEGIN osacquire( cout ) << "E" << endl; END |
---|
556 | @COEND@ |
---|
557 | @COFOR@( i, 1, 10, |
---|
558 | osacquire( cout ) << i << endl; |
---|
559 | ) |
---|
560 | } |
---|
561 | \end{uC++} |
---|
562 | & |
---|
563 | \begin{cfa} |
---|
564 | #include <mutex_stmt.hfa> |
---|
565 | #include <$cofor$.hfa> |
---|
566 | int main() { |
---|
567 | { |
---|
568 | @corun@ { mutex( sout ) sout | "A"; } |
---|
569 | corun { mutex( sout ) sout | "B"; } |
---|
570 | corun { mutex( sout ) sout | "C"; } |
---|
571 | corun { mutex( sout ) sout | "D"; } |
---|
572 | corun { mutex( sout ) sout | "E"; } |
---|
573 | } |
---|
574 | @cofor@( i; 10 ) { |
---|
575 | mutex( sout ) sout | i; |
---|
576 | } |
---|
577 | } |
---|
578 | \end{cfa} |
---|
579 | \end{tabular} |
---|
580 | \end{cquote} |
---|
581 | |
---|
582 | |
---|
583 | \section{Actor} |
---|
584 | |
---|
585 | \begin{cquote} |
---|
586 | \begin{tabular}{@{}l|ll@{}} |
---|
587 | \begin{uC++} |
---|
588 | #include <iostream> |
---|
589 | using namespace std; |
---|
590 | #include <uActor.h> |
---|
591 | |
---|
592 | struct StrMsg : @public uActor::Message@ { |
---|
593 | const char * val; // string message |
---|
594 | |
---|
595 | |
---|
596 | StrMsg( const char * val ) : |
---|
597 | @Message( uActor::Delete )@, // delete after use |
---|
598 | val( val ) {} |
---|
599 | }; |
---|
600 | _Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$ |
---|
601 | Allocation receive( Message & msg ) { |
---|
602 | Case( StrMsg, msg ) { // discriminate |
---|
603 | osacquire( cout ) << msg_d->val << endl; |
---|
604 | }; |
---|
605 | return Delete; // delete after use |
---|
606 | } |
---|
607 | }; |
---|
608 | int main() { |
---|
609 | @uActor::start();@ // start actor system |
---|
610 | *new Hello() | *new StrMsg( "hello" ); |
---|
611 | *new Hello() | *new StrMsg( "bonjour" ); |
---|
612 | @uActor::stop();@ // wait for all actors to terminate |
---|
613 | } |
---|
614 | \end{uC++} |
---|
615 | & |
---|
616 | \begin{cfa} |
---|
617 | #include <fstream.hfa> |
---|
618 | #include <mutex_stmt.hfa> |
---|
619 | #include <actor.hfa> |
---|
620 | |
---|
621 | struct StrMsg { |
---|
622 | @inline message;@ // derived message |
---|
623 | const char * val; // string message |
---|
624 | }; |
---|
625 | void ?{}( StrMsg & msg, char * str ) { |
---|
626 | msg.val = str; |
---|
627 | @set_allocation( msg, Delete );@ // delete after use |
---|
628 | } |
---|
629 | struct Hello { |
---|
630 | @inline actor;@ // derived actor |
---|
631 | }; |
---|
632 | allocation receive( Hello & receiver, StrMsg & msg ) { |
---|
633 | mutex( sout ) sout | msg.val; |
---|
634 | return Delete; // delete after use |
---|
635 | } |
---|
636 | |
---|
637 | int main() { |
---|
638 | @start_actor_system();@ // start actor system |
---|
639 | *(Hello *)new() | *(StrMsg *)new( "hello" ); |
---|
640 | *(Hello *)new() | *(StrMsg *)new( "bonjour" ); |
---|
641 | @stop_actor_system();@ // wait for all actors to terminate |
---|
642 | } |
---|
643 | \end{cfa} |
---|
644 | \end{tabular} |
---|
645 | \end{cquote} |
---|
646 | |
---|
647 | |
---|
648 | \section{Threads} |
---|
649 | |
---|
650 | \begin{cquote} |
---|
651 | \begin{tabular}{@{}l|ll@{}} |
---|
652 | \begin{uC++} |
---|
653 | |
---|
654 | @_Task@ T { |
---|
655 | // private task fields |
---|
656 | void main() { |
---|
657 | ... _Resume E( ... ) _At partner; |
---|
658 | ... uThisTask(); ... |
---|
659 | } |
---|
660 | public: |
---|
661 | }; |
---|
662 | \end{uC++} |
---|
663 | & |
---|
664 | \begin{cfa} |
---|
665 | #include <$thread$.hfa> |
---|
666 | @thread@ T { |
---|
667 | // private task fields |
---|
668 | |
---|
669 | }; |
---|
670 | void main( @T & t@ ) { |
---|
671 | ... resumeAt( partner, ExceptionInst( E, ... ) ); |
---|
672 | ... active_thread(); ... |
---|
673 | } |
---|
674 | \end{cfa} |
---|
675 | \\ |
---|
676 | \multicolumn{2}{@{}l@{}}{\lstinline{T t; // start thread in main routine}} |
---|
677 | \end{tabular} |
---|
678 | \end{cquote} |
---|
679 | |
---|
680 | |
---|
681 | \section{Locks} |
---|
682 | |
---|
683 | \begin{cquote} |
---|
684 | \begin{tabular}{@{}l|ll@{}} |
---|
685 | \begin{uC++} |
---|
686 | |
---|
687 | uOwnerLock m; |
---|
688 | uCondLock s; |
---|
689 | m.acquire(); |
---|
690 | if ( ! s.empty() ) s.wait( m ); |
---|
691 | else { |
---|
692 | m.release(); |
---|
693 | } |
---|
694 | @osacquire( cout )@ << i << endl; |
---|
695 | \end{uC++} |
---|
696 | & |
---|
697 | \begin{cfa} |
---|
698 | #include <locks.hfa> |
---|
699 | owner_lock m; |
---|
700 | condition_variable( owner_lock ) s; // generic type on mutex lock |
---|
701 | lock( m ); |
---|
702 | if ( ! empty( s ) ) wait( s, m ); |
---|
703 | else { |
---|
704 | unlock( m ); |
---|
705 | } |
---|
706 | @mutex( sout )@ sout | i; // thread safe I/O |
---|
707 | \end{cfa} |
---|
708 | \end{tabular} |
---|
709 | \end{cquote} |
---|
710 | |
---|
711 | |
---|
712 | \section{Monitors} |
---|
713 | |
---|
714 | Internal Scheduling |
---|
715 | \begin{cquote} |
---|
716 | \begin{tabular}{@{}l|ll@{}} |
---|
717 | \begin{uC++} |
---|
718 | |
---|
719 | @_Monitor@ BoundedBufferI { |
---|
720 | @uCondition@ full, empty; |
---|
721 | int front = 0, back = 0, count = 0; |
---|
722 | int elements[20]; |
---|
723 | public: |
---|
724 | |
---|
725 | |
---|
726 | |
---|
727 | @_Nomutex@ int query() const { return count; } |
---|
728 | |
---|
729 | void insert( int elem ) { |
---|
730 | if ( count == 20 ) @empty.wait();@ |
---|
731 | elements[back] = elem; |
---|
732 | back = ( back + 1 ) % 20; |
---|
733 | count += 1; |
---|
734 | @full.signal();@ |
---|
735 | } |
---|
736 | int remove() { |
---|
737 | if ( count == 0 ) @full.wait();@ |
---|
738 | int elem = elements[front]; |
---|
739 | front = ( front + 1 ) % 20; |
---|
740 | count -= 1; |
---|
741 | @empty.signal();@ |
---|
742 | return elem; |
---|
743 | } |
---|
744 | }; |
---|
745 | \end{uC++} |
---|
746 | & |
---|
747 | \begin{cfa} |
---|
748 | #include <$monitor$.hfa> |
---|
749 | @monitor@ BoundedBufferI { |
---|
750 | @condition@ full, empty; |
---|
751 | int front, back, count; |
---|
752 | int elements[20]; |
---|
753 | }; |
---|
754 | void ?{}( BoundedBufferI & buf ) with( buf ) { |
---|
755 | front = back = count = 0; |
---|
756 | } |
---|
757 | int query( BoundedBufferI & buf ) { return buf.count; } |
---|
758 | int remove( BoundedBufferI & @mutex@ buf ); // forward |
---|
759 | void insert( BoundedBufferI & @mutex@ buf, int elem ) with( buf ) { |
---|
760 | if ( count == 20 ) @wait( empty );@ |
---|
761 | elements[back] = elem; |
---|
762 | back = ( back + 1 ) % 20; |
---|
763 | count += 1 |
---|
764 | @signal( full );@ |
---|
765 | } |
---|
766 | int remove( BoundedBufferI & @mutex@ buf ) with( buf ) { |
---|
767 | if ( count == 0 ) @wait( full );@ |
---|
768 | int elem = elements[front]; |
---|
769 | front = ( front + 1 ) % 20; |
---|
770 | count -= 1; |
---|
771 | @signal( empty );@ |
---|
772 | return elem; |
---|
773 | } |
---|
774 | |
---|
775 | \end{cfa} |
---|
776 | \end{tabular} |
---|
777 | \end{cquote} |
---|
778 | \enlargethispage{1000pt} |
---|
779 | External Scheduling |
---|
780 | \begin{cquote} |
---|
781 | \begin{tabular}{@{}l|ll@{}} |
---|
782 | \begin{uC++} |
---|
783 | |
---|
784 | _Monitor BoundedBuffer { |
---|
785 | int front = 0, back = 0, count = 0; |
---|
786 | int elements[20]; |
---|
787 | public: |
---|
788 | _Nomutex int query() const { return count; } |
---|
789 | void insert( int elem ); |
---|
790 | int remove(); |
---|
791 | }; |
---|
792 | |
---|
793 | void BoundedBuffer::insert( int elem ) { |
---|
794 | if ( count == 20 ) @_Accept( remove );@ |
---|
795 | elements[back] = elem; |
---|
796 | back = ( back + 1 ) % 20; |
---|
797 | count += 1; |
---|
798 | } |
---|
799 | int BoundedBuffer::remove() { |
---|
800 | if ( count == 0 ) @_Accept( insert );@ |
---|
801 | int elem = elements[front]; |
---|
802 | front = ( front + 1 ) % 20; |
---|
803 | count -= 1; |
---|
804 | return elem; |
---|
805 | } |
---|
806 | \end{uC++} |
---|
807 | & |
---|
808 | \begin{cfa} |
---|
809 | #include <$monitor$.hfa> |
---|
810 | monitor BoundedBuffer { |
---|
811 | int front, back, count; |
---|
812 | int elements[20]; |
---|
813 | }; |
---|
814 | void ?{}( BoundedBuffer & buf ) with( buf ) { |
---|
815 | front = back = count = 0; |
---|
816 | } |
---|
817 | int query( BoundedBuffer & buf ) { return buf.count; } |
---|
818 | int remove( BoundedBuffer & @mutex@ buf ); // forward |
---|
819 | void insert( BoundedBuffer & @mutex@ buf, int elem ) with( buf ) { |
---|
820 | if ( count == 20 ) @waitfor( remove : buf );@ |
---|
821 | elements[back] = elem; |
---|
822 | back = ( back + 1 ) % 20; |
---|
823 | count += 1; |
---|
824 | } |
---|
825 | int remove( BoundedBuffer & @mutex@ buf ) with( buf ) { |
---|
826 | if ( count == 0 ) @waitfor( insert : buf );@ |
---|
827 | int elem = elements[front]; |
---|
828 | front = ( front + 1 ) % 20; |
---|
829 | count -= 1; |
---|
830 | return elem; |
---|
831 | } |
---|
832 | \end{cfa} |
---|
833 | \end{tabular} |
---|
834 | \end{cquote} |
---|
835 | |
---|
836 | |
---|
837 | \input{uC++toCFA.ind} |
---|
838 | |
---|
839 | % \bibliographystyle{plain} |
---|
840 | % \bibliography{pl} |
---|
841 | |
---|
842 | \end{document} |
---|
843 | |
---|
844 | % Local Variables: % |
---|
845 | % tab-width: 4 % |
---|
846 | % fill-column: 100 % |
---|
847 | % compile-command: "make" % |
---|
848 | % End: % |
---|