Commits

Yung-Yu Chen  committed 0cc1548

Add overloading.

  • Participants
  • Parent commits 7a6c254

Comments (0)

Files changed (7)

File document/source/inter/bpy/index.rst

 .. literalinclude:: sec1/zoo.cpp
   :language: c++
   :linenos:
-  :emphasize-lines: 10-12
 
 It simply return a string from C++ to Python.  Boost.Python will do all the
 conversion and interfacing for us:
 Share Instances between C++ and Python
 ======================================
 
+Method Overloading
+==================
+
 Irregular Arguments
 ===================
 
+http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/args.html
+
 Call Back to Python
 ===================
 

File document/source/inter/bpy/zoo/Makefile

+CC = g++
+PYLIBPATH = $(shell python-config --exec-prefix)/lib
+LIB = -L$(PYLIBPATH) $(shell python-config --libs) -lboost_python
+OPTS = $(shell python-config --include) -O2
+
+default: zoo.so
+	@python ./visit_zoo.py
+
+zoo.so: zoo.o
+	$(CC) $(LIB) -Wl,-rpath,$(PYLIBPATH) -shared $< -o $@
+
+zoo.o: zoo.cpp Makefile
+	$(CC) $(OPTS) -c $< -o $@
+
+clean:
+	rm -rf *.so *.o
+
+.PHONY: default clean

File document/source/inter/bpy/zoo/visit_zoo.py

+import zoo
+# In zoo.cpp we expose hello() function, and it now exists in the zoo module.
+assert 'hello' in dir(zoo)
+# zoo.hello is a callable.
+assert callable(zoo.hello)
+# Call the C++ hello() function from Python.
+print zoo.hello()
+
+# Create an animal.
+animal = zoo.Animal("dog")
+# The Python object.
+print animal
+# Use the exposed method to show the address of the C++ object.
+print "The C++ object is at 0x%016x" % animal.get_address()
+# Use the exposed property accessor.
+print "I see a \"%s\"" % animal.name
+animal.name = "cat"
+print "I see a \"%s\"" % animal.name
+
+print animal.act()
+print animal.act("eat")

File document/source/inter/bpy/zoo/zoo.cpp

+/*
+ * This inclusion should be put at the beginning.  It will include <Python.h>.
+ */
+#include <boost/python.hpp>
+#include <cstdint>
+#include <string>
+#include <vector>
+#include <boost/utility.hpp>
+#include <boost/shared_ptr.hpp>
+
+/*
+ * This is the C++ function we write and want to expose to Python.
+ */
+const std::string hello() {
+    return std::string("hello, zoo");
+}
+
+/*
+ * Create a C++ class to represent animals in the zoo.
+ */
+class Animal {
+public:
+    // Constructor.  Note no default constructor is defined.
+    Animal(std::string const & in_name): m_name(in_name) {}
+    // Copy constructor.
+    Animal(Animal const & in_other): m_name(in_other.m_name) {}
+    // Copy assignment.
+    Animal & operator=(Animal const & in_other) {
+        this->m_name = in_other.m_name;
+        return *this;
+    }
+
+    // Utility method to get the address of the instance.
+    uintptr_t get_address() const {
+        return reinterpret_cast<uintptr_t>(this);
+    }
+
+    // Getter of the name property.
+    std::string get_name() const {
+        return this->m_name;
+    }
+    // Setter of the name property.
+    void set_name(std::string const & in_name) {
+        this->m_name = in_name;
+    }
+
+    // A method that "acts".
+    std::string act() const { return this->m_name + " (does nothing)"; }
+    std::string act(
+            std::string const & verb,
+            std::string const & object) const {
+        return this->m_name + " " + verb + " " + object;
+    }
+
+private:
+    // The only property: the name of the animal.
+    std::string m_name;
+};
+
+/*
+ * This is a macro Boost.Python provides to signify a Python extension module.
+ */
+BOOST_PYTHON_MODULE(zoo) {
+    // An established convention for using boost.python.
+    using namespace boost::python;
+
+    // Expose the function hello().
+    def("hello", hello);
+
+    // Expose the class Animal.
+    class_<Animal>("Animal",
+        init<std::string const &>())
+        .def("get_address", &Animal::get_address)
+        .add_property("name", &Animal::get_name, &Animal::set_name)
+        // Assign 
+        .def("act",
+             static_cast<std::string (Animal::*)() const>
+                        (&Animal::act))
+        .def("act",
+             static_cast<std::string (Animal::*)
+                         (std::string const &, std::string const &) const>
+                        (&Animal::act),
+             (arg("verb")="", arg("object")=""))
+    ;
+}
+
+// vim: set ai et nu sw=4 ts=4 tw=79:

File examples/inter_bpy/staget/Makefile

-CC = g++
-PYLIBPATH = $(shell python-config --exec-prefix)/lib
-LIB = -L$(PYLIBPATH) $(shell python-config --libs) -lboost_python
-OPTS = $(shell python-config --include) -O2
-
-default: zoo.so
-	python ./visit_zoo.py
-
-zoo.so: zoo.o
-	$(CC) $(LIB) -Wl,-rpath,$(PYLIBPATH) -shared $< -o $@
-
-zoo.o: zoo.cpp Makefile
-	$(CC) $(OPTS) -c $< -o $@
-
-clean:
-	rm -rf *.so *.o
-
-.PHONY: default clean

File examples/inter_bpy/staget/visit_zoo.py

-import zoo
-
-def main():
-    print zoo.hello()
-    animal = zoo.Animal()
-    print "I see a \"%s\"" % animal.name
-    animal = zoo.Animal("dog")
-    print "I see a \"%s\"" % animal.name
-    animal.name = "cat"
-    print animal.name
-    print animal.act()
-
-if __name__ == '__main__':
-    main()
-
-# vim: set ai et nu sw=4 ts=4 tw=79:

File examples/inter_bpy/staget/zoo.cpp

-#include <boost/python.hpp>
-#include <string>
-#include <boost/shared_ptr.hpp>
-
-const std::string hello() {
-    return std::string("hello, world");
-}
-
-class Animal {
-public:
-    Animal(): m_name("") {}
-
-    Animal(std::string const & in_name): m_name(in_name) {}
-
-    std::string get_name() const {
-        return this->m_name;
-    }
-
-    void set_name(std::string const & in_name) {
-        this->m_name = in_name;
-    }
-
-    std::string act(
-            std::string const & verb,
-            std::string const & object) const {
-        return this->m_name + verb + object;
-    }
-
-private:
-    std::string m_name;
-};
-
-BOOST_PYTHON_MODULE(zoo) {
-    // an established convention for using boost.python.
-    using namespace boost::python;
-
-    def("hello", hello);
-
-    class_<Animal, boost::shared_ptr<Animal> >("Animal",
-        init<>())
-        .def(init<std::string const &>())
-        .add_property("name", &Animal::get_name, &Animal::set_name)
-        .def("act", &Animal::act,
-             (arg("verb")="", arg("object")=""))
-    ;
-
-}
-
-// vim: set ai et nu sw=4 ts=4 tw=79: