Commit f58c14a7 authored by Max Kellermann's avatar Max Kellermann Committed by Max Kellermann

Java: no namespace indent

parent a52ce7bb
/* /*
* Copyright 2010-2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -36,23 +36,24 @@ ...@@ -36,23 +36,24 @@
#include <cassert> #include <cassert>
namespace Java { namespace Java {
/**
/**
* Wrapper for a local "jclass" reference. * Wrapper for a local "jclass" reference.
*/ */
class Class : public LocalRef<jclass> { class Class : public LocalRef<jclass> {
public: public:
Class(JNIEnv *env, jclass cls) noexcept Class(JNIEnv *env, jclass cls) noexcept
:LocalRef<jclass>(env, cls) {} :LocalRef<jclass>(env, cls) {}
Class(JNIEnv *env, const char *name) noexcept Class(JNIEnv *env, const char *name) noexcept
:LocalRef<jclass>(env, env->FindClass(name)) {} :LocalRef<jclass>(env, env->FindClass(name)) {}
}; };
/** /**
* Wrapper for a global "jclass" reference. * Wrapper for a global "jclass" reference.
*/ */
class TrivialClass : public TrivialRef<jclass> { class TrivialClass : public TrivialRef<jclass> {
public: public:
void Find(JNIEnv *env, const char *name) noexcept { void Find(JNIEnv *env, const char *name) noexcept {
assert(env != nullptr); assert(env != nullptr);
assert(name != nullptr); assert(name != nullptr);
...@@ -76,7 +77,8 @@ namespace Java { ...@@ -76,7 +77,8 @@ namespace Java {
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);
return true; return true;
} }
}; };
}
} // namespace Java
#endif #endif
/* /*
* Copyright 2010-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
......
/* /*
* Copyright 2010-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -35,28 +35,30 @@ ...@@ -35,28 +35,30 @@
#include <jni.h> #include <jni.h>
namespace Java { namespace Java {
class Exception : public std::runtime_error {
public: class Exception : public std::runtime_error {
public:
explicit Exception(JNIEnv *env, jthrowable e) noexcept; explicit Exception(JNIEnv *env, jthrowable e) noexcept;
}; };
/** /**
* Check if a Java exception has occurred, and if yes, convert * Check if a Java exception has occurred, and if yes, convert
* it to a C++ #Exception and throw that. * it to a C++ #Exception and throw that.
*/ */
void RethrowException(JNIEnv *env); void RethrowException(JNIEnv *env);
/** /**
* Check if an exception has occurred, and discard it. * Check if an exception has occurred, and discard it.
* *
* @return true if an exception was found (and discarded) * @return true if an exception was found (and discarded)
*/ */
static inline bool DiscardException(JNIEnv *env) noexcept { static inline bool DiscardException(JNIEnv *env) noexcept {
bool result = env->ExceptionCheck(); bool result = env->ExceptionCheck();
if (result) if (result)
env->ExceptionClear(); env->ExceptionClear();
return result; return result;
}
} }
} // namespace Java
#endif #endif
/* /*
* Copyright 2010-2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -37,13 +37,14 @@ ...@@ -37,13 +37,14 @@
class AllocatedPath; class AllocatedPath;
namespace Java { namespace Java {
/**
/**
* Wrapper for a java.io.File object. * Wrapper for a java.io.File object.
*/ */
class File : public LocalObject { class File : public LocalObject {
static jmethodID getAbsolutePath_method; static jmethodID getAbsolutePath_method;
public: public:
gcc_nonnull_all gcc_nonnull_all
static void Initialise(JNIEnv *env) noexcept; static void Initialise(JNIEnv *env) noexcept;
...@@ -60,7 +61,8 @@ namespace Java { ...@@ -60,7 +61,8 @@ namespace Java {
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
static AllocatedPath ToAbsolutePath(JNIEnv *env, static AllocatedPath ToAbsolutePath(JNIEnv *env,
jobject file) noexcept; jobject file) noexcept;
}; };
}
} // namespace Java
#endif #endif
/* /*
* Copyright 2010-2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -30,10 +30,12 @@ ...@@ -30,10 +30,12 @@
#include "Global.hxx" #include "Global.hxx"
namespace Java { namespace Java {
JavaVM *jvm;
void Init(JNIEnv *env) noexcept JavaVM *jvm;
{
void Init(JNIEnv *env) noexcept
{
env->GetJavaVM(&jvm); env->GetJavaVM(&jvm);
}
} }
} // namespace Java
/* /*
* Copyright 2010-2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -35,24 +35,26 @@ ...@@ -35,24 +35,26 @@
#include <jni.h> #include <jni.h>
namespace Java { namespace Java {
extern JavaVM *jvm;
void Init(JNIEnv *env) noexcept; extern JavaVM *jvm;
static inline void void Init(JNIEnv *env) noexcept;
DetachCurrentThread() noexcept
{ static inline void
DetachCurrentThread() noexcept
{
if (jvm != nullptr) if (jvm != nullptr)
jvm->DetachCurrentThread(); jvm->DetachCurrentThread();
} }
static inline gcc_pure static inline gcc_pure
JNIEnv *GetEnv() noexcept JNIEnv *GetEnv() noexcept
{ {
JNIEnv *env; JNIEnv *env;
jvm->AttachCurrentThread(&env, nullptr); jvm->AttachCurrentThread(&env, nullptr);
return env; return env;
}
} }
} // namespace Java
#endif #endif
/* /*
* Copyright 2010-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -37,13 +37,14 @@ ...@@ -37,13 +37,14 @@
#include <cassert> #include <cassert>
namespace Java { namespace Java {
/**
/**
* Wrapper for a local "jobject" reference. * Wrapper for a local "jobject" reference.
*/ */
typedef LocalRef<jobject> LocalObject; typedef LocalRef<jobject> LocalObject;
class GlobalObject : public GlobalRef<jobject> { class GlobalObject : public GlobalRef<jobject> {
public: public:
/** /**
* Constructs an uninitialized object. The method * Constructs an uninitialized object. The method
* set() must be called before it is destructed. * set() must be called before it is destructed.
...@@ -52,15 +53,15 @@ namespace Java { ...@@ -52,15 +53,15 @@ namespace Java {
GlobalObject(JNIEnv *env, jobject obj) noexcept GlobalObject(JNIEnv *env, jobject obj) noexcept
:GlobalRef<jobject>(env, obj) {} :GlobalRef<jobject>(env, obj) {}
}; };
/** /**
* Utilities for java.net.Object. * Utilities for java.net.Object.
*/ */
class Object { class Object {
static jmethodID toString_method; static jmethodID toString_method;
public: public:
static void Initialise(JNIEnv *env); static void Initialise(JNIEnv *env);
static jstring toString(JNIEnv *env, jobject o) { static jstring toString(JNIEnv *env, jobject o) {
...@@ -70,7 +71,8 @@ namespace Java { ...@@ -70,7 +71,8 @@ namespace Java {
return (jstring)env->CallObjectMethod(o, toString_method); return (jstring)env->CallObjectMethod(o, toString_method);
} }
}; };
}
} // namespace Java
#endif #endif
/* /*
* Copyright 2010-2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -38,15 +38,16 @@ ...@@ -38,15 +38,16 @@
#include <utility> #include <utility>
namespace Java { namespace Java {
/**
/**
* Hold a local reference on a JNI object. * Hold a local reference on a JNI object.
*/ */
template<typename T> template<typename T>
class LocalRef { class LocalRef {
JNIEnv *env; JNIEnv *env;
T value = nullptr; T value = nullptr;
public: public:
LocalRef() noexcept = default; LocalRef() noexcept = default;
/** /**
...@@ -90,16 +91,16 @@ namespace Java { ...@@ -90,16 +91,16 @@ namespace Java {
operator T() const noexcept { operator T() const noexcept {
return value; return value;
} }
}; };
/** /**
* Hold a global reference on a JNI object. * Hold a global reference on a JNI object.
*/ */
template<typename T> template<typename T>
class GlobalRef { class GlobalRef {
T value; T value;
public: public:
/** /**
* Constructs an uninitialized object. The method * Constructs an uninitialized object. The method
* set() must be called before it is destructed. * set() must be called before it is destructed.
...@@ -140,20 +141,20 @@ namespace Java { ...@@ -140,20 +141,20 @@ namespace Java {
operator T() const noexcept { operator T() const noexcept {
return value; return value;
} }
}; };
/** /**
* Container for a global reference to a JNI object that gets * Container for a global reference to a JNI object that gets
* initialised and deinitialised explicitly. Since there is * initialised and deinitialised explicitly. Since there is
* no implicit initialisation in the default constructor, this * no implicit initialisation in the default constructor, this
* is a trivial C++ class. It should only be used for global * is a trivial C++ class. It should only be used for global
* variables that are implicitly initialised with zeroes. * variables that are implicitly initialised with zeroes.
*/ */
template<typename T> template<typename T>
class TrivialRef { class TrivialRef {
T value; T value;
public: public:
TrivialRef() = default; TrivialRef() = default;
TrivialRef(const TrivialRef &other) = delete; TrivialRef(const TrivialRef &other) = delete;
...@@ -201,7 +202,8 @@ namespace Java { ...@@ -201,7 +202,8 @@ namespace Java {
operator T() const noexcept { operator T() const noexcept {
return value; return value;
} }
}; };
}
} // namespace Java
#endif #endif
/* /*
* Copyright 2010-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
...@@ -38,11 +38,12 @@ ...@@ -38,11 +38,12 @@
#include <string> #include <string>
namespace Java { namespace Java {
/**
/**
* Wrapper for a local "jstring" reference. * Wrapper for a local "jstring" reference.
*/ */
class String : public LocalRef<jstring> { class String : public LocalRef<jstring> {
public: public:
String(JNIEnv *env, jstring value) noexcept String(JNIEnv *env, jstring value) noexcept
:LocalRef<jstring>(env, value) {} :LocalRef<jstring>(env, value) {}
...@@ -76,7 +77,8 @@ namespace Java { ...@@ -76,7 +77,8 @@ namespace Java {
std::string ToString() const noexcept { std::string ToString() const noexcept {
return ToString(GetEnv(), Get()); return ToString(GetEnv(), Get());
} }
}; };
}
} // namespace Java
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment