2010-07-03 5 views
1

xml 파일을 구문 분석하는 데 tinyxml을 사용하고 있으며 여기에서 오류 처리가 화살표 코드에 적합하다는 것을 알았습니다. 우리의 오류 처리는 단순히 메시지를 파일에보고하는 것입니다. 여기 xml 파싱에 대한 오류 처리

은 예입니다

const TiXmlElement *objectType = dataRoot->FirstChildElement("game_object"); 
    if (objectType) { 
    do { 
     const char *path = objectType->Attribute("path"); 
     if (path) { 
     const TiXmlElement *instance = objectType->FirstChildElement("instance"); 
     if (instance) { 
      do { 
      int x, y = 0; 
      instance->QueryIntAttribute("x", &x); 
      instance->QueryIntAttribute("y", &y); 
      if (x >= 0 && y >= 0) { 
       AddGameObject(new GameObject(path, x, y)); 
      } else { 
       LogErr("Tile location negative for GameObject in state file."); 
       return false; 
      } 
      } while (instance = instance->NextSiblingElement("instance")); 
     } else { 
      LogErr("No instances specified for GameObject in state file."); 
      return false; 
     } 
     } else { 
     LogErr("No path specified for GameObject in state file."); 
     return false; 
     } 
    } while (objectType = objectType->NextSiblingElement("game_object")); 
    } else { 
    LogErr("No game_object specified in <game_objects>. Thus, not necessary."); 
    return false; 
    } 
    return true; 

내가 화나게하고 그 위에 피고 아니지만, 사람이 작업을 수행 할 수있는 청소기 방법을 생각할 수 있다면 감사하겠습니다.

P. 예외는 옵션이 아닙니다.

편집 :

이렇게하는 것이 좋을까요?

이렇게하면 화살표 코드가 제거되지만 화살표 코드 종류는 모든 오류 로깅을 한 곳에서 처리합니다.

답변

2

반환 값을 오류 코드로 사용하면 이러한 코드가 생성되므로 그다지 향상시킬 수 없습니다. 약간 더 깨끗한 방법은 모든 오류 처리를 단일 블록으로 그룹화하고 블록 중첩을 줄이기 위해 goto을 사용합니다.

그러나 이것은 실제 값을 반환하지 않습니다. 반환 값은 오류 코드입니다. C에서는 대안이 없지만 C++ 예외는 사용할 수 있으므로 사용해야합니다. 옵션이 아닌 경우 가지고있는 것을 가지고 있습니다.

0

if (!var) { .. return false; } 및 오류보고를 캡슐화하는 매크로를 만들 수 있습니다.

그러나이 모든 것을 향상시킬 수있는 방법은 없습니다. 그것이 바로 그 길입니다. C'est la vie. C'est le code ...

0

내가 화나게하고 그 위에 피고 아니에요, 하지만 사람이 이러한 목표를 달성하기 위해 청소기 방법을 생각할 수있는 경우가 될 것이다 에 감사드립니다.

나는 오류 수익 문에 중첩 된 IFS (이 코드 또한 당신의 DO 루프 (그래서 그것을 이해할 수에로 loopps 대체했다. 대신 "화살표 모양"가는 "흘러"을하게 대체했다) 더 나은.

이 당신이 원하는 무엇인가?

const TiXmlElement *objectType = dataRoot->FirstChildElement("game_object"); 
if (!objectType) { 
    LogErr("No game_object specified in <game_objects>. Thus, not necessary."); 
    return false; 
} 

for(; objectType != 0; objectType = objectType->NextSiblingElement("game_object")) { 
    const char *path = objectType->Attribute("path"); 
    if (!path) { 
     LogErr("No path specified for GameObject in state file."); 
     return false; 
    } 

    const TiXmlElement *instance = objectType->FirstChildElement("instance"); 
    if (!instance) { 
     LogErr("No instances specified for GameObject in state file."); 
     return false; 
    } 

    for(; instance != 0; instance = instance->NextSiblingElement("instance")) { 
     int x, y = 0; 
     instance->QueryIntAttribute("x", &x); 
     instance->QueryIntAttribute("y", &y); 
     if (x >= 0 && y >= 0) { 
      AddGameObject(new GameObject(path, x, y)); 
     } else { 
      LogErr("Tile location negative for GameObject in state file."); 
      return false; 
     } 
    } 
} 
return true; 
0

나는 그것이 조금 늦게 알아,하지만 난 QueryIntAttribute 당신이 원하는 오류가 경우에 처리에 사용할 수있는 값을 반환 것을 알고 당신의 속성도 있습니다.

if (instance->QueryIntAttribute("x",&x)!=TIXML_SUCCESS) 
    cout << "No x value found";