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

Move Corpus metafunction to metaprogramming. #244

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ cc_library(
"//implementation:class",
"//implementation:class_loader",
"//implementation:constructor",
"//implementation:corpus",
"//implementation:corpus_tag",
"//implementation:default_class_loader",
"//implementation:field",
"//implementation:find_class_fallback",
Expand Down Expand Up @@ -67,6 +65,8 @@ cc_library(
"//implementation:static_ref",
"//implementation:string_ref",
"//implementation:supported_class_set",
"//metaprogramming:corpus",
"//metaprogramming:corpus_tag",
],
)

Expand Down
33 changes: 3 additions & 30 deletions implementation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -293,34 +293,6 @@ cc_test(
],
)

cc_library(
name = "corpus",
hdrs = ["corpus.h"],
deps = [
":corpus_tag",
"//metaprogramming:concatenate",
"//metaprogramming:detect",
],
)

cc_library(
name = "corpus_tag",
hdrs = ["corpus_tag.h"],
deps = [
],
)

cc_test(
name = "corpus_test",
srcs = ["corpus_test.cc"],
deps = [
":corpus",
":corpus_tag",
"//metaprogramming:detect",
"@googletest//:gtest_main",
],
)

cc_library(
name = "default_class_loader",
hdrs = ["default_class_loader.h"],
Expand Down Expand Up @@ -949,8 +921,7 @@ cc_library(
deps = [
":class",
":class_loader",
":corpus",
":corpus_tag",
":forward_declarations",
":object",
":proxy_convenience_aliases",
":ref_base",
Expand All @@ -959,6 +930,8 @@ cc_library(
"//metaprogramming:cartesian_product",
"//metaprogramming:combine",
"//metaprogramming:concatenate",
"//metaprogramming:corpus",
"//metaprogramming:corpus_tag",
"//metaprogramming:flatten",
"//metaprogramming:invoke",
"//metaprogramming:per_element",
Expand Down
3 changes: 3 additions & 0 deletions implementation/forward_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ namespace jni {
#define JNI_BIND_C_ENTRYPOINT(class_name, return_type, method_name, ...) \
return_type class_name_##method_name(JNIEnv*, jclass, ##__VA_ARGS__)

// Provide this base tag to UserDefined to enable custom types.
struct JniUserDefinedCorpusTag {};

// ArrayView Helper.
template <typename T>
struct ArrayViewHelper;
Expand Down
14 changes: 7 additions & 7 deletions implementation/proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

#include "implementation/class.h"
#include "implementation/class_loader.h"
#include "implementation/corpus.h"
#include "implementation/corpus_tag.h"
#include "implementation/forward_declarations.h"
#include "implementation/object.h"
#include "implementation/proxy_convenience_aliases.h"
#include "implementation/ref_base.h"
Expand All @@ -34,6 +33,7 @@
#include "metaprogramming/cartesian_product.h"
#include "metaprogramming/combine.h"
#include "metaprogramming/concatenate.h"
#include "metaprogramming/corpus.h"
#include "metaprogramming/flatten.h"
#include "metaprogramming/invoke.h"
#include "metaprogramming/per_element.h"
Expand All @@ -50,11 +50,11 @@ template <typename t1, typename t2 = void>
struct Proxy;

// CDecls for all declarable types (these index into proxy definitions).
using AllKeys =
Corpus_t<JniUserDefinedCorpusTag, void, jboolean, jbyte, jshort, jint,
jfloat, jlong, jchar, jdouble, jstring, jobject, Self, jarray,
jobjectArray, jintArray, jbooleanArray, jbyteArray, jcharArray,
jshortArray, jdoubleArray, jfloatArray, jlongArray>;
using AllKeys = metaprogramming::Corpus_t<
JniUserDefinedCorpusTag, void, jboolean, jbyte, jshort, jint, jfloat, jlong,
jchar, jdouble, jstring, jobject, Self, jarray, jobjectArray, jintArray,
jbooleanArray, jbyteArray, jcharArray, jshortArray, jdoubleArray,
jfloatArray, jlongArray>;

template <typename TUndecayed>
struct ProxyHelper {
Expand Down
4 changes: 2 additions & 2 deletions jni_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include "jni_dep.h"

// JNI Corpus.
#include "implementation/corpus.h"
#include "implementation/corpus_tag.h"
#include "metaprogramming/corpus.h"
#include "metaprogramming/corpus_tag.h"

// Headers for static definitions.
#include "implementation/array.h"
Expand Down
26 changes: 26 additions & 0 deletions metaprogramming/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,32 @@ cc_test(
],
)

cc_library(
name = "corpus",
hdrs = ["corpus.h"],
deps = [
":concatenate",
":corpus_tag",
":detect",
],
)

cc_library(
name = "corpus_tag",
hdrs = ["corpus_tag.h"],
)

cc_test(
name = "corpus_test",
srcs = ["corpus_test.cc"],
deps = [
":corpus",
":corpus_tag",
":detect",
"@googletest//:gtest_main",
],
)

cc_library(
name = "deep_equal",
hdrs = ["deep_equal.h"],
Expand Down
18 changes: 8 additions & 10 deletions implementation/corpus.h → metaprogramming/corpus.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@

#include <tuple>

#include "implementation/corpus_tag.h"
#include "metaprogramming/concatenate.h"
#include "metaprogramming/detect.h"
#include "concatenate.h"
#include "corpus_tag.h"
#include "detect.h"

namespace jni {
namespace jni::metaprogramming {

// Provides the universe of keys, including user defined types if any.
// Users define custom types by partially specialising UserDefined (see test).
template <typename... Defaults>
struct Corpus {
template <typename T, bool = ::jni::metaprogramming::Detect_v<
UserDefined, JniUserDefinedCorpusTag> >
template <typename T, bool = Detect_v<UserDefined, T> >
struct Helper {
using type = ::jni::metaprogramming::ConcatenateTup_t<
::jni::metaprogramming::Detect_t<UserDefined, JniUserDefinedCorpusTag>,
std::tuple<Defaults...> >;
using type =
ConcatenateTup_t<Detect_t<UserDefined, T>, std::tuple<Defaults...> >;
};

template <typename T>
Expand All @@ -48,6 +46,6 @@ struct Corpus {
template <typename T, typename... Defaults>
using Corpus_t = typename Corpus<Defaults...>::template type<T>;

} // namespace jni
} // namespace jni::metaprogramming

#endif // JNI_BIND_IMPLEMENTATION_CORPUS_H_
7 changes: 2 additions & 5 deletions implementation/corpus_tag.h → metaprogramming/corpus_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
#ifndef JNI_BIND_IMPLEMENTATION_CORPUS_TAG_H_
#define JNI_BIND_IMPLEMENTATION_CORPUS_TAG_H_

namespace jni {

// Provide this base tag to UserDefined to enable custom types.
struct JniUserDefinedCorpusTag {};
namespace jni::metaprogramming {

// Provide a partial specialization to this class to provide custom types.
template <typename T>
struct UserDefined;

} // namespace jni
} // namespace jni::metaprogramming

#endif // JNI_BIND_IMPLEMENTATION_CORPUS_TAG_H_
23 changes: 12 additions & 11 deletions implementation/corpus_test.cc → metaprogramming/corpus_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,39 @@
* limitations under the License.
*/
#include <tuple>
#include <type_traits>

#include "implementation/corpus_tag.h"
#include "corpus_tag.h"

struct CorpusTag {};

struct A {};
struct B {};
struct C {};

// Note: This ordering is intentional (see `::jni::Corpus`).
namespace jni {
namespace jni::metaprogramming {

template <>
struct UserDefined<JniUserDefinedCorpusTag> {
struct UserDefined<CorpusTag> {
using type = std::tuple<A, B, C>;
};

} // namespace jni
} // namespace jni::metaprogramming

#include "implementation/corpus.h"
#include "metaprogramming/corpus.h"
#include "metaprogramming/detect.h"

using ::jni::Corpus_t;
using ::jni::JniUserDefinedCorpusTag;
using ::jni::UserDefined;
using ::jni::metaprogramming::Corpus_t;
using ::jni::metaprogramming::Detect_t;
using ::jni::metaprogramming::UserDefined;

namespace {

static_assert(
std::is_same_v<Detect_t<jni::UserDefined, JniUserDefinedCorpusTag>,
std::tuple<A, B, C>>);
std::is_same_v<Detect_t<UserDefined, CorpusTag>, std::tuple<A, B, C>>);

static_assert(std::is_same_v<Corpus_t<JniUserDefinedCorpusTag, int, float>,
static_assert(std::is_same_v<Corpus_t<CorpusTag, int, float>,
std::tuple<A, B, C, int, float>>);

} // namespace