Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#44 Allow for C++20 standard compiling #45

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions obfuscate.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ std::cout << obfuscated_string << std::endl;
----------------------------------------------------------------------------- */

#pragma once
#if __cplusplus >= 202002L
#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
// See: https://developercommunity.visualstudio.com/t/-line-cannot-be-used-as-an-argument-for-constexpr/195665
Expand All @@ -51,6 +56,8 @@ namespace ay
using size_type = unsigned long long;
using key_type = unsigned long long;

// 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 @@ -75,7 +82,7 @@ namespace ay
using char_type = typename remove_const_ref<T>::type;

// Generate a pseudo-random key that spans all 8 bytes
constexpr key_type generate_key(key_type seed)
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 @@ -108,7 +115,7 @@ namespace ay
{
public:
// Obfuscates the string 'data' on construction
constexpr obfuscator(const CHAR_TYPE* data)
AY_CONSTEVAL obfuscator(const CHAR_TYPE* data)
{
// Copy data
for (size_type i = 0; i < N; i++)
Expand All @@ -126,12 +133,12 @@ namespace ay
return &m_data[0];
}

constexpr size_type size() const
AY_CONSTEVAL size_type size() const
{
return N;
}

constexpr key_type key() const
AY_CONSTEVAL key_type key() const
{
return KEY;
}
Expand Down Expand Up @@ -211,7 +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>
constexpr auto make_obfuscator(const CHAR_TYPE(&data)[N])
AY_CONSTEVAL auto make_obfuscator(const CHAR_TYPE(&data)[N])
{
return obfuscator<N, KEY, CHAR_TYPE>(data);
}
Expand Down
Loading