Created
July 20, 2019 11:49
-
-
Save nadult/c881024706ec278ce983ec5178012ad2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prosta klasa parsera | |
// Jest oznaczona EXCEPT, to znaczy, że każda funkcja z tej klasy może rzucić wyjątek | |
class EXCEPT TextParser { | |
TextParser &operator>>(double &); | |
TextParser &operator>>(bool &); | |
... | |
}; | |
struct Params { | |
string name; | |
int numVals; | |
int vals[3]; | |
}; | |
// Ta funkcja musi być oznaczona jako EXCEPT, z dwóch powodów: | |
// - używa funkcji parsera, a wszystkie są EXCEPT | |
// - zawiera instrukcję RAISE która rzuca wyjątek | |
void operator>>(TextParser &parser, Params &out) EXCEPT { | |
parser >> out.name >> out.numVals; | |
if(out.numVals >= 0 && out.numVals <= 3) | |
for(int n : intRange(out.numVals)) | |
parser >> out.vals[n]; | |
else | |
RAISE("Invalid nr of arguments: %", numVals); | |
} | |
// Ta funkcja trawi wyjątki, jeśli jakieś się pojawią; | |
// NOEXCEPT jest po to, żeby poinformować exception-checker, że nie ważne co się stanie, | |
// ta funkcja nie zwróci wyjątku (jesteśmy za to odpowiedzialni) | |
bool tryParse(TextParser &parser, Params &out) NOEXCEPT { | |
parser >> out; | |
if(anyExceptions()) { | |
clearExceptions(); | |
return false; | |
} | |
return true; | |
} | |
// Excepted uzupełnia system wyjątków w taki sposób, że wyjątki mogą być łatwo przeniesione | |
// do expected; Dodatkowo zabronione jest (są odpowiednie checki) rzucanie wyjątków z funkcji | |
// która expected zwraca; Checker też o tym wie, więc nie potrzebne są żadne oznaczenia | |
Expected<Params> parseParams(TextParser &parser) { | |
Params out; | |
parser >> out; | |
EXPECT_CATCH(); // automatycznie zwraca wyjątek jako błąd | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment