|
| 1 | +// Copyright Fady Essam 2019. Distributed under the Boost |
| 2 | +// Software License, Version 1.0. (See accompanying |
| 3 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 4 | +#include <boost/python/module.hpp> |
| 5 | +#define BOOST_ENABLE_ASSERT_HANDLER |
| 6 | +#include <boost/assert.hpp> |
| 7 | + |
| 8 | +#include <boost/python/def.hpp> |
| 9 | +#include <boost/python/class.hpp> |
| 10 | +#include <boost/python/set.hpp> |
| 11 | +#include <exception> |
| 12 | +#include <string> |
| 13 | +#include <iostream> |
| 14 | + |
| 15 | +using namespace boost::python; |
| 16 | + |
| 17 | +object new_set() |
| 18 | +{ |
| 19 | + return set(); |
| 20 | +} |
| 21 | + |
| 22 | +object data_set() |
| 23 | +{ |
| 24 | + set tmp1; |
| 25 | + tmp1.add("value1"); |
| 26 | + |
| 27 | + tmp1.add(2); |
| 28 | + return tmp1; |
| 29 | +} |
| 30 | + |
| 31 | +object set_from_sequence(object sequence) |
| 32 | +{ |
| 33 | + return set(sequence); |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +void work_with_set(set data1) |
| 38 | +{ |
| 39 | + if (!data1.contains("k1")) { |
| 40 | + std::cout << "data1 doesn't have k1" << std::endl; |
| 41 | + } |
| 42 | + data1.add("k1"); |
| 43 | + |
| 44 | + if (data1.contains("k1")) { |
| 45 | + std::cout << "data1 now has k1" << std::endl; |
| 46 | + } |
| 47 | + |
| 48 | + data1.discard("k1"); |
| 49 | + if (!data1.contains("k1")) { |
| 50 | + std::cout << "data1 doesn't have k1 again" << std::endl; |
| 51 | + } |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +void test_templates(object print) |
| 56 | +{ |
| 57 | + std::string key = "key"; |
| 58 | + |
| 59 | + set tmp; |
| 60 | + tmp.add("a test string"); |
| 61 | + print(tmp); |
| 62 | + tmp.add(13); |
| 63 | + print(tmp.contains(1.5)); |
| 64 | + print(tmp.contains(13)); |
| 65 | + print(tmp); |
| 66 | + |
| 67 | + BOOST_ASSERT(tmp.__len__() == 2); |
| 68 | +} |
| 69 | + |
| 70 | +BOOST_PYTHON_MODULE(set_ext) |
| 71 | +{ |
| 72 | + def("new_set", new_set); |
| 73 | + def("data_set", data_set); |
| 74 | + def("set_from_sequence", set_from_sequence); |
| 75 | + def("work_with_set", work_with_set); |
| 76 | + def("test_templates", test_templates); |
| 77 | +} |
| 78 | + |
| 79 | +#include "module_tail.cpp" |
0 commit comments