catch_interfaces_enum_values_registry.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Created by Phil on 4/4/2019.
  3. * Copyright 2019 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef TWOBLUECUBES_CATCH_INTERFACESENUMVALUESREGISTRY_H_INCLUDED
  9. #define TWOBLUECUBES_CATCH_INTERFACESENUMVALUESREGISTRY_H_INCLUDED
  10. #include "catch_stringref.h"
  11. #include <vector>
  12. namespace Catch {
  13. namespace Detail {
  14. struct EnumInfo {
  15. StringRef m_name;
  16. std::vector<std::pair<int, StringRef>> m_values;
  17. ~EnumInfo();
  18. StringRef lookup( int value ) const;
  19. };
  20. } // namespace Detail
  21. struct IMutableEnumValuesRegistry {
  22. virtual ~IMutableEnumValuesRegistry();
  23. virtual Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector<int> const& values ) = 0;
  24. template<typename E>
  25. Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::initializer_list<E> values ) {
  26. static_assert(sizeof(int) >= sizeof(E), "Cannot serialize enum to int");
  27. std::vector<int> intValues;
  28. intValues.reserve( values.size() );
  29. for( auto enumValue : values )
  30. intValues.push_back( static_cast<int>( enumValue ) );
  31. return registerEnum( enumName, allEnums, intValues );
  32. }
  33. };
  34. } // Catch
  35. #endif //TWOBLUECUBES_CATCH_INTERFACESENUMVALUESREGISTRY_H_INCLUDED