Add simple std::set<> typemaps too

This is similar to the previous commit for std::map<>.
This commit is contained in:
Vadim Zeitlin 2021-10-07 14:19:39 +02:00
parent 9efb508eda
commit 0706ed4d35
3 changed files with 82 additions and 1 deletions

View File

@ -0,0 +1,33 @@
#include "li_std_set/li_std_set_wrap.h"
#include <assert.h>
int main() {
{
IntSet* is = IntSet_new();
IntSet_add(is, 1);
IntSet_add(is, 4);
IntSet_add(is, 9);
assert( IntSet_size(is) == 3 );
assert( IntSet_has(is, 4) );
assert( !IntSet_has(is, 16) );
IntSet_delete(is);
}
{
StringSet* ss = StringSet_new();
StringSet_add(ss, "foo");
StringSet_add(ss, "bar");
assert( StringSet_size(ss) == 2 );
assert( StringSet_has(ss, "bar") );
assert( !StringSet_has(ss, "baz") );
StringSet_delete(ss);
}
return 0;
}

View File

@ -22,7 +22,7 @@
%template(set_int) std::multiset<int>;
%template(v_int) std::vector<int>;
%template(set_string) std::set<std::string>;
#elif defined(SWIGJAVA) || defined(SWIGCSHARP)
#elif defined(SWIGC) || defined(SWIGJAVA) || defined(SWIGCSHARP)
// This operator is only defined because it's needed to store objects of
// type Foo in std::set in C++, we don't need to wrap it.
%ignore operator<;

48
Lib/c/std_set.i Normal file
View File

@ -0,0 +1,48 @@
/* -----------------------------------------------------------------------------
* std_set.i
*
* SWIG typesets for std::set
* ----------------------------------------------------------------------------- */
%include <std_common.i>
// ------------------------------------------------------------------------
// std::set
// ------------------------------------------------------------------------
%{
#include <set>
#include <stdexcept>
%}
namespace std {
template<class T> class set {
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T key_type;
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
set();
set(const set& other);
size_t size() const;
bool empty() const;
void clear();
%extend {
bool add(const T& item) {
return self->insert(item).second;
}
bool del(const T& item) {
return self->erase(item) != 0;
}
bool has(const T& item) {
return self->count(item) != 0;
}
}
};
}