Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- wOOP is a powerful yet elegant Object Oriented Programming library. Class setup is nice as is instancing objects.
- In fact, wOOP was written because other libraries (in the opinion of the creator of wOOP) were "ugly" code.
- wOOP is based around userdata to support the __gc and __len metamethods. These functions do not work in tables in Lua 5.1.
- To create a class, you can do the following:
- ]]
- class "className" "extendsClass"
- {
- property=15;
- otherProperty=20;
- method=
- function(a,b)
- self.property=self.property+a
- self.otherProperty=self.otherProperty-b
- end;
- _readOnlyProperty=30;
- __privateProperty="String";
- __settings=
- {
- new=
- function(name)
- return{name=name}
- end;
- mt=
- {
- __gc=
- function(self)
- print(self.name.." is saying goodbye forever!");
- end
- }
- }
- }
- --[[
- You create a class by the name of "className", which inherits from the class "extendsClass".
- property and otherProperty are simply numbers stored inside the class.
- Notice that in "method" there is no self argument, but it uses one. This is because the "self" variable is "implied".
- You can use it in a function without explicitly declaring it as an argument.
- In wOOP, methods are called with a "." instead of a ":". (This is configurable).
- To create an object, simply call the class name as a function! The arguments will go into the new function.]]
- local object=className("Joe")
- print(object.name,object.property)
- object.method(5,5)
- --[[
- If you attempt to do any of the following, it will error. (This is configurable.)
- ]]
- object.asdf="String" --Attempt to set "asdf", a nil value.
- object._asdf=30 --Attempt to set a read only value.
- print(object.__asdf) --Attempt to index "__asdf", a private value.
- --[[
- Of course, the methods get direct access to the tables themselves.
- This means that inside method, the object can access itself.
- This is how private and read only values are useful in wOOP.
- To trigger the finalizer, simply do this:
- ]]
- remove(object)
- --[[
- This will remove and garbage collect the item. This is noticeable because "object" is now nil.
- ]]
- print(object) --nil
- --[[
- That covers the basic usage of wOOP. If you find any bugs/errors, please report them to LuaWeaver via Love2D forums.
- Additional usage:
- Configuration table.]]
- local config=
- {
- imply_self=true; --Remove the need for setting a self value in functions (removes the need for a colon)
- self_name="self"; --What the name is for the self value
- using_userdata=true; --Use userdata to allow __gc and __len metamethods. NOTE - WITHOUT THIS, STRICT ERRORS, PRIVATE, AND READ ONLY VALUES WILL NOT FUNCTION.
- strict_errors=true; --Error if 1) Value inside an object does not exist or 2) Attempt to set a value of an object where the field does not exist
- private_values=true; --Use private values
- private_value_index="__"; --How to start private values
- read_only_values=true; --Use read only values
- read_only_value_index="_"; --How to start read only values
- class_config_location="__settings"; --Where the config is located.
- class_config_setup= --How the config is setup.
- {
- new="new";
- mt="mt";
- };
- }
- --[[
- These should all be self explanatory. Simply modify the values to tweak it.
- ]]
Advertisement
Add Comment
Please, Sign In to add comment