mirror of
https://github.com/genodelabs/genode.git
synced 2025-01-26 22:29:19 +00:00
46 lines
828 B
C++
46 lines
828 B
C++
/*
|
|
* \brief Utility for checking array bounds
|
|
* \author Norman Feske
|
|
* \date 2011-02-18
|
|
*/
|
|
|
|
/*
|
|
* Copyright (C) 2011-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 _NOUX__RANGE_CHECKED_INDEX_H_
|
|
#define _NOUX__RANGE_CHECKED_INDEX_H_
|
|
|
|
namespace Noux {
|
|
|
|
class Index_out_of_range { };
|
|
template <typename> struct Range_checked_index;
|
|
}
|
|
|
|
|
|
template <typename T>
|
|
struct Noux::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;
|
|
}
|
|
|
|
operator T () { return value; }
|
|
};
|
|
|
|
#endif /* _NOUX__RANGE_CHECKED_INDEX_H_ */
|