blob: 22ec6d207b600327599b33c0561b471d476964a9 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef LIB_ESCHER_UTIL_ENUM_UTILS_H_
#define LIB_ESCHER_UTIL_ENUM_UTILS_H_
// TODO(ES-147): move EnumCast and EnumCount into this file, and update clients.
#include "lib/escher/util/enum_cast.h"
#include "lib/escher/util/enum_count.h"
namespace escher {
// Cycle through an enum's values, safely wrapping around in either direction.
// The enum must meet the requirements of EnumCount().
template <typename E>
E EnumCycle(E e, bool reverse = false) {
size_t count = EnumCount<E>();
auto underlying_value = EnumCast(e);
underlying_value = (underlying_value + (reverse ? count - 1 : 1)) % count;
return static_cast<E>(underlying_value);
}
} // namespace escher
#endif // LIB_ESCHER_UTIL_ENUM_UTILS_H_