Luabind implements the function of getting the object size in lua

Source: Internet
Author: User

In lua, the # operator is used to obtain the object size. For table, the number of table elements is obtained, and for string, the string length is obtained.
The other way to obtain data is table. getn (obj). However, this method has been marked as abolished. Use the generic and concise # operator whenever possible.
 
Lua_objlen () is required to use the lua api to implement this function, but this function is not provided in luabind.
First, find the type function of the object type in the object. hpp of luabind source code, and add the following code
 
1: template <class ValueWrapper>
2: inline int obj_size (ValueWrapper const & value)
3 :{
4: lua_State * interpreter = value_wrapper_traits <ValueWrapper >:: interpreter (
5: value
6 :);
7:
8: value_wrapper_traits <ValueWrapper >:: unwrap (interpreter, value );
9: detail: stack_pop pop (interpreter, 1 );
10: return lua_objlen (interpreter,-1 );
11 :}
 
Re-compile your code, you can use luabind: obj_size (obj) to get the object size.

 

From the treasure Building
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.