Java: no namespace indent
This commit is contained in:
parent
a52ce7bb7b
commit
f58c14a74a
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -36,47 +36,49 @@
|
||||
#include <cassert>
|
||||
|
||||
namespace Java {
|
||||
/**
|
||||
* Wrapper for a local "jclass" reference.
|
||||
*/
|
||||
class Class : public LocalRef<jclass> {
|
||||
public:
|
||||
Class(JNIEnv *env, jclass cls) noexcept
|
||||
:LocalRef<jclass>(env, cls) {}
|
||||
|
||||
Class(JNIEnv *env, const char *name) noexcept
|
||||
:LocalRef<jclass>(env, env->FindClass(name)) {}
|
||||
};
|
||||
/**
|
||||
* Wrapper for a local "jclass" reference.
|
||||
*/
|
||||
class Class : public LocalRef<jclass> {
|
||||
public:
|
||||
Class(JNIEnv *env, jclass cls) noexcept
|
||||
:LocalRef<jclass>(env, cls) {}
|
||||
|
||||
/**
|
||||
* Wrapper for a global "jclass" reference.
|
||||
*/
|
||||
class TrivialClass : public TrivialRef<jclass> {
|
||||
public:
|
||||
void Find(JNIEnv *env, const char *name) noexcept {
|
||||
assert(env != nullptr);
|
||||
assert(name != nullptr);
|
||||
Class(JNIEnv *env, const char *name) noexcept
|
||||
:LocalRef<jclass>(env, env->FindClass(name)) {}
|
||||
};
|
||||
|
||||
jclass cls = env->FindClass(name);
|
||||
assert(cls != nullptr);
|
||||
/**
|
||||
* Wrapper for a global "jclass" reference.
|
||||
*/
|
||||
class TrivialClass : public TrivialRef<jclass> {
|
||||
public:
|
||||
void Find(JNIEnv *env, const char *name) noexcept {
|
||||
assert(env != nullptr);
|
||||
assert(name != nullptr);
|
||||
|
||||
Set(env, cls);
|
||||
env->DeleteLocalRef(cls);
|
||||
}
|
||||
jclass cls = env->FindClass(name);
|
||||
assert(cls != nullptr);
|
||||
|
||||
bool FindOptional(JNIEnv *env, const char *name) noexcept {
|
||||
assert(env != nullptr);
|
||||
assert(name != nullptr);
|
||||
Set(env, cls);
|
||||
env->DeleteLocalRef(cls);
|
||||
}
|
||||
|
||||
jclass cls = env->FindClass(name);
|
||||
if (DiscardException(env))
|
||||
return false;
|
||||
bool FindOptional(JNIEnv *env, const char *name) noexcept {
|
||||
assert(env != nullptr);
|
||||
assert(name != nullptr);
|
||||
|
||||
Set(env, cls);
|
||||
env->DeleteLocalRef(cls);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
jclass cls = env->FindClass(name);
|
||||
if (DiscardException(env))
|
||||
return false;
|
||||
|
||||
Set(env, cls);
|
||||
env->DeleteLocalRef(cls);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Java
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -35,28 +35,30 @@
|
||||
#include <jni.h>
|
||||
|
||||
namespace Java {
|
||||
class Exception : public std::runtime_error {
|
||||
public:
|
||||
explicit Exception(JNIEnv *env, jthrowable e) noexcept;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a Java exception has occurred, and if yes, convert
|
||||
* it to a C++ #Exception and throw that.
|
||||
*/
|
||||
void RethrowException(JNIEnv *env);
|
||||
class Exception : public std::runtime_error {
|
||||
public:
|
||||
explicit Exception(JNIEnv *env, jthrowable e) noexcept;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if an exception has occurred, and discard it.
|
||||
*
|
||||
* @return true if an exception was found (and discarded)
|
||||
*/
|
||||
static inline bool DiscardException(JNIEnv *env) noexcept {
|
||||
bool result = env->ExceptionCheck();
|
||||
if (result)
|
||||
env->ExceptionClear();
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Check if a Java exception has occurred, and if yes, convert
|
||||
* it to a C++ #Exception and throw that.
|
||||
*/
|
||||
void RethrowException(JNIEnv *env);
|
||||
|
||||
/**
|
||||
* Check if an exception has occurred, and discard it.
|
||||
*
|
||||
* @return true if an exception was found (and discarded)
|
||||
*/
|
||||
static inline bool DiscardException(JNIEnv *env) noexcept {
|
||||
bool result = env->ExceptionCheck();
|
||||
if (result)
|
||||
env->ExceptionClear();
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace Java
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -37,30 +37,32 @@
|
||||
class AllocatedPath;
|
||||
|
||||
namespace Java {
|
||||
|
||||
/**
|
||||
* Wrapper for a java.io.File object.
|
||||
*/
|
||||
class File : public LocalObject {
|
||||
static jmethodID getAbsolutePath_method;
|
||||
|
||||
public:
|
||||
gcc_nonnull_all
|
||||
static void Initialise(JNIEnv *env) noexcept;
|
||||
|
||||
gcc_nonnull_all
|
||||
static jstring getAbsolutePath(JNIEnv *env, jobject file) noexcept {
|
||||
return (jstring)env->CallObjectMethod(file,
|
||||
getAbsolutePath_method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for a java.io.File object.
|
||||
* Invoke File.getAbsolutePath() and release the
|
||||
* specified File reference.
|
||||
*/
|
||||
class File : public LocalObject {
|
||||
static jmethodID getAbsolutePath_method;
|
||||
gcc_pure gcc_nonnull_all
|
||||
static AllocatedPath ToAbsolutePath(JNIEnv *env,
|
||||
jobject file) noexcept;
|
||||
};
|
||||
|
||||
public:
|
||||
gcc_nonnull_all
|
||||
static void Initialise(JNIEnv *env) noexcept;
|
||||
|
||||
gcc_nonnull_all
|
||||
static jstring getAbsolutePath(JNIEnv *env, jobject file) noexcept {
|
||||
return (jstring)env->CallObjectMethod(file,
|
||||
getAbsolutePath_method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke File.getAbsolutePath() and release the
|
||||
* specified File reference.
|
||||
*/
|
||||
gcc_pure gcc_nonnull_all
|
||||
static AllocatedPath ToAbsolutePath(JNIEnv *env,
|
||||
jobject file) noexcept;
|
||||
};
|
||||
}
|
||||
} // namespace Java
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -30,10 +30,12 @@
|
||||
#include "Global.hxx"
|
||||
|
||||
namespace Java {
|
||||
JavaVM *jvm;
|
||||
|
||||
void Init(JNIEnv *env) noexcept
|
||||
{
|
||||
env->GetJavaVM(&jvm);
|
||||
}
|
||||
JavaVM *jvm;
|
||||
|
||||
void Init(JNIEnv *env) noexcept
|
||||
{
|
||||
env->GetJavaVM(&jvm);
|
||||
}
|
||||
|
||||
} // namespace Java
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -35,24 +35,26 @@
|
||||
#include <jni.h>
|
||||
|
||||
namespace Java {
|
||||
extern JavaVM *jvm;
|
||||
|
||||
void Init(JNIEnv *env) noexcept;
|
||||
extern JavaVM *jvm;
|
||||
|
||||
static inline void
|
||||
DetachCurrentThread() noexcept
|
||||
{
|
||||
if (jvm != nullptr)
|
||||
jvm->DetachCurrentThread();
|
||||
}
|
||||
void Init(JNIEnv *env) noexcept;
|
||||
|
||||
static inline gcc_pure
|
||||
JNIEnv *GetEnv() noexcept
|
||||
{
|
||||
JNIEnv *env;
|
||||
jvm->AttachCurrentThread(&env, nullptr);
|
||||
return env;
|
||||
}
|
||||
static inline void
|
||||
DetachCurrentThread() noexcept
|
||||
{
|
||||
if (jvm != nullptr)
|
||||
jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
static inline gcc_pure
|
||||
JNIEnv *GetEnv() noexcept
|
||||
{
|
||||
JNIEnv *env;
|
||||
jvm->AttachCurrentThread(&env, nullptr);
|
||||
return env;
|
||||
}
|
||||
|
||||
} // namespace Java
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -37,40 +37,42 @@
|
||||
#include <cassert>
|
||||
|
||||
namespace Java {
|
||||
|
||||
/**
|
||||
* Wrapper for a local "jobject" reference.
|
||||
*/
|
||||
typedef LocalRef<jobject> LocalObject;
|
||||
|
||||
class GlobalObject : public GlobalRef<jobject> {
|
||||
public:
|
||||
/**
|
||||
* Wrapper for a local "jobject" reference.
|
||||
* Constructs an uninitialized object. The method
|
||||
* set() must be called before it is destructed.
|
||||
*/
|
||||
typedef LocalRef<jobject> LocalObject;
|
||||
GlobalObject() = default;
|
||||
|
||||
class GlobalObject : public GlobalRef<jobject> {
|
||||
public:
|
||||
/**
|
||||
* Constructs an uninitialized object. The method
|
||||
* set() must be called before it is destructed.
|
||||
*/
|
||||
GlobalObject() = default;
|
||||
GlobalObject(JNIEnv *env, jobject obj) noexcept
|
||||
:GlobalRef<jobject>(env, obj) {}
|
||||
};
|
||||
|
||||
GlobalObject(JNIEnv *env, jobject obj) noexcept
|
||||
:GlobalRef<jobject>(env, obj) {}
|
||||
};
|
||||
/**
|
||||
* Utilities for java.net.Object.
|
||||
*/
|
||||
class Object {
|
||||
static jmethodID toString_method;
|
||||
|
||||
/**
|
||||
* Utilities for java.net.Object.
|
||||
*/
|
||||
class Object {
|
||||
static jmethodID toString_method;
|
||||
public:
|
||||
static void Initialise(JNIEnv *env);
|
||||
|
||||
public:
|
||||
static void Initialise(JNIEnv *env);
|
||||
static jstring toString(JNIEnv *env, jobject o) {
|
||||
assert(env != nullptr);
|
||||
assert(o != nullptr);
|
||||
assert(toString_method != nullptr);
|
||||
|
||||
static jstring toString(JNIEnv *env, jobject o) {
|
||||
assert(env != nullptr);
|
||||
assert(o != nullptr);
|
||||
assert(toString_method != nullptr);
|
||||
return (jstring)env->CallObjectMethod(o, toString_method);
|
||||
}
|
||||
};
|
||||
|
||||
return (jstring)env->CallObjectMethod(o, toString_method);
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace Java
|
||||
|
||||
#endif
|
||||
|
280
src/java/Ref.hxx
280
src/java/Ref.hxx
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -38,170 +38,172 @@
|
||||
#include <utility>
|
||||
|
||||
namespace Java {
|
||||
/**
|
||||
* Hold a local reference on a JNI object.
|
||||
*/
|
||||
template<typename T>
|
||||
class LocalRef {
|
||||
JNIEnv *env;
|
||||
T value = nullptr;
|
||||
|
||||
public:
|
||||
LocalRef() noexcept = default;
|
||||
/**
|
||||
* Hold a local reference on a JNI object.
|
||||
*/
|
||||
template<typename T>
|
||||
class LocalRef {
|
||||
JNIEnv *env;
|
||||
T value = nullptr;
|
||||
|
||||
/**
|
||||
* The local reference is obtained by the caller. May
|
||||
* be nullptr.
|
||||
*/
|
||||
LocalRef(JNIEnv *_env, T _value) noexcept
|
||||
:env(_env), value(_value)
|
||||
{
|
||||
assert(env != nullptr);
|
||||
}
|
||||
|
||||
LocalRef(LocalRef &&src) noexcept
|
||||
:env(src.env),
|
||||
value(std::exchange(src.value, nullptr)) {}
|
||||
|
||||
~LocalRef() noexcept {
|
||||
if (value != nullptr)
|
||||
env->DeleteLocalRef(value);
|
||||
}
|
||||
|
||||
LocalRef &operator=(LocalRef &&src) noexcept {
|
||||
using std::swap;
|
||||
swap(env, src.env);
|
||||
swap(value, src.value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
JNIEnv *GetEnv() const noexcept {
|
||||
return env;
|
||||
}
|
||||
|
||||
operator bool() const noexcept {
|
||||
return value != nullptr;
|
||||
}
|
||||
|
||||
T Get() const noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
operator T() const noexcept {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
public:
|
||||
LocalRef() noexcept = default;
|
||||
|
||||
/**
|
||||
* Hold a global reference on a JNI object.
|
||||
* The local reference is obtained by the caller. May
|
||||
* be nullptr.
|
||||
*/
|
||||
template<typename T>
|
||||
class GlobalRef {
|
||||
T value;
|
||||
LocalRef(JNIEnv *_env, T _value) noexcept
|
||||
:env(_env), value(_value)
|
||||
{
|
||||
assert(env != nullptr);
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructs an uninitialized object. The method
|
||||
* set() must be called before it is destructed.
|
||||
*/
|
||||
GlobalRef() = default;
|
||||
LocalRef(LocalRef &&src) noexcept
|
||||
:env(src.env),
|
||||
value(std::exchange(src.value, nullptr)) {}
|
||||
|
||||
GlobalRef(JNIEnv *env, T _value) noexcept
|
||||
:value(_value)
|
||||
{
|
||||
assert(env != nullptr);
|
||||
assert(value != nullptr);
|
||||
~LocalRef() noexcept {
|
||||
if (value != nullptr)
|
||||
env->DeleteLocalRef(value);
|
||||
}
|
||||
|
||||
value = (T)env->NewGlobalRef(value);
|
||||
}
|
||||
LocalRef &operator=(LocalRef &&src) noexcept {
|
||||
using std::swap;
|
||||
swap(env, src.env);
|
||||
swap(value, src.value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
~GlobalRef() noexcept {
|
||||
GetEnv()->DeleteGlobalRef(value);
|
||||
}
|
||||
JNIEnv *GetEnv() const noexcept {
|
||||
return env;
|
||||
}
|
||||
|
||||
GlobalRef(const GlobalRef &other) = delete;
|
||||
GlobalRef &operator=(const GlobalRef &other) = delete;
|
||||
operator bool() const noexcept {
|
||||
return value != nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the object, ignoring the previous value. This
|
||||
* is only allowed once after the default constructor
|
||||
* was used.
|
||||
*/
|
||||
void Set(JNIEnv *env, T _value) noexcept {
|
||||
assert(_value != nullptr);
|
||||
T Get() const noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
value = (T)env->NewGlobalRef(_value);
|
||||
}
|
||||
operator T() const noexcept {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
T Get() const noexcept {
|
||||
return value;
|
||||
}
|
||||
/**
|
||||
* Hold a global reference on a JNI object.
|
||||
*/
|
||||
template<typename T>
|
||||
class GlobalRef {
|
||||
T value;
|
||||
|
||||
operator T() const noexcept {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
public:
|
||||
/**
|
||||
* Constructs an uninitialized object. The method
|
||||
* set() must be called before it is destructed.
|
||||
*/
|
||||
GlobalRef() = default;
|
||||
|
||||
GlobalRef(JNIEnv *env, T _value) noexcept
|
||||
:value(_value)
|
||||
{
|
||||
assert(env != nullptr);
|
||||
assert(value != nullptr);
|
||||
|
||||
value = (T)env->NewGlobalRef(value);
|
||||
}
|
||||
|
||||
~GlobalRef() noexcept {
|
||||
GetEnv()->DeleteGlobalRef(value);
|
||||
}
|
||||
|
||||
GlobalRef(const GlobalRef &other) = delete;
|
||||
GlobalRef &operator=(const GlobalRef &other) = delete;
|
||||
|
||||
/**
|
||||
* Container for a global reference to a JNI object that gets
|
||||
* initialised and deinitialised explicitly. Since there is
|
||||
* no implicit initialisation in the default constructor, this
|
||||
* is a trivial C++ class. It should only be used for global
|
||||
* variables that are implicitly initialised with zeroes.
|
||||
* Sets the object, ignoring the previous value. This
|
||||
* is only allowed once after the default constructor
|
||||
* was used.
|
||||
*/
|
||||
template<typename T>
|
||||
class TrivialRef {
|
||||
T value;
|
||||
void Set(JNIEnv *env, T _value) noexcept {
|
||||
assert(_value != nullptr);
|
||||
|
||||
public:
|
||||
TrivialRef() = default;
|
||||
value = (T)env->NewGlobalRef(_value);
|
||||
}
|
||||
|
||||
TrivialRef(const TrivialRef &other) = delete;
|
||||
TrivialRef &operator=(const TrivialRef &other) = delete;
|
||||
T Get() const noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
bool IsDefined() const noexcept {
|
||||
return value != nullptr;
|
||||
}
|
||||
operator T() const noexcept {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Obtain a global reference on the specified object
|
||||
* and store it. This object must not be set already.
|
||||
*/
|
||||
void Set(JNIEnv *env, T _value) noexcept {
|
||||
assert(value == nullptr);
|
||||
assert(_value != nullptr);
|
||||
/**
|
||||
* Container for a global reference to a JNI object that gets
|
||||
* initialised and deinitialised explicitly. Since there is
|
||||
* no implicit initialisation in the default constructor, this
|
||||
* is a trivial C++ class. It should only be used for global
|
||||
* variables that are implicitly initialised with zeroes.
|
||||
*/
|
||||
template<typename T>
|
||||
class TrivialRef {
|
||||
T value;
|
||||
|
||||
value = (T)env->NewGlobalRef(_value);
|
||||
}
|
||||
public:
|
||||
TrivialRef() = default;
|
||||
|
||||
/**
|
||||
* Release the global reference and clear this object.
|
||||
*/
|
||||
void Clear(JNIEnv *env) noexcept {
|
||||
assert(value != nullptr);
|
||||
TrivialRef(const TrivialRef &other) = delete;
|
||||
TrivialRef &operator=(const TrivialRef &other) = delete;
|
||||
|
||||
env->DeleteGlobalRef(value);
|
||||
value = nullptr;
|
||||
}
|
||||
bool IsDefined() const noexcept {
|
||||
return value != nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the global reference and clear this object.
|
||||
* It is allowed to call this method without ever
|
||||
* calling Set().
|
||||
*/
|
||||
void ClearOptional(JNIEnv *env) noexcept {
|
||||
if (value != nullptr)
|
||||
Clear(env);
|
||||
}
|
||||
/**
|
||||
* Obtain a global reference on the specified object
|
||||
* and store it. This object must not be set already.
|
||||
*/
|
||||
void Set(JNIEnv *env, T _value) noexcept {
|
||||
assert(value == nullptr);
|
||||
assert(_value != nullptr);
|
||||
|
||||
T Get() const noexcept {
|
||||
return value;
|
||||
}
|
||||
value = (T)env->NewGlobalRef(_value);
|
||||
}
|
||||
|
||||
operator T() const noexcept {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Release the global reference and clear this object.
|
||||
*/
|
||||
void Clear(JNIEnv *env) noexcept {
|
||||
assert(value != nullptr);
|
||||
|
||||
env->DeleteGlobalRef(value);
|
||||
value = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the global reference and clear this object.
|
||||
* It is allowed to call this method without ever
|
||||
* calling Set().
|
||||
*/
|
||||
void ClearOptional(JNIEnv *env) noexcept {
|
||||
if (value != nullptr)
|
||||
Clear(env);
|
||||
}
|
||||
|
||||
T Get() const noexcept {
|
||||
return value;
|
||||
}
|
||||
|
||||
operator T() const noexcept {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Java
|
||||
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* 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
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -38,45 +38,47 @@
|
||||
#include <string>
|
||||
|
||||
namespace Java {
|
||||
|
||||
/**
|
||||
* Wrapper for a local "jstring" reference.
|
||||
*/
|
||||
class String : public LocalRef<jstring> {
|
||||
public:
|
||||
String(JNIEnv *env, jstring value) noexcept
|
||||
:LocalRef<jstring>(env, value) {}
|
||||
|
||||
String(JNIEnv *_env, const char *_value) noexcept
|
||||
:LocalRef<jstring>(_env, _env->NewStringUTF(_value)) {}
|
||||
|
||||
/**
|
||||
* Wrapper for a local "jstring" reference.
|
||||
* Copy the value to the specified buffer. Truncates
|
||||
* the value if it does not fit into the buffer.
|
||||
*
|
||||
* @return a pointer to the terminating null byte,
|
||||
* nullptr on error
|
||||
*/
|
||||
class String : public LocalRef<jstring> {
|
||||
public:
|
||||
String(JNIEnv *env, jstring value) noexcept
|
||||
:LocalRef<jstring>(env, value) {}
|
||||
static char *CopyTo(JNIEnv *env, jstring value,
|
||||
char *buffer, size_t max_size) noexcept;
|
||||
|
||||
String(JNIEnv *_env, const char *_value) noexcept
|
||||
:LocalRef<jstring>(_env, _env->NewStringUTF(_value)) {}
|
||||
/**
|
||||
* Copy the value to the specified buffer. Truncates
|
||||
* the value if it does not fit into the buffer.
|
||||
*
|
||||
* @return a pointer to the terminating null byte,
|
||||
* nullptr on error
|
||||
*/
|
||||
char *CopyTo(JNIEnv *env,
|
||||
char *buffer, size_t max_size) noexcept {
|
||||
return CopyTo(env, Get(), buffer, max_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the value to the specified buffer. Truncates
|
||||
* the value if it does not fit into the buffer.
|
||||
*
|
||||
* @return a pointer to the terminating null byte,
|
||||
* nullptr on error
|
||||
*/
|
||||
static char *CopyTo(JNIEnv *env, jstring value,
|
||||
char *buffer, size_t max_size) noexcept;
|
||||
static std::string ToString(JNIEnv *env, jstring s) noexcept;
|
||||
|
||||
/**
|
||||
* Copy the value to the specified buffer. Truncates
|
||||
* the value if it does not fit into the buffer.
|
||||
*
|
||||
* @return a pointer to the terminating null byte,
|
||||
* nullptr on error
|
||||
*/
|
||||
char *CopyTo(JNIEnv *env,
|
||||
char *buffer, size_t max_size) noexcept {
|
||||
return CopyTo(env, Get(), buffer, max_size);
|
||||
}
|
||||
std::string ToString() const noexcept {
|
||||
return ToString(GetEnv(), Get());
|
||||
}
|
||||
};
|
||||
|
||||
static std::string ToString(JNIEnv *env, jstring s) noexcept;
|
||||
|
||||
std::string ToString() const noexcept {
|
||||
return ToString(GetEnv(), Get());
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace Java
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user