From 36a686d51ca9e61779ae60d651e80c7713dd5289 Mon Sep 17 00:00:00 2001 From: cloudygoose Date: Sun, 31 May 2015 00:16:01 +0800 Subject: modified doc/nerv.md --- doc/nerv.md | 10 ++++++++++ examples/cumatrix_example.lua | 1 + 2 files changed, 11 insertions(+) diff --git a/doc/nerv.md b/doc/nerv.md index 85a86ca..91bc8c9 100644 --- a/doc/nerv.md +++ b/doc/nerv.md @@ -1,5 +1,15 @@ #The Nerv utility functions# Part of the [Nerv](../README.md) toolkit. ##Methods## +* __string = nerv.typename(obj a)__ +A registered function, the original function is `luaT_lua_typename`. In some cases if you call `type(a)` for object of some class in __Nerv__(like __Nerv.CuMatrix__) it will only return "userdata"(because it is created in C), in this case you can use this method to get its type. + +--- + +* __metatable = nerv.getmetatable(string tname)__ +A registered function, the original function is `luaT_lua_getmetatable`. `tname` should be a class name that has been registered in __luaT__. + +* __metatable = nerv.newmetatable(string tname, string parenttname, function constructor, function destructor, function factory)__ +A registered function, the original function is `luaT_newmetatable`, it returns the metatable of the created class by the name `tname`. * __string = nerv.setmetatable(table self, string tname)__ A registered function, the original function is `luaT_lua_setmetatable`. It assigns the metatable registered in __luaT__ by the name *tname* to the table *self*. And return *tname* to user. diff --git a/examples/cumatrix_example.lua b/examples/cumatrix_example.lua index 544fc7f..ff9dd78 100644 --- a/examples/cumatrix_example.lua +++ b/examples/cumatrix_example.lua @@ -2,6 +2,7 @@ m = 4 n = 4 fm = nerv.CuMatrixFloat(m, n) dm = nerv.CuMatrixDouble(m, n) +print(type(dm), nerv.typename(dm)) for i = 0, m - 1 do for j = 0, n - 1 do -- local t = math.random(10) -- cgit v1.2.3 From c4a70df7297f43e3251fa5c0149e813451e52391 Mon Sep 17 00:00:00 2001 From: cloudygoose Date: Sun, 31 May 2015 00:16:29 +0800 Subject: ... --- examples/cumatrix_example.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/cumatrix_example.lua b/examples/cumatrix_example.lua index ff9dd78..544fc7f 100644 --- a/examples/cumatrix_example.lua +++ b/examples/cumatrix_example.lua @@ -2,7 +2,6 @@ m = 4 n = 4 fm = nerv.CuMatrixFloat(m, n) dm = nerv.CuMatrixDouble(m, n) -print(type(dm), nerv.typename(dm)) for i = 0, m - 1 do for j = 0, n - 1 do -- local t = math.random(10) -- cgit v1.2.3 From 29535cd402af7eaf8be7b48620826fc82591a010 Mon Sep 17 00:00:00 2001 From: cloudygoose Date: Sun, 31 May 2015 11:35:32 +0800 Subject: doc change --- README.md | 4 +++- doc/nerv_class.md | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 doc/nerv_class.md diff --git a/README.md b/README.md index 54d0218..27e0d3e 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ The pull&merge request can be found on your dashboard in github. See this [sync- ##Nerv Packages## * __luaT__ Nerv uses [luaT]\(a [Torch] library\) to define lua class in C. +* __[The Nerv OOP](doc/nerv_class.md)__ +Enables object-oriented programming in Nerv. * __[The Nerv utility functions](doc/nerv.md)__ Inlcudes some utility functions from luaT to implement __Nerv.Class__. * __The Nerv Matrix Package__ @@ -31,4 +33,4 @@ The Matrix package is a basic package in __Nerv__ that is used to store and mani [luaT]:https://github.com/torch/torch7/tree/master/lib/luaT [Torch]:https://github.com/torch -[sync-help]:https://help.github.com/articles/syncing-a-fork/ \ No newline at end of file +[sync-help]:https://help.github.com/articles/syncing-a-fork/ diff --git a/doc/nerv_class.md b/doc/nerv_class.md new file mode 100644 index 0000000..3c3d09e --- /dev/null +++ b/doc/nerv_class.md @@ -0,0 +1,3 @@ +#The Nerv OOP# +Part of the [Nerv](../README.md) toolkit. +##Methods## -- cgit v1.2.3 From d195028baca29cb5ca1f90df413b3d0a8df79857 Mon Sep 17 00:00:00 2001 From: cloudygoose Date: Sun, 31 May 2015 11:36:32 +0800 Subject: ... --- README.md | 2 +- doc/nerv.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 27e0d3e..f72b271 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ The pull&merge request can be found on your dashboard in github. See this [sync- ##Nerv Packages## * __luaT__ Nerv uses [luaT]\(a [Torch] library\) to define lua class in C. -* __[The Nerv OOP](doc/nerv_class.md)__ +* __[The Nerv OOP](doc/nerv_class.md)__ Enables object-oriented programming in Nerv. * __[The Nerv utility functions](doc/nerv.md)__ Inlcudes some utility functions from luaT to implement __Nerv.Class__. diff --git a/doc/nerv.md b/doc/nerv.md index 91bc8c9..22b4072 100644 --- a/doc/nerv.md +++ b/doc/nerv.md @@ -1,15 +1,15 @@ #The Nerv utility functions# Part of the [Nerv](../README.md) toolkit. ##Methods## -* __string = nerv.typename(obj a)__ +* __string = nerv.typename(obj a)__ A registered function, the original function is `luaT_lua_typename`. In some cases if you call `type(a)` for object of some class in __Nerv__(like __Nerv.CuMatrix__) it will only return "userdata"(because it is created in C), in this case you can use this method to get its type. --- -* __metatable = nerv.getmetatable(string tname)__ +* __metatable = nerv.getmetatable(string tname)__ A registered function, the original function is `luaT_lua_getmetatable`. `tname` should be a class name that has been registered in __luaT__. -* __metatable = nerv.newmetatable(string tname, string parenttname, function constructor, function destructor, function factory)__ +* __metatable = nerv.newmetatable(string tname, string parenttname, function constructor, function destructor, function factory)__ A registered function, the original function is `luaT_newmetatable`, it returns the metatable of the created class by the name `tname`. * __string = nerv.setmetatable(table self, string tname)__ A registered function, the original function is `luaT_lua_setmetatable`. It assigns the metatable registered in __luaT__ by the name *tname* to the table *self*. And return *tname* to user. -- cgit v1.2.3 From 00c724864321be2b77891154d73fef18871182e1 Mon Sep 17 00:00:00 2001 From: cloudygoose Date: Sun, 31 May 2015 15:40:37 +0800 Subject: doc change --- doc/nerv_class.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/doc/nerv_class.md b/doc/nerv_class.md index 3c3d09e..99f63e7 100644 --- a/doc/nerv_class.md +++ b/doc/nerv_class.md @@ -1,3 +1,36 @@ #The Nerv OOP# Part of the [Nerv](../README.md) toolkit. ##Methods## +* __metatable mt, metatable mpt = nerv.class(string tname, string parenttname)__ +This method is used to create a class by the name `tname`, which inherits `parenttname` in __Nerv__, then you create a new instance of this class by calling `obj=tname(...)`. The `tname.__init(...)` method(if defined) will be called in the constructing. The metatable of the class and its parent class will be returned. + +##Examples## +* This example implements a simple `nerv.Counter` class which is inherited by `nerv.BetterCounter`. + +``` +do + nerv.class("nerv.Counter") + function nerv.Counter:__init(c) + if (c) then + self.c = c + else + self.c = 0 + end + end +end +do + local mt, mpt = nerv.class("nerv.BetterCounter", "nerv.Counter") + function nerv.BetterCounter:__init(c, bc) + mpt.__init(self, c) + if (bc) then + self.bc = bc + else + self.bc = 0 + end + end +end +c1 = nerv.Counter(1) +print(c1.c) +bc1 = nerv.BetterCounter(1, 1) +print(bc1.c, bc1.bc) +``` \ No newline at end of file -- cgit v1.2.3