diff --git a/pyste/NEWS b/pyste/NEWS index 3505706d..97ba5a86 100644 --- a/pyste/NEWS +++ b/pyste/NEWS @@ -1,3 +1,6 @@ +2 June 2003 +Added a new construct, add_method. See documentation. + 23 May 2003 Support for global variables added. Various bug fixes. @@ -7,8 +10,10 @@ Fixed bug where in a certain cases the GCCXMLParser would end up with multiple declarations of the same class 22 Apr 2003 -- Now shows a warning when the user tries to export a forward-declared class. Forward-declared classes are ignored by the AllFromHeader construct. -- Fixed a bug where classes, functions and enums where being exported, even if excluded from a AllFromHeader construct. +- Now shows a warning when the user tries to export a forward-declared class. + Forward-declared classes are ignored by the AllFromHeader construct. +- Fixed a bug where classes, functions and enums where being exported, even if + excluded from a AllFromHeader construct. 16 Apr 2003 Added a more generic (but ugly) code to declare the smart pointer converters. diff --git a/pyste/doc/adding_new_methods.html b/pyste/doc/adding_new_methods.html new file mode 100644 index 00000000..ccaca6f7 --- /dev/null +++ b/pyste/doc/adding_new_methods.html @@ -0,0 +1,78 @@ + + + +Adding New Methods + + + + + + + + + +
+ + Adding New Methods +
+
+ + + + + + +
+

+Suppose that you want to add a function to a class, turning it into a method:

+
+    struct World
+    {
+        void set(std::string msg) { this->msg = msg; }
+        std::string msg;
+    };
+
+    std::string greet(World& w)
+    {
+        return w.msg;
+    }
+
+

+Here, we want to make greet work as a method of the class World. We do +that using the add_method construct:

+
+    W = Class("World", "hello.h")
+    add_method(W, "greet")
+
+

+Notice also that then you can rename it, set its policy, just like a regular +method:

+
+    rename(W.greet, 'Greet')
+
+

+Now from Python:

+
+    >>> import hello
+    >>> w = hello.World()
+    >>> w.set('Ni')
+    >>> w.greet()
+    'Ni'
+    >>> print 'Oh no! The knights who say Ni!'
+    Oh no! The knights who say Ni!
+
+ + + + + + +
+
+
+ + diff --git a/pyste/doc/global_variables.html b/pyste/doc/global_variables.html index 84b3b9d3..59d8c477 100644 --- a/pyste/doc/global_variables.html +++ b/pyste/doc/global_variables.html @@ -4,6 +4,7 @@ Global Variables + @@ -20,7 +21,7 @@ - +

@@ -36,7 +37,7 @@ functions, and export those.

- +
diff --git a/pyste/doc/pyste.txt b/pyste/doc/pyste.txt index b37c6833..d5a8b7ad 100644 --- a/pyste/doc/pyste.txt +++ b/pyste/doc/pyste.txt @@ -477,3 +477,40 @@ Beware of non-const global variables: changes in Python won't reflect in C++! If you really must change them in Python, you will have to write some accessor functions, and export those. +[page:1 Adding New Methods] + +Suppose that you want to add a function to a class, turning it into a method: + + struct World + { + void set(std::string msg) { this->msg = msg; } + std::string msg; + }; + + std::string greet(World& w) + { + return w.msg; + } + +Here, we want to make [^greet] work as a method of the class [^World]. We do +that using the [^add_method] construct: + + W = Class("World", "hello.h") + add_method(W, "greet") + +Notice also that then you can rename it, set its policy, just like a regular +method: + + rename(W.greet, 'Greet') + +Now from Python: + + >>> import hello + >>> w = hello.World() + >>> w.set('Ni') + >>> w.greet() + 'Ni' + >>> print 'Oh no! The knights who say Ni!' + Oh no! The knights who say Ni! + + diff --git a/pyste/index.html b/pyste/index.html index b9c21920..04afe216 100644 --- a/pyste/index.html +++ b/pyste/index.html @@ -70,6 +70,11 @@ Global Variables + + + Adding New Methods + +