diff options
author | Petre Eftime <[email protected]> | 2016-05-06 19:08:25 +0300 |
---|---|---|
committer | Petre Eftime <[email protected]> | 2016-05-06 19:10:51 +0300 |
commit | b0545a46bcfec1571c75499f0f2ed319f1f73c2e (patch) | |
tree | ef7a321e07863d15f83aecf50a3a47ccf1d8a98d | |
parent | 4f79c8d481119f8fa3e63f77286faddbeadb2b3c (diff) |
c++, java: Optional values return null when they are not available
Signed-off-by: Petre Eftime <[email protected]>
-rw-r--r-- | api/tinyb/BluetoothAdapter.hpp | 2 | ||||
-rw-r--r-- | api/tinyb/BluetoothDevice.hpp | 12 | ||||
-rw-r--r-- | java/jni/BluetoothAdapter.cxx | 6 | ||||
-rw-r--r-- | java/jni/BluetoothDevice.cxx | 12 | ||||
-rw-r--r-- | src/BluetoothAdapter.cpp | 7 | ||||
-rw-r--r-- | src/BluetoothDevice.cpp | 14 |
6 files changed, 34 insertions, 19 deletions
diff --git a/api/tinyb/BluetoothAdapter.hpp b/api/tinyb/BluetoothAdapter.hpp index 2d138705..e7bb5eb1 100644 --- a/api/tinyb/BluetoothAdapter.hpp +++ b/api/tinyb/BluetoothAdapter.hpp @@ -196,6 +196,6 @@ public: /** Returns the local ID of the adapter. * @return The local ID of the adapter. */ - std::string get_modalias (); + std::unique_ptr<std::string> get_modalias (); }; diff --git a/api/tinyb/BluetoothDevice.hpp b/api/tinyb/BluetoothDevice.hpp index 876782c7..79cda77f 100644 --- a/api/tinyb/BluetoothDevice.hpp +++ b/api/tinyb/BluetoothDevice.hpp @@ -169,7 +169,7 @@ public: /** Returns the proposed icon name of the device. * @return The proposed icon name, or NULL if not set. */ - std::string get_icon (); + std::unique_ptr<std::string> get_icon (); /** Returns the paired state the device. * @return The paired state of the device. @@ -199,8 +199,8 @@ public: */ bool get_legacy_pairing (); - /** Returns the Received Signal Strength Indicator of the device. - * @return The Received Signal Strength Indicator of the device. + /** Returns the Received Signal Strength Indicator of the device (0 means unknown). + * @return The Received Signal Strength Indicator of the device (0 means unknown). */ int16_t get_rssi (); @@ -214,10 +214,10 @@ public: */ std::vector<std::string> get_uuids (); - /** Returns the local ID of the adapter. - * @return The local ID of the adapter. + /** Returns the local ID of the adapter, or nullptr. + * @return The local ID of the adapter, or nullptr. */ - std::string get_modalias (); + std::unique_ptr<std::string> get_modalias (); /** Returns the adapter on which this device was discovered or * connected. diff --git a/java/jni/BluetoothAdapter.cxx b/java/jni/BluetoothAdapter.cxx index c8bf58bd..c7c38589 100644 --- a/java/jni/BluetoothAdapter.cxx +++ b/java/jni/BluetoothAdapter.cxx @@ -222,9 +222,11 @@ jobjectArray Java_tinyb_BluetoothAdapter_getUuids(JNIEnv *env, jobject obj) jstring Java_tinyb_BluetoothAdapter_getModalias(JNIEnv *env, jobject obj) { BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); - std::string modalias = obj_adapter->get_modalias(); + std::unique_ptr<std::string> modalias = obj_adapter->get_modalias(); + if(modalias == nullptr) + return nullptr; - return env->NewStringUTF((const char *)modalias.c_str()); + return env->NewStringUTF((const char *)modalias->c_str()); } void Java_tinyb_BluetoothAdapter_delete(JNIEnv *env, jobject obj) diff --git a/java/jni/BluetoothDevice.cxx b/java/jni/BluetoothDevice.cxx index 21ffac0b..d0e70007 100644 --- a/java/jni/BluetoothDevice.cxx +++ b/java/jni/BluetoothDevice.cxx @@ -151,9 +151,11 @@ jshort Java_tinyb_BluetoothDevice_getAppearance(JNIEnv *env, jobject obj) jstring Java_tinyb_BluetoothDevice_getIcon(JNIEnv *env, jobject obj) { BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::string icon = obj_device->get_icon(); + std::unique_ptr<std::string> icon = obj_device->get_icon(); + if (icon == nullptr) + return nullptr; - return env->NewStringUTF((const char *)icon.c_str()); + return env->NewStringUTF((const char *)icon->c_str()); } jboolean Java_tinyb_BluetoothDevice_getPaired(JNIEnv *env, jobject obj) @@ -236,9 +238,11 @@ jobjectArray Java_tinyb_BluetoothDevice_getUuids(JNIEnv *env, jobject obj) jstring Java_tinyb_BluetoothDevice_getModalias(JNIEnv *env, jobject obj) { BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); - std::string modalias = obj_device->get_modalias(); + std::unique_ptr<std::string> modalias = obj_device->get_modalias(); + if (modalias == nullptr) + return nullptr; - return env->NewStringUTF((const char *)modalias.c_str()); + return env->NewStringUTF((const char *)modalias->c_str()); } jobject Java_tinyb_BluetoothDevice_getAdapter(JNIEnv *env, jobject obj) diff --git a/src/BluetoothAdapter.cpp b/src/BluetoothAdapter.cpp index 821e3c37..86f1084e 100644 --- a/src/BluetoothAdapter.cpp +++ b/src/BluetoothAdapter.cpp @@ -246,7 +246,10 @@ std::vector<std::string> BluetoothAdapter::get_uuids () return uuids; } -std::string BluetoothAdapter::get_modalias () +std::unique_ptr<std::string> BluetoothAdapter::get_modalias () { - return std::string(adapter1_get_modalias (object)); + const gchar *modalias= adapter1_get_modalias (object); + if (modalias == nullptr) + return std::unique_ptr<std::string>(); + return std::unique_ptr<std::string>(new std::string(modalias)); } diff --git a/src/BluetoothDevice.cpp b/src/BluetoothDevice.cpp index cf3a59d4..3d37f862 100644 --- a/src/BluetoothDevice.cpp +++ b/src/BluetoothDevice.cpp @@ -232,9 +232,12 @@ uint16_t BluetoothDevice::get_appearance () return device1_get_appearance (object); } -std::string BluetoothDevice::get_icon () +std::unique_ptr<std::string> BluetoothDevice::get_icon () { - return std::string(device1_get_icon (object)); + const gchar *icon = device1_get_icon (object); + if (icon == nullptr) + return std::unique_ptr<std::string>(); + return std::unique_ptr<std::string>(new std::string(icon)); } bool BluetoothDevice::get_paired () @@ -287,9 +290,12 @@ std::vector<std::string> BluetoothDevice::get_uuids () return uuids; } -std::string BluetoothDevice::get_modalias () +std::unique_ptr<std::string> BluetoothDevice::get_modalias () { - return std::string(device1_get_modalias (object)); + const gchar *modalias= device1_get_modalias (object); + if (modalias == nullptr) + return std::unique_ptr<std::string>(); + return std::unique_ptr<std::string>(new std::string(modalias)); } BluetoothAdapter BluetoothDevice::get_adapter () |