Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / bindings / java / jxbt_utilities.hpp
1 /* Various JNI helper functions                                             */
2
3 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #ifndef JXBT_UTILITIES_HPP
9 #define JXBT_UTILITIES_HPP
10
11 #include <cstdint>
12 #include <jni.h>
13 #include <string>
14
15 /* Search a class and throw an exception if not found */
16 jclass jxbt_get_class(JNIEnv* env, const char* name);
17
18 /* Search a method in a class and throw an exception if not found(it's ok to to pass a NULL class: it's a noop) */
19 jmethodID jxbt_get_jmethod(JNIEnv* env, jclass cls, const char* name, const char* signature);
20
21 /* Like the jxbt_get_class() but get a static method */
22 jmethodID jxbt_get_static_jmethod(JNIEnv* env, jclass cls, const char* name, const char* signature);
23
24 /* Search a field in a class and throw an exception if not found (it's ok to to pass a NULL class: it's a noop) */
25 jfieldID jxbt_get_jfield(JNIEnv* env, jclass cls, const char* name, const char* signature);
26
27 /* Search a method in a class and throw an exception if not found (it's ok to to pass a NULL class: it's a noop) */
28 jmethodID jxbt_get_smethod(JNIEnv* env, const char* classname, const char* name, const char* signature);
29
30 /* Like the jxbt_get_smethod() but get a static method */
31 jmethodID jxbt_get_static_smethod(JNIEnv* env, const char* classname, const char* name, const char* signature);
32
33 /* Search a field in a class and throw an exception if not found (it's ok to to pass a NULL class: it's a noop) */
34 jfieldID jxbt_get_sfield(JNIEnv* env, const char* classname, const char* name, const char* signature);
35
36 /* Throws an exception according to its name */
37 void jxbt_throw_by_name(JNIEnv* env, const char* name, const std::string& msg);
38 /** Thrown on internal error of this layer, or on problem with JNI */
39 void jxbt_throw_jni(JNIEnv* env, const std::string& msg);
40 /** Thrown when using an object not bound to a native one where it should, or reverse (kinda JNI issue) */
41 void jxbt_throw_notbound(JNIEnv* env, const std::string& kind, void* pointer);
42 /** Thrown if NULL gets used */
43 void jxbt_throw_null(JNIEnv* env, const std::string& msg);
44
45 /** Thrown on illegal arguments */
46 void jxbt_throw_illegal(JNIEnv* env, const std::string& msg);
47 /** Thrown when looking for a host from name does not lead to anything */
48 void jxbt_throw_host_not_found(JNIEnv* env, const std::string& invalid_name);
49 /** Thrown when looking for a host from name does not lead to anything */
50 void jxbt_throw_process_not_found(JNIEnv* env, const std::string& invalid_name);
51 /** Thrown when a transfer failure occurs while Sending task */
52 void jxbt_throw_transfer_failure(JNIEnv* env, const std::string& detail);
53 /** Thrown when a host failure occurs while Sending a task*/
54 void jxbt_throw_host_failure(JNIEnv* env, const std::string& details);
55 /** Thrown when a timeout occurs while Sending a task */
56 void jxbt_throw_time_out_failure(JNIEnv* env, const std::string& details);
57 /**Thrown when a task is canceled */
58 void jxbt_throw_task_cancelled(JNIEnv* env, const std::string& details);
59
60 class jstring_wrapper {
61   JNIEnv* env_  = nullptr;
62   jstring jstr_ = nullptr;
63
64 public:
65   const char* value = nullptr;
66
67   jstring_wrapper(JNIEnv* env, jstring jstr) : env_(env), jstr_(jstr)
68   {
69     if (jstr != nullptr)
70       value = env_->GetStringUTFChars(jstr_, nullptr);
71   }
72   void reset(JNIEnv* env, jstring jstr)
73   {
74     if (jstr_ != nullptr)
75       env_->ReleaseStringUTFChars(jstr_, value);
76     env_  = env;
77     jstr_ = jstr;
78     if (jstr != nullptr)
79       value = env_->GetStringUTFChars(jstr_, nullptr);
80   }
81   ~jstring_wrapper()
82   {
83     if (jstr_ != nullptr)
84       env_->ReleaseStringUTFChars(jstr_, value);
85   }
86   operator const char*() const { return value; }
87   operator const std::string() const { return std::string(value); }
88 };
89
90 #endif