java/*: add noexcept

This commit is contained in:
Max Kellermann 2018-08-28 13:27:28 +02:00
parent 7137ca375a
commit 35eca08d48
10 changed files with 50 additions and 44 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2018 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2010-2018 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
@ -41,10 +41,10 @@ namespace Java {
*/
class Class : public LocalRef<jclass> {
public:
Class(JNIEnv *env, jclass cls)
Class(JNIEnv *env, jclass cls) noexcept
:LocalRef<jclass>(env, cls) {}
Class(JNIEnv *env, const char *name)
Class(JNIEnv *env, const char *name) noexcept
:LocalRef<jclass>(env, env->FindClass(name)) {}
};
@ -53,7 +53,7 @@ namespace Java {
*/
class TrivialClass : public TrivialRef<jclass> {
public:
void Find(JNIEnv *env, const char *name) {
void Find(JNIEnv *env, const char *name) noexcept {
assert(env != nullptr);
assert(name != nullptr);
@ -64,7 +64,7 @@ namespace Java {
env->DeleteLocalRef(cls);
}
bool FindOptional(JNIEnv *env, const char *name) {
bool FindOptional(JNIEnv *env, const char *name) noexcept {
assert(env != nullptr);
assert(name != nullptr);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2012 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2010-2018 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,7 +38,7 @@ namespace Java {
*
* @return true if an exception was found (and discarded)
*/
static inline bool DiscardException(JNIEnv *env) {
static inline bool DiscardException(JNIEnv *env) noexcept {
bool result = env->ExceptionCheck();
if (result)
env->ExceptionClear();

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2014 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2010-2018 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,7 +38,7 @@
jmethodID Java::File::getAbsolutePath_method;
void
Java::File::Initialise(JNIEnv *env)
Java::File::Initialise(JNIEnv *env) noexcept
{
Class cls(env, "java/io/File");
@ -47,7 +47,7 @@ Java::File::Initialise(JNIEnv *env)
}
AllocatedPath
Java::File::ToAbsolutePath(JNIEnv *env, jobject _file)
Java::File::ToAbsolutePath(JNIEnv *env, jobject _file) noexcept
{
assert(env != nullptr);
assert(_file != nullptr);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2014 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2010-2018 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
@ -45,10 +45,10 @@ namespace Java {
public:
gcc_nonnull_all
static void Initialise(JNIEnv *env);
static void Initialise(JNIEnv *env) noexcept;
gcc_nonnull_all
static jstring getAbsolutePath(JNIEnv *env, jobject file) {
static jstring getAbsolutePath(JNIEnv *env, jobject file) noexcept {
return (jstring)env->CallObjectMethod(file,
getAbsolutePath_method);
}
@ -59,7 +59,7 @@ namespace Java {
*/
gcc_pure gcc_nonnull_all
static AllocatedPath ToAbsolutePath(JNIEnv *env,
jobject file);
jobject file) noexcept;
};
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2011 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2010-2018 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
@ -32,7 +32,7 @@
namespace Java {
JavaVM *jvm;
void Init(JNIEnv *env)
void Init(JNIEnv *env) noexcept
{
env->GetJavaVM(&jvm);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2011 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2010-2018 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,17 +37,17 @@
namespace Java {
extern JavaVM *jvm;
void Init(JNIEnv *env);
void Init(JNIEnv *env) noexcept;
static inline void
DetachCurrentThread()
DetachCurrentThread() noexcept
{
if (jvm != nullptr)
jvm->DetachCurrentThread();
}
static inline gcc_pure
JNIEnv *GetEnv()
JNIEnv *GetEnv() noexcept
{
JNIEnv *env;
jvm->AttachCurrentThread(&env, nullptr);

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2011 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2010-2018 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
@ -48,7 +48,8 @@ namespace Java {
*/
Object() = default;
Object(JNIEnv *env, jobject obj):GlobalRef<jobject>(env, obj) {}
Object(JNIEnv *env, jobject obj) noexcept
:GlobalRef<jobject>(env, obj) {}
};
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2011 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2010-2018 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
@ -49,23 +49,25 @@ namespace Java {
/**
* The local reference is obtained by the caller.
*/
LocalRef(JNIEnv *_env, T _value):env(_env), value(_value) {
LocalRef(JNIEnv *_env, T _value) noexcept
:env(_env), value(_value)
{
assert(env != nullptr);
assert(value != nullptr);
}
~LocalRef() {
~LocalRef() noexcept {
env->DeleteLocalRef(value);
}
LocalRef(const LocalRef &other) = delete;
LocalRef &operator=(const LocalRef &other) = delete;
T Get() const {
T Get() const noexcept {
return value;
}
operator T() const {
operator T() const noexcept {
return value;
}
};
@ -84,14 +86,16 @@ namespace Java {
*/
GlobalRef() = default;
GlobalRef(JNIEnv *env, T _value):value(_value) {
GlobalRef(JNIEnv *env, T _value) noexcept
:value(_value)
{
assert(env != nullptr);
assert(value != nullptr);
value = (T)env->NewGlobalRef(value);
}
~GlobalRef() {
~GlobalRef() noexcept {
GetEnv()->DeleteGlobalRef(value);
}
@ -102,17 +106,17 @@ namespace Java {
* Sets the object, ignoring the previous value. This is only
* allowed once after the default constructor was used.
*/
void Set(JNIEnv *env, T _value) {
void Set(JNIEnv *env, T _value) noexcept {
assert(_value != nullptr);
value = (T)env->NewGlobalRef(_value);
}
T Get() const {
T Get() const noexcept {
return value;
}
operator T() const {
operator T() const noexcept {
return value;
}
};
@ -129,12 +133,12 @@ namespace Java {
T value;
public:
constexpr TrivialRef() {};
TrivialRef() = default;
TrivialRef(const TrivialRef &other) = delete;
TrivialRef &operator=(const TrivialRef &other) = delete;
bool IsDefined() const {
bool IsDefined() const noexcept {
return value != nullptr;
}
@ -142,7 +146,7 @@ namespace Java {
* Obtain a global reference on the specified object and store it.
* This object must not be set already.
*/
void Set(JNIEnv *env, T _value) {
void Set(JNIEnv *env, T _value) noexcept {
assert(value == nullptr);
assert(_value != nullptr);
@ -152,7 +156,7 @@ namespace Java {
/**
* Release the global reference and clear this object.
*/
void Clear(JNIEnv *env) {
void Clear(JNIEnv *env) noexcept {
assert(value != nullptr);
env->DeleteGlobalRef(value);
@ -163,16 +167,16 @@ namespace Java {
* Release the global reference and clear this object. It is
* allowed to call this method without ever calling Set().
*/
void ClearOptional(JNIEnv *env) {
void ClearOptional(JNIEnv *env) noexcept {
if (value != nullptr)
Clear(env);
}
T Get() const {
T Get() const noexcept {
return value;
}
operator T() const {
operator T() const noexcept {
return value;
}
};

View File

@ -32,7 +32,7 @@
char *
Java::String::CopyTo(JNIEnv *env, jstring value,
char *buffer, size_t max_size)
char *buffer, size_t max_size) noexcept
{
const char *p = env->GetStringUTFChars(value, nullptr);
if (p == nullptr)

View File

@ -42,10 +42,10 @@ namespace Java {
*/
class String : public LocalRef<jstring> {
public:
String(JNIEnv *env, jstring value)
String(JNIEnv *env, jstring value) noexcept
:LocalRef<jstring>(env, value) {}
String(JNIEnv *_env, const char *_value)
String(JNIEnv *_env, const char *_value) noexcept
:LocalRef<jstring>(_env, _env->NewStringUTF(_value)) {}
/**
@ -56,7 +56,7 @@ namespace Java {
* nullptr on error
*/
static char *CopyTo(JNIEnv *env, jstring value,
char *buffer, size_t max_size);
char *buffer, size_t max_size) noexcept;
/**
* Copy the value to the specified buffer. Truncates
@ -65,7 +65,8 @@ namespace Java {
* @return a pointer to the terminating null byte,
* nullptr on error
*/
char *CopyTo(JNIEnv *env, char *buffer, size_t max_size) {
char *CopyTo(JNIEnv *env,
char *buffer, size_t max_size) noexcept {
return CopyTo(env, Get(), buffer, max_size);
}
};