bool CMetaDataFunction::CallFuction(int param_count, void **pParam, void *pReturn) { if (!m_pFunction) return false; SMetaDataCalledFunctionDataPacket Packet = { param_count, pParam, pReturn }; TpMDCalledFunction pFunc(reinterpret_cast<TpMDCalledFunction>(m_pFunction)); return pFunc(Packet); }
void ReorderOddEven(int *pData,unsigned int length,bool (* pFunc)(int)) { if(length<=0 || pData==NULL) return; int *head = pData; int *tail = pData+length-1; while (head<tail) { while (head<tail && !pFunc(*head)) { head++; } while (head<tail && pFunc(*tail)) { tail--; } if(head<tail) { int temp = *head; *head = *tail; *tail = temp; } } }
void WiiRemote::task(void (*pFunc)(void)) { Max.Task(); Usb.Task(); delay(1); // re-initialize if (Usb.getUsbTaskState() == USB_DETACHED_SUBSTATE_INITIALIZE) { /* TODO */ wiiremote_status_ = 0; } // wait for addressing state if (Usb.getUsbTaskState() == USB_STATE_CONFIGURING) { initBTController(); if (wiiremote_status_ & WIIREMOTE_STATE_USB_CONFIGURED) { hci_state_ = HCI_INIT_STATE; hci_counter_ = 10; l2cap_state_ = L2CAP_DOWN_STATE; Usb.setUsbTaskState(USB_STATE_RUNNING); } } if (Usb.getUsbTaskState() == USB_STATE_RUNNING) { HCI_task(); // poll the HCI event pipe L2CAP_task(); // start polling the ACL input pipe too, though discard // data until connected } //if (l2cap_state_ == L2CAP_READY_STATE) { if (wiiremote_status_ & WIIREMOTE_STATE_RUNNING) { if (pFunc) { pFunc(); } } } // task
static long ged_dispatch(GED_BRIDGE_PACKAGE *psBridgePackageKM) { int ret = -EFAULT; void *pvInt, *pvOut; typedef int (ged_bridge_func_type)(void*, void*); ged_bridge_func_type* pFunc = NULL; if ((psBridgePackageKM->i32InBufferSize >=0) && (psBridgePackageKM->i32OutBufferSize >=0) && (psBridgePackageKM->i32InBufferSize + psBridgePackageKM->i32OutBufferSize < GED_IOCTL_PARAM_BUF_SIZE)) { pvInt = gvIOCTLParamBuf; pvOut = (void*)((char*)pvInt + (uintptr_t)psBridgePackageKM->i32InBufferSize); if (psBridgePackageKM->i32InBufferSize > 0) { if (0 != ged_copy_from_user(pvInt, psBridgePackageKM->pvParamIn, psBridgePackageKM->i32InBufferSize)) { GED_LOGE("ged_copy_from_user fail\n"); return ret; } } // we will change the below switch into a function pointer mapping table in the future switch(GED_GET_BRIDGE_ID(psBridgePackageKM->ui32FunctionID)) { case GED_BRIDGE_COMMAND_LOG_BUF_GET: pFunc = (ged_bridge_func_type*)ged_bridge_log_buf_get; break; case GED_BRIDGE_COMMAND_LOG_BUF_WRITE: pFunc = (ged_bridge_func_type*)ged_bridge_log_buf_write; break; case GED_BRIDGE_COMMAND_LOG_BUF_RESET: pFunc = (ged_bridge_func_type*)ged_bridge_log_buf_reset; break; case GED_BRIDGE_COMMAND_BOOST_GPU_FREQ: pFunc = (ged_bridge_func_type*)ged_bridge_boost_gpu_freq; break; case GED_BRIDGE_COMMAND_MONITOR_3D_FENCE: pFunc = (ged_bridge_func_type*)ged_bridge_monitor_3D_fence; break; default: GED_LOGE("Unknown Bridge ID: %u\n", GED_GET_BRIDGE_ID(psBridgePackageKM->ui32FunctionID)); break; } if (pFunc) { ret = pFunc(pvInt, pvOut); } if (psBridgePackageKM->i32OutBufferSize > 0) { if (0 != ged_copy_to_user(psBridgePackageKM->pvParamOut, pvOut, psBridgePackageKM->i32OutBufferSize)) { return ret; } } } return ret; }
/** \brief Sorts objects of list, use compare pFunc * \param pList ArrayList* Pointer to arrayList * \param pFunc (*pFunc) Pointer to fuction to compare elements of arrayList * \param order int [1] indicate UP - [0] indicate DOWN * \return int Return (-1) if Error [pList or pFunc are NULL pointer] * - (0) if ok */ int al_sort(ArrayList* pList, int (*pFunc)(void* ,void*), int order) { int returnAux = -1, i,j,r; void* dato1, *dato2, *auxP; if(pList != NULL && pFunc != NULL && (order ==0 ||order ==1) ) { for(i=0; i<pList->size-1; i++) { for(j=i+1; j<pList->size; j++) { dato1 = *((pList->pElements)+i); dato2 = *((pList->pElements)+j); r=pFunc(dato1, dato2); if(r==1 && order == 1) { auxP=*((pList->pElements)+i); *((pList->pElements)+i)=*((pList->pElements)+j); *((pList->pElements)+j)=auxP; } if(r==-1 && order == 0) { auxP=*((pList->pElements)+i); *((pList->pElements)+i)=*((pList->pElements)+j); *((pList->pElements)+j)=auxP; } } } returnAux = 0; } return returnAux; }
static void bfs(TMGraph* graph, int v, int visited[], MGraph_Printf* pFunc) { LinkQueue* queue = LinkQueue_Create(); if( queue != NULL ) { LinkQueue_Append(queue, graph->v + v); visited[v] = 1; while( LinkQueue_Length(queue) > 0 ) { int i = 0; v = (MVertex**)LinkQueue_Retrieve(queue) - graph->v; pFunc(graph->v[v]); printf(", "); for(i=0; i<graph->count; i++) { if( (graph->matrix[v][i] != 0) && !visited[i] ) { LinkQueue_Append(queue, graph->v + i); visited[i] = 1; } } } } LinkQueue_Destroy(queue); }
static void recursive_display(BSTreeNode* node, BSTree_Printf* pFunc, int format, int gap, char div) // O(n) { int i = 0; if( (node != NULL) && (pFunc != NULL) ) { for(i=0; i<format; i++) { printf("%c", div); } pFunc(node); printf("\n"); if( (node->left != NULL) || (node->right != NULL) ) { recursive_display(node->left, pFunc, format + gap, gap, div); recursive_display(node->right, pFunc, format + gap, gap, div); } } else { for(i=0; i<format; i++) { printf("%c", div); } printf("\n"); } }
/* execute a function on all list members. The functions receives a * user-supplied parameter, which may be either a simple value * or a pointer to a structure with more data. If the user-supplied * function does not return RS_RET_OK, this function here terminates. * rgerhards, 2007-08-02 * rgerhards, 2007-11-21: added functionality to delete a list element. * If the called user function returns RS_RET_OK_DELETE_LISTENTRY the current element * is deleted. */ rsRetVal llExecFunc(linkedList_t *pThis, rsRetVal (*pFunc)(void*, void*), void* pParam) { DEFiRet; rsRetVal iRetLL; void *pData; linkedListCookie_t llCookie = NULL; linkedListCookie_t llCookiePrev = NULL; /* previous list element (needed for deletion, NULL = at root) */ assert(pThis != NULL); assert(pFunc != NULL); while((iRetLL = llGetNextElt(pThis, &llCookie, (void**)&pData)) == RS_RET_OK) { iRet = pFunc(pData, pParam); if(iRet == RS_RET_OK_DELETE_LISTENTRY) { /* delete element */ CHKiRet(llUnlinkAndDelteElt(pThis, llCookie, llCookiePrev)); /* we need to revert back, as we have just deleted the current element. * So the actual current element is the one before it, which happens to be * stored in llCookiePrev. -- rgerhards, 2007-11-21 */ llCookie = llCookiePrev; } else if (iRet != RS_RET_OK) { FINALIZE; } llCookiePrev = llCookie; } if(iRetLL != RS_RET_END_OF_LINKEDLIST) iRet = iRetLL; finalize_it: RETiRet; }
void test() { char dllname[] = {'c', ':', '\\', 't', 'e', 's', 't', '.', 'd', 'l', 'l', '\0'}; //必须用2088770939这个 LoadLibrary 函数的绝对地址(在我的xp下是这个地址,使用时应该在自己的windows下获取) pLoadLibrary pFunc = pLoadLibrary(2088770939); //调用LoadLibrary,因为LoadLibrary函数在windows下每个进程中绝对地址都是一样的。 pFunc(dllname); }
HRESULT _ATL_OBJMAP_ENTRY::RegisterClassObject(DWORD dwClsContext, DWORD dwFlags) { CComPtr<IUnknown> p; HRESULT hRes = pFunc(NULL, IID_IUnknown, (LPVOID*) &p); if (SUCCEEDED(hRes)) hRes = CoRegisterClassObject(*pclsid, p, dwClsContext, dwFlags, &dwRegister); return hRes; }
/** \brief Sorts objects of list, use compare pFunc * \param pList ArrayList* Pointer to arrayList * \param pFunc (*pFunc) Pointer to fuction to compare elements of arrayList * \param order int [1] indicate UP - [0] indicate DOWN * \return int Return (-1) if Error [pList or pFunc are NULL pointer] * - (0) if ok */ int al_sort(ArrayList* pList, int (*pFunc)(void* ,void*), int order) { int returnAux = -1; int i; int j; void *punt_temp; if((pList != NULL) && (pFunc != NULL) && (order == 1 || order == 0)) { if(order == 1) { for(i=0;i<pList->size-1;i++) { for(j=i+1;j<pList->size;j++) { if(pFunc(*(pList->pElements + i),*(pList->pElements + j))==1) { punt_temp=*(pList->pElements+i); *(pList->pElements + i)=*(pList->pElements+j); *(pList->pElements + j)=punt_temp; } } } } else { for(i=0;i<pList->size-1;i++) { for(j=i+1;j<pList->size;j++) { if(pFunc(*(pList->pElements+i),*(pList->pElements+j))==-1) { punt_temp =*(pList->pElements+i); *(pList->pElements + i)=*(pList->pElements+j); *(pList->pElements + j)=punt_temp; } } } } returnAux=0; } return returnAux; }
static void __cdecl CrtDoForAllClientObjects_thunk(void *pData, void *pContext) { _PHEAP_m pFunc = (_PHEAP_m) DecodePointer(__pfnHeapfunc); if (pFunc) { pFunc(pData, pContext); } }
/** \brief Sorts objects of list, use compare pFunc * \param pList ArrayList* Pointer to arrayList * \param pFunc (*pFunc) Pointer to fuction to compare elements of arrayList * \param order int [1] indicate UP - [0] indicate DOWN * \return int Return (-1) if Error [pList or pFunc are NULL pointer] * - (0) if ok */ int al_sort(ArrayList* pList, int (*pFunc)(void* ,void*), int order) { int returnAux = -1; int i,j; void* aux; if(pList!=NULL&&pFunc!=NULL) { if(order==1) { returnAux=0; for(i=0;i<pList->size-1;i++) { for(j=i+1;j<pList->size;j++) { if(pFunc(pList->pElements[i],pList->pElements[j]))//si elemento i es mayor que elemento j... { aux=pList->pElements[i]; pList->pElements[i]=pList->pElements[j]; pList->pElements[j]=aux; } } } } if(order==0) { returnAux=0; for(i=0;i<pList->size-1;i++) { for(j=i+1;j<pList->size;j++) { if(pFunc(pList->pElements[i],pList->pElements[j])==-1)//si elemento i es mayor que elemento j... { aux=pList->pElements[i]; pList->pElements[i]=pList->pElements[j]; pList->pElements[j]=aux; } } } } } return returnAux; }
/** \brief find Employee by id * \param pList ArrayList* Pointer to arrayList * \param pFunc (*pFunc) Pointer to fuction to compare elements of arrayList * \return int -(-1)if not found -(index of array)if found */ int al_find(ArrayList* pList,int (*pFunc)(void* ,void*),int id) { int returnAux=-1; int i; for(i=0;i<pList->size;i++) { if(pFunc(*(pList->pElements+i),((int*)id))==0)//tal vez ahi que probar de otra manera sin castear return i; } return returnAux; }
FX_BOOL CPDF_StitchFunc::v_Init(CPDF_Object* pObj) { CPDF_Dictionary* pDict = pObj->GetDict(); if (!pDict) { return FALSE; } if (m_nInputs != kRequiredNumInputs) { return FALSE; } CPDF_Array* pArray = pDict->GetArrayBy("Functions"); if (!pArray) { return FALSE; } uint32_t nSubs = pArray->GetCount(); if (nSubs == 0) return FALSE; m_nOutputs = 0; for (uint32_t i = 0; i < nSubs; i++) { CPDF_Object* pSub = pArray->GetDirectObjectAt(i); if (pSub == pObj) return FALSE; std::unique_ptr<CPDF_Function> pFunc(CPDF_Function::Load(pSub)); if (!pFunc) return FALSE; // Check that the input dimensionality is 1, and that all output // dimensionalities are the same. if (pFunc->CountInputs() != kRequiredNumInputs) return FALSE; if (pFunc->CountOutputs() != m_nOutputs) { if (m_nOutputs) return FALSE; m_nOutputs = pFunc->CountOutputs(); } m_pSubFunctions.push_back(std::move(pFunc)); } m_pBounds = FX_Alloc(FX_FLOAT, nSubs + 1); m_pBounds[0] = m_pDomains[0]; pArray = pDict->GetArrayBy("Bounds"); if (!pArray) return FALSE; for (uint32_t i = 0; i < nSubs - 1; i++) m_pBounds[i + 1] = pArray->GetFloatAt(i); m_pBounds[nSubs] = m_pDomains[1]; m_pEncode = FX_Alloc2D(FX_FLOAT, nSubs, 2); pArray = pDict->GetArrayBy("Encode"); if (!pArray) return FALSE; for (uint32_t i = 0; i < nSubs * 2; i++) m_pEncode[i] = pArray->GetFloatAt(i); return TRUE; }
/******************************************************************** Method: TraverseList Parameter: 单链表,函数指针 Returns: Purpose: 输出单链表 *********************************************************************/ bool TraverseList(LinkList L, bool (*pFunc)(Node * )) { Node *s = L->next; while (s) { pFunc(s); s = s->next; } printf("\n"); return true; }
static void recursive_dfs(TMGraph* graph, int v, int visited[], MGraph_Printf* pFunc) { int i = 0; pFunc(graph->v[v]); visited[v] = 1; printf(", "); for(i=0; i<graph->count; i++){ if( (graph->matrix[v][i] != 0) && !visited[i] ){ // 深度优先遍历 直接遍历某个节点边对应的另外一个端点 recursive_dfs(graph, i, visited, pFunc); } } }
int main() { TrampolineMgr* mgr = new TrampolineMgr; TrampolineMgr::loadFunctionInfo("/tmp/fi"); float (*pFunc)(int,int,double) = (float (*)(int,int,double)) mgr->generate((void*) &mytestfunc, "mytestfunc"); int (*pPrintf)(FILE* f, const char*,...) = (int (*)(FILE* f, const char*,...)) mgr->generate((void*) &fprintf, "printf"); std::cout << pFunc(2,3,0.5) << std::endl; //pPrintf(stdout, "Hello world: %s\n", "Test"); delete mgr; return 0; }
void MGraph_Display(MGraph* graph, MGraph_Printf* pFunc) // O(n*n) { TMGraph* tGraph = (TMGraph*)graph; if( (tGraph != NULL) && (pFunc != NULL) ) { int i = 0; int j = 0; for(i=0; i<tGraph->count; i++) { printf("%d:", i); pFunc(tGraph->v[i]); printf(" "); } printf("\n"); for(i=0; i<tGraph->count; i++) { for(j=0; j<tGraph->count; j++) { if( tGraph->matrix[i][j] != 0 ) { printf("<"); pFunc(tGraph->v[i]); printf(", "); pFunc(tGraph->v[j]); printf(", %d", tGraph->matrix[i][j]); printf(">"); printf(" "); } } } printf("\n"); } }
/** \brief Sorts objects of list, use compare pFunc * \param pList ArrayList* Pointer to arrayList * \param pFunc (*pFunc) Pointer to fuction to compare elements of arrayList * \param order int [1] indicate UP - [0] indicate DOWN * \return int Return (-1) if Error [pList or pFunc are NULL pointer] * - (0) if ok */ int al_sort(ArrayList* pList, int (*pFunc)(void*,void*), int order) { int i,j; int retornFuncion; int retorno=-1; int tam = al_len(pList); int flag=0; void* aux; if (pList != NULL && pFunc != NULL && (order==1 || order==0)) { for (i=0; i< tam-1; i++ ) { for(j=i+1; j< tam; j++ ) { retornFuncion = pFunc(al_get(pList, i), al_get(pList, j)); if(retornFuncion ==1 && order == 1) { aux=al_get(pList,i); al_set(pList,i,al_get(pList,j)); al_set(pList,j,aux); } else if(retornFuncion == -1 && order == 0) { aux=al_get(pList,i); al_set(pList,i,al_get(pList,j)); al_set(pList,j,aux); } } } flag=1; } if (flag) { retorno=0; } return retorno; }
void ReplaceVariable(ELEMENT* pEle) { /*递归函数,用来访问pEle下所有元素*/ function<void(ELEMENT*, function<void(ELEMENT*)>)> recursiveFunc = [&recursiveFunc](ELEMENT* pEle, function<void(ELEMENT*)> pFunc)->void { if (pFunc != nullptr) { pFunc(pEle); } for (ELEMENT* p = pEle->FirstChildElement(); p != 0; p = p->NextSiblingElement()) { recursiveFunc(p, pFunc); } }; /*以下用来将内容替换成变量中内容*/ for (ELEMENT* p = pEle->FirstChildElement("Variable")->FirstChildElement(); p != 0; p=p->NextSiblingElement()) { std::string vName, vValue; vName = string("$(") + string(p->Name()) + string(")"); vValue = std::string(p->GetText()); recursiveFunc(pEle, [vName,vValue](ELEMENT* p)->void { const char* text; if ((text=p->GetText()) != nullptr) { string str(text); std::size_t pos; while ((pos = str.find(vName)) != str.npos) { str.replace(pos, vName.length(), vValue); } p->SetText(str.c_str()); } }); } }
// Basic test. Makes @size chunks of 64 elements and writes in them random values, reverse them and checks if they are ok. // IF @flagForPrintValues is set, prints the valus of the arrays(and the result for every chunk). void testValueValidation(void(*pFunc)(char*, int), int size, char flagForPrintValues) { // Allocate memory for the control values(@arr) and the same one in @arrReversed which will be reversed int sizeMul64 = size << 6; char * arr = new char[sizeMul64]; char * arrReversed = new char[sizeMul64]; printf("Starting value validation tests with %d chunks (%d elements)!\n", size, sizeMul64); // Fill with some random values. srand(0); for (size_t i = 0; i < sizeMul64; ++i) arr[i] = arrReversed[i] = rand() % 256 - 128; // -128 to 127 // Prints the origin values. if (flagForPrintValues) printArr(arr, sizeMul64); // Reverse it in every chunk pFunc(arrReversed, size); if (flagForPrintValues) { printf("\n[REVERSED]\n\n"); // Prints the reversed values values. printArr(arrReversed, sizeMul64); printf("\nLet us check the values:\n"); } // Check if it`s reversed correctly. if (compareArrayWithReversedOneByChunks(arr, arrReversed, size, flagForPrintValues)) printf(" ---> [Passed!]\n"); else printf(" ---> [Failed!]\n"); // Delete memory delete[] arr; delete[] arrReversed; }
static void recursive_display(GTreeNode* node, GTree_Printf* pFunc, int format, int gap, char div) { int i = 0; if( (node != NULL) && (pFunc != NULL) ) { for(i=0; i<format; i++) { printf("%c", div); } pFunc(node->data); printf("\n"); for(i=0; i<LinkList_Length(node->child); i++) { TLNode* trNode = (TLNode*)LinkList_Get(node->child, i); recursive_display(trNode->node, pFunc, format + gap, gap, div); } } }
int main(int argc, char* argv[]) { HMODULE h = NULL; FILE* fp = NULL; jmp_buf env; int i; char* buf; ucontext_t context; h = LoadLibrary("windll.dll"); if (h != NULL) { pFunc = (void*)GetProcAddress(h, "windll"); if (pFunc != NULL) { printf("winmain: Call windll\n"); pFunc(); } } if (argc >= 3) { memcpy(env, argv[argc-2], sizeof(jmp_buf)); sscanf(argv[argc-1], "%p", &__longjmp); fp = fopen("__funp.txt", "w"); if (fp != NULL) fprintf(fp, "%p\n", pFunc); fclose(fp); fp = fopen("__env.bin", "rb"); if (fp != NULL) fread(&context, sizeof(char), sizeof(ucontext_t), fp); fclose(fp); setcontext(&context); __longjmp(env, 2); printf("Never Executed!\n"); } }
static void bfs(TMGraph* graph, int v, int visited[], MGraph_Printf* pFunc) { LinkQueue* queue = LinkQueue_Create(); if( queue != NULL ){ LinkQueue_Append(queue, graph->v + v); // 直接+v 跳到v对应的地方 实际上就是 v[v][0] visited[v] = 1; while( LinkQueue_Length(queue) > 0 ){ int i = 0; v = (MVertex**)LinkQueue_Retrieve(queue) - graph->v; // 出队列减去 graph->v 起始地址得到偏移量 pFunc(graph->v[v]); printf(", "); for(i=0; i<graph->count; i++){ if( (graph->matrix[v][i] != 0) && !visited[i] ){ LinkQueue_Append(queue, graph->v + i); // 压人的时候 实际上是压入了定点v 这里的遍历可以理解成一颗大树 visited[i] = 1; } } } } LinkQueue_Destroy(queue); }
void XMLParser::parseElement(istream & input, string sPrefix, vector<XMLSerializable*> & vObjects, int &index) { string sElementName; getline(input, sElementName, '>'); //cout << sPrefix << sElementName << " (start)" << endl; map<string,function<XMLSerializable*()>> mapConstructor; mapConstructor["Item"] = []() { return new Item; }; mapConstructor["Enemy"] = []() { return new Enemy; }; mapConstructor["Armor"] = []() { return new Armor; }; mapConstructor["Weapon"] = []() { return new Weapon; }; mapConstructor["Consumable"] = []() { return new Consumable; }; function<XMLSerializable*()> pFunc; XMLSerializable* myObject = NULL; if (sPrefix == "-> ") { index++; pFunc = mapConstructor[sElementName]; myObject = pFunc(); vObjects.push_back(myObject); } bool bDone = false; string sContent; while( !bDone ) { string sEndTag; char c = input.get(); if( c == '<' ) { if( input.peek() == '/' ) { getline(input, sEndTag, '>'); if( sEndTag == "/" + sElementName ) { // cout << sPrefix << sElementName << ": " << sContent << " (end)" << endl; if (sPrefix == "-> -> ") { vObjects[index]->setElementData(sElementName, sContent); } bDone = true; } else { cout << "Inavlid XML!" << endl; } } else { parseElement(input, sPrefix + "-> ", vObjects, index); } } else if( c != '\n' ) { sContent.push_back(c); } } if (myObject != NULL) { } return; }
// Examples: // // 1. prog -c123 --> (c,123) // 2. prog -c 123 --> (c,123) // 3. prog -c=123 --> (c,123) // 4. prog -cs 123 --> (c,123) (s) // 5. prog -sc=123 --> (s) (c,123) // 6. prog -cs123 --> (c,s123) // void CLI_Process ( int argc, char *argv[], CLI_OptionEntry *aOptionTable, int nOptionTable, CLI_CALLBACKFUNC *pFunc ) { int minNonOption = 0; int bEndOfOptions = 0; for (int i = 1; i < argc; i++) { char *pArgv = argv[i]; int iType = 0; if (!bEndOfOptions) { iType = iArgType(pArgv); } if (iType == 0) { // Non-option argument. // if (minNonOption <= i) { // We haven't associated it with an option, yet, so // pass it in by itself. // pFunc(0, pArgv); } continue; } if (minNonOption < i+1) { minNonOption = i+1; } if (iType == 3) { // A "--" causes the remaining unpaired arguments to be // treated as non-option arguments. // bEndOfOptions = 1; continue; } const char *p = pArgv+iType; if (iType == 2) { // We have a long option. // const char *pEqual = strchr(p, '='); size_t nLen; if (pEqual) { nLen = pEqual - p; } else { nLen = strlen(p); } for (int j = 0; j < nOptionTable; j++) { if ( !strncmp(aOptionTable[j].m_Flag, p, nLen) && aOptionTable[j].m_Flag[nLen] == '\0') { switch (aOptionTable[j].m_ArgControl) { case CLI_NONE: pFunc(aOptionTable + j, 0); break; case CLI_OPTIONAL: case CLI_REQUIRED: if (pEqual) { pFunc(aOptionTable + j, pEqual+1); break; } int bFound = 0; for (; minNonOption < argc; minNonOption++) { int iType2 = iArgType(argv[minNonOption]); if (iType2 == 0) { pFunc(aOptionTable + j, argv[minNonOption]); minNonOption++; bFound = 1; break; } else if (iType2 == 3) { // End of options. Stop. // break; } } if ( !bFound && aOptionTable[j].m_ArgControl == CLI_OPTIONAL) { pFunc(aOptionTable + j, 0); } break; } break; } } continue; } // At this point, the only possibilities left are a short // option. // while (*p) { int ch = *p++; for (int j = 0; j < nOptionTable; j++) { if ( aOptionTable[j].m_Flag[0] == ch && aOptionTable[j].m_Flag[1] == '\0') { switch (aOptionTable[j].m_ArgControl) { case CLI_NONE: pFunc(aOptionTable + j, 0); break; case CLI_OPTIONAL: case CLI_REQUIRED: if (*p) { // Value follows option letter // if (*p == '=') { p++; } pFunc(aOptionTable + j, p); p = ""; break; } int bFound = 0; for (; minNonOption < argc; minNonOption++) { int iType2 = iArgType(argv[minNonOption]); if (iType2 == 0) { pFunc(aOptionTable + j, argv[minNonOption]); minNonOption++; bFound = 1; break; } else if (iType2 == 3) { // End of options. Stop. // break; } } if ( !bFound && aOptionTable[j].m_ArgControl == CLI_OPTIONAL) { pFunc(aOptionTable + j, 0); } break; } break; } } } } }
template<> LogWrapper& LogWrapper::operator<<(LogWrapper::LoggerFunction pFunc) { return pFunc(*this); }
bool CMetaDataFunction::CallFuction(int param_count, ...) { if (!m_pFunction || (!m_pParamTable && param_count != 0) || (m_pParamTable && param_count != m_pParamTable->size())) return false; bool ret; va_list pList; unsigned int param_addr; bool *pNeedRelease; CParamVector::iterator itr; int type_size; void **pParamPtrBuffer; int index; SMetaDataCalledFunctionDataPacket Packet; TpMDCalledFunction pFunc(reinterpret_cast<TpMDCalledFunction>(m_pFunction)); pNeedRelease = new bool [param_count]; try { va_start(pList, param_count); try { param_addr = reinterpret_cast<unsigned int>(pList); if (m_pParamTable) { pParamPtrBuffer = new void* [param_count]; try { memset(pParamPtrBuffer, 0x00, sizeof(void*) * param_count); index = 0; for (itr = m_pParamTable->begin(); itr != m_pParamTable->end(); ++itr) { if ((*itr)->GetPtrLevel() <= 0) type_size = SizeInVarParamFunc((*itr)->GetMDType(), reinterpret_cast<void*>(param_addr), pParamPtrBuffer + index); else type_size = sizeof(void*); pNeedRelease[index] = pParamPtrBuffer[index] != NULL; if (!pNeedRelease[index]) { pParamPtrBuffer[index] = reinterpret_cast<void*>(param_addr); } ++index; param_addr += MD_FUNC_VA_INTSIZEOF(type_size); } Packet.ParamCount = param_count; Packet.pParam = pParamPtrBuffer; if (m_pReturnInfo) { Packet.pReturn = *reinterpret_cast<void**>(param_addr); } else Packet.pReturn = NULL; ret = pFunc(Packet); for (index = 0; index < param_count; ++index) { if (pNeedRelease[index]) delete pParamPtrBuffer[index]; } } catch(...) { delete pParamPtrBuffer; throw; } delete pParamPtrBuffer; } else { Packet.ParamCount = 0; Packet.pParam = NULL; if (m_pReturnInfo) { Packet.pReturn = *reinterpret_cast<void**>(param_addr); } else Packet.pReturn = NULL; ret = pFunc(Packet); } } catch(...) { va_end(pList); throw; } va_end(pList); } catch(...) { delete pNeedRelease; throw; } delete pNeedRelease; return ret; }
bool CMetaDataFunction::CallFuction(CParamVector *pParamTypes, va_list pParamList, void *pReturn) { if (!m_pFunction || (!m_pParamTable && (pParamTypes) && pParamTypes->size()!= 0) || (m_pParamTable && pParamTypes && m_pParamTable->size() != pParamTypes->size()) || (m_pParamTable && !pParamTypes && m_pParamTable->size() != 0)) return false; bool ret; unsigned int param_addr; int arr_size; bool *pNeedRelease; void **pParamPtrBuffer; CParamVector::iterator itr_in; CParamVector::iterator itr_func; int type_size; int index; bool bParamsOK(true); SMetaDataCalledFunctionDataPacket Packet; TpMDCalledFunction pFunc(reinterpret_cast<TpMDCalledFunction>(m_pFunction)); if (m_pParamTable) arr_size = m_pParamTable->size(); else arr_size = 0; pNeedRelease = new bool [arr_size]; try { memset(pNeedRelease, 0x00, sizeof(bool) * arr_size); param_addr = reinterpret_cast<unsigned int>(pParamList); if (arr_size > 0) { if (param_addr == 0) { throw new ExceptionMetaData(D_E_ID_ERR_MD_CALL_META_DATA_OF_FUNC, "错误:调用参数表缺失!"); } pParamPtrBuffer = new void* [arr_size]; try { memset(pParamPtrBuffer, 0x00, sizeof(void*) * arr_size); index = 0; for (itr_func = m_pParamTable->begin(), itr_in = pParamTypes->begin(); itr_func != m_pParamTable->end() && itr_in != pParamTypes->end(); ++itr_func, ++itr_in) { if ((*itr_func)->GetMDType() == (*itr_in)->GetMDType() && (*itr_func)->GetPtrLevel() == (*itr_in)->GetPtrLevel()) { if ((*itr_func)->GetPtrLevel() <= 0) type_size = SizeInVarParamFunc((*itr_func)->GetMDType(), reinterpret_cast<void*>(param_addr), pParamPtrBuffer + index); else type_size = sizeof(void*); pNeedRelease[index] = pParamPtrBuffer[index] != NULL; if (!pNeedRelease[index]) { pParamPtrBuffer[index] = reinterpret_cast<void*>(param_addr); } ++index; param_addr += MD_FUNC_VA_INTSIZEOF(type_size); } else { bParamsOK = false; break; } } if (bParamsOK) bParamsOK = itr_func == m_pParamTable->end() && itr_in == pParamTypes->end(); if (bParamsOK) { Packet.ParamCount = arr_size; Packet.pParam = pParamPtrBuffer; if (m_pReturnInfo) { Packet.pReturn = pReturn; } else Packet.pReturn = NULL; ret = pFunc(Packet); } else ret = false; for (index = 0; index < arr_size; ++index) { if (pNeedRelease[index]) delete pParamPtrBuffer[index]; } } catch(...) { delete pParamPtrBuffer; throw; } delete pParamPtrBuffer; } else { Packet.ParamCount = 0; Packet.pParam = NULL; if (m_pReturnInfo) { Packet.pReturn = pReturn; } else Packet.pReturn = NULL; ret = pFunc(Packet); } } catch(...) { delete pNeedRelease; throw; } delete pNeedRelease; return ret; }