genode/repos/os/src/server/nic_dump/pointer.h
Martin Stein 35cc020e9c os/server: nic_dump
A tiny bump-in-the-wire tool for dumping NIC packet information.

Ref #2314
2017-03-15 12:32:26 +01:00

56 lines
901 B
C++

/*
* \brief Pointer that can be dereferenced only when valid
* \author Martin Stein
* \date 2017-03-08
*/
/*
* Copyright (C) 2016-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _POINTER_H_
#define _POINTER_H_
/* Genode includes */
#include <base/exception.h>
namespace Net { template <typename> class Pointer; }
template <typename T>
class Net::Pointer
{
private:
T *_ptr;
public:
struct Valid : Genode::Exception { };
struct Invalid : Genode::Exception { };
Pointer() : _ptr(nullptr) { }
T &deref() const
{
if (_ptr == nullptr) {
throw Invalid(); }
return *_ptr;
}
void set(T &ptr)
{
if (_ptr != nullptr) {
throw Valid(); }
_ptr = &ptr;
}
void unset() { _ptr = nullptr; }
};
#endif /* _POINTER_H_ */