Commits

Yung-Yu Chen committed 0b39a68

Reorganize inter_bpy into inter/bpy/index

Comments (0)

Files changed (25)

document/source/index.rst

    :numbered:
 
    basic
-   inter
+   inter/index
    mgmt
    num
    hpc

document/source/inter.rst

-==========================
-Multi-Language Programming
-==========================
-
-.. toctree::
-
-   inter_build
-   inter_ffi
-   inter_cython
-   inter_bpy
-   inter_ext
-
-.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter/bpy/index.rst

+==========================
+Wrap C++ with Boost.Python
+==========================
+
+`Boost <http://www.boost.org/>`_ is a high-quality, widely-used, open-source
+C++ library.  `Boost.Python
+<http://www.boost.org/doc/libs/1_55_0/libs/python/doc/index.html>`_ is one
+consisting project that provides a comprehensive wrapping capabilities between
+C++ and Python.  By using Boost.Python, one can easily create a `Python
+extension module <https://docs.python.org/2/extending/index.html>`__ with C++.
+
+Create a Python Extension
+=========================
+
+This is our first Python extension module by Boost.Python; call it ``zoo.cpp``:
+
+.. literalinclude:: stage0/zoo.cpp
+  :language: c++
+  :linenos:
+
+It simply return a string from C++ to Python.  Boost.Python will do all the
+conversion and interfacing for us:
+
+.. literalinclude:: stage0/visit_zoo.py
+  :language: python
+  :linenos:
+
+Running the above script (call it ``visit_zoo.py``) will get::
+
+  hello, zoo
+
+The following makefile will help us build the module (and run it):
+
+.. literalinclude:: stage0/Makefile
+  :language: makefile
+  :linenos:
+
+Wrap a Class
+============
+
+Expose a class ``Animal`` from C++ to Python:
+
+.. literalinclude:: stage1/zoo.cpp
+  :language: c++
+  :linenos:
+
+The script changes to:
+
+.. literalinclude:: stage1/visit_zoo.py
+  :language: python
+  :linenos:
+
+The output is::
+
+  hello, zoo
+  <zoo.Animal object at 0x102437890>
+  The C++ object is at 0x00007fb0c860ac20
+  I see a "dog"
+  I see a "cat"
+
+Share Instances between C++ and Python
+======================================
+
+Call Back to Python
+===================
+
+.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter/bpy/stage0/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

document/source/inter/bpy/stage0/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()

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

+/*
+ * This inclusion should be put at the beginning.  It will include <Python.h>.
+ */
+#include <boost/python.hpp>
+#include <string>
+
+/*
+ * This is the C++ function we write and want to expose to Python.
+ */
+const std::string hello() {
+    return std::string("hello, zoo");
+}
+
+/*
+ * 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);
+}
+
+// vim: set ai et nu sw=4 ts=4 tw=79:

document/source/inter/bpy/stage1/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

document/source/inter/bpy/stage1/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

document/source/inter/bpy/stage1/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;
+    }
+
+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)
+    ;
+}
+
+// vim: set ai et nu sw=4 ts=4 tw=79:

document/source/inter/build.rst

+============
+Build System
+============
+
+If you want to use Python with other programming languages, a build system is
+usually needed.  A build system is used to `automate the processes of
+compiling, linking, packaging, and deploying software
+<http://en.wikipedia.org/wiki/Build_automation>`__.  This chapter will focus on
+a tool called `SCons <http://www.scons.org/>`_, which is implement with pure
+Python.  Building scripts of SCons can be highly modularized and reused, and
+cross-platform as well.
+
+Using a build system involves writing building scripts.  Building scripts of
+SCons can have three parts:
+
+- Front-end script (``SConstruct``),
+- Rule script (``Sconscript``), and
+- Tools (``site_scons/site_tools/*``).
+
+Below is the ``SConstruct`` and the ``SConscript`` files of an example project.
+The ``SConstruct`` file is:
+
+.. literalinclude:: ../../examples/inter_build/SConstruct
+  :language: python
+
+The ``SConscript`` file is:
+
+.. literalinclude:: ../../examples/inter_build/SConscript
+  :language: python
+
+SCons tools provide a means to reuse the building code.  For example, we can
+use the `SCons tools provided by the Cython team
+<https://github.com/cython/cython/tree/master/Tools/site_scons/site_tools>`__
+to build your cython code, by copying the files ``cython.py`` and ``pyext.py``
+into the directory ``site_scons/site_tools`` inside your project.
+
+.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter/cython.rst

+==========================
+Generate Code Using Cython
+==========================
+
+.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter/ext.rst

+===================================
+Developing Python Extension Modules
+===================================
+
+.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter/ffi.rst

+==========================
+Foreign Function Interface
+==========================
+
+.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter/index.rst

+==========================
+Multi-Language Programming
+==========================
+
+.. toctree::
+
+   build
+   ffi
+   cython
+   bpy/index
+   ext
+
+.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter_bpy.rst

-==========================
-Wrap C++ with Boost.Python
-==========================
-
-`Boost <http://www.boost.org/>`_ is a high-quality, widely-used, open-source
-C++ library.  `Boost.Python
-<http://www.boost.org/doc/libs/1_55_0/libs/python/doc/index.html>`_ is one
-consisting project that provides a comprehensive wrapping capabilities between
-C++ and Python.  By using Boost.Python, one can easily create a `Python
-extension module <https://docs.python.org/2/extending/index.html>`__ with C++.
-
-Create a Python Extension
-=========================
-
-This is our first Python extension module by Boost.Python; call it ``zoo.cpp``:
-
-.. literalinclude:: ../../examples/inter_bpy/stage0/zoo.cpp
-  :language: c++
-  :linenos:
-
-It simply return a string from C++ to Python.  Boost.Python will do all the
-conversion and interfacing for us:
-
-.. literalinclude:: ../../examples/inter_bpy/stage0/visit_zoo.py
-  :language: python
-  :linenos:
-
-Running the above script (call it ``visit_zoo.py``) will get::
-
-  hello, zoo
-
-The following makefile will help us build the module (and run it):
-
-.. literalinclude:: ../../examples/inter_bpy/stage0/Makefile
-  :language: makefile
-  :linenos:
-
-Wrap a Class
-============
-
-Expose a class ``Animal`` from C++ to Python:
-
-.. literalinclude:: ../../examples/inter_bpy/stage1/zoo.cpp
-  :language: c++
-  :linenos:
-
-The script changes to:
-
-.. literalinclude:: ../../examples/inter_bpy/stage0/visit_zoo.py
-  :language: python
-  :linenos:
-
-Share Instances between C++ and Python
-======================================
-
-Call Back to Python
-===================
-
-.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter_build.rst

-============
-Build System
-============
-
-If you want to use Python with other programming languages, a build system is
-usually needed.  A build system is used to `automate the processes of
-compiling, linking, packaging, and deploying software
-<http://en.wikipedia.org/wiki/Build_automation>`__.  This chapter will focus on
-a tool called `SCons <http://www.scons.org/>`_, which is implement with pure
-Python.  Building scripts of SCons can be highly modularized and reused, and
-cross-platform as well.
-
-Using a build system involves writing building scripts.  Building scripts of
-SCons can have three parts:
-
-- Front-end script (``SConstruct``),
-- Rule script (``Sconscript``), and
-- Tools (``site_scons/site_tools/*``).
-
-Below is the ``SConstruct`` and the ``SConscript`` files of an example project.
-The ``SConstruct`` file is:
-
-.. literalinclude:: ../../examples/inter_build/SConstruct
-  :language: python
-
-The ``SConscript`` file is:
-
-.. literalinclude:: ../../examples/inter_build/SConscript
-  :language: python
-
-SCons tools provide a means to reuse the building code.  For example, we can
-use the `SCons tools provided by the Cython team
-<https://github.com/cython/cython/tree/master/Tools/site_scons/site_tools>`__
-to build your cython code, by copying the files ``cython.py`` and ``pyext.py``
-into the directory ``site_scons/site_tools`` inside your project.
-
-.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter_cython.rst

-==========================
-Generate Code Using Cython
-==========================
-
-.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter_ext.rst

-===================================
-Developing Python Extension Modules
-===================================
-
-.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

document/source/inter_ffi.rst

-==========================
-Foreign Function Interface
-==========================
-
-.. vim: set spell ft=rst ff=unix fenc=utf8 ai et sw=4 ts=4 tw=79

examples/inter_bpy/stage0/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

examples/inter_bpy/stage0/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()

examples/inter_bpy/stage0/zoo.cpp

-/*
- * This inclusion should be put at the beginning.  It will include <Python.h>.
- */
-#include <boost/python.hpp>
-#include <string>
-
-/*
- * This is the C++ function we write and want to expose to Python.
- */
-const std::string hello() {
-    return std::string("hello, zoo");
-}
-
-/*
- * 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);
-}
-
-// vim: set ai et nu sw=4 ts=4 tw=79:

examples/inter_bpy/stage1/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

examples/inter_bpy/stage1/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

examples/inter_bpy/stage1/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;
-    }
-
-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)
-    ;
-}
-
-// vim: set ai et nu sw=4 ts=4 tw=79: