X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/c4231c6faf4e1b4650b075c641b0bb8c053739e4..66a27d2fc7c1ad4e97de76d4982168a0fed9920a:/src/find.h diff --git a/src/find.h b/src/find.h index 3342536..cd7f577 100644 --- a/src/find.h +++ b/src/find.h @@ -8,7 +8,10 @@ template using opt = std::optional; +template +using tup = std::tuple; +// find an item in a vector by predicate template opt find(const std::vector & ts, std::function f) { @@ -18,6 +21,7 @@ opt find(const std::vector & ts, std::function f) return nullopt; } +// same as above but return pointer into raw array held by vector template opt findPtr(const std::vector & ts, std::function f) { @@ -27,12 +31,12 @@ opt findPtr(const std::vector & ts, std::function f) return nullopt; } -std::optional< - std::tuple< - std::shared_ptr, - std::vector>> +opt, + std::vector>> getContext(std::shared_ptr ctx, const std::vector & namespacePrefix) { + // try finding a continuos series of namespaces in a given context auto result = ctx; for (auto name : namespacePrefix) @@ -48,6 +52,8 @@ getContext(std::shared_ptr ctx, const std::vector & namesp } } + // if the found context is the end of a series of namespaces, also return + // a vector of namespace names std::vector namespaces; for (auto it = result; it != nullptr; it = it->parent) { @@ -65,9 +71,13 @@ getContext(std::shared_ptr ctx, const std::vector & namesp return std::make_tuple(result, namespaces); } +// all of the following functions work the same way, +// walking up the context hierarchy until the global context. +// return the first found instance that matches provided criteria +// theres also a variant to get a pointer instead for functions and +// structs used for generic instantiation - -opt>> findFunction( +opt>> findFunction( const std::string & name, const std::vector & namespacePrefix, std::shared_ptr ctx) @@ -85,7 +95,7 @@ opt>> findFunction( return nullopt; } -opt>> findFunctionPtr( +opt>> findFunctionPtr( const std::string & name, const std::vector & namespacePrefix, std::shared_ptr ctx) @@ -105,7 +115,7 @@ opt>> findFunctionPtr( -opt>> findStruct( +opt>> findStruct( const std::string & name, const std::vector & namespacePrefix, std::shared_ptr ctx) @@ -123,7 +133,7 @@ opt>> findStruct( return nullopt; } -opt>> findStructPtr( +opt>> findStructPtr( const std::string & name, const std::vector & namespacePrefix, std::shared_ptr ctx) @@ -143,7 +153,7 @@ opt>> findStructPtr( -opt>> findVariable( +opt>> findVariable( const std::string & name, const std::vector & namespacePrefix, std::shared_ptr ctx) @@ -163,6 +173,8 @@ opt>> findVariable( +// find struct members and pointer variants + opt> findStructMethod( const std::string & name, const Struct & s)