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_ */