-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.tex
1370 lines (1169 loc) · 33.7 KB
/
document.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% !TeX options = -shell-escape
\documentclass[10pt]{beamer}
\usetheme[progressbar=frametitle, block=fill]{metropolis}
\usepackage{xcolor}
\usepackage{multirow}
\usepackage{pgfpages}
\usepackage{pifont}
\usepackage{textpos}
\newcommand{\cmark}{\ding{51}}
\newcommand{\xmark}{\ding{55}}
\setbeamertemplate{note page}{\insertnote}
%\setbeameroption{show notes on second screen=left}
\setbeameroption{hide notes}
\definecolor{amethyst}{rgb}{0.5, 0.4, 1.0}
\definecolor{amethystgrey}{rgb}{0.85, 0.85, 1.0}
\definecolor{amethystdark}{rgb}{0.4, 0.3, 0.9}
\definecolor{orangedark}{rgb}{0.0, 0.9, 0}
%\definecolor{titlebg}{HTML}{4e8074}
%\definecolor{titlebg}{HTML}{3e7985}
\definecolor{titlebg}{HTML}{fbf8ff}
\definecolor{font}{HTML}{23373b}
%\setbeamercolor{title}{fg=amethyst, bg=amethyst}
\setbeamercolor{frametitle}{fg= font, bg=titlebg}
%\setbeamercolor{section title}{black}
%\setbeamercolor{structure}{fg=amethyst, bg=amethyst}
\setbeamercolor{progress bar}{ fg = amethyst, bg= amethystgrey }
%\setbeamercolor{itemize item}{fg=amethyst,bg=white}
\setbeamercolor{alerted text}{fg=amethystdark}
%\setbeamercolor{title separator}{ ... }
%\setbeamercolor{progress bar in head/foot}{ ... }
%\setbeamercolor{progress bar in section page}{ ... }
\addtobeamertemplate{frametitle}{}{%
\begin{textblock*}{100mm}(1\textwidth, -1cm)
\includegraphics[height=0.97cm]{TT_Logo_bckg.png}
\end{textblock*}}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage[scale=2]{ccicons}
\usepackage{minted}
\setminted[c++]{linenos = true}
\usepackage{pgfplots}
\usepackage{xspace}
\title{Futuristic Error Handling}
\subtitle{Error handling in C++ today and tomorrow}
%\logo{\includegraphics[width=0.05\linewidth]{logo.png}}
% \date{\today}
\date{}
\author{Dawid Pilarski}
\institute{[email protected]}
\begin{document}
\maketitle
%\begin{frame}{Table of contents}
% \setbeamertemplate{section in toc}[sections numbered]
% \tableofcontents[hideallsubsections]
%\end{frame}
\begin{frame}{Why am I here?}
Why should we bother with error handling?
\end{frame}
\begin{frame}{Recommendable error handling mechanism}
\note{Q: ask which mechanism would be chosen}
Which error mechanism would you choose?
There exist two common strategies for error handling:
\begin{itemize}
\item error codes
\item exceptions
\end{itemize}
\end{frame}
\begin{frame}{Who am I?}
\centering \alert{Dawid Pilarski}
\vskip 1em
\begin{columns}[onlytextwidth]
\begin{column}{0.7\textwidth}
\begin{itemize}
\item Senior Software Developer in TomTom
\item Member of the ISO/JTC1/SC22/WG21
\item Member of the PKN KT {\tiny(programming languages)}
\item C++ blog writer
\end{itemize}
\end{column}
\begin{column}{0.29\textwidth}
\includegraphics[width=\linewidth]{Dawid_Pilarski.jpg}
\end{column}
\end{columns}
\end{frame}
\section{Error codes nowadays}
\begin{frame}{What are the error codes?}
According to the \alert{Wikipedia} :
\begin{itemize}[<+- | alert@+>]
\item error code is an enumerated message,
\item that corresponds to the status of a specific software application.
\item They are typically used to identify faults, such as those in faulty hardware, software, or incorrect user input
\end{itemize}
\end{frame}
\begin{frame}{The error codes.}
\begin{itemize}[<+- | alert@+>]
\item Old. C-compatible. Comes from assembly time.
\item Machine friendly.
\item Super fast.
\item Used till today.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Error code example}
\begin{onlyenv}<1->
\begin{minted}{c++}
int sqlite3_open( const char *filename, sqlite3 **ppDb );
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\hrulefill
\begin{minted}[highlightlines={2,4}]{c++}
int open_status = sqlite3_open(/* ... */ );
if(open_status == SQLITE_OK){
// make use of opened database
} else if( open_status == SQLITE_CANTOPEN_ISDIR ) {
// handle the error
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\hrulefill
\begin{minted}[highlightlines={3}]{c++}
int open_status = sqlite3_open(/* ... */ );
if(open_status == SQLITE_OK){
// make use of opened database
} else if( open_status == SQLITE_CANTOPEN_ISDIR ) {
// handle the error
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
\hrulefill
\begin{minted}[highlightlines={5}]{c++}
int open_status = sqlite3_open(/* ... */ );
if(open_status == SQLITE_OK){
// make use of opened database
} else if( open_status == SQLITE_CANTOPEN_ISDIR ) {
// handle the error
}
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}{Handle the error}
How to handle the error correctly?
\pause
\begin{itemize}[<+- | alert@+>]
\item \texttt{std::terminate()}
\item take the error callback
\item propagate the error to the caller
\end{itemize}
\end{frame}
\section{Error codes - propagation}
\begin{frame}[fragile]{Propagation}
\begin{onlyenv}<1>
\begin{minted}[highlightlines=1]{c++}
void foo_bar(int& errc /*...*/){
errc = foo();
// ...
errc = bar();
// ...
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={2,4}]{c++}
void foo_bar(int& errc /*...*/){
errc = foo();
// ...
errc = bar();
// ...
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{minted}[highlightlines={3,6}]{c++}
void foo_bar(foo_bar_errc errc&){
foo_errc ferrc = foo();
errc = translate_foo(ferrc);
// ...
bar_errc berrc = bar();
errc = translate_foo(berrc);
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
\begin{minted}[highlightlines={4-6}]{c++}
void foo_bar(foo_bar_errc errc&){
foo_errc ferrc = foo();
errc = translate_foo(ferrc);
if(errc != foo_errc::SUCCESS){
return;
}
// ...
}
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}{C-style error codes summary}
So we can see {\color{red}serious disadvantages} (except for {\color{blue}obvious advantages}):
\begin{itemize}[<+- | alert@+>]
\item success path same as error path
\item cluttering code with translations
\item boring manual error propagation
\end{itemize}
\end{frame}
\section{Error codes - modern approach}
\begin{frame}{Standard library support - what do we need?}
\begin{itemize}
\item A way to define new error codes
\item A way to distinguish domain of the error codes
\end{itemize}
\end{frame}
\begin{frame}{Standard library support - what we get?}
We get three new major types:
\begin{itemize}[<+- | alert@+>]
\item std::error\_code
\item std::error\_category
\item std::error\_condition
\end{itemize}
\end{frame}
\begin{frame}[fragile]{std::error\_code in action}
\begin{onlyenv}<1>
\begin{minted}[highlightlines={1-2}]{c++}
std::error_code errcode;
is_regular_file("non_existent_directory", errcode);
std::cout << errcode << std::endl;
std::cout << errcode.value() << std::endl;
std::cout << errcode.message() << std::endl;
std::cout << errcode.category().name() << std::endl;
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={4}]{c++}
std::error_code errcode;
is_regular_file("non_existent_directory", errcode);
std::cout << errcode << std::endl;
std::cout << errcode.value() << std::endl;
std::cout << errcode.message() << std::endl;
std::cout << errcode.category().name() << std::endl;
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{minted}[highlightlines={5}]{c++}
std::error_code errcode;
is_regular_file("non_existent_directory", errcode);
std::cout << errcode << std::endl;
std::cout << errcode.value() << std::endl;
std::cout << errcode.message() << std::endl;
std::cout << errcode.category().name() << std::endl;
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
\begin{minted}[highlightlines={6}]{c++}
std::error_code errcode;
is_regular_file("non_existent_directory", errcode);
std::cout << errcode << std::endl;
std::cout << errcode.value() << std::endl;
std::cout << errcode.message() << std::endl;
std::cout << errcode.category().name() << std::endl;
\end{minted}
\end{onlyenv}
\begin{onlyenv}<5>
\begin{minted}[highlightlines={7}]{c++}
std::error_code errcode;
is_regular_file("non_existent_directory", errcode);
std::cout << errcode << std::endl;
std::cout << errcode.value() << std::endl;
std::cout << errcode.message() << std::endl;
std::cout << errcode.category().name() << std::endl;
\end{minted}
\end{onlyenv}
\hrulefill
\begin{onlyenv}<1>
\begin{block}{output}
\texttt{\\
\$ generic:2 \\
\$ 2 \\
\$ No such file or directory \\
\$ generic}
\end{block}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{block}{output}
\texttt{\\
\$ \alert{generic:2} \\
\$ 2 \\
\$ No such file or directory \\
\$ generic}
\end{block}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{block}{output}
\texttt{\\
\$ generic:2 \\
\$ \alert{2} \\
\$ No such file or directory \\
\$ generic}
\end{block}
\end{onlyenv}
\begin{onlyenv}<4>
\begin{block}{output}
\texttt{\\
\$ generic:2 \\
\$ 2 \\
\$ \alert{No such file or directory} \\
\$ generic}
\end{block}
\end{onlyenv}
\begin{onlyenv}<5>
\begin{block}{output}
\texttt{\\
\$ generic:2 \\
\$ 2 \\
\$ No such file or directory \\
\$ \alert{generic}}
\end{block}
\end{onlyenv}
\end{frame}
\begin{frame}[fragile]{Acting upon error}
\begin{onlyenv}<1>
\begin{minted}[highlightlines={1-2}]{c++}
std::error_code errcode;
is_regular_file("non_existent_file", errcode);
if(errcode == errc::no_such_file_or_directory){
// creating a file
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={4-6}]{c++}
std::error_code errcode;
is_regular_file("non_existent_file", errcode);
if(errcode == errc::no_such_file_or_directory){
// creating a file
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\centerline{With enum error codes.}
\vfill
\begin{minted}[highlightlines={3}]{c++}
void foo_bar(foo_bar_errc errc&){
foo_errc ferrc = foo();
errc = translate_foo(ferrc);
if(errc != foo_errc::SUCCESS){
return;
}
// ...
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
\centerline{With std::error\_code.}
\vfill
\begin{minted}[highlightlines={3}]{c++}
void foo_bar(std::error_code errc&){
errc = foo();
if(errc){
return;
}
// ...
}
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}{Let's define our own error code}
Steps to create own error code:
\begin{itemize}[<+- | alert@+>]
\item define custom enum with error codes
\item inform, that the enum is an error code
\item create custom error category
\item create enum to error code factory function
\item define custom error condition
\begin{itemize}
\item define error condition enum
\item inform the world about new error condition enum
\item create custom error category for condition enum
\item make conversion function from new error condition enum to error condition
\end{itemize}
\item enjoy!
\end{itemize}
\end{frame}
\section{Error codes - \\ defining custom error codes}
\begin{frame}[fragile]{Step 1 - define custom enum with error codes}
\begin{onlyenv}<1>
\begin{minted}[highlightlines={2}]{c++}
enum class open_file_error {
SUCCESS, // zero means success
NO_SUCH_FILE_OR_DIRECTORY,
FILE_IS_DIRECTORY,
LACK_OF_RESOURCES,
FILE_BROKEN,
NO_PERMISSIONS
};
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={3,4}]{c++}
enum class open_file_error {
SUCCESS, // zero means success
NO_SUCH_FILE_OR_DIRECTORY,
FILE_IS_DIRECTORY,
LACK_OF_RESOURCES,
FILE_BROKEN,
NO_PERMISSIONS
};
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{minted}[highlightlines={5,6}]{c++}
enum class open_file_error {
SUCCESS, // zero means success
NO_SUCH_FILE_OR_DIRECTORY,
FILE_IS_DIRECTORY,
LACK_OF_RESOURCES,
FILE_BROKEN,
NO_PERMISSIONS
};
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
\begin{minted}[highlightlines={7}]{c++}
enum class open_file_error {
SUCCESS, // zero means success
NO_SUCH_FILE_OR_DIRECTORY,
FILE_IS_DIRECTORY,
LACK_OF_RESOURCES,
FILE_BROKEN,
NO_PERMISSIONS
};
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}[fragile]{Step 2 - inform the world about new error code type}
\begin{minted}{c++}
namespace std{
template <> struct
is_error_code_enum<open_file_error> : std::true_type{};
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{Step 3 - custom error category}
\begin{minted}{c++}
struct open_file_error_domain : std::error_category {
const char *name() const noexcept override;
std::string message(int errc) const override;
};
\end{minted}
\end{frame}
\begin{frame}[fragile]{Step 3 - custom error category}
\begin{minted}{c++}
const char* open_file_error_domain::name() const noexcept{
return "Open File Error";
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{Step 3 - custom error category}
\begin{onlyenv}<1>
\begin{minted}[highlightlines={3}]{c++}
std::string open_file_error_domain::message(int errc) const{
if(errc < 0 or errc > 4) return "UNKNOWN ERROR";
switch (static_cast<open_file_error>(errc)){
case open_file_error::SUCCESS:
return "Success.";
case open_file_error::NO_SUCH_FILE_OR_DIRECTORY:
return "File does not exist.";
// other cases
case open_file_error::NO_PERMISSIONS:
return "Missing permissions to open the file."
}
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={4,5}]{c++}
std::string open_file_error_domain::message(int errc) const{
if(errc < 0 or errc > 4) return "UNKNOWN ERROR";
switch (static_cast<open_file_error>(errc)){
case open_file_error::SUCCESS:
return "Success.";
case open_file_error::NO_SUCH_FILE_OR_DIRECTORY:
return "File does not exist.";
// other cases
case open_file_error::NO_PERMISSIONS:
return "Missing permissions to open the file."
}
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{minted}[highlightlines={6,7}]{c++}
std::string open_file_error_domain::message(int errc) const{
if(errc < 0 or errc > 4) return "UNKNOWN ERROR";
switch (static_cast<open_file_error>(errc)){
case open_file_error::SUCCESS:
return "Success.";
case open_file_error::NO_SUCH_FILE_OR_DIRECTORY:
return "File does not exist.";
// other cases
case open_file_error::NO_PERMISSIONS:
return "Missing permissions to open the file."
}
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
\begin{minted}[highlightlines={8-10}]{c++}
std::string open_file_error_domain::message(int errc) const{
if(errc < 0 or errc > 4) return "UNKNOWN ERROR";
switch (static_cast<open_file_error>(errc)){
case open_file_error::SUCCESS:
return "Success.";
case open_file_error::NO_SUCH_FILE_OR_DIRECTORY:
return "File does not exist.";
// other cases
case open_file_error::NO_PERMISSIONS:
return "Missing permissions to open the file."
}
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<5>
\begin{minted}[highlightlines={2}]{c++}
std::string open_file_error_domain::message(int errc) const{
if(errc < 0 or errc > 4) return "UNKNOWN ERROR";
switch (static_cast<open_file_error>(errc)){
case open_file_error::SUCCESS:
return "Success.";
case open_file_error::NO_SUCH_FILE_OR_DIRECTORY:
return "File does not exist.";
// other cases
case open_file_error::NO_PERMISSIONS:
return "Missing permissions to open the file."
}
}
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}[fragile]{Step 4 - factory function}
\begin{onlyenv}<1>
\begin{minted}[highlightlines={3}]{c++}
namespace std{
template <typename ErrorCode>
error_code::error_code(typename std::enable_if<
is_error_code_enum<
ErrorCode>
::value, ErrorCode>
::type errcode) noexcept
: error_code(make_error_code(errcode))
{}
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={4-7}]{c++}
namespace std{
template <typename ErrorCode>
error_code::error_code(typename std::enable_if<
is_error_code_enum<
ErrorCode>
::value, ErrorCode>
::type errcode) noexcept
: error_code(make_error_code(errcode))
{}
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{minted}[highlightlines={8}]{c++}
namespace std{
template <typename ErrorCode>
error_code::error_code(typename std::enable_if<
is_error_code_enum<
ErrorCode>
::value, ErrorCode>
::type errcode) noexcept
: error_code(make_error_code(errcode))
{}
}
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}[fragile]{Step 4 - factory function}
\begin{minted}{c++}
std::error_code make_error_code(open_file_error errc){
return {static_cast<int>(errc), open_file_error_domain};
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{Step 5 - custom error condition}
\begin{minted}{c++}
enum class library_error_condition{
SUCCESS,
WRONG_ARGUMENT,
OS_ERROR,
PERMISSIONS_ERROR
};
\end{minted}
\begin{onlyenv}<2>
\hrule
\vfill
\begin{minted}[fontsize=\footnotesize]{c++}
enum class open_file_error {
SUCCESS, // zero means success
NO_SUCH_FILE_OR_DIRECTORY,
FILE_IS_DIRECTORY,
LACK_OF_RESOURCES,
FILE_BROKEN,
NO_PERMISSIONS
};
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}[fragile]{Step 5 - custom error condition}
\begin{minted}{c++}
namespace std{
template <> struct
is_error_condition_enum<library_error_condition>
: std::true_type{};
}
\end{minted}
\end{frame}
\begin{frame}[fragile]{Step 5 - custom error condition}
\begin{minted}{c++}
struct library_error_domain : std::error_category{
const char *name() const noexcept override;
std::string message(int errc) const override;
bool equivalent(const std::error_code &errc, int condition)
const noexcept override;
};
\end{minted}
\end{frame}
\begin{frame}[fragile]{Step 5 - custom error condition}
\begin{onlyenv}<1>
\begin{minted}[highlightlines={4}]{c++}
bool library_error_domain::equivalent(
const std::error_code &errc, int condition)
const noexcept{
switch (static_cast<library_error_condition>(condition)){
case library_error::SUCCESS:
if(errc == open_file_error::SUCCESS)
return true;
case library_error::WRONG_ARGUMENT:
if(errc ==
open_file_error::NO_SUCH_FILE_OR_DIRECTORY or
errc ==
open_file_error::FILE_IS_DIRECTORY)
return true;
//other cases
}
return false;
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={5-7}]{c++}
bool library_error_domain::equivalent(
const std::error_code &errc, int condition)
const noexcept{
switch (static_cast<library_error_condition>(condition)){
case library_error::SUCCESS:
if(errc == open_file_error::SUCCESS)
return true;
case library_error::WRONG_ARGUMENT:
if(errc ==
open_file_error::NO_SUCH_FILE_OR_DIRECTORY or
errc ==
open_file_error::FILE_IS_DIRECTORY)
return true;
//other cases
}
return false;
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{minted}[highlightlines={8-13}]{c++}
bool library_error_domain::equivalent(
const std::error_code &errc, int condition)
const noexcept{
switch (static_cast<library_error_condition>(condition)){
case library_error::SUCCESS:
if(errc == open_file_error::SUCCESS)
return true;
case library_error::WRONG_ARGUMENT:
if(errc ==
open_file_error::NO_SUCH_FILE_OR_DIRECTORY or
errc ==
open_file_error::FILE_IS_DIRECTORY)
return true;
//other cases
}
return false;
}
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}[fragile]{Step 6 - Enjoy - real life example}
\begin{onlyenv}<1>
\begin{minted}[highlightlines={1-2}]{c++}
std::error_code errcode;
auto settings = read_user_settings("settings.txt", errcode);
if(!errcode)
return settings;
std::cout << errcode.category().name() << " : " <<
errcode.message() << std::endl;
if(errcode == library_error_condition::PERMISSIONS_ERROR)
ask_for_permissions();
else if (errcode == library_error_condition::OS_ERROR)
std::terminate();
else if (errcode == library_error_condition::WRONG_ARGUMENTS)
std::terminate();
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={4-5}]{c++}
std::error_code errcode;
auto settings = read_user_settings("settings.txt", errcode);
if(!errcode)
return settings;
std::cout << errcode.category().name() << " : " <<
errcode.message() << std::endl;
if(errcode == library_error_condition::PERMISSIONS_ERROR)
ask_for_permissions();
else if (errcode == library_error_condition::OS_ERROR)
std::terminate();
else if (errcode == library_error_condition::WRONG_ARGUMENTS)
std::terminate();
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{minted}[highlightlines={10-11}]{c++}
std::error_code errcode;
auto settings = read_user_settings("settings.txt", errcode);
if(!errcode)
return settings;
std::cout << errcode.category().name() << " : " <<
errcode.message() << std::endl;
if(errcode == library_error_condition::PERMISSIONS_ERROR)
ask_for_permissions();
else if (errcode == library_error_condition::OS_ERROR)
std::terminate();
else if (errcode == library_error_condition::WRONG_ARGUMENTS)
std::terminate();
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
\begin{minted}[highlightlines={12-15}]{c++}
std::error_code errcode;
auto settings = read_user_settings("settings.txt", errcode);
if(!errcode)
return settings;
std::cout << errcode.category().name() << " : " <<
errcode.message() << std::endl;
if(errcode == library_error_condition::PERMISSIONS_ERROR)
ask_for_permissions();
else if (errcode == library_error_condition::OS_ERROR)
std::terminate();
else if (errcode == library_error_condition::WRONG_ARGUMENTS)
std::terminate();
\end{minted}
\end{onlyenv}
\end{frame}
\begin{frame}[fragile]{Step 6 - Enjoy - real life example}
\begin{onlyenv}<1>
\begin{minted}[highlightlines={1-2}]{c++}
settings read_user_settings(std::string_view filename,
error_code& errc){
auto file_handle = open(filename, errc);
if (errc) return {};
ensure_file_correct(file_handle, errc);
if(errc) return {};
return read_settings(file_handle);
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{minted}[highlightlines={3-4}]{c++}
settings read_user_settings(std::string_view filename,
error_code& errc){
auto file_handle = open(filename, errc);
if (errc) return {};
ensure_file_correct(file_handle, errc);
if(errc) return {};
return read_settings(file_handle);
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<3>
\begin{minted}[highlightlines={6-7}]{c++}
settings read_user_settings(std::string_view filename,
error_code& errc){
auto file_handle = open(filename, errc);
if (errc) return {};
ensure_file_correct(file_handle, errc);
if(errc) return {};
return read_settings(file_handle);
}
\end{minted}
\end{onlyenv}
\begin{onlyenv}<4>
\begin{minted}[highlightlines={9}]{c++}
settings read_user_settings(std::string_view filename,
error_code& errc){
auto file_handle = open(filename, errc);
if (errc) return {};
ensure_file_correct(file_handle, errc);
if(errc) return {};
return read_settings(file_handle);
}
\end{minted}
\end{onlyenv}
\end{frame}
\section{Error codes - summary}
\begin{frame}{Error codes summary}
\begin{columns}[T]
\begin{column}{0.48\linewidth}
Pros
\vfill
\begin{itemize}
\item Performance
\begin{itemize}[<+- | alert@+>]
\item speed
\item small (occupied memory)
\item speed predictability
\item memory occupation predictability
\item C compatibility
\end{itemize} \pause
\end{itemize}
\end{column}
\begin{column}{0.48\linewidth}
Cons
\vfill
\begin{itemize}[<+- | alert@+>]
\item business logic cluttering
\item massive amount of boilerplate code
\item template magic in case of std::error\_code
\end{itemize}
\end{column}
\end{columns}
\end{frame}
\section{Exceptions to the rescue (?)}
\begin{frame}[fragile]{Brief look at the example}
\begin{minted}{c++}
try{
auto settings = read_user_settings("settings.txt");
} catch(permissions_error& err){
// logic
} /* catch(path_not_found& err){
// logic
} catch(std::invalid_argument& ){
// logic
} */
\end{minted}
\end{frame}
\begin{frame}[fragile]{Brief look at the example}
\begin{minted}{c++}
settings read_user_settings(std::string_view filename){
auto file_handle = open(filename);
ensure_file_correct(file_handle);
return read_settings(file_handle);
}
\end{minted}