|
|
 |
Ответов
|
May 26 2015, 11:18
|

Местный
  
Группа: Участник
Сообщений: 329
Регистрация: 23-04-14
Пользователь №: 81 502

|
Цитата(Jenya7 @ May 26 2015, 11:24)  совет хороший но как его применить. у каждой команды есть своя функция и соответственно свой указатель. я ищу генерик метод подставить указатель по условию. я могу тупо перебрать их все но хочется как то красиво. "Сделайте мне красиво"  Вот вам в качестве примера кривая реализация функторов на сях. Может подвигнет к размышлениям (а может к очередной порции страданий, что опять все плохо...). CODE //-- pointer to function that performs concrete command processing
struct TCommandFunctor; typedef int (*PF_CmdProcessor) (const TCommandFunctor* apFunctor);
//-- command functor. encapsulated parameters and a pointer to processing function struct TCommandFunctor { uint32 arg1; uint32 arg2; uint8 arg3; uint16 arg4;
PF_CmdProcessor pCmdProcessor; };
int DoCallFunctor(const TCommandFunctor* apFunctor) { return apFunctor->pCmdProcessor(apFunctor); }
//-- define command processor functions
int ProcessCmd1(const TCommandFunctor* apFunctor) { uint32 res1 = apFunctor->arg1 + apFunctor->arg2; return (res1 > 733); }
int ProcessCmd2(const TCommandFunctor* apFunctor) { //-- do something else uint32 res1 = apFunctor->arg3 > apFunctor->arg1; return res1; }
//-- define and populate functors TCommandFunctor cmdFunctors[2] = { {1, 2, 3, 4, ProcessCmd1}, {11,22,23,24, ProcessCmd2} };
void DoWhatever() { for(int i=0; i<2; ++i) { DoCallFunctor(&cmdFunctors[i]); }
}
Сообщение отредактировал CrimsonPig - May 26 2015, 11:48
|
|
|
|
|
May 26 2015, 13:43
|
Профессионал
    
Группа: Участник
Сообщений: 1 778
Регистрация: 29-03-12
Пользователь №: 71 075

|
Цитата(CrimsonPig @ May 26 2015, 16:18)  "Сделайте мне красиво"  Вот вам в качестве примера кривая реализация функторов на сях. Может подвигнет к размышлениям (а может к очередной порции страданий, что опять все плохо...). CODE //-- pointer to function that performs concrete command processing
struct TCommandFunctor; typedef int (*PF_CmdProcessor) (const TCommandFunctor* apFunctor);
//-- command functor. encapsulated parameters and a pointer to processing function struct TCommandFunctor { uint32 arg1; uint32 arg2; uint8 arg3; uint16 arg4;
PF_CmdProcessor pCmdProcessor; };
int DoCallFunctor(const TCommandFunctor* apFunctor) { return apFunctor->pCmdProcessor(apFunctor); }
//-- define command processor functions
int ProcessCmd1(const TCommandFunctor* apFunctor) { uint32 res1 = apFunctor->arg1 + apFunctor->arg2; return (res1 > 733); }
int ProcessCmd2(const TCommandFunctor* apFunctor) { //-- do something else uint32 res1 = apFunctor->arg3 > apFunctor->arg1; return res1; }
//-- define and populate functors TCommandFunctor cmdFunctors[2] = { {1, 2, 3, 4, ProcessCmd1}, {11,22,23,24, ProcessCmd2} };
void DoWhatever() { for(int i=0; i<2; ++i) { DoCallFunctor(&cmdFunctors[i]); }
}
спасибо. попробую разобраться.
|
|
|
|
Сообщений в этой теме
Jenya7 Перебрать все члены структуры May 25 2015, 07:09 jcxz Через массив указателей на члены. May 25 2015, 07:19 Jenya7 Цитата(jcxz @ May 25 2015, 12:19) Через м... May 25 2015, 07:30  jcxz Цитата(Jenya7 @ May 25 2015, 13:30) это н... May 25 2015, 08:59   Jenya7 Цитата(jcxz @ May 25 2015, 13:59) А что В... May 25 2015, 09:03    ViKo Цитата(Jenya7 @ May 25 2015, 12:03) член ... May 25 2015, 09:54     Jenya7 Цитата(ViKo @ May 25 2015, 14:54) А как в... May 25 2015, 10:45    jcxz Цитата(Jenya7 @ May 25 2015, 15:03) дайте... May 25 2015, 13:36 ViKo Цитата(Jenya7 @ May 25 2015, 10:09) я мог... May 25 2015, 07:54 ViKo Вам надо обратиться к C++ с его полиморфизмом. May 25 2015, 10:59 Jenya7 Цитата(ViKo @ May 25 2015, 15:59) Вам над... May 25 2015, 11:10 esaulenka Во-первых, у вас не структура, а юнион. И в данном... May 25 2015, 13:15 Jenya7 Цитата(esaulenka @ May 25 2015, 18:15) Во... May 25 2015, 13:49  zltigo QUOTE (Jenya7 @ May 25 2015, 16:49) так к... May 25 2015, 14:32   Jenya7 Цитата(zltigo @ May 25 2015, 19:32) Вы по... May 25 2015, 14:54    zltigo QUOTE (Jenya7 @ May 25 2015, 17:54) да я ... May 25 2015, 22:00  _Pasha Цитата(jcxz @ May 26 2015, 10:53) И чем В... May 26 2015, 10:16   jcxz Цитата(_Pasha @ May 26 2015, 16:16) А чег... May 26 2015, 16:27 ar__systems Если хочется перебирать единообразно все злементы ... Jun 3 2015, 05:47
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0
|
|
|