File: SetWrapper.h

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (42 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef PYTHON_SETWRAPPER_H
#define PYTHON_SETWRAPPER_H

#include <set>
#include <string>

#include <boost/python.hpp>


namespace FreeOrionPython {
    /* SetWrapper class encapsulates functions that expose the STL std::set<>
     * class to Python in a limited, read-only fashion.  The set can be iterated
     * through in Python, and printed. */
    template <typename SetT>
    class SetWrapper {
    public:
        using Set = SetT;
        using Key = typename Set::key_type;
        using Compare = typename Set::key_compare;
        using SetIterator = typename Set::const_iterator;

        static unsigned int size(const Set& self) {
            return static_cast<unsigned int>(self.size());  // ignore warning http://lists.boost.org/Archives/boost/2007/04/120377.php
        }
        static bool         empty(const Set& self) { return self.empty(); }
        static bool         contains(const Set& self, const Key& item) { return self.contains(item); }
        static SetIterator  begin(const Set& self) { return self.begin(); }
        static SetIterator  end(const Set& self) { return self.end(); }

        static void Wrap(const std::string& python_name) {
            namespace py = boost::python;

            py::class_<Set>(python_name.c_str(), py::no_init)
                .def("__len__",         &size)
                .def("empty",           &empty)
                .def("__contains__",    &contains)
                .def("__iter__",        py::iterator<Set>())
            ;
        }
    };
}
#endif