Skip to content

Commit

Permalink
Adopt scoped_ptr in multiple places
Browse files Browse the repository at this point in the history
  • Loading branch information
Silviu Caragea committed Jun 12, 2017
1 parent 6122657 commit 3157e2b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
46 changes: 19 additions & 27 deletions c_src/data_conversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "erlcass.h"
#include "nif_utils.h"
#include "uuid_serialization.h"
#include "macros.h"

#include <memory>
#include <string.h>

ERL_NIF_TERM string_to_erlang_term(ErlNifEnv* env, const CassValue* value);
Expand Down Expand Up @@ -208,25 +210,21 @@ ERL_NIF_TERM collection_to_erlang_term(ErlNifEnv* env, const CassValue* value)

if(cass_value_type(value) == CASS_VALUE_TYPE_MAP)
{
CassIterator* iterator = cass_iterator_from_map(value);
scoped_ptr(iterator, CassIterator, cass_iterator_from_map(value), cass_iterator_free);

while (cass_iterator_next(iterator))
while (cass_iterator_next(iterator.get()))
{
const CassValue* c_key = cass_iterator_get_map_key(iterator);
const CassValue* c_value = cass_iterator_get_map_value(iterator);
const CassValue* c_key = cass_iterator_get_map_key(iterator.get());
const CassValue* c_value = cass_iterator_get_map_value(iterator.get());
itemsList[rowIndex++] = enif_make_tuple2(env, cass_value_to_nif_term(env, c_key), cass_value_to_nif_term(env, c_value));
}

cass_iterator_free(iterator);
}
else
{
CassIterator* iterator = cass_iterator_from_collection(value);

while (cass_iterator_next(iterator))
itemsList[rowIndex++] = cass_value_to_nif_term(env, cass_iterator_get_value(iterator));
scoped_ptr(iterator, CassIterator, cass_iterator_from_collection(value), cass_iterator_free);

cass_iterator_free(iterator);
while (cass_iterator_next(iterator.get()))
itemsList[rowIndex++] = cass_value_to_nif_term(env, cass_iterator_get_value(iterator.get()));
}

return enif_make_list_from_array(env, itemsList, static_cast<unsigned>(itemsCount));
Expand All @@ -242,12 +240,10 @@ ERL_NIF_TERM tuple_to_erlang_term(ErlNifEnv* env, const CassValue* value)
ERL_NIF_TERM itemsList[itemsCount];
size_t rowIndex = 0;

CassIterator* iterator = cass_iterator_from_tuple(value);

while (cass_iterator_next(iterator))
itemsList[rowIndex++] = cass_value_to_nif_term(env, cass_iterator_get_value(iterator));
scoped_ptr(iterator, CassIterator, cass_iterator_from_tuple(value), cass_iterator_free);

cass_iterator_free(iterator);
while (cass_iterator_next(iterator.get()))
itemsList[rowIndex++] = cass_value_to_nif_term(env, cass_iterator_get_value(iterator.get()));

return enif_make_tuple_from_array(env, itemsList, static_cast<unsigned>(itemsCount));
}
Expand All @@ -262,22 +258,20 @@ ERL_NIF_TERM udt_to_erlang_term(ErlNifEnv* env, const CassValue* value)
ERL_NIF_TERM items_list[items_count];
size_t rowIndex = 0;

CassIterator* iterator = cass_iterator_fields_from_user_type(value);
scoped_ptr(iterator, CassIterator, cass_iterator_fields_from_user_type(value), cass_iterator_free);

while (cass_iterator_next(iterator))
while (cass_iterator_next(iterator.get()))
{
const char* field_name_ptr;
size_t field_name_length;

if(cass_iterator_get_user_type_field_name(iterator, &field_name_ptr, &field_name_length) != CASS_OK)
if(cass_iterator_get_user_type_field_name(iterator.get(), &field_name_ptr, &field_name_length) != CASS_OK)
return ATOMS.atomNull;

ERL_NIF_TERM field_value = cass_value_to_nif_term(env, cass_iterator_get_user_type_field_value(iterator));
ERL_NIF_TERM field_value = cass_value_to_nif_term(env, cass_iterator_get_user_type_field_value(iterator.get()));
items_list[rowIndex++] = enif_make_tuple2(env, make_binary(env, field_name_ptr, field_name_length), field_value);
}

cass_iterator_free(iterator);

return enif_make_list_from_array(env, items_list, items_count);
}

Expand All @@ -295,21 +289,19 @@ ERL_NIF_TERM cass_result_to_erlang_term(ErlNifEnv* env, const CassResult* result
ERL_NIF_TERM nifArrayColumns[columnsCount];
ERL_NIF_TERM nifArrayRows[rowsCount];

CassIterator* iterator = cass_iterator_from_result(result);
scoped_ptr(iterator, CassIterator, cass_iterator_from_result(result), cass_iterator_free);

size_t rowIndex = 0;

while (cass_iterator_next(iterator))
while (cass_iterator_next(iterator.get()))
{
const CassRow* row = cass_iterator_get_row(iterator);
const CassRow* row = cass_iterator_get_row(iterator.get());

for(size_t i = 0; i < columnsCount; i++)
nifArrayColumns[i] = cass_value_to_nif_term(env, cass_row_get_column(row, i));

nifArrayRows[rowIndex++] = enif_make_tuple_from_array(env, nifArrayColumns, static_cast<unsigned>(columnsCount));
}

cass_iterator_free(iterator);

return enif_make_list_from_array(env, nifArrayRows, static_cast<unsigned>(rowsCount));
}
2 changes: 1 addition & 1 deletion c_src/nif_cass_cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ ERL_NIF_TERM internal_cass_cluster_set_ssl(ErlNifEnv* env, ERL_NIF_TERM term_opt

ERL_NIF_TERM head;

std::unique_ptr<CassSsl, decltype(&cass_ssl_free)> ssl(cass_ssl_new(), &cass_ssl_free);
scoped_ptr(ssl, CassSsl, cass_ssl_new(), cass_ssl_free);

while(enif_get_list_cell(env, term_value, &head, &term_value))
{
Expand Down
2 changes: 1 addition & 1 deletion c_src/nif_cass_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ ERL_NIF_TERM nif_cass_session_execute_batch(ErlNifEnv* env, int argc, const ERL_
if(enif_get_local_pid(env, argv[4], &pid) == 0)
return make_badarg(env);
std::unique_ptr<CassBatch, decltype(&cass_batch_free)> batch(cass_batch_new(static_cast<CassBatchType>(batch_type)), &cass_batch_free);
scoped_ptr(batch, CassBatch, cass_batch_new(static_cast<CassBatchType>(batch_type)), cass_batch_free);
if(!batch.get())
return make_error(env, erlcass::kFailedToCreateBatchObjectMsg);
Expand Down

0 comments on commit 3157e2b

Please sign in to comment.