use type enum

This commit is contained in:
lyon 2021-11-15 09:36:46 +08:00
parent 3ab412f219
commit b88b729039
7 changed files with 84 additions and 111 deletions

View File

@ -14,7 +14,7 @@ Arg* arg_setMetaObj(char* objName, char* className, NewFun objPtr) {
Args* buffs = New_strBuff();
Arg* argNew = New_arg(NULL);
/* m means mate-object */
argNew = arg_setPtr(argNew, objName, "m", objPtr);
argNew = arg_setPtr(argNew, objName, TYPE_MATE_OBJECT, objPtr);
args_deinit(buffs);
return argNew;
}

View File

@ -17,9 +17,9 @@ int32_t deinitEachSubObj(Arg* argEach, Args* handleArgs) {
/* error: tOhis handle not need handle args */
return 1;
}
char* type = arg_getType(argEach);
ArgType type = arg_getType(argEach);
/* deinit sub object */
if (strEqu(type, "c")) {
if (type == TYPE_OBJECT) {
PikaObj* subObj = arg_getPtr(argEach);
obj_deinit(subObj);
}
@ -190,11 +190,6 @@ int32_t obj_freeObj(PikaObj* self, char* objPath) {
return 0;
}
int32_t obj_bind(PikaObj* self, char* type, char* name, void* pointer) {
args_bind(self->list, type, name, pointer);
return 0;
}
char* obj_print(PikaObj* self, char* name) {
if (NULL == self) {
return NULL;
@ -202,21 +197,6 @@ char* obj_print(PikaObj* self, char* name) {
return args_print(self->list, name);
}
int32_t obj_bindInt(PikaObj* self, char* name, int32_t* valPtr) {
args_bindInt(self->list, name, valPtr);
return 0;
}
int32_t obj_bindFloat(PikaObj* self, char* name, float* valPtr) {
args_bindFloat(self->list, name, valPtr);
return 0;
}
int32_t obj_bindString(PikaObj* self, char* name, char** valPtr) {
args_bindStr(self->list, name, valPtr);
return 0;
}
PikaObj* obj_getClassObjByNewFun(PikaObj* context,
char* name,
NewFun newClassFun) {
@ -257,13 +237,12 @@ PikaObj* obj_getClassObj(PikaObj* obj) {
void* getNewClassObjFunByName(PikaObj* obj, char* name) {
char* classPath = name;
/* init the subprocess */
void * newClassObjFun=
args_getPtr(obj->list, classPath);
void* newClassObjFun = args_getPtr(obj->list, classPath);
return newClassObjFun;
}
int32_t __foreach_removeMethodInfo(Arg* argNow, Args* argList) {
if (strEqu(arg_getType(argNow), "M")) {
if (arg_getType(argNow) == TYPE_METHOD) {
args_removeArg(argList, argNow);
return 0;
}
@ -283,7 +262,7 @@ PikaObj* newRootObj(char* name, NewFun newObjFun) {
PikaObj* initObj(PikaObj* obj, char* name) {
PikaObj* res = NULL;
NewFun newObjFun = (NewFun) getNewClassObjFunByName(obj, name);
NewFun newObjFun = (NewFun)getNewClassObjFunByName(obj, name);
Args* buffs = New_args(NULL);
PikaObj* thisClass;
PikaObj* newObj;
@ -295,7 +274,7 @@ PikaObj* initObj(PikaObj* obj, char* name) {
thisClass = obj_getClassObjByNewFun(obj, name, newObjFun);
newObj = removeMethodInfo(thisClass);
args_setPtrWithType(obj->list, name, "c", newObj);
args_setPtrWithType(obj->list, name, TYPE_OBJECT, newObj);
res = obj_getPtr(obj, name);
goto exit;
exit:
@ -308,13 +287,13 @@ PikaObj* obj_getObjDirect(PikaObj* self, char* name) {
return NULL;
}
/* finded object, check type*/
char* type = args_getType(self->list, name);
ArgType type = args_getType(self->list, name);
/* found mate Object */
if (strEqu(type, "m")) {
if (type == TYPE_MATE_OBJECT) {
return initObj(self, name);
}
/* found Objcet */
if (strEqu(type, "c")) {
if (type == TYPE_OBJECT) {
return obj_getPtr(self, name);
}
return NULL;
@ -349,7 +328,7 @@ void* methodArg_getPtr(Arg* method_arg) {
char* methodArg_getDec(Arg* method_arg) {
uint32_t size_ptr = sizeof(void*);
void* info = arg_getContent(method_arg);
return (char *)((uint64_t)info + size_ptr);
return (char*)((uint64_t)info + size_ptr);
}
void obj_saveMethodInfo(PikaObj* self,
@ -365,9 +344,9 @@ void obj_saveMethodInfo(PikaObj* self,
void* info = args_getBuff(buffs, size_info);
memcpy(info, &method_ptr, size_ptr);
/* +1 to add \0 */
memcpy((void *)((uint64_t)info + size_ptr), pars, size_pars + 1);
memcpy((void*)((uint64_t)info + size_ptr), pars, size_pars + 1);
arg = arg_setName(arg, method_name);
arg = arg_setType(arg, "M");
arg = arg_setType(arg, TYPE_METHOD);
arg = arg_setContent(arg, info, size_info);
args_setArg(self->list, arg);
@ -432,8 +411,7 @@ int32_t obj_removeArg(PikaObj* self, char* argPath) {
goto exit;
}
argName = strsGetLastToken(buffs, argPath, '.');
res =
args_removeArg(objHost->list, args_getArg(objHost->list, argName));
res = args_removeArg(objHost->list, args_getArg(objHost->list, argName));
if (1 == res) {
/*[error] not found arg*/
err = 2;

View File

@ -185,7 +185,9 @@ Arg* pikaVM_runInstruct(PikaObj* self,
Arg* outArg = NULL;
Arg* arg1 = arg_copy(queue_popArg(invokeQuene1));
Arg* arg2 = arg_copy(queue_popArg(invokeQuene1));
if (strEqu(arg_getType(arg1), "i") && strEqu(arg_getType(arg2), "i")) {
ArgType type_arg1 = arg_getType(arg1);
ArgType type_arg2 = arg_getType(arg2);
if ((type_arg1 == TYPE_INT) && (type_arg2 == TYPE_INT)) {
int num1 = arg_getInt(arg1);
int num2 = arg_getInt(arg2);
if (strEqu("+", data)) {
@ -217,7 +219,7 @@ Arg* pikaVM_runInstruct(PikaObj* self,
goto OPT_exit;
}
}
if (strEqu(arg_getType(arg1), "f") && strEqu(arg_getType(arg2), "i")) {
if ((type_arg1 == TYPE_FLOAT) && (type_arg2 == TYPE_INT)) {
float num1 = arg_getFloat(arg1);
int num2 = arg_getInt(arg2);
if (strEqu("+", data)) {
@ -249,7 +251,7 @@ Arg* pikaVM_runInstruct(PikaObj* self,
goto OPT_exit;
}
}
if (strEqu(arg_getType(arg1), "i") && strEqu(arg_getType(arg2), "f")) {
if ((type_arg1 == TYPE_INT) && (type_arg2 == TYPE_FLOAT)) {
int num1 = arg_getInt(arg1);
float num2 = arg_getFloat(arg2);
if (strEqu("+", data)) {
@ -281,7 +283,7 @@ Arg* pikaVM_runInstruct(PikaObj* self,
goto OPT_exit;
}
}
if (strEqu(arg_getType(arg1), "f") && strEqu(arg_getType(arg2), "f")) {
if ((type_arg1 == TYPE_FLOAT) && (type_arg2 == TYPE_FLOAT)) {
float num1 = arg_getFloat(arg1);
float num2 = arg_getFloat(arg2);
if (strEqu("+", data)) {
@ -358,7 +360,7 @@ Arg* pikaVM_runInstruct(PikaObj* self,
goto RUN_exit;
}
/* get method Ptr */
methodPtr = (Method) methodArg_getPtr(method_arg);
methodPtr = (Method)methodArg_getPtr(method_arg);
/* get method Decleartion */
methodDec = strsCopy(buffs, methodArg_getDec(method_arg));
arg_deinit(method_arg);

View File

@ -27,7 +27,6 @@ uint16_t content_sizeOffset(uint8_t* self) {
uint16_t content_getSize(uint8_t* self) {
uint16_t size = 0;
// add 0x30 to avoid 0
size += self[content_sizeOffset(self) + 1];
size = (size << 8);
size += self[content_sizeOffset(self)];
@ -56,16 +55,16 @@ Hash hash_time33(char* str) {
}
uint8_t* content_init_hash(Hash nameHash,
char* type,
ArgType type,
uint8_t* content,
uint16_t size,
uint8_t* next) {
const uint8_t nextLength = sizeof(uint8_t*);
const uint8_t sizeLength = 2;
uint16_t nameSize = sizeof(Hash); // use hash
uint16_t typeSize = strGetSize(type);
uint16_t nameSize = sizeof(Hash); // use hash
uint16_t typeSize = sizeof(ArgType); // use enum
uint8_t* self = (uint8_t*)pikaMalloc(nextLength + sizeLength + nameSize +
size + typeSize + 1);
size + typeSize);
uint8_t* nextDir = self;
uint8_t* sizeDir = nextDir + nextLength;
@ -74,7 +73,7 @@ uint8_t* content_init_hash(Hash nameHash,
uint8_t* typeDir = contentDir + size;
memcpy(nameDir, &nameHash, nameSize); // use hash
memcpy(typeDir, type, typeSize + 1);
memcpy(typeDir, &type, typeSize);
sizeDir[0] = size;
sizeDir[1] = size >> 8;
if (NULL != content) {
@ -94,7 +93,7 @@ uint8_t* content_init_hash(Hash nameHash,
}
uint8_t* content_init(char* name,
char* type,
ArgType type,
uint8_t* content,
uint16_t size,
uint8_t* next) {
@ -103,11 +102,11 @@ uint8_t* content_init(char* name,
}
uint16_t content_totleSize(uint8_t* self) {
char* type = content_getType(self);
const uint8_t sizeLenth = 2;
const uint8_t nextLength = sizeof(uint8_t*);
uint16_t size = content_getSize(self);
return size + sizeof(Hash) + strGetSize(type) + 1 + sizeLenth + nextLength;
const uint8_t size_size = 2;
const uint8_t size_next = sizeof(uint8_t*);
uint16_t size_content = content_getSize(self);
return size_content + sizeof(Hash) + sizeof(ArgType) + size_size +
size_next;
}
void arg_freeContent(Arg* self) {
@ -137,10 +136,10 @@ uint8_t* content_deinit(uint8_t* self) {
uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size) {
if (NULL == self) {
return content_init("", "", content, size, NULL);
return content_init("", TYPE_NONE, content, size, NULL);
}
Hash nameHash = content_getNameHash(self);
char* type = content_getType(self);
ArgType type = content_getType(self);
uint8_t* next = content_getNext(self);
uint8_t* newContent =
content_init_hash(nameHash, type, content, size, next);
@ -150,9 +149,9 @@ uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size) {
uint8_t* content_setNameHash(uint8_t* self, Hash nameHash) {
if (NULL == self) {
return content_init_hash(nameHash, "", NULL, 0, NULL);
return content_init_hash(nameHash, TYPE_NONE, NULL, 0, NULL);
}
char* type = content_getType(self);
ArgType type = content_getType(self);
uint8_t* content = content_getContent(self);
uint16_t size = content_getSize(self);
uint8_t* next = content_getNext(self);
@ -164,9 +163,9 @@ uint8_t* content_setNameHash(uint8_t* self, Hash nameHash) {
uint8_t* content_setName(uint8_t* self, char* name) {
if (NULL == self) {
return content_init(name, "", NULL, 0, NULL);
return content_init(name, TYPE_NONE, NULL, 0, NULL);
}
char* type = content_getType(self);
ArgType type = content_getType(self);
uint8_t* content = content_getContent(self);
uint16_t size = content_getSize(self);
uint8_t* next = content_getNext(self);
@ -175,7 +174,7 @@ uint8_t* content_setName(uint8_t* self, char* name) {
return newContent;
}
uint8_t* content_setType(uint8_t* self, char* type) {
uint8_t* content_setType(uint8_t* self, ArgType type) {
if (NULL == self) {
return content_init("", type, NULL, 0, NULL);
}
@ -190,7 +189,7 @@ uint8_t* content_setType(uint8_t* self, char* type) {
}
Arg* arg_newContent(Arg* self, uint32_t size) {
uint8_t* newContent = content_init("", "", NULL, size, NULL);
uint8_t* newContent = content_init("", TYPE_NONE, NULL, size, NULL);
arg_freeContent(self);
return newContent;
}
@ -207,12 +206,15 @@ Arg* arg_setNameHash(Arg* self, Hash nameHash) {
return content_setNameHash(self, nameHash);
}
Arg* arg_setType(Arg* self, char* type) {
Arg* arg_setType(Arg* self, ArgType type) {
return content_setType(self, type);
}
char* content_getType(uint8_t* self) {
return (char*)self + content_typeOffset(self);
ArgType content_getType(uint8_t* self) {
void* type_ptr = (uint8_t*)self + content_typeOffset(self);
ArgType type;
memcpy(&type, type_ptr, sizeof(ArgType));
return type;
}
uint16_t content_contentOffset(uint8_t* self) {
@ -256,7 +258,7 @@ Arg* arg_setInt(Arg* self, char* name, int64_t val) {
contentBuff[i] = int64Temp;
int64Temp = int64Temp >> 8;
}
return content_init(name, "i", contentBuff, 4, NULL);
return content_init(name, TYPE_INT, contentBuff, 4, NULL);
}
Arg* arg_setFloat(Arg* self, char* name, float val) {
@ -266,7 +268,7 @@ Arg* arg_setFloat(Arg* self, char* name, float val) {
// add 0x30 to void \0
contentBuff[i] = valPtr[i];
}
return content_init(name, "f", contentBuff, 4, NULL);
return content_init(name, TYPE_FLOAT, contentBuff, 4, NULL);
}
float arg_getFloat(Arg* self) {
@ -283,7 +285,7 @@ float arg_getFloat(Arg* self) {
return valOut;
}
Arg* arg_setPtr(Arg* self, char* name, char* type, void* pointer) {
Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer) {
uint64_t pointerTemp = (uint64_t)pointer;
uint8_t contentBuff[8];
for (uint32_t i = 0; i < sizeof(uint8_t*); i++) {
@ -295,7 +297,7 @@ Arg* arg_setPtr(Arg* self, char* name, char* type, void* pointer) {
}
Arg* arg_setStr(Arg* self, char* name, char* string) {
return content_init(name, "s", (uint8_t*)string, strGetSize(string) + 1,
return content_init(name, TYPE_STRING, (uint8_t*)string, strGetSize(string) + 1,
NULL);
}
@ -347,9 +349,9 @@ Hash arg_getNameHash(Arg* self) {
return content_getNameHash(self);
}
char* arg_getType(Arg* self) {
ArgType arg_getType(Arg* self) {
if (NULL == self) {
return NULL;
return TYPE_NONE;
}
return content_getType(self);
}

View File

@ -9,6 +9,16 @@
typedef uint8_t Arg;
typedef uint32_t Hash;
typedef enum {
TYPE_NONE,
TYPE_OBJECT,
TYPE_INT,
TYPE_FLOAT,
TYPE_METHOD,
TYPE_POINTER,
TYPE_MATE_OBJECT,
TYPE_STRING
} ArgType;
uint16_t content_typeOffset(uint8_t* content);
uint16_t content_contentOffset(uint8_t* content);
@ -18,7 +28,7 @@ uint16_t content_nextOffset(uint8_t* self);
uint8_t content_nameOffset(uint8_t* self);
uint32_t content_getNameHash(uint8_t* content);
char* content_getType(uint8_t* content);
ArgType content_getType(uint8_t* self);
uint8_t* content_getNext(uint8_t* self);
uint16_t content_getSize(uint8_t* self);
uint8_t* content_getContent(uint8_t* content);
@ -26,13 +36,13 @@ uint8_t* content_getContent(uint8_t* content);
uint16_t content_totleSize(uint8_t* self);
uint8_t* content_init(char* name,
char* type,
ArgType type,
uint8_t* content,
uint16_t size,
uint8_t* next);
uint8_t* content_deinit(uint8_t* self);
uint8_t* content_setName(uint8_t* self, char* name);
uint8_t* content_setType(uint8_t* self, char* type);
uint8_t* content_setType(uint8_t* self, ArgType type);
uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size);
void content_setNext(uint8_t* self, uint8_t* next);
@ -42,16 +52,16 @@ void arg_freeContent(Arg* self);
Arg* arg_setName(Arg* self, char* name);
Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size);
Arg* arg_newContent(Arg* self, uint32_t size);
Arg* arg_setType(Arg* self, char* type);
Arg* arg_setType(Arg* self, ArgType type);
Hash arg_getNameHash(Arg* self);
char* arg_getType(Arg* self);
ArgType arg_getType(Arg* self);
uint8_t* arg_getContent(Arg* self);
uint16_t arg_getContentSize(Arg* self);
Hash hash_time33(char* str);
Arg* arg_setInt(Arg* self, char* name, int64_t val);
Arg* arg_setFloat(Arg* self, char* name, float val);
Arg* arg_setPtr(Arg* self, char* name, char* type, void* pointer);
Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer);
Arg* arg_setStr(Arg* self, char* name, char* string);
int64_t arg_getInt(Arg* self);

View File

@ -38,7 +38,7 @@ void* args_getPtr(Args* self, char* name) {
int32_t args_setPtr(Args* self, char* name, void* argPointer) {
int32_t errCode = 0;
Arg* argNew = New_arg(NULL);
argNew = arg_setPtr(argNew, name, "p", argPointer);
argNew = arg_setPtr(argNew, name, TYPE_POINTER, argPointer);
args_setArg(self, argNew);
return errCode;
}
@ -95,11 +95,11 @@ int32_t args_getSize(Args* self) {
return link_getSize(self);
}
char* args_getType(Args* self, char* name) {
ArgType args_getType(Args* self, char* name) {
Arg* arg = NULL;
arg = args_getArg(self, name);
if (NULL == arg) {
return NULL;
return TYPE_NONE;
}
return arg_getType(arg);
}
@ -244,28 +244,6 @@ Arg* args_getArg(Args* self, char* name) {
return node;
}
void args_bind(Args* self, char* type, char* name, void* pointer) {
Args* buffs = New_strBuff();
char* typeWithBind = strsAppend(buffs, "_bind-", type);
Arg* argNew = New_arg(NULL);
argNew = arg_setPtr(argNew, name, typeWithBind, pointer);
args_setArg(self, argNew);
args_deinit(buffs);
return;
}
void args_bindInt(Args* self, char* name, int32_t* intPtr) {
args_bind(self, "i", name, intPtr);
}
void args_bindFloat(Args* self, char* name, float* floatPtr) {
args_bind(self, "f", name, floatPtr);
}
void args_bindStr(Args* self, char* name, char** stringPtr) {
args_bind(self, "s", name, stringPtr);
}
char* getPrintSring(Args* self, char* name, char* valString) {
Args* buffs = New_strBuff();
char* printName = strsFormat(buffs, 128, "[printBuff]%s", name);
@ -306,32 +284,32 @@ char* getPrintStringFromPtr(Args* self, char* name, void* val) {
char* args_print(Args* self, char* name) {
char* res = NULL;
char* type = args_getType(self, name);
ArgType type = args_getType(self, name);
Args* buffs = New_strBuff();
if (NULL == type) {
if (TYPE_NONE == type) {
/* can not get arg */
res = NULL;
goto exit;
}
if (strEqu(type, "i")) {
if (type == TYPE_INT) {
int32_t val = args_getInt(self, name);
res = getPrintStringFromInt(self, name, val);
goto exit;
}
if (strEqu(type, "f")) {
if (type == TYPE_FLOAT) {
float val = args_getFloat(self, name);
res = getPrintStringFromFloat(self, name, val);
goto exit;
}
if (strEqu(type, "s")) {
if (type == TYPE_STRING) {
res = args_getStr(self, name);
goto exit;
}
if (strEqu(type, "p")) {
if (type == TYPE_POINTER) {
void* val = args_getPtr(self, name);
res = getPrintStringFromPtr(self, name, val);
goto exit;
@ -346,7 +324,10 @@ exit:
return res;
}
int32_t args_setPtrWithType(Args* self, char* name, char* type, void* objPtr) {
int32_t args_setPtrWithType(Args* self,
char* name,
ArgType type,
void* objPtr) {
Arg* argNew = New_arg(NULL);
argNew = arg_setPtr(argNew, name, type, objPtr);
args_setArg(self, argNew);

View File

@ -28,7 +28,7 @@ int32_t args_setArg(Args* self, Arg* arg);
int32_t args_copyArgByName(Args* self, char* name, Args* directList);
int32_t args_copyArg(Args* self, Arg* argToBeCopy);
char* args_getType(Args* self, char* name);
ArgType args_getType(Args* self, char* name);
int32_t args_isArgExist_hash(Args* self, Hash nameHash);
int32_t args_isArgExist(Args* self, char* name);
@ -65,8 +65,8 @@ int32_t args_setObjectWithClass(Args* self,
char* className,
void* objectPtr);
int32_t args_setPtrWithType(Args* self,
char* objName,
char* type,
char* name,
ArgType type,
void* objPtr);
int32_t args_foreach(Args* self,
int32_t (*eachHandle)(Arg* argEach, Args* handleArgs),