Fix std::map<> typemap

Use simple fixed typemap instead of trying to use the much more complex
one from the UTL which doesn't work for C.

Add a simple test case for std::map<>.
This commit is contained in:
Vadim Zeitlin 2021-10-06 20:20:55 +02:00
commit 9efb508eda
3 changed files with 87 additions and 2 deletions

View file

@ -66,7 +66,6 @@ FAILING_CPP_TESTS := \
li_boost_shared_ptr_director \
li_boost_shared_ptr_template \
li_std_deque \
li_std_map \
li_std_pair_using \
li_std_wstring \
li_windows \

View file

@ -0,0 +1,26 @@
#include "li_std_map/li_std_map_wrap.h"
#include <assert.h>
int main() {
A* a1 = A_new_i(3);
A* a2 = A_new_i(7);
mapA* mA = mapA_new();
mapA_set(mA, 1, a1);
mapA_set(mA, 2, a2);
assert( mapA_size(mA) == 2 );
{
A* a = mapA_get(mA, 1);
assert( A_val_get(a) == 3 );
}
assert( !mapA_has_key(mA, 3) );
mapA_delete(mA);
A_delete(a2);
A_delete(a1);
return 0;
}

View file

@ -1 +1,61 @@
%include <std/std_map.i>
/* -----------------------------------------------------------------------------
* std_map.i
*
* SWIG typemaps for std::map
* ----------------------------------------------------------------------------- */
%include <std_common.i>
// ------------------------------------------------------------------------
// std::map
// ------------------------------------------------------------------------
%{
#include <map>
#include <stdexcept>
%}
namespace std {
template<class K, class T, class C = std::less<K> > class map {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef K key_type;
typedef T mapped_type;
typedef std::pair< const K, T > value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
map();
map(const map& other);
size_t size() const;
bool empty() const;
void clear();
%extend {
const T& get(const K& key) throw (std::out_of_range) {
std::map< K, T, C >::iterator i = self->find(key);
if (i != self->end())
return i->second;
else
throw std::out_of_range("key not found");
}
void set(const K& key, const T& x) {
(*self)[key] = x;
}
void del(const K& key) throw (std::out_of_range) {
std::map< K, T, C >::iterator i = self->find(key);
if (i != self->end())
self->erase(i);
else
throw std::out_of_range("key not found");
}
bool has_key(const K& key) {
std::map< K, T, C >::iterator i = self->find(key);
return i != self->end();
}
}
};
}