nic_router: safe pointer class for const objects

Const_pointer class that enables the use of the pointer wrapper for
const ojects.

Ref #2670
This commit is contained in:
Martin Stein 2018-03-13 18:00:53 +01:00 committed by Christian Helmuth
parent 5926261e08
commit e0081cfc29

View File

@ -17,7 +17,11 @@
/* Genode includes */
#include <base/exception.h>
namespace Net { template <typename> class Pointer; }
namespace Net {
template <typename> class Pointer;
template <typename> class Const_pointer;
}
template <typename T>
class Net::Pointer
@ -54,4 +58,37 @@ class Net::Pointer
void unset() { _ptr = nullptr; }
};
template <typename T>
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_ */