2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief Utility for checking array bounds
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2011-02-18
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 21:44:47 +01:00
|
|
|
* Copyright (C) 2011-2013 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NOUX__RANGE_CHECKED_INDEX_H_
|
|
|
|
#define _NOUX__RANGE_CHECKED_INDEX_H_
|
|
|
|
|
|
|
|
namespace Noux {
|
|
|
|
|
|
|
|
class Index_out_of_range { };
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Range_checked_index
|
|
|
|
{
|
|
|
|
T value;
|
|
|
|
T const max;
|
|
|
|
|
|
|
|
Range_checked_index(T value, T max)
|
|
|
|
: value(value), max(max) { }
|
|
|
|
|
|
|
|
T operator++ (int)
|
|
|
|
{
|
|
|
|
T old_value = value;
|
|
|
|
|
|
|
|
if (++value >= max)
|
|
|
|
throw Index_out_of_range();
|
|
|
|
|
|
|
|
return old_value;
|
|
|
|
}
|
|
|
|
|
2012-03-19 22:52:26 +01:00
|
|
|
operator T () { return value; }
|
2011-12-22 16:19:25 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _NOUX__RANGE_CHECKED_INDEX_H_ */
|