常量
const常量和enum枚举
const常量类型
常量使用关键字 const 定义,用于存储不会改变的数据。
存储在常量中的数据类型可以是bool, int,string,map等。
常量的定义格式:const [type] identifier = value
例如
const map EQUIP_NAME = {
"weapon":"武器",
"cloth" : "防具",
"accessories" : "饰品",
"shoes" : "鞋子",
"fabao" : "法宝",
"gubao" : "古宝"
};
const string str = "Hello World";
常量的值必须是能够在编译时就能够确定的;你可以在其赋值表达式中涉及计算过程,但是所有用于计算的值必须在编译期间就能获得。
正确的做法:const int c1 = 2+3 错误的做法:const int c2 = getNumber()
enum枚举类型
enum是枚举类型,继承自int类型,主要是将相同类别不同特征的一类东西放在一起,方便取值调用。
enum的type_name是map,其功能与map有点类似(key是特征名,val是int类型数值,默认从0递增),用法与C类似。
enum的基本语法格式如下:
enum enum_name
{
value1 = constant1,
value2 = constant2,
value3 = constant3,
...
}
enum的基本语法解释:
enum_name是enum的名字
1.必须满足变量名字格式
value为enum的特征名,
1.必须满足变量名字格式
2.一般情况下使用全大写,便于区分其它数据格式
constant为enum特征对应的值
1.constant只能为int类型
2.constant可以不写,如果第一个value不写constant,则它对应的值是0
3.如果某个value没有constant,则它对应的值为上一个value对应的值+1
举个enum例子:
enum State
{
IDLE = 10,
RUN,
WAIT = 35,
EAT,
DRINK,
};
bool falg = (State.IDLE == 10); // flag = true
此时 State对应的value和constant分别为:
"IDLE" : 10,
"RUN" : 11,
"WAIT" : 35,
"EAT" : 36,
"DRINK" : 37,
enum常用的外部函数
下面列出enum类型一些常用的外部函数以及用法(基于以上State)。
1. 特征名
函数原型:
mixed get_enum_name(string enum_name, int val)
使用方法:
get_enum_name("State", 11); // 得到 RUN
2. 特征信息
函数原型:
mixed get_enum_info(string enum_name, mixed key_or_val)
使用方法:
get_enum_info("State", 10); // 输出在表格下面
3. 所有数据
函数原型:
mixed get_enum_data(string enum_name, mixed enum_module = nil)
使用方法:
get_enum_data("State"); // 输出在表格下面
4. 是否enum
函数原型:
bool is_enum(mixed val)
使用方法:
bool flag = is_enum(State.IDLE); // flag = true
备注
第2项输出为:
{ /* sizeof() == 2 */
"name" : "IDLE",
"value" : State.IDLE(10) @ /simple.gs,
}
备注
第3项输出为:
{ /* sizeof() == 6 */
"IDLE" : State.IDLE(10) @ /simple.gs,
"RUN" : State.RUN(11) @ /simple.gs,
"WAIT" : State.WAIT(35) @ /simple.gs,
"EAT" : State.EAT(36) @ /simple.gs,
"DRINK" : State.DRINK(37) @ /simple.gs,
"__reverse_enum_map__" : { /* sizeof() == 5 */
10 : "IDLE",
11 : "RUN",
35 : "WAIT",
36 : "EAT",
37 : "DRINK",
},
}
gs语言内置的enum
shell > 'get_all_enums()
{ /* sizeof() == 3 */
"id" : 1,
"name" : "bool",
"module" : "/#e/bool.gs",
},
{ /* sizeof() == 3 */
"id" : 2,
"name" : "ErrorCode",
"module" : "/#e/ErrorCode.gs",
},
{ /* sizeof() == 3 */
"id" : 3,
"name" : "ValueType",
"module" : "/#e/ValueType.gs",
},
{ /* sizeof() == 3 */
"id" : 4,
"name" : "HandleState",
"module" : "/#e/HandleState.gs",
},
{ /* sizeof() == 3 */
"id" : 5,
"name" : "UnitChangedState",
"module" : "/#e/UnitChangedState.gs",
},
{ /* sizeof() == 3 */
"id" : 6,
"name" : "HandleEvtType",
"module" : "/#e/HandleEvtType.gs",
},
{ /* sizeof() == 3 */
"id" : 7,
"name" : "CallType",
"module" : "/#e/CallType.gs",
},
{ /* sizeof() == 3 */
"id" : 8,
"name" : "ObjectState",
"module" : "/#e/ObjectState.gs",
},
{ /* sizeof() == 3 */
"id" : 9,
"name" : "ProgramAttrib",
"module" : "/#e/ProgramAttrib.gs",
},
{ /* sizeof() == 3 */
"id" : 10,
"name" : "CoAttrib",
"module" : "/#e/CoAttrib.gs",
},
{ /* sizeof() == 3 */
"id" : 11,
"name" : "Priority",
"module" : "/#e/Priority.gs",
},
{ /* sizeof() == 3 */
"id" : 12,
"name" : "WaitRet",
"module" : "/#e/WaitRet.gs",
},
{ /* sizeof() == 3 */
"id" : 13,
"name" : "CoState",
"module" : "/#e/CoState.gs",
},
{ /* sizeof() == 3 */
"id" : 14,
"name" : "NumLimit",
"module" : "/#e/NumLimit.gs",
},
{ /* sizeof() == 3 */
"id" : 15,
"name" : "Iterator",
"module" : "/#e/Iterator.gs",
},
{ /* sizeof() == 3 */
"id" : 16,
"name" : "SortType",
"module" : "/#e/SortType.gs",
},
{ /* sizeof() == 3 */
"id" : 17,
"name" : "TrimType",
"module" : "/#e/TrimType.gs",
},
{ /* sizeof() == 3 */
"id" : 18,
"name" : "TimeType",
"module" : "/#e/TimeType.gs",
},
{ /* sizeof() == 3 */
"id" : 19,
"name" : "SaveValue",
"module" : "/#e/SaveValue.gs",
},
{ /* sizeof() == 3 */
"id" : 20,
"name" : "ReferenceType",
"module" : "/#e/ReferenceType.gs",
},
{ /* sizeof() == 3 */
"id" : 21,
"name" : "SeekOrigin",
"module" : "/#e/SeekOrigin.gs",
},
{ /* sizeof() == 3 */
"id" : 22,
"name" : "DeviceFd",
"module" : "/#e/DeviceFd.gs",
},
{ /* sizeof() == 3 */
"id" : 23,
"name" : "IOError",
"module" : "/#e/IOError.gs",
},
{ /* sizeof() == 3 */
"id" : 24,
"name" : "SockDomain",
"module" : "/#e/SockDomain.gs",
},
{ /* sizeof() == 3 */
"id" : 25,
"name" : "SockType",
"module" : "/#e/SockType.gs",
},
{ /* sizeof() == 3 */
"id" : 26,
"name" : "SockOptLevel",
"module" : "/#e/SockOptLevel.gs",
},
{ /* sizeof() == 3 */
"id" : 27,
"name" : "SockOpt",
"module" : "/#e/SockOpt.gs",
},
{ /* sizeof() == 3 */
"id" : 28,
"name" : "SockParam",
"module" : "/#e/SockParam.gs",
},
{ /* sizeof() == 3 */
"id" : 29,
"name" : "CType",
"module" : "/#e/CType.gs",
},
{ /* sizeof() == 3 */
"id" : 30,
"name" : "PendingMode",
"module" : "/#e/PendingMode.gs",
},
{ /* sizeof() == 3 */
"id" : 31,
"name" : "TypeDesc",
"module" : "/#e/TypeDesc.gs",
},
{ /* sizeof() == 3 */
"id" : 32,
"name" : "CompressMode",
"module" : "/#e/CompressMode.gs",
},
{ /* sizeof() == 3 */
"id" : 33,
"name" : "Crypt",
"module" : "/#e/Crypt.gs",
},
{ /* sizeof() == 3 */
"id" : 34,
"name" : "PktField",
"module" : "/#e/PktField.gs",
},
{ /* sizeof() == 3 */
"id" : 35,
"name" : "PktEncapsType",
"module" : "/#e/PktEncapsType.gs",
},
{ /* sizeof() == 3 */
"id" : 36,
"name" : "PortFlags",
"module" : "/#e/PortFlags.gs",
},
{ /* sizeof() == 3 */
"id" : 37,
"name" : "RegexFlags",
"module" : "/#e/RegexFlags.gs",
},
{ /* sizeof() == 3 */
"id" : 38,
"name" : "Xml",
"module" : "/#e/Xml.gs",
},
{ /* sizeof() == 3 */
"id" : 39,
"name" : "ByteSize",
"module" : "/builtin/ffi/gsffi.gs",
},
{ /* sizeof() == 3 */
"id" : 40,
"name" : "ArgType",
"module" : "/gs/util/input_args.gs",
},
{ /* sizeof() == 3 */
"id" : 41,
"name" : "TIFF_TAG",
"module" : "/gs/util/jpeg_exif.gs",
},
{ /* sizeof() == 3 */
"id" : 42,
"name" : "GPS_TAG",
"module" : "/gs/util/jpeg_exif.gs",
},
{ /* sizeof() == 3 */
"id" : 43,
"name" : "LogLevel",
"module" : "/gs/util/trace.gs",
},
{ /* sizeof() == 3 */
"id" : 44,
"name" : "Trim",
"module" : "/gs/lang/string.gs",
},