2014-01-18 3 views
0

이 C 함수를 고려 다음 제조 함수 지연된 long * 출력 변수에 대한 포인터를 저장하기 위해 사용된다이중 함수로 인수를 바인딩하는 방법은 무엇입니까?

#define INDICATE_SPECIAL_CASE -1 
void prepare (long *length_or_indicator); 
void execute(); 

.

은 다음과 같이 C에서 사용할 수 있습니다

int main (void) { 
    long length; 
    long indicator; 
    prepare (out length, out indicator); 
    execute(); 
    if (indicator == INDICATE_SPECIAL_CASE) { 
     // do something to handle special case 
    } 
    else { 
     // do something to handle the normal case which has a length 
    } 
} 

가 어떻게 발라에서 prepare()INDICATE_SPECIAL_CASE 바인딩 작성 :

int main (void) { 
    long length_or_indicator; 
    prepare (&length_or_indicator); 
    execute(); 
    if (length_or_indicator == INDICATE_SPECIAL_CASE) { 
     // do something to handle special case 
    } 
    else { 
     long length = lengh_or_indicator; 
     // do something to handle the normal case which has a length 
    } 
} 

내가 발라에서이 같은 일을 달성하기 위해 노력하고 있어요?

변수를 두 개로 분할 할 수 있습니까?

out 변수를 prepare() (execute())으로 호출 한 후 변수에 쓸지라도 포인터를 사용하지 않도록 할 수 있습니까?

답변

2

out을 사용할 때의 문제는 Vala가 길을 따라 많은 임시 변수를 생성하여 참조가 잘못 될 수 있다는 것입니다. VAPI에 다음을 모두 숨기는 메소드를 만드는 것이 좋습니다.

[CCode(cname = "prepare")] 
private void _prepare (long *length_or_indicator); 
[CCode(cname = "execute")] 
private void _execute(); 
[CCode(cname = "prepare_and_exec")] 
public bool execute(out long length) { 
    long length_or_indicator = 0; 
    prepare (&length_or_indicator); 
    execute(); 
    if (length_or_indicator == INDICATE_SPECIAL_CASE) { 
    length = 0; 
    return false; 
    } else { 
    length = lengh_or_indicator; 
    return true; 
} 
} 
관련 문제