@@ -306,8 +306,7 @@ Term make_resource_binary(ErlNifEnv *env, ResourcePtr<T> resource,
306306//
307307// This is useful when returning large binary from a NIF and the source
308308// buffer does not outlive the return.
309- inline fine::Term make_new_binary (ErlNifEnv *env, const char *data,
310- size_t size) {
309+ inline Term make_new_binary (ErlNifEnv *env, const char *data, size_t size) {
311310 ERL_NIF_TERM term;
312311 auto term_data = enif_make_new_binary (env, size, &term);
313312 if (term_data == nullptr ) {
@@ -440,9 +439,12 @@ template <> struct Decoder<std::string_view> {
440439 }
441440};
442441
443- template <> struct Decoder <std::string> {
444- static std::string decode (ErlNifEnv *env, const ERL_NIF_TERM &term) {
445- return std::string (fine::decode<std::string_view>(env, term));
442+ template <typename Alloc>
443+ struct Decoder <std::basic_string<char , std::char_traits<char >, Alloc>> {
444+ using string = std::basic_string<char , std::char_traits<char >, Alloc>;
445+
446+ static string decode (ErlNifEnv *env, const ERL_NIF_TERM &term) {
447+ return string (fine::decode<std::string_view>(env, term));
446448 }
447449};
448450
@@ -529,35 +531,38 @@ template <typename... Args> struct Decoder<std::tuple<Args...>> {
529531 }
530532};
531533
532- template <typename T> struct Decoder <std::vector<T>> {
533- static std::vector<T> decode (ErlNifEnv *env, const ERL_NIF_TERM &term) {
534+ template <typename T, typename Alloc> struct Decoder <std::vector<T, Alloc>> {
535+ static std::vector<T, Alloc> decode (ErlNifEnv *env,
536+ const ERL_NIF_TERM &term) {
534537 unsigned int length;
535538
536539 if (!enif_get_list_length (env, term, &length)) {
537540 throw std::invalid_argument (" decode failed, expected a list" );
538541 }
539542
540- std::vector<T> vector;
543+ std::vector<T, Alloc > vector;
541544 vector.reserve (length);
542545
543546 auto list = term;
544547
545548 ERL_NIF_TERM head, tail;
546549 while (enif_get_list_cell (env, list, &head, &tail)) {
547550 auto elem = fine::decode<T>(env, head);
548- vector.push_back ( elem);
551+ vector.emplace_back ( std::move ( elem) );
549552 list = tail;
550553 }
551554
552555 return vector;
553556 }
554557};
555558
556- template <typename K, typename V> struct Decoder <std::map<K, V>> {
557- static std::map<K, V> decode (ErlNifEnv *env, const ERL_NIF_TERM &term) {
558- auto map = std::map<K, V>();
559+ template <typename K, typename V, typename Compare, typename Alloc>
560+ struct Decoder <std::map<K, V, Compare, Alloc>> {
561+ static std::map<K, V, Compare, Alloc> decode (ErlNifEnv *env,
562+ const ERL_NIF_TERM &term) {
563+ std::map<K, V, Compare, Alloc> map;
559564
560- ERL_NIF_TERM key, value ;
565+ ERL_NIF_TERM key_term, value_term ;
561566 ErlNifMapIterator iter;
562567 if (!enif_map_iterator_create (env, term, &iter,
563568 ERL_NIF_MAP_ITERATOR_FIRST)) {
@@ -567,8 +572,12 @@ template <typename K, typename V> struct Decoder<std::map<K, V>> {
567572 // Define RAII cleanup for the iterator
568573 auto cleanup = IterCleanup{env, iter};
569574
570- while (enif_map_iterator_get_pair (env, &iter, &key, &value)) {
571- map[fine::decode<K>(env, key)] = fine::decode<V>(env, value);
575+ while (enif_map_iterator_get_pair (env, &iter, &key_term, &value_term)) {
576+ auto key = fine::decode<K>(env, key_term);
577+ auto value = fine::decode<V>(env, value_term);
578+
579+ map.insert_or_assign (std::move (key), std::move (value));
580+
572581 enif_map_iterator_next (env, &iter);
573582 }
574583
@@ -713,8 +722,11 @@ template <> struct Encoder<std::string_view> {
713722 }
714723};
715724
716- template <> struct Encoder <std::string> {
717- static ERL_NIF_TERM encode (ErlNifEnv *env, const std::string &string) {
725+ template <typename Alloc>
726+ struct Encoder <std::basic_string<char , std::char_traits<char >, Alloc>> {
727+ static ERL_NIF_TERM
728+ encode (ErlNifEnv *env,
729+ const std::basic_string<char , std::char_traits<char >, Alloc> &string) {
718730 return fine::encode<std::string_view>(env, string);
719731 }
720732};
@@ -783,8 +795,9 @@ template <typename... Args> struct Encoder<std::tuple<Args...>> {
783795 }
784796};
785797
786- template <typename T> struct Encoder <std::vector<T>> {
787- static ERL_NIF_TERM encode (ErlNifEnv *env, const std::vector<T> &vector) {
798+ template <typename T, typename Alloc> struct Encoder <std::vector<T, Alloc>> {
799+ static ERL_NIF_TERM encode (ErlNifEnv *env,
800+ const std::vector<T, Alloc> &vector) {
788801 auto terms = std::vector<ERL_NIF_TERM>();
789802 terms.reserve (vector.size ());
790803
@@ -797,8 +810,10 @@ template <typename T> struct Encoder<std::vector<T>> {
797810 }
798811};
799812
800- template <typename K, typename V> struct Encoder <std::map<K, V>> {
801- static ERL_NIF_TERM encode (ErlNifEnv *env, const std::map<K, V> &map) {
813+ template <typename K, typename V, typename Compare, typename Alloc>
814+ struct Encoder <std::map<K, V, Compare, Alloc>> {
815+ static ERL_NIF_TERM encode (ErlNifEnv *env,
816+ const std::map<K, V, Compare, Alloc> &map) {
802817 auto keys = std::vector<ERL_NIF_TERM>();
803818 auto values = std::vector<ERL_NIF_TERM>();
804819
0 commit comments