Commits

Yung-Yu Chen committed 9ea4401

Add container.

  • Participants
  • Parent commits 0cc1548

Comments (0)

Files changed (3)

document/source/inter/bpy/index.rst

 Irregular Arguments
 ===================
 
-http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/args.html
+- http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/args.html
+- http://www.boost.org/doc/libs/1_55_0/libs/python/doc/v2/def.html
 
 Call Back to Python
 ===================

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

 animal.name = "cat"
 print "I see a \"%s\"" % animal.name
 
-print animal.act()
-print animal.act("eat")
+print animal.eat()
+print animal.eat("fish")
+print animal.eat(prey="birds")
+
+# See.  Wrapper placed last is match first.
+print animal.chase()
+print animal.chase("dogs")
+print animal.chase(target="rats")
+print animal.hunt()
+print animal.hunt("dogs")
+print animal.hunt(target="rats")

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

         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;
+    // Eating methods.
+    std::string eat() const { return this->m_name + " (eats nothing)"; }
+    std::string eat(std::string const & prey) const {
+        return this->m_name + " eats " + prey;
+    }
+
+    // Chasing methods.
+    std::string chase() const { return this->m_name + " (chases nothing)"; }
+    std::string chase(std::string const & target) const {
+        return this->m_name + " chases " + target;
+    }
+
+    // Hunting methods.
+    std::string hunt() const { return this->m_name + " (hunts nothing)"; }
+    std::string hunt(std::string const & target) const {
+        return this->m_name + " hunts " + target;
     }
 
 private:
     std::string m_name;
 };
 
+class Container {
+public:
+    typedef Animal value_content_type;
+    typedef boost::shared_ptr<value_content_type> value_type;
+
+    Container() {}
+    Container(Container const & in_other) : m_content(in_other.m_content) {}
+    Container & operator=(Container const & in_other) {
+        this->m_content.clear();
+        this->m_content.assign(in_other.m_content.begin(),
+                               in_other.m_content.end());
+        return *this;
+    }
+
+    void push_back(value_type const & in_obj) {
+        this->m_content.push_back(in_obj);
+    }
+
+private:
+    std::vector<value_type> m_content;
+};
+
 /*
  * This is a macro Boost.Python provides to signify a Python extension module.
  */
         init<std::string const &>())
         .def("get_address", &Animal::get_address)
         .add_property("name", &Animal::get_name, &Animal::set_name)
-        // Assign 
-        .def("act",
+        // Expose the eating methods.
+        .def("eat",
              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")=""))
+                        (&Animal::eat))
+        .def("eat",
+             static_cast<std::string (Animal::*)(std::string const &) const>
+                        (&Animal::eat),
+             (arg("prey")))
+        // Expose the chasing methods.
+        .def("chase",
+             static_cast<std::string (Animal::*)() const>
+                        (&Animal::chase))
+        .def("chase",
+             static_cast<std::string (Animal::*)(std::string const &) const>
+                        (&Animal::chase),
+             (arg("target")=""))
+        // Expose the hunting methods.
+        .def("hunt",
+             static_cast<std::string (Animal::*)(std::string const &) const>
+                        (&Animal::hunt),
+             (arg("target")=""))
+        .def("hunt",
+             static_cast<std::string (Animal::*)() const>
+                        (&Animal::hunt))
     ;
 }