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 15 09:55:34 2024
|
---|
14 | %% Update Count : 6249
|
---|
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{Constructor / Destructor}
|
---|
360 |
|
---|
361 | \begin{cquote}
|
---|
362 | \begin{tabular}{@{}l|l@{}}
|
---|
363 | \begin{uC++}
|
---|
364 |
|
---|
365 | struct S {
|
---|
366 | int i, j;
|
---|
367 |
|
---|
368 | @S@( int i, int j ) { S::i = i; S::j = j; }
|
---|
369 | @~S@() {}
|
---|
370 | };
|
---|
371 | S s1 = { 1, 2 };
|
---|
372 |
|
---|
373 | S * s2 = new S{ 1, 2 };
|
---|
374 | delete s2;
|
---|
375 | s2 = new S{ 1, 2 };
|
---|
376 | delete s2;
|
---|
377 | S & s3 = *new S{ 1, 2 };
|
---|
378 | delete &s3;
|
---|
379 | s3 = *new S{ 1, 2 };
|
---|
380 | delete &s3;
|
---|
381 | \end{uC++}
|
---|
382 | &
|
---|
383 | \begin{cfa}
|
---|
384 | #include <stdlib.hfa> // new (malloc)
|
---|
385 | struct S {
|
---|
386 | int i, j;
|
---|
387 | };
|
---|
388 | void @?{}@( S & s, int i, int j ) { s.i = i; s.j = j; }
|
---|
389 | void @^?{}@( S & s ) { s.i = 0; s.j = 0; }
|
---|
390 |
|
---|
391 | S s1 = { 1, 2 };
|
---|
392 | // cannot use 0/1 (zero_t/one_t) with "new"
|
---|
393 | S * s2 = new( 1@n@, 2 ); // n => (int)
|
---|
394 | delete( s2 );
|
---|
395 | s2 = new( 1n, 2 );
|
---|
396 | delete( s2 );
|
---|
397 | S & s3 = *new( 1n, 2 );
|
---|
398 | delete( s3 );
|
---|
399 | &s3 = &*new( 1n, 2 );
|
---|
400 | delete( s3 );
|
---|
401 | \end{cfa}
|
---|
402 | \end{tabular}
|
---|
403 | \end{cquote}
|
---|
404 |
|
---|
405 |
|
---|
406 | \section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
|
---|
407 |
|
---|
408 | \begin{cquote}
|
---|
409 | \begin{tabular}{@{}l|l@{}}
|
---|
410 | \begin{uC++}
|
---|
411 | struct S {
|
---|
412 | int i = 0; // cheat, implicit default constructor
|
---|
413 | int setter( int j ) { int t = i; i = j; return t; }
|
---|
414 | int getter() { return i; }
|
---|
415 | };
|
---|
416 | S s;
|
---|
417 | @s.@setter( 3 ); // object calls
|
---|
418 | int k = @s.@getter();
|
---|
419 | \end{uC++}
|
---|
420 | &
|
---|
421 | \begin{cfa}
|
---|
422 | struct S { int i; };
|
---|
423 | void ?{}( S & s ) { s.i = 0; } // explicit default constructor
|
---|
424 | int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
|
---|
425 | int getter( @S & s@ ) @with( s )@ { return i; }
|
---|
426 |
|
---|
427 | S s;
|
---|
428 | setter( @s,@ 3 ); // normal calls
|
---|
429 | int k = getter( @s@ );
|
---|
430 | \end{cfa}
|
---|
431 | \end{tabular}
|
---|
432 | \end{cquote}
|
---|
433 |
|
---|
434 |
|
---|
435 | \section{String}
|
---|
436 |
|
---|
437 | \begin{cquote}
|
---|
438 | \begin{tabular}{@{}l|l@{}}
|
---|
439 | \multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\
|
---|
440 | \begin{uC++}
|
---|
441 | s1 = "abcdefg";
|
---|
442 | s2 = s1;
|
---|
443 | s1 += s2;
|
---|
444 | s1 == s2; s1 != s2;
|
---|
445 | s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
|
---|
446 | s1.length();
|
---|
447 | s1[3];
|
---|
448 | s1.substr( 2 ); s1.substr( 2, 3 );
|
---|
449 | s1.replace( 2, 5, s2 );
|
---|
450 | s1.find( s2 ); s1.rfind( s2 );
|
---|
451 | s1.find_first_of( s2 ); s1.find_last_of( s2 );
|
---|
452 | s1.find_first_not_of( s2 ); s1.find_last_not_of( s2 );
|
---|
453 | getline( cin, s1 );
|
---|
454 | cout << s1 << endl;
|
---|
455 | \end{uC++}
|
---|
456 | &
|
---|
457 | \begin{cfa}
|
---|
458 | s1 = "abcdefg";
|
---|
459 | s2 = s1;
|
---|
460 | s1 += s2;
|
---|
461 | s1 == s2; s1 != s2;
|
---|
462 | s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
|
---|
463 | size( s1 );
|
---|
464 | s1[3];
|
---|
465 | s1( 2 ); s1( 2, 3 );
|
---|
466 | // replace( s1, 2, 5, s2 );
|
---|
467 | // find( s1, s2 ), rfind( s1, s2 );
|
---|
468 | // find_first_of( s2 ); find_last_of( s2 );
|
---|
469 | // find_first_not_of( s1, s2 ); find_last_not_of( s1, s2 );
|
---|
470 | sin | getline( s1 );
|
---|
471 | sout | s1;
|
---|
472 | \end{cfa}
|
---|
473 | \end{tabular}
|
---|
474 | \end{cquote}
|
---|
475 |
|
---|
476 |
|
---|
477 | \section{\texorpdfstring{\lstinline{uArray}}{uArray}}
|
---|
478 |
|
---|
479 | \begin{cquote}
|
---|
480 | \begin{tabular}{@{}l|l@{}}
|
---|
481 | \begin{uC++}
|
---|
482 | #include <iostream>
|
---|
483 | using namespace std;
|
---|
484 | struct S {
|
---|
485 | int i;
|
---|
486 | S( int i ) { S::i = i; cout << "ctor " << S::i << endl; }
|
---|
487 | ~S() { S::i = i; cout << "dtor " << S::i << endl; }
|
---|
488 | };
|
---|
489 | int main() {
|
---|
490 | enum { N = 5 };
|
---|
491 | @uArray( S, s, N );@ // no constructor calls
|
---|
492 | for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // constructor calls
|
---|
493 | for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl;
|
---|
494 | }
|
---|
495 | \end{uC++}
|
---|
496 | &
|
---|
497 | \begin{cfa}
|
---|
498 | #include <fstream.hfa>
|
---|
499 | #include <array.hfa>
|
---|
500 | struct S {
|
---|
501 | int i;
|
---|
502 | };
|
---|
503 | void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; }
|
---|
504 | void ^?{}( S & s ) { sout | "dtor" | s.i; }
|
---|
505 | int main() {
|
---|
506 | enum { N = 5 };
|
---|
507 | @array( S, N ) s = { delay_init };@ // no constructor calls
|
---|
508 | for ( i; N ) @s[i]{ i }@; // constructor calls
|
---|
509 | for ( i; N ) sout | s[i]@.@i;
|
---|
510 | }
|
---|
511 | \end{cfa}
|
---|
512 | \end{tabular}
|
---|
513 | \end{cquote}
|
---|
514 |
|
---|
515 |
|
---|
516 | \section{Coroutine}
|
---|
517 |
|
---|
518 | \begin{cquote}
|
---|
519 | \begin{tabular}{@{}l|ll@{}}
|
---|
520 | \begin{uC++}
|
---|
521 |
|
---|
522 | @_Coroutine@ C {
|
---|
523 | // private coroutine fields
|
---|
524 | void main() {
|
---|
525 | ... @suspend();@ ...
|
---|
526 | ... @_Resume E( ... ) _At partner;@
|
---|
527 | ... @uThisCoroutine();@ ...
|
---|
528 |
|
---|
529 | }
|
---|
530 | public:
|
---|
531 | void mem( ... ) {
|
---|
532 | ... @resume();@ ...
|
---|
533 | }
|
---|
534 | };
|
---|
535 | \end{uC++}
|
---|
536 | &
|
---|
537 | \begin{cfa}
|
---|
538 | #include <$coroutine$.hfa>
|
---|
539 | @coroutine@ C {
|
---|
540 | // private coroutine fields
|
---|
541 |
|
---|
542 | };
|
---|
543 | void main( C & c ) {
|
---|
544 | ... @suspend;@ ... // keyword not routine
|
---|
545 | ... @resumeAt( partner, ExceptionInst( E, ... ) );@
|
---|
546 | ... @active_coroutine();@ ...
|
---|
547 | }
|
---|
548 | void mem( C & c, ... ) {
|
---|
549 | ... @resume( c );@ ...
|
---|
550 | }
|
---|
551 | \end{cfa}
|
---|
552 | \\
|
---|
553 | \multicolumn{2}{@{}l@{}}{\lstinline{C c;}}
|
---|
554 | \end{tabular}
|
---|
555 | \end{cquote}
|
---|
556 |
|
---|
557 |
|
---|
558 | \section{Thread}
|
---|
559 |
|
---|
560 | \begin{cquote}
|
---|
561 | \begin{tabular}{@{}l|ll@{}}
|
---|
562 | \begin{uC++}
|
---|
563 |
|
---|
564 | @_Task@ T {
|
---|
565 | // private task fields
|
---|
566 | void main() {
|
---|
567 | ... @_Resume E( ... ) _At partner@;
|
---|
568 | ... @uThisTask();@ ...
|
---|
569 | }
|
---|
570 | public:
|
---|
571 | };
|
---|
572 | \end{uC++}
|
---|
573 | &
|
---|
574 | \begin{cfa}
|
---|
575 | #include <$thread$.hfa>
|
---|
576 | @thread@ T {
|
---|
577 | // private task fields
|
---|
578 |
|
---|
579 | };
|
---|
580 | void main( @T & t@ ) {
|
---|
581 | ... @resumeAt( partner, ExceptionInst( E, ... )@ );
|
---|
582 | ... @active_thread();@ ...
|
---|
583 | }
|
---|
584 | \end{cfa}
|
---|
585 | \\
|
---|
586 | \multicolumn{2}{@{}l@{}}{\lstinline{T t; // start thread in main routine}}
|
---|
587 | \end{tabular}
|
---|
588 | \end{cquote}
|
---|
589 |
|
---|
590 |
|
---|
591 | \section{\lstinline{COBEGIN}/\lstinline{COFOR}}
|
---|
592 |
|
---|
593 | \begin{cquote}
|
---|
594 | \begin{tabular}{@{}l|ll@{}}
|
---|
595 | \begin{uC++}
|
---|
596 |
|
---|
597 | #include <uCobegin.h>
|
---|
598 | int main() {
|
---|
599 | @COBEGIN@
|
---|
600 | BEGIN osacquire( cout ) << "A" << endl; END
|
---|
601 | BEGIN osacquire( cout ) << "B" << endl; END
|
---|
602 | BEGIN osacquire( cout ) << "C" << endl; END
|
---|
603 | BEGIN osacquire( cout ) << "D" << endl; END
|
---|
604 | BEGIN osacquire( cout ) << "E" << endl; END
|
---|
605 | @COEND@
|
---|
606 | @COFOR@( i, 1, 10,
|
---|
607 | osacquire( cout ) << i << endl;
|
---|
608 | )
|
---|
609 | }
|
---|
610 | \end{uC++}
|
---|
611 | &
|
---|
612 | \begin{cfa}
|
---|
613 | #include <mutex_stmt.hfa>
|
---|
614 | #include <$cofor$.hfa>
|
---|
615 | int main() {
|
---|
616 | {
|
---|
617 | @corun@ { mutex( sout ) sout | "A"; }
|
---|
618 | corun { mutex( sout ) sout | "B"; }
|
---|
619 | corun { mutex( sout ) sout | "C"; }
|
---|
620 | corun { mutex( sout ) sout | "D"; }
|
---|
621 | corun { mutex( sout ) sout | "E"; }
|
---|
622 | }
|
---|
623 | @cofor@( i; 10 ) {
|
---|
624 | mutex( sout ) sout | i;
|
---|
625 | }
|
---|
626 | }
|
---|
627 | \end{cfa}
|
---|
628 | \end{tabular}
|
---|
629 | \end{cquote}
|
---|
630 |
|
---|
631 |
|
---|
632 | \section{Actor}
|
---|
633 |
|
---|
634 | \begin{cquote}
|
---|
635 | \begin{tabular}{@{}l|ll@{}}
|
---|
636 | \begin{uC++}
|
---|
637 | #include <iostream>
|
---|
638 | using namespace std;
|
---|
639 | #include <uActor.h>
|
---|
640 |
|
---|
641 | struct StrMsg : @public uActor::Message@ {
|
---|
642 |
|
---|
643 | const char * val; // string message
|
---|
644 |
|
---|
645 | StrMsg( const char * val ) :
|
---|
646 | @Message( uActor::Delete )@, // delete after use
|
---|
647 | val( val ) {}
|
---|
648 | };
|
---|
649 | _Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$
|
---|
650 | Allocation receive( Message & msg ) {
|
---|
651 | Case( @StartMsg@, msg ) { // discriminate
|
---|
652 |
|
---|
653 | } else Case( StrMsg, msg ) {
|
---|
654 | osacquire( cout ) << msg_d->val << endl;
|
---|
655 |
|
---|
656 | } else Case( @StopMsg@, msg )
|
---|
657 | return Delete; // delete actor
|
---|
658 | return Nodelete; // reuse actor
|
---|
659 | }
|
---|
660 | };
|
---|
661 | int main() {
|
---|
662 | @uActor::start();@ // start actor system
|
---|
663 | *new Hello() | uActor::startMsg
|
---|
664 | | *new StrMsg( "hello" ) | uActor::stopMsg;
|
---|
665 | *new Hello() | uActor::startMsg
|
---|
666 | | *new StrMsg( "bonjour" ) | uActor::stopMsg;
|
---|
667 | @uActor::stop();@ // wait for actors to terminate
|
---|
668 | }
|
---|
669 | \end{uC++}
|
---|
670 | &
|
---|
671 | \begin{cfa}
|
---|
672 | #include <fstream.hfa>
|
---|
673 | #include <mutex_stmt.hfa>
|
---|
674 | #include <actor.hfa>
|
---|
675 |
|
---|
676 | struct StrMsg {
|
---|
677 | @inline message;@ // derived message
|
---|
678 | const char * val; // string message
|
---|
679 | };
|
---|
680 | void ?{}( StrMsg & msg, const char * str ) {
|
---|
681 | @set_allocation( msg, Delete );@ // delete after use
|
---|
682 | msg.val = str;
|
---|
683 | }
|
---|
684 | struct Hello { @inline actor;@ }; // derived actor
|
---|
685 | allocation receive( Hello & receiver, @start_msg_t@ & ) {
|
---|
686 | return Nodelete;
|
---|
687 | }
|
---|
688 | allocation receive( Hello & receiver, StrMsg & msg ) {
|
---|
689 | mutex( sout ) sout | msg.val;
|
---|
690 | return Nodelete; // reuse actor
|
---|
691 | }
|
---|
692 | allocation receive( Hello & receiver, @stop_msg_t@ & ) {
|
---|
693 | return Delete; // delete actor
|
---|
694 | }
|
---|
695 |
|
---|
696 | int main() {
|
---|
697 | @actor_start();@ // start actor system
|
---|
698 | *(Hello *)new() | start_msg
|
---|
699 | | *(StrMsg *)new( "hello" ) | stop_msg;
|
---|
700 | *(Hello *)new() | start_msg
|
---|
701 | | *(StrMsg *)new( "bonjour" ) | stop_msg;
|
---|
702 | @actor_stop();@ // wait for actors to terminate
|
---|
703 | }
|
---|
704 | \end{cfa}
|
---|
705 | \end{tabular}
|
---|
706 | \end{cquote}
|
---|
707 |
|
---|
708 |
|
---|
709 | \section{Locks}
|
---|
710 |
|
---|
711 | \begin{cquote}
|
---|
712 | \begin{tabular}{@{}l|ll@{}}
|
---|
713 | \begin{uC++}
|
---|
714 |
|
---|
715 | uOwnerLock m;
|
---|
716 | uCondLock s;
|
---|
717 | m.acquire();
|
---|
718 | if ( ! s.empty() ) s.wait( m );
|
---|
719 | else {
|
---|
720 | m.release();
|
---|
721 | }
|
---|
722 | @osacquire( cout )@ << i << endl;
|
---|
723 | \end{uC++}
|
---|
724 | &
|
---|
725 | \begin{cfa}
|
---|
726 | #include <locks.hfa>
|
---|
727 | owner_lock m;
|
---|
728 | condition_variable( owner_lock ) s; // generic type on mutex lock
|
---|
729 | lock( m );
|
---|
730 | if ( ! empty( s ) ) wait( s, m );
|
---|
731 | else {
|
---|
732 | unlock( m );
|
---|
733 | }
|
---|
734 | @mutex( sout )@ sout | i; // thread safe I/O
|
---|
735 | \end{cfa}
|
---|
736 | \end{tabular}
|
---|
737 | \end{cquote}
|
---|
738 |
|
---|
739 |
|
---|
740 | \section{Barrier}
|
---|
741 |
|
---|
742 | \begin{cquote}
|
---|
743 | \begin{tabular}{@{}l|ll@{}}
|
---|
744 | \begin{uC++}
|
---|
745 | #include <iostream>
|
---|
746 | using namespace std;
|
---|
747 | #include <uBarrier.h>
|
---|
748 |
|
---|
749 | @_Cormonitor@ Barrier
|
---|
750 | : @public uBarrier@ { // inheritance
|
---|
751 | int total;
|
---|
752 | void @last@() { cout << total << endl; }
|
---|
753 | public:
|
---|
754 | Barrier( unsigned int group ) :
|
---|
755 | @uBarrier( group )@ {
|
---|
756 | total = 0;
|
---|
757 | }
|
---|
758 | void @block@( int subtotal ) {
|
---|
759 |
|
---|
760 |
|
---|
761 | total += subtotal;
|
---|
762 | @uBarrier::block();@
|
---|
763 | }
|
---|
764 | };
|
---|
765 | enum { N = 3 };
|
---|
766 | Barrier b{ N };
|
---|
767 |
|
---|
768 | _Task T {
|
---|
769 | void main() {
|
---|
770 | for ( int i = 0; i < 10; i += 1 ) {
|
---|
771 | b.block( 1 );
|
---|
772 | }
|
---|
773 | }
|
---|
774 | };
|
---|
775 | int main() {
|
---|
776 | uProcessor p[N - 1];
|
---|
777 | T t[N];
|
---|
778 | }
|
---|
779 | \end{uC++}
|
---|
780 | &
|
---|
781 | \begin{cfa}
|
---|
782 | #include <fstream.hfa>
|
---|
783 | #include <$thread$.hfa>
|
---|
784 | #include <barrier.hfa>
|
---|
785 | #include <mutex_stmt.hfa>
|
---|
786 | struct Barrier {
|
---|
787 | @barrier b;@ // containment
|
---|
788 | int total;
|
---|
789 |
|
---|
790 | };
|
---|
791 | void ?{}( Barrier & B, unsigned int group ) with(B) {
|
---|
792 | @?{}( b, group );@ // initialize barrier
|
---|
793 | total = 0;
|
---|
794 | }
|
---|
795 | unsigned int block( Barrier & B, int subtotal ) with(B) {
|
---|
796 | void @last@() { sout | total; } // called by Gth arriving thread
|
---|
797 | @mutex( b )@ { // use barrier's mutual exclusion
|
---|
798 | total += subtotal;
|
---|
799 | return @block@( b, last ); // wait for barrier trigger
|
---|
800 | }
|
---|
801 | }
|
---|
802 | enum { N = 3 };
|
---|
803 | Barrier b{ N };
|
---|
804 |
|
---|
805 | thread T {};
|
---|
806 | void main( T & ) {
|
---|
807 | for ( 10 ) {
|
---|
808 | block( b, 1 );
|
---|
809 | }
|
---|
810 | }
|
---|
811 |
|
---|
812 | int main() {
|
---|
813 | processor p[N - 1];
|
---|
814 | T t[N];
|
---|
815 | }
|
---|
816 | \end{cfa}
|
---|
817 | \end{tabular}
|
---|
818 | \end{cquote}
|
---|
819 |
|
---|
820 | \newpage
|
---|
821 |
|
---|
822 | \section{Monitor}
|
---|
823 |
|
---|
824 | Internal Scheduling
|
---|
825 | \begin{cquote}
|
---|
826 | \begin{tabular}{@{}l|ll@{}}
|
---|
827 | \begin{uC++}
|
---|
828 |
|
---|
829 | @_Monitor@ BoundedBufferI {
|
---|
830 | @uCondition@ full, empty;
|
---|
831 | int front = 0, back = 0, count = 0;
|
---|
832 | int elements[20];
|
---|
833 | public:
|
---|
834 |
|
---|
835 |
|
---|
836 |
|
---|
837 | @_Nomutex@ int query() const { return count; }
|
---|
838 |
|
---|
839 | void insert( int elem ) {
|
---|
840 | if ( count == 20 ) @empty.wait();@
|
---|
841 | elements[back] = elem;
|
---|
842 | back = ( back + 1 ) % 20;
|
---|
843 | count += 1;
|
---|
844 | @full.signal();@
|
---|
845 | }
|
---|
846 | int remove() {
|
---|
847 | if ( count == 0 ) @full.wait();@
|
---|
848 | int elem = elements[front];
|
---|
849 | front = ( front + 1 ) % 20;
|
---|
850 | count -= 1;
|
---|
851 | @empty.signal();@
|
---|
852 | return elem;
|
---|
853 | }
|
---|
854 | };
|
---|
855 | \end{uC++}
|
---|
856 | &
|
---|
857 | \begin{cfa}
|
---|
858 | #include <$monitor$.hfa>
|
---|
859 | @monitor@ BoundedBufferI {
|
---|
860 | @condition@ full, empty;
|
---|
861 | int front, back, count;
|
---|
862 | int elements[20];
|
---|
863 | };
|
---|
864 | void ?{}( BoundedBufferI & buf ) with( buf ) {
|
---|
865 | front = back = count = 0;
|
---|
866 | }
|
---|
867 | int query( BoundedBufferI & buf ) { return buf.count; }
|
---|
868 | int remove( BoundedBufferI & @mutex@ buf ); // forward
|
---|
869 | void insert( BoundedBufferI & @mutex@ buf, int elem ) with( buf ) {
|
---|
870 | if ( count == 20 ) @wait( empty );@
|
---|
871 | elements[back] = elem;
|
---|
872 | back = ( back + 1 ) % 20;
|
---|
873 | count += 1
|
---|
874 | @signal( full );@
|
---|
875 | }
|
---|
876 | int remove( BoundedBufferI & @mutex@ buf ) with( buf ) {
|
---|
877 | if ( count == 0 ) @wait( full );@
|
---|
878 | int elem = elements[front];
|
---|
879 | front = ( front + 1 ) % 20;
|
---|
880 | count -= 1;
|
---|
881 | @signal( empty );@
|
---|
882 | return elem;
|
---|
883 | }
|
---|
884 |
|
---|
885 | \end{cfa}
|
---|
886 | \end{tabular}
|
---|
887 | \end{cquote}
|
---|
888 |
|
---|
889 | \enlargethispage{1000pt}
|
---|
890 |
|
---|
891 | \noindent
|
---|
892 | External Scheduling
|
---|
893 | \begin{cquote}
|
---|
894 | \begin{tabular}{@{}l|ll@{}}
|
---|
895 | \begin{uC++}
|
---|
896 |
|
---|
897 | _Monitor BoundedBuffer {
|
---|
898 | int front = 0, back = 0, count = 0;
|
---|
899 | int elements[20];
|
---|
900 | public:
|
---|
901 | _Nomutex int query() const { return count; }
|
---|
902 | void insert( int elem );
|
---|
903 | int remove();
|
---|
904 | };
|
---|
905 |
|
---|
906 | void BoundedBuffer::insert( int elem ) {
|
---|
907 | if ( count == 20 ) @_Accept( remove );@
|
---|
908 | elements[back] = elem;
|
---|
909 | back = ( back + 1 ) % 20;
|
---|
910 | count += 1;
|
---|
911 | }
|
---|
912 | int BoundedBuffer::remove() {
|
---|
913 | if ( count == 0 ) @_Accept( insert );@
|
---|
914 | int elem = elements[front];
|
---|
915 | front = ( front + 1 ) % 20;
|
---|
916 | count -= 1;
|
---|
917 | return elem;
|
---|
918 | }
|
---|
919 | \end{uC++}
|
---|
920 | &
|
---|
921 | \begin{cfa}
|
---|
922 | #include <$monitor$.hfa>
|
---|
923 | monitor BoundedBuffer {
|
---|
924 | int front, back, count;
|
---|
925 | int elements[20];
|
---|
926 | };
|
---|
927 | void ?{}( BoundedBuffer & buf ) with( buf ) {
|
---|
928 | front = back = count = 0;
|
---|
929 | }
|
---|
930 | int query( BoundedBuffer & buf ) { return buf.count; }
|
---|
931 | int remove( BoundedBuffer & @mutex@ buf ); // forward
|
---|
932 | void insert( BoundedBuffer & @mutex@ buf, int elem ) with( buf ) {
|
---|
933 | if ( count == 20 ) @waitfor( remove : buf );@
|
---|
934 | elements[back] = elem;
|
---|
935 | back = ( back + 1 ) % 20;
|
---|
936 | count += 1;
|
---|
937 | }
|
---|
938 | int remove( BoundedBuffer & @mutex@ buf ) with( buf ) {
|
---|
939 | if ( count == 0 ) @waitfor( insert : buf );@
|
---|
940 | int elem = elements[front];
|
---|
941 | front = ( front + 1 ) % 20;
|
---|
942 | count -= 1;
|
---|
943 | return elem;
|
---|
944 | }
|
---|
945 | \end{cfa}
|
---|
946 | \end{tabular}
|
---|
947 | \end{cquote}
|
---|
948 |
|
---|
949 |
|
---|
950 | \input{uC++toCFA.ind}
|
---|
951 |
|
---|
952 | % \bibliographystyle{plain}
|
---|
953 | % \bibliography{pl}
|
---|
954 |
|
---|
955 | \end{document}
|
---|
956 |
|
---|
957 | % Local Variables: %
|
---|
958 | % tab-width: 4 %
|
---|
959 | % fill-column: 100 %
|
---|
960 | % compile-command: "make" %
|
---|
961 | % End: %
|
---|