2016-09-15 11 views
1

내가이 예에서와 같이 속성 트리로 구성 할 수있는 클래스를 만들고 싶습니다속성 트리 : 전체 경로와 이름

<?xml version="1.0" encoding="utf-8"?> 
<config> 
    <name>testing</name> 
    <!-- Test property tree --> 
    <lambda min="200000" max="200">100</lambda> 
... 
속성 트리 쉽게

,하지만 난 이 클래스로, 하위 트리의 두 가지 속성에 액세스해야합니다 밈/최대가 lambda이 유효하지, 나는

로 읽을 수있는 예외를 던질 필요에 대한 속성을 XML에서

parameter::parameter(boost::property_tree::ptree t) 
{ 
    // Set the value 
    value = t.get_value<double>(); 

    // ????? 
    auto nodename = t.something(); 

    // ????? 
    std::string nodepath = t.somethingelse(); 

    // Get the attributes (or empty) 
    auto p = t.get_child("<xmlattr>", boost::property_tree::ptree()); 

    // If we have attributes, read them 
    if (p != boost::property_tree::ptree()) 
    { 
     min = t.get<double>("<xmlattr>.min"); 
     max = t.get<double>("<xmlattr>.max"); 

     if (min > max) 
      throw std::runtime_error("Min and max values invalid for the parameter " + nodename + ", path: " + nodepath); 
    } 
    else 
    { 
     min = +1.0; 
     max = -1.0; 
    } 
} 

// ... Someplace else 
lambda = parameter(config.get_child("config.lambda")); 

물론 문자열을 전달할 수는 있지만 목적을 벗어날 수 있습니다. 나는 t의 이터레이터와 data을 엉망으로 만들려고했지만 아무 것도 얻지 못했다.

ptree에서 해당 값을 얻을 수 있습니까?

감사합니다. 당신이 정보를 삭제하지 않도록

답변

1

나는 약간 주위의 인터페이스를 셔플 거라고 성급하게 필요

Live On Coliru

#include <boost/property_tree/xml_parser.hpp> 

using boost::property_tree::ptree; 

struct parameter { 
    parameter(ptree const& tree, ptree::path_type const& nodepath) 
    { 
     ptree const& t = tree.get_child(nodepath); 
     // Set the value 
     value = t.get_value<double>(); 

     auto nodename = [nodepath] { 
      auto copy = nodepath; 
      while (!copy.single()) copy.reduce(); 
      return copy.reduce(); 
     }(); 

     // Get the attributes (or empty) 
     auto p = t.get_child("<xmlattr>", boost::property_tree::ptree()); 

     // If we have attributes, read them 
     if (p != boost::property_tree::ptree()) 
     { 
      auto min = t.get<double>("<xmlattr>.min"); 
      auto max = t.get<double>("<xmlattr>.max"); 

      if (min > max) 
       throw std::runtime_error("Min and max values invalid for the parameter " + nodename + ", path: " + nodepath.dump()); 
     } 
     else 
     { 
      min = +1.0; 
      max = -1.0; 
     } 
    } 

    private: 
    double min, max; 
    double value; 
}; 

int main() { 
    ptree config; 
    std::ifstream xml("input.txt"); 
    read_xml(xml, config); 

    auto lambda = parameter(config, "config.lambda"); 
} 

인쇄

terminate called after throwing an instance of 'std::runtime_error' 
    what(): Min and max values invalid for the parameter lambda, path: config.lambda 
+0

감사합니다, @ 예. 하지만 경로 매개 변수를 삭제할 수 있습니까? 노드가 주어진다면 루트에서 경로를 검색하고 싶습니다. – senseiwa

+0

혼란스러워. 내 대답은 무엇을 기대합니까? 제 질문을 읽을 수 있어요. (그래서, 그랬어.) 내가 할 일이 없기 때문에 나는 당신의 코드를 뒤적 거리지 않을 것이다 ... – sehe

+0

미안하지만, 나는 무례하다는 뜻이 아니었다. 방법이 있는지 묻고 있습니다. 코드가 완벽하게 작동하며 사용했습니다. 어쩌면 올바른 질문은 ptree의 경로가 항상 노드에 상대적인지 (루트가 아님) 아니면 절대 경로 (루트 - 노드 경로를 알고 있는지)인지 여부입니다. – senseiwa