본문 바로가기

정보정보

firebreath 에서 정의되지 않은 object의 처리

 

 

Plugin을 쉽게 만드는 툴로 firebreath 를 들 수 있는데
make_method / registerProperty 를 사용해서 plugin 의 method,property를 쓸 수 있는 건
포함된 예제를 보고 파악 가능한데,

아래처럼 정의되지 않는 object의 처리는 구글링에서 찾기가 힘들어 블로그에 써봄.
///JS
function set_store_fruit_type()
{
     var APPLE = 1;
     var ORANGE = 2;
     var SUPAK = 3;
     var MELON = 4; 
     
     var data = new Object();
     data.fruit_type = APPLE;
     var result = plugin().store.set("store_fruit_type",data, handleVehicleData, handleError);
}

 

이런 경우 아래와 같이 처리하면 된다.

std::string Store::set(std::string method,const FB::variant& args ,FB::JSObjectPtr successCallback, FB::JSObjectPtr errorCallback)
{
      FB::JSAPIPtr ptr;
      if (args.get_type() == typeid(FB::JSObjectPtr))  //object 가 생성되어 온 타입인지를 확인.
      {
         ptr = args.cast<FB::JSObjectPtr>();
         FB::JSObjectPtr jso = FB::ptr_cast<FB::JSObject>(ptr);    //js의 object pointer 을 가져옴. 
         if (jso)
         {
            if (!jso->HasProperty("fruit_type"))    //"fruit_type" property 타입이 있는지 체크.있으면 true가됨.
               return "has property is false";        
          else
          {
               FB::variant val = jso->GetProperty("fruit_type"); 
             
//-->property 타입에서 "fruit_type"에 해당하는 값을 가져옴
    
                if  (val.is_of_type<std::string>()) {
                     return "value type is  string";
                }
                else if (val.is_of_type<FB::VariantMap>())
                {
                      return "value type is VariantMap";
                }
                 else if (val.is_of_type<FB::VariantList>())
                 {
                        return "value type is VariantList";
                 }  
                 else if (val.is_of_type<unsigned int>()
                 || val.is_of_type<unsigned short>()
                 || val.is_of_type<unsigned char>()
                 || val.is_of_type<unsigned long>()) 
                 {
                       return ""value type is int/short/char/long";
                 } 
                //위 js의 경우 float/double 타입이 넘어옴.
                 else if (val.is_of_type<double>() || val.is_of_type<float>()) 
                 {
                        return  val.convert_cast<std::string>();    
                        //<-  이런식으로 원하는 값으로 타입변환하여 plugin 내부에서 원하는 식으로 구현하면 됨

                  }  
                  else if (val.is_of_type<bool>()) 
                  {
                          return "value type is bool";
                   }
            } 
        else
        {
              return "SET error";
          }
      }
  }
}
};

 

반대로 plugin 에서 web으로 정의되지 않은 object 를 넘기는 법

          FB::VariantMap varmap;
          varmap["type"] ="store_fruit_type";
          varmap["detail"] = "apple";
          return varmap;

이렇게 쓰면 web스크립트에서
function checkfruit(data)
{
    if (data.type =="store_fruit_type" &&  data.detail=="apple" )
}
이런식으로 처리가능함.    

이런식으로 Support list를 넘기고 싶을땐

function get_Supported_FruitTypes_list()
{
          
     var supportedEventType = plugin().frut.getSupportedFruitTypes("Applestatus", false); 
     for (itr = 0; itr < supportedEventType.length; itr++) {
      alert(supportedEventType[itr]);
     }           
 }

쏘쓰

FB::VariantList Vehicle::getSupportedEventTypesList(const std::string method,const bool writable)
{
     std::vector<std::string> ret;

    //ret에 지원하는 함수 string push_back.
  return FB::make_variant_list(ret);
}
이렇게 하면 web에서 list 로 처리가능.

fire breath 홈피

http://www.firebreath.org/display/documentation/FireBreath+Home