-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimenting with string literals for non-type template parameters.
C++20 offers string literals as non-type template parameters and this would likely allow us to move away from using a clang extension which would allow for gcc support. This is just experimentation for now. See #4. PiperOrigin-RevId: 687923300
- Loading branch information
1 parent
acce094
commit cfc4a35
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef JNI_BIND_METAPROGRAMMING_STRING_LITERAL_H_ | ||
#define JNI_BIND_METAPROGRAMMING_STRING_LITERAL_H_ | ||
|
||
#ifdef __cplusplus | ||
#if __cplusplus >= 202002L | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <string_view> | ||
|
||
namespace jni::metaprogramming { | ||
|
||
// Inspired by Kevin Hartman's StringLiteral implementation. | ||
// https://ctrpeach.io/posts/cpp20-string-literal-template-parameters/ | ||
// | ||
// This class is not currently being used, but is being used to prototype | ||
// changes that will be needed for consumers of `InvocableMap`. This class is | ||
// not included by default for build because it requires C++20. | ||
template <size_t N> | ||
struct StringLiteral { | ||
constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } | ||
|
||
constexpr StringLiteral<N> Self() { return *this; } | ||
|
||
template <std::size_t U> | ||
constexpr bool operator==(const StringLiteral<U>& rhs) const { | ||
if constexpr (N != U) { | ||
return false; | ||
} else { | ||
return std::string_view{value} == std::string_view{rhs.value}; | ||
} | ||
} | ||
|
||
template <std::size_t U> | ||
constexpr bool operator!=(const StringLiteral<U>& rhs) const { | ||
return !(*this == rhs); | ||
} | ||
|
||
char value[N]; | ||
}; | ||
|
||
template <size_t N> | ||
StringLiteral(const char (&str)[N]) -> StringLiteral<N>; | ||
|
||
#endif // __cplusplus >= 202002L | ||
#endif // __cplusplus | ||
|
||
} // namespace jni::metaprogramming | ||
|
||
#endif // JNI_BIND_METAPROGRAMMING_STRING_LITERAL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "string_literal.h" | ||
|
||
#include <cstddef> | ||
|
||
using jni::metaprogramming::StringLiteral; | ||
|
||
namespace { | ||
|
||
template <size_t N> | ||
constexpr StringLiteral<N> Foo(const char (&str)[N]) { | ||
StringLiteral b{str}; | ||
return b; | ||
} | ||
|
||
// String literals can now peer through callsites. This was previously not | ||
// possible in C++17 and should hopefully simplify method selection. | ||
static_assert(Foo("Testing") == StringLiteral{"Testing"}); | ||
static_assert(Foo("abcdefg") != StringLiteral{"Testing"}); | ||
static_assert(Foo("Testing") != StringLiteral{"abcdefg"}); | ||
static_assert(Foo("Bar") != StringLiteral{"Testing"}); | ||
static_assert(Foo("Testing") != StringLiteral{"Bar"}); | ||
|
||
// This represents a container class. Any classes currently constructed with | ||
// const char* will need to migrate to this new form which explicitly captures | ||
// length. | ||
template <std::size_t N> | ||
struct SampleContainer { | ||
constexpr SampleContainer(const char (&val_)[N]) : val_(val_) {} | ||
|
||
StringLiteral<N> val_; | ||
}; | ||
|
||
template <std::size_t N> | ||
SampleContainer(const char (&val_)[N]) -> SampleContainer<N>; | ||
|
||
static_assert(SampleContainer("Testing").val_ == StringLiteral{"Testing"}); | ||
static_assert(SampleContainer("abcdefg").val_ != StringLiteral{"Testing"}); | ||
static_assert(SampleContainer("Foo").val_ != StringLiteral{"Testing"}); | ||
|
||
} // namespace |