From: Daniel Elstner Date: Sun, 11 Oct 2015 01:38:19 +0000 (+0200) Subject: C++: Consistently use nullptr instead of NULL X-Git-Tag: libsigrok-0.4.0~181 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=58e21229dd553446fd389f75c6f595da9567f886 C++: Consistently use nullptr instead of NULL --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index a00f2967..63de4b0a 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -39,12 +39,9 @@ static void check(int result) } /** Helper function to obtain valid strings from possibly null input. */ -static const char *valid_string(const char *input) +static inline const char *valid_string(const char *input) { - if (input != NULL) - return input; - else - return ""; + return (input) ? input : ""; } /** Helper function to convert between map and GHashTable */ @@ -123,7 +120,7 @@ shared_ptr Context::create() Context::Context() : UserOwned(_structure), - _session(NULL) + _session(nullptr) { check(sr_init(&_structure)); @@ -213,20 +210,14 @@ void Context::set_log_level(const LogLevel *level) static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args) { - va_list args_copy; - va_copy(args_copy, args); - int length = vsnprintf(NULL, 0, format, args_copy); - va_end(args_copy); - char *buf = (char *) g_malloc(length + 1); - vsprintf(buf, format, args); - string message(buf, length); - g_free(buf); + const unique_ptr + message {g_strdup_vprintf(format, args), &g_free}; LogCallbackFunction callback = *((LogCallbackFunction *) cb_data); try { - callback(LogLevel::get(loglevel), message); + callback(LogLevel::get(loglevel), message.get()); } catch (Error e) { @@ -377,7 +368,7 @@ shared_ptr Context::open_stream(string header) map Context::serials(shared_ptr driver) { - GSList *serial_list = sr_serial_list(driver ? driver->_structure : NULL); + GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr); map serials; for (GSList *serial = serial_list; serial; serial = serial->next) { @@ -391,7 +382,7 @@ map Context::serials(shared_ptr driver) Driver::Driver(struct sr_dev_driver *structure) : ParentOwned(structure), - Configurable(structure, NULL, NULL), + Configurable(structure, nullptr, nullptr), _initialized(false) { } @@ -421,7 +412,7 @@ vector> Driver::scan( } /* Translate scan options to GSList of struct sr_config pointers. */ - GSList *option_list = NULL; + GSList *option_list = nullptr; for (auto entry : options) { auto key = entry.first; @@ -555,7 +546,7 @@ bool Configurable::config_check(const ConfigKey *key, } Device::Device(struct sr_dev_inst *structure) : - Configurable(sr_dev_inst_driver_get(structure), structure, NULL), + Configurable(sr_dev_inst_driver_get(structure), structure, nullptr), _structure(structure) { for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) @@ -1035,7 +1026,7 @@ void Session::set_trigger(shared_ptr trigger) { if (!trigger) // Set NULL trigger, i.e. remove any trigger from the session. - check(sr_session_trigger_set(_structure, NULL)); + check(sr_session_trigger_set(_structure, nullptr)); else check(sr_session_trigger_set(_structure, trigger->_structure)); _trigger = move(trigger); @@ -1463,7 +1454,7 @@ bool OutputFormat::test_flag(const OutputFlag *flag) Output::Output(shared_ptr format, shared_ptr device, const map &options) : UserOwned(sr_output_new(format->_structure, - map_to_hash_variant(options), device->_structure, NULL)), + map_to_hash_variant(options), device->_structure, nullptr)), _format(move(format)), _device(move(device)), _options(options)