Добавлен Koladik
Класическая задача, мне нужно построить обработчик, таковой, что обращаясь к нему, заложив параметры, можно было бы выбирать по индексу метод обработки. Можно построить дерево if-elseif-elseif-elseif-end но такое решение, возможно, не самое элегантное, хотел создать массив code, чтобы напрямую запускать функцию из массива, но это запрещено по причине указанный в комментариях в посте, там же можно найти обходное решение следуя которому, я написал этого монстра.
code vJass
library TestLib initializer TestINI
struct ExampleStruct
static constant integer MARC_1 = 0
static constant integer MARC_2 = 1
static constant integer MARC_3 = 2
static constant integer MARC_4 = 3
static constant integer MARC_5 = 4
//ets...
//
private static trigger array ACTIONTRIGGERS
//
private static integer GLOB_LOC_ADRESS
private static string GLOB_LOC_OUTPUT
//
private static method GetAction1 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+1)
return true
endmethod
private static method GetAction2 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+2)
return true
endmethod
private static method GetAction3 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+3)
return true
endmethod
private static method GetAction4 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+4)
return true
endmethod
private static method GetAction5 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+5)
return true
endmethod
//etc...
static method GetOutput takes integer method, integer adress returns string
set GLOB_LOC_ADRESS = adress
if(TriggerEvaluate(ACTIONTRIGGERS[method]))then
return GLOB_LOC_OUTPUT
else
return ""
endif
endmethod
static method STRUCTINI takes nothing returns nothing
set ACTIONTRIGGERS[MARC_1] = CreateTrigger()
call TriggerAddCondition(ACTIONTRIGGERS[MARC_1],function s__ExampleStruct_GetAction1)
//
set ACTIONTRIGGERS[MARC_2] = CreateTrigger()
call TriggerAddCondition(ACTIONTRIGGERS[MARC_2],function s__ExampleStruct_GetAction2)
//
set ACTIONTRIGGERS[MARC_3] = CreateTrigger()
call TriggerAddCondition(ACTIONTRIGGERS[MARC_3],function s__ExampleStruct_GetAction3)
//
set ACTIONTRIGGERS[MARC_4] = CreateTrigger()
call TriggerAddCondition(ACTIONTRIGGERS[MARC_4],function s__ExampleStruct_GetAction4)
//
set ACTIONTRIGGERS[MARC_5] = CreateTrigger()
call TriggerAddCondition(ACTIONTRIGGERS[MARC_5],function s__ExampleStruct_GetAction5)
//etc...
endmethod
endstruct
function TestStructFunc takes nothing returns nothing
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_1,12))
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_2,12))
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_3,12))
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_4,12))
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_5,12))
endfunction
function TestINI takes nothing returns nothing
call ExampleStruct.STRUCTINI()
call TimerStart(CreateTimer(), 1, false, function TestStructFunc)
endfunction
endlibrary
Мне кажется, что подобное решение нормальному человеку и показывать нельзя. Но может кто-то подскажет как сделать лучше
Переписано на
code vJass
library TestLib initializer TestINI
struct ExampleStruct
static constant integer MARC_1 = 0
static constant integer MARC_2 = 1
static constant integer MARC_3 = 2
static constant integer MARC_4 = 3
static constant integer MARC_5 = 4
//ets...
//
private static boolexpr array CodeAr
//
private static integer GLOB_LOC_ADRESS
private static string GLOB_LOC_OUTPUT
//
private static method GetAction1 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+1)
return true
endmethod
private static method GetAction2 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+2)
return true
endmethod
private static method GetAction3 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+3)
return true
endmethod
private static method GetAction4 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+4)
return true
endmethod
private static method GetAction5 takes nothing returns boolean
set GLOB_LOC_OUTPUT = I2S(GLOB_LOC_ADRESS+5)
return true
endmethod
//etc...
static method GetOutput takes integer method, integer adress returns string
set GLOB_LOC_ADRESS = adress
call ForceEnumPlayersCounted(bj_FORCE_PLAYER[0], CodeAr[method],1)
return GLOB_LOC_OUTPUT
endmethod
static method STRUCTINI takes nothing returns nothing
//
set CodeAr[MARC_1] = Condition(function s__ExampleStruct_GetAction1)
set CodeAr[MARC_2] = Condition(function s__ExampleStruct_GetAction2)
set CodeAr[MARC_3] = Condition(function s__ExampleStruct_GetAction3)
set CodeAr[MARC_4] = Condition(function s__ExampleStruct_GetAction4)
set CodeAr[MARC_5] = Condition(function s__ExampleStruct_GetAction5)
//etc...
endmethod
endstruct
function TestStructFunc takes nothing returns nothing
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_1,12))
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_2,12))
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_3,12))
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_4,12))
call BJDebugMsg(ExampleStruct.GetOutput(ExampleStruct.MARC_5,12))
endfunction
function TestINI takes nothing returns nothing
call ExampleStruct.STRUCTINI()
call TimerStart(CreateTimer(), 1, false, function TestStructFunc)
endfunction
endlibrary
Принятый ответ
Koladik:
Для jass такие решения уже есть, cjass добавляет лишь сахар, который легко изымается при необходимостиобработчик, таковой, что обращаясь к нему, заложив параметры, можно было бы выбирать по индексу метод обработки
Koladik:
Можно нагрузить пустую карту сравниваемыми операциями, и посмотреть, какие будут нагружать сильнеепроверить точно, насколько доступ к lua таблицам быстрее чем к хэш-таблицам на жазе невозможно
`
ОЖИДАНИЕ РЕКЛАМЫ...
Чтобы оставить комментарий, пожалуйста, войдите на сайт.
Отредактирован Koladik
Функция как объект
Отредактирован Koladik
Отредактирован DracoL1ch
Отредактирован Koladik