mirror of
https://github.com/genodelabs/genode.git
synced 2025-01-18 18:56:29 +00:00
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:
parent
5926261e08
commit
e0081cfc29
@ -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_ */
|
||||
|
Loading…
Reference in New Issue
Block a user