Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / bindings / java / jxbt_utilities.hpp
index 4106d32..4209f53 100644 (file)
@@ -1,6 +1,6 @@
 /* Various JNI helper functions                                             */
 
-/* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -57,4 +57,34 @@ void jxbt_throw_time_out_failure(JNIEnv* env, const std::string& details);
 /**Thrown when a task is canceled */
 void jxbt_throw_task_cancelled(JNIEnv* env, const std::string& details);
 
+class jstring_wrapper {
+  JNIEnv* env_  = nullptr;
+  jstring jstr_ = nullptr;
+
+public:
+  const char* value = nullptr;
+
+  jstring_wrapper(JNIEnv* env, jstring jstr) : env_(env), jstr_(jstr)
+  {
+    if (jstr != nullptr)
+      value = env_->GetStringUTFChars(jstr_, nullptr);
+  }
+  void reset(JNIEnv* env, jstring jstr)
+  {
+    if (jstr_ != nullptr)
+      env_->ReleaseStringUTFChars(jstr_, value);
+    env_  = env;
+    jstr_ = jstr;
+    if (jstr != nullptr)
+      value = env_->GetStringUTFChars(jstr_, nullptr);
+  }
+  ~jstring_wrapper()
+  {
+    if (jstr_ != nullptr)
+      env_->ReleaseStringUTFChars(jstr_, value);
+  }
+  operator const char*() const { return value; }
+  operator const std::string() const { return value; }
+};
+
 #endif