| [697c957] | 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
|
|---|
| [2b6db03] | 13 | %% Last Modified On : Sat Apr 13 11:11:39 2024
|
|---|
| 14 | %% Update Count : 5969
|
|---|
| [697c957] | 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}
|
|---|
| [946a6e4] | 121 | \uC to \CFA Cheat Sheet}
|
|---|
| [697c957] | 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}
|
|---|
| [2b6db03] | 143 | int i; char i; double i; $\C[2in]{// overload name i}$
|
|---|
| [697c957] | 144 | int i(); double i(); char i();
|
|---|
| [2b6db03] | 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$
|
|---|
| [697c957] | 151 | \end{cfa}
|
|---|
| 152 | \CFA has rebindable references.
|
|---|
| 153 | \begin{cquote}
|
|---|
| [2b6db03] | 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;}} \\
|
|---|
| [697c957] | 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
|
|---|
| [946a6e4] | 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}
|
|---|
| [2b6db03] | 203 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [946a6e4] | 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}
|
|---|
| [697c957] | 221 |
|
|---|
| 222 |
|
|---|
| 223 | \section{Looping}
|
|---|
| 224 |
|
|---|
| 225 | \begin{cquote}
|
|---|
| [2b6db03] | 226 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [697c957] | 227 | \begin{uC++}
|
|---|
| [946a6e4] | 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++}
|
|---|
| [697c957] | 245 | int i = 0
|
|---|
| 246 | for ( i = 0; i < 10; i += 1 ) { ... }
|
|---|
| [946a6e4] | 247 | @if ( i == 10 )@ { ... }
|
|---|
| [697c957] | 248 | \end{uC++}
|
|---|
| 249 | &
|
|---|
| 250 | \begin{cfa}
|
|---|
| 251 |
|
|---|
| 252 | for ( i; 10 ) { ... }
|
|---|
| [946a6e4] | 253 | @else@ { ... } // i == 10
|
|---|
| 254 | \end{cfa}
|
|---|
| 255 | \\
|
|---|
| 256 | \hline
|
|---|
| 257 | \begin{uC++}
|
|---|
| 258 | L1: for ( ;; ) {
|
|---|
| 259 | L2: for ( ;; ) {
|
|---|
| 260 | ... @break L1@; ... @break L2@; ...
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 | \end{uC++}
|
|---|
| 264 | &
|
|---|
| 265 | \begin{cfa}
|
|---|
| 266 | L1: for () {
|
|---|
| 267 | L2: for () {
|
|---|
| 268 | ... @break L1@; ... @break L2@; ...
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| [697c957] | 271 | \end{cfa}
|
|---|
| 272 | \end{tabular}
|
|---|
| 273 | \end{cquote}
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| [946a6e4] | 276 | \section{Exception}
|
|---|
| [697c957] | 277 |
|
|---|
| 278 | Currently, \CFA uses macros @ExceptionDecl@ and @ExceptionInst@ to declare and instantiate an exception.
|
|---|
| 279 | \begin{cquote}
|
|---|
| [2b6db03] | 280 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 281 | \begin{uC++}
|
|---|
| 282 |
|
|---|
| [946a6e4] | 283 | @_Exception@ E { // local or global scope
|
|---|
| [697c957] | 284 | ... // exception fields
|
|---|
| 285 | };
|
|---|
| 286 | try {
|
|---|
| 287 | ...
|
|---|
| [946a6e4] | 288 | if ( ... ) @_Resume@ E( /* initialization */ );
|
|---|
| 289 | if ( ... ) @_Throw@ E( /* initialization */ );
|
|---|
| 290 | ...
|
|---|
| 291 | } @_CatchResume@( E & ) { // should be reference
|
|---|
| [697c957] | 292 | ...
|
|---|
| 293 | } catch( E & ) {
|
|---|
| 294 | ...
|
|---|
| 295 | }
|
|---|
| 296 | \end{uC++}
|
|---|
| 297 | &
|
|---|
| 298 | \begin{cfa}
|
|---|
| 299 | #include <Exception.hfa>
|
|---|
| 300 | @ExceptionDecl@( E, // must be global scope
|
|---|
| 301 | ... // exception fields
|
|---|
| 302 | );
|
|---|
| 303 | try {
|
|---|
| 304 | ...
|
|---|
| [946a6e4] | 305 | if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ );
|
|---|
| 306 | if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ );
|
|---|
| [697c957] | 307 | ...
|
|---|
| [946a6e4] | 308 | } @catchResume@( E * ) { // must be pointer
|
|---|
| [697c957] | 309 | ...
|
|---|
| 310 | } catch( E * ) {
|
|---|
| 311 | ...
|
|---|
| 312 | }
|
|---|
| 313 | \end{cfa}
|
|---|
| 314 | \end{tabular}
|
|---|
| 315 | \end{cquote}
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| [946a6e4] | 318 | \section{Non-local Exception}
|
|---|
| 319 |
|
|---|
| 320 | \begin{cquote}
|
|---|
| [2b6db03] | 321 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [946a6e4] | 322 | \begin{uC++}
|
|---|
| [697c957] | 323 |
|
|---|
| [946a6e4] | 324 |
|
|---|
| 325 | void main() {
|
|---|
| 326 | try {
|
|---|
| 327 | _Enable {
|
|---|
| 328 | ... suspend(); ...
|
|---|
| 329 | }
|
|---|
| 330 | } @_CatchResume@( E & ) { // reference
|
|---|
| 331 | ...
|
|---|
| 332 | } catch( E & ) {
|
|---|
| 333 | ...
|
|---|
| 334 | }
|
|---|
| 335 | }
|
|---|
| 336 | \end{uC++}
|
|---|
| 337 | &
|
|---|
| 338 | \begin{cfa}
|
|---|
| 339 | #define resumePoll( coroutine ) resume( coroutine ); checked_poll()
|
|---|
| 340 | #define suspendPoll suspend; checked_poll()
|
|---|
| 341 | void main() {
|
|---|
| 342 | try {
|
|---|
| 343 | enable_ehm();
|
|---|
| 344 | ... suspendPoll ...
|
|---|
| 345 | disable_ehm();
|
|---|
| 346 | } @catchResume@( E * ) { // pointer
|
|---|
| 347 | ...
|
|---|
| 348 | } catch( E & ) {
|
|---|
| 349 | ...
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|
| 352 | \end{cfa}
|
|---|
| 353 | \end{tabular}
|
|---|
| 354 | \end{cquote}
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 | \section{Constructor / Destructor}
|
|---|
| [697c957] | 358 |
|
|---|
| 359 | \begin{cquote}
|
|---|
| [2b6db03] | 360 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [697c957] | 361 | \begin{uC++}
|
|---|
| [946a6e4] | 362 | struct S {
|
|---|
| 363 | ... // fields
|
|---|
| 364 | @S@(...) { ... }
|
|---|
| 365 | @~S@(...) { ... }
|
|---|
| 366 | };
|
|---|
| [697c957] | 367 | \end{uC++}
|
|---|
| 368 | &
|
|---|
| 369 | \begin{cfa}
|
|---|
| [946a6e4] | 370 | struct S {
|
|---|
| 371 | ... // fields
|
|---|
| 372 | };
|
|---|
| 373 | @?{}@( @S & s,@ ...) { ... }
|
|---|
| 374 | @^?{}@( @S & s@ ) { ... }
|
|---|
| [697c957] | 375 | \end{cfa}
|
|---|
| 376 | \end{tabular}
|
|---|
| 377 | \end{cquote}
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 | \section{String}
|
|---|
| 381 |
|
|---|
| 382 | \begin{cquote}
|
|---|
| [2b6db03] | 383 | \begin{tabular}{@{}l|l@{}}
|
|---|
| 384 | \multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\
|
|---|
| [697c957] | 385 | \begin{uC++}
|
|---|
| 386 | s1 = "hi";
|
|---|
| 387 | s2 = s1;
|
|---|
| 388 | s1 += s2;
|
|---|
| 389 | s1 == s2; s1 != s2;
|
|---|
| 390 | s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
|
|---|
| 391 | s1.length();
|
|---|
| 392 | s1[3];
|
|---|
| 393 | s1.substr( 2 ); s1.substr( 2, 3 );
|
|---|
| 394 | s1.replace( 2, 5, s2 );
|
|---|
| 395 | s1.find( s2 ), s1.rfind( s2 );
|
|---|
| 396 | s1.find_first_of( s2 ); s1.find_last_of( s2 );
|
|---|
| 397 | s1.find_first_not_of(s2 ); s1.find_last_not_of( s2 );
|
|---|
| 398 | getline( cin, s1 );
|
|---|
| 399 | cout << s1 << endl;
|
|---|
| 400 | \end{uC++}
|
|---|
| 401 | &
|
|---|
| 402 | \begin{cfa}
|
|---|
| 403 | s1 = "hi";
|
|---|
| 404 | s2 = s1;
|
|---|
| 405 | s1 += s2;
|
|---|
| 406 | s1 == s2; s1 != s2;
|
|---|
| 407 | s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
|
|---|
| 408 | size( s1 );
|
|---|
| 409 | s1[3];
|
|---|
| 410 | s1( 2 ); s1( 2, 3 );
|
|---|
| 411 | //s1.replace( 2, 5, s2 );
|
|---|
| 412 | find( s1, s2 ), rfind( s1, s2 );
|
|---|
| 413 | find_first_of( .substr, s2 ); s1.find_last_of( s2 );
|
|---|
| 414 | s1.find_first_not_of(s2 ); s1.find_last_not_of( s2 );
|
|---|
| 415 | sin | getline( s1 );
|
|---|
| 416 | sout | s1;
|
|---|
| 417 | \end{cfa}
|
|---|
| 418 | \end{tabular}
|
|---|
| 419 | \end{cquote}
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 | \section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
|
|---|
| 423 |
|
|---|
| 424 | \begin{cquote}
|
|---|
| [2b6db03] | 425 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [697c957] | 426 | \begin{uC++}
|
|---|
| 427 | struct S {
|
|---|
| [538cc35] | 428 | int i = 0; // cheat, implicit default constructor
|
|---|
| [697c957] | 429 | int setter( int j ) { int t = i; i = j; return t; }
|
|---|
| 430 | int getter() { return i; }
|
|---|
| 431 | };
|
|---|
| 432 |
|
|---|
| 433 | S s;
|
|---|
| [538cc35] | 434 | @s.@setter( 3 ); // object-oriented calls
|
|---|
| [697c957] | 435 | int k = @s.@getter();
|
|---|
| 436 | \end{uC++}
|
|---|
| 437 | &
|
|---|
| 438 | \begin{cfa}
|
|---|
| 439 | struct S {
|
|---|
| 440 | int i;
|
|---|
| 441 | };
|
|---|
| [538cc35] | 442 | void ?{}( S & s ) { s.i = 0; } // explicit default constructor
|
|---|
| [697c957] | 443 | int setter( @S & s,@ int j ) @with(s)@ { int t = i; i = j; return t; }
|
|---|
| 444 | int getter( @S & s@ ) @with(s)@ { return i; }
|
|---|
| 445 | S s;
|
|---|
| [538cc35] | 446 | setter( @s,@ 3 ); // normal routine calls
|
|---|
| [697c957] | 447 | int k = getter( @s@ );
|
|---|
| 448 | \end{cfa}
|
|---|
| 449 | \end{tabular}
|
|---|
| 450 | \end{cquote}
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 | \section{\texorpdfstring{\lstinline{uNoCtor}}{uNoCtor}}
|
|---|
| 454 |
|
|---|
| 455 | \begin{cquote}
|
|---|
| [2b6db03] | 456 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [697c957] | 457 | \begin{uC++}
|
|---|
| [538cc35] | 458 |
|
|---|
| [697c957] | 459 | struct S {
|
|---|
| 460 | int i;
|
|---|
| [538cc35] | 461 | S( int i ) { S::i = i; cout << "ctor " << S::i << endl; }
|
|---|
| 462 | ~S() { S::i = i; cout << "dtor " << S::i << endl; }
|
|---|
| [697c957] | 463 | };
|
|---|
| 464 | int main() {
|
|---|
| [538cc35] | 465 | enum { N = 5 };
|
|---|
| 466 | @uNoCtor<S>@ s[N]; // no constructor calls
|
|---|
| 467 | for ( int i = 0; i < N; i += 1 ) @s[i].ctor( i )@;
|
|---|
| 468 | for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl;
|
|---|
| [697c957] | 469 | }
|
|---|
| 470 | \end{uC++}
|
|---|
| 471 | &
|
|---|
| 472 | \begin{cfa}
|
|---|
| [538cc35] | 473 | #include @<raii.hfa>@ // uninit
|
|---|
| [697c957] | 474 | struct S {
|
|---|
| 475 | int i;
|
|---|
| 476 | };
|
|---|
| [538cc35] | 477 | void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; }
|
|---|
| 478 | void ^?{}( S & s ) { sout | "dtor" | s.i; }
|
|---|
| [697c957] | 479 | int main() {
|
|---|
| [538cc35] | 480 | enum { N = 5 };
|
|---|
| 481 | @uninit(S)@ s[N]; // no constructor calls
|
|---|
| 482 | for ( i; N ) @s[i]{ i }@;
|
|---|
| 483 | for ( i; N ) sout | s[i]@.@i;
|
|---|
| [697c957] | 484 | }
|
|---|
| 485 | \end{cfa}
|
|---|
| 486 | \end{tabular}
|
|---|
| 487 | \end{cquote}
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 | \section{Coroutines}
|
|---|
| 491 |
|
|---|
| 492 | \begin{cquote}
|
|---|
| [2b6db03] | 493 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 494 | \begin{uC++}
|
|---|
| 495 |
|
|---|
| 496 | _Coroutine C {
|
|---|
| 497 | // private coroutine fields
|
|---|
| 498 | void main() {
|
|---|
| 499 | ... suspend(); ...
|
|---|
| 500 | ... _Resume E( ... ) _At partner;
|
|---|
| 501 | }
|
|---|
| 502 | public:
|
|---|
| 503 | void mem( ... ) {
|
|---|
| 504 | ... resume() ...
|
|---|
| 505 | }
|
|---|
| 506 | };
|
|---|
| 507 | \end{uC++}
|
|---|
| 508 | &
|
|---|
| 509 | \begin{cfa}
|
|---|
| 510 | #include <$coroutine$.hfa>
|
|---|
| 511 | coroutine C {
|
|---|
| 512 | // private coroutine fields
|
|---|
| 513 |
|
|---|
| 514 | };
|
|---|
| 515 | void main( C & c ) {
|
|---|
| 516 | ... suspend; ... // keyword not routine
|
|---|
| 517 | ... resumeAt( partner, ExceptionInst( E, ... ) );
|
|---|
| 518 | }
|
|---|
| 519 | void mem( C & c, ... ) {
|
|---|
| 520 | ... resume(); ...
|
|---|
| 521 | }
|
|---|
| 522 | \end{cfa}
|
|---|
| 523 | \\
|
|---|
| 524 | \multicolumn{2}{l}{\lstinline{C c;}}
|
|---|
| 525 | \end{tabular}
|
|---|
| 526 | \end{cquote}
|
|---|
| 527 |
|
|---|
| 528 |
|
|---|
| 529 | \section{Locks}
|
|---|
| 530 |
|
|---|
| 531 | \begin{cquote}
|
|---|
| [2b6db03] | 532 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 533 | \begin{uC++}
|
|---|
| 534 |
|
|---|
| 535 | uOwnerLock m;
|
|---|
| 536 | uCondLock s;
|
|---|
| 537 | bool avail = true;
|
|---|
| 538 | m.acquire();
|
|---|
| 539 | if ( ! avail ) s.wait( m );
|
|---|
| 540 | else {
|
|---|
| 541 | avail = false;
|
|---|
| 542 | m.release();
|
|---|
| 543 | }
|
|---|
| 544 | @osacquire( cout )@ << i << endl;
|
|---|
| 545 | \end{uC++}
|
|---|
| 546 | &
|
|---|
| 547 | \begin{cfa}
|
|---|
| 548 | #include <locks.hfa>
|
|---|
| 549 | owner_lock m;
|
|---|
| 550 | condition_variable( owner_lock ) s;
|
|---|
| 551 | bool avail = true;
|
|---|
| 552 | lock( m );
|
|---|
| 553 | if ( ! avail ) wait( s, m );
|
|---|
| 554 | else {
|
|---|
| 555 | avail = false;
|
|---|
| 556 | unlock( m );
|
|---|
| 557 | }
|
|---|
| 558 | @mutex( sout )@ sout | i; // safe I/O
|
|---|
| 559 | \end{cfa}
|
|---|
| 560 | \end{tabular}
|
|---|
| 561 | \end{cquote}
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 | \section{Monitors}
|
|---|
| 565 |
|
|---|
| 566 | \begin{cquote}
|
|---|
| [2b6db03] | 567 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 568 | \begin{uC++}
|
|---|
| 569 |
|
|---|
| 570 | @_Monitor@ M {
|
|---|
| 571 | @uCondition@ c;
|
|---|
| 572 | bool avail = true;
|
|---|
| 573 | public:
|
|---|
| 574 |
|
|---|
| 575 | void rtn() {
|
|---|
| 576 | if ( ! avail ) c.wait();
|
|---|
| 577 | else avail = false;
|
|---|
| 578 | }
|
|---|
| 579 | };
|
|---|
| 580 | \end{uC++}
|
|---|
| 581 | &
|
|---|
| 582 | \begin{cfa}
|
|---|
| 583 | #include <$monitor$.hfa>
|
|---|
| 584 | @monitor@ M {
|
|---|
| 585 | @condition@ c;
|
|---|
| 586 | bool avail;
|
|---|
| 587 | };
|
|---|
| 588 | void ?{}( M & m ) { m.avail = true; }
|
|---|
| 589 | void rtn( M & m ) with( m ) {
|
|---|
| 590 | if ( ! avail ) wait( c );
|
|---|
| 591 | else avail = false;
|
|---|
| 592 | }
|
|---|
| 593 |
|
|---|
| 594 | \end{cfa}
|
|---|
| 595 | \\
|
|---|
| 596 | \multicolumn{2}{l}{\lstinline{M m;}}
|
|---|
| 597 | \end{tabular}
|
|---|
| 598 | \end{cquote}
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 | \section{Threads}
|
|---|
| 602 |
|
|---|
| 603 | \begin{cquote}
|
|---|
| [2b6db03] | 604 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 605 | \begin{uC++}
|
|---|
| 606 |
|
|---|
| 607 | @_Task@ T {
|
|---|
| 608 | // private task fields
|
|---|
| 609 | void main() {
|
|---|
| 610 | ... _Resume E( ... ) _At partner;
|
|---|
| 611 | }
|
|---|
| 612 | public:
|
|---|
| 613 | };
|
|---|
| 614 | \end{uC++}
|
|---|
| 615 | &
|
|---|
| 616 | \begin{cfa}
|
|---|
| 617 | #include <$thread$.hfa>
|
|---|
| 618 | @thread@ T {
|
|---|
| 619 | // private task fields
|
|---|
| 620 |
|
|---|
| 621 | };
|
|---|
| 622 | void main( @T & t@ ) {
|
|---|
| 623 | ... resumeAt( partner, ExceptionInst( E, ... ) );
|
|---|
| 624 | }
|
|---|
| 625 | \end{cfa}
|
|---|
| 626 | \\
|
|---|
| 627 | \multicolumn{2}{l}{\lstinline{T t; // start thread in main routine}}
|
|---|
| 628 | \end{tabular}
|
|---|
| 629 | \end{cquote}
|
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 | \input{uC++toCFA.ind}
|
|---|
| 633 |
|
|---|
| 634 | % \bibliographystyle{plain}
|
|---|
| 635 | % \bibliography{pl}
|
|---|
| 636 |
|
|---|
| 637 | \end{document}
|
|---|
| 638 |
|
|---|
| 639 | % Local Variables: %
|
|---|
| 640 | % tab-width: 4 %
|
|---|
| 641 | % fill-column: 100 %
|
|---|
| 642 | % compile-command: "make" %
|
|---|
| 643 | % End: %
|
|---|