From e0081cfc29b33acad307edad64768e3a58dfb572 Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Tue, 13 Mar 2018 18:00:53 +0100 Subject: [PATCH] nic_router: safe pointer class for const objects Const_pointer class that enables the use of the pointer wrapper for const ojects. Ref #2670 --- repos/os/src/server/nic_router/pointer.h | 39 +++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/repos/os/src/server/nic_router/pointer.h b/repos/os/src/server/nic_router/pointer.h index 90e95308f8..cf74d2dbaf 100644 --- a/repos/os/src/server/nic_router/pointer.h +++ b/repos/os/src/server/nic_router/pointer.h @@ -17,7 +17,11 @@ /* Genode includes */ #include -namespace Net { template class Pointer; } +namespace Net { + + template class Pointer; + template class Const_pointer; +} template class Net::Pointer @@ -54,4 +58,37 @@ class Net::Pointer void unset() { _ptr = nullptr; } }; +template +class Net::Const_pointer +{ + private: + + T const *_ptr; + bool _valid; + + public: + + struct Valid : Genode::Exception { }; + struct Invalid : Genode::Exception { }; + + Const_pointer() : _ptr(nullptr), _valid(false) { } + + Const_pointer(T const &ref) : _ptr(&ref), _valid(true) { } + + T const &deref() const + { + if (!_valid) { throw Invalid(); } + return *_ptr; + } + + void set(T const &ptr) + { + if (_valid) { throw Valid(); } + _ptr = &ptr; + _valid = true; + } + + void unset() { _valid = false; } +}; + #endif /* _POINTER_H_ */