Skip to content

Commit

Permalink
Resolved review comments
Browse files Browse the repository at this point in the history
* Use macro to use consteval when compiling in C++20 standard, or constexpr for C++14/17.
  Only for those functions which require compile-time evaluation.
* Keep STL independent, because not all users may be willing or able to use the STL in their environment.
  • Loading branch information
Maarten van Geijn committed Jul 9, 2024
1 parent 0cbf8c0 commit 57d1125
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions obfuscate.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ std::cout << obfuscated_string << std::endl;

#pragma once
#if __cplusplus >= 202002L
#include <type_traits>
#define AY_CONSTEVAL consteval
#else
#define AY_CONSTEVAL constexpr
#endif

// Workaround for __LINE__ not being constexpr when /ZI (Edit and Continue) is enabled in Visual Studio
Expand All @@ -54,10 +56,8 @@ namespace ay
using size_type = unsigned long long;
using key_type = unsigned long long;

#if __cplusplus >= 202002L
template <typename T>
using char_type = std::remove_cvref_t<T>;
#else
// libstdc++ has std::remove_cvref_t<T> since C++20, but because not every user will be
// able or willing to link to the STL, we prefer to do this functionality ourselves here.
template <typename T>
struct remove_const_ref {
using type = T;
Expand All @@ -80,14 +80,9 @@ namespace ay

template <typename T>
using char_type = typename remove_const_ref<T>::type;
#endif

// Generate a pseudo-random key that spans all 8 bytes
#if __cplusplus >= 202002L
consteval key_type generate_key(key_type seed)
#else
constexpr key_type generate_key(key_type seed)
#endif
AY_CONSTEVAL key_type generate_key(key_type seed)
{
// Use the MurmurHash3 64-bit finalizer to hash our seed
key_type key = seed;
Expand Down Expand Up @@ -120,11 +115,7 @@ namespace ay
{
public:
// Obfuscates the string 'data' on construction
#if __cplusplus >= 202002L
consteval obfuscator(const CHAR_TYPE* data)
#else
constexpr obfuscator(const CHAR_TYPE* data)
#endif
AY_CONSTEVAL obfuscator(const CHAR_TYPE* data)
{
// Copy data
for (size_type i = 0; i < N; i++)
Expand All @@ -142,20 +133,12 @@ namespace ay
return &m_data[0];
}

#if __cplusplus >= 202002L
consteval size_type size() const
#else
constexpr size_type size() const
#endif
AY_CONSTEVAL size_type size() const
{
return N;
}

#if __cplusplus >= 202002L
consteval key_type key() const
#else
constexpr key_type key() const
#endif
AY_CONSTEVAL key_type key() const
{
return KEY;
}
Expand Down Expand Up @@ -235,11 +218,7 @@ namespace ay
// This function exists purely to extract the number of elements 'N' in the
// array 'data'
template <size_type N, key_type KEY = AY_OBFUSCATE_DEFAULT_KEY, typename CHAR_TYPE = char>
#if __cplusplus >= 202002L
consteval auto make_obfuscator(const CHAR_TYPE(&data)[N])
#else
constexpr auto make_obfuscator(const CHAR_TYPE(&data)[N])
#endif
AY_CONSTEVAL auto make_obfuscator(const CHAR_TYPE(&data)[N])
{
return obfuscator<N, KEY, CHAR_TYPE>(data);
}
Expand Down

0 comments on commit 57d1125

Please sign in to comment.