1
I 사용자가 Json::Value에서 TT에서 Json::Value에 변환을 정의 할 템플릿 전문를 작성할 수 JsonCpp를 래핑 a library 만드는거야

. 그것은 작동하지만 전문화 구문은 매우 우아하지 않으므로 반복하지 않아도됩니다. 내가 알않도록 반복

namespace ssvuj // my library's namespace 
{ 
    namespace Internal 
    { 
     template<> struct FromJson<sf::Color> 
     { 
      inline static sf::Color conv(const Obj& mObj) 
      { 
       return sf::Color(as<float>(mObj, 0), as<float>(mObj, 1), as<float>(mObj, 2), as<float>(mObj, 3)); 
      } 
     }; 

     template<> struct ToJson<sf::Color> 
     { 
      inline static Obj conv(const sf::Color& mValue) 
      { 
       Obj result; 
       set(result, 0, mValue.r); 
       set(result, 1, mValue.g); 
       set(result, 2, mValue.b); 
       set(result, 3, mValue.a); 
       return result; 
      } 
     }; 
    } 
} 

// example usage 
ssvuj::Obj objColor; // this Json object contains sf::Color data 
ssvuj::Obj objEmpty; // this Json object is empty 

sf::Color colorFromObj{ssvuj::as<sf::Color>(objColor)}; // color is initialized by "deserializing" the Json object 
ssvuj::set(objEmpty, colorFromObj); // the color is "serialized" into the empty Json object 

문제 : 다음

는 현재 변환을 정의 할 수있는 방법

이 경우
  • 유형의 반복, sf::Color
  • 구조체 전문화 사용의 필요성 static void (전문 기능을 시도했지만 T = std::vector<T>과 같은 부분 특수화에는 작동하지 않음)

이 덜 장황하고 더 우아하게 만들 수있는 유일한 방법은 매크로이지만, 전처리기를 사용하지 않고도 할 수있는 일이있을 것입니다. 아이디어?

+1

왜 변환이 아닌'fromJson'와'toJson' 기능을 제공하는 하나의 유형에 대한 두 가지 유형시겠습니까? 또는 지원하려는 유형에 대해 전문화가 아닌 오버로드를 사용할 수 있습니다 ... ADL을 동시에 활성화하면 사용자 코드가 훨씬 더 간단 해집니다. –

+0

struct가 'static void'함수를 사용하여 어디에 있습니까? – Walter

+0

@ DavidRodríguez-dribeas : 당신 말이 맞아요. 단일 변환기 ''구조체로는 훨씬 더 좋을 것 같습니다. 나는 그것을 시험해 볼 것이고, 내가 만족 스럽다면 당신에게 알려 주어 내가 받아 들일 수있는 대답을 게시 할 수있게 할 것이다. –

답변

0

, 사용자가 전문 수있다.

예 :

template<> struct Converter<sf::Color> 
{ 
    using T = sf::Color; 
    inline static void fromObj(T& mValue, const Obj& mObj) 
    { 
     mValue.r = as<float>(mObj, 0); 
     mValue.g = as<float>(mObj, 1); 
     mValue.b = as<float>(mObj, 2); 
     mValue.a = as<float>(mObj, 3); 
    } 
    inline static void toObj(Obj& mObj, const T& mValue) 
    { 
     set(mObj, 0, mValue.r); 
     set(mObj, 1, mValue.g); 
     set(mObj, 2, mValue.b); 
     set(mObj, 3, mValue.a); 
    } 
}; 
3

ToJson 방향의 경우 모든 템플릿이 필요하지 않습니다 - 그것은 입력 종류에 무료 함수를 오버로드 할 수있는 충분한입니다 : 내 솔루션은 template<typename T> class Converter;을 구현했다

inline static Obj conv(const sf::Color& mValue) 
{ 
    Obj result; 
    set(result, 0, mValue.r); 
    set(result, 1, mValue.g); 
    set(result, 2, mValue.b); 
    set(result, 3, mValue.a); 
    return result; 
} 
+0

명백하게 전문화되지 않은 경우 "대체"케이스가 있기 때문에 작동하지 않을 것입니다. (https://github.com/SuperV1234/SSVUtilsJson/blob/master/include/SSVUtilsJson/Utils /UtilsJson.h#L39). 나 맞아? –

+0

'T'가 암시 적으로'Obj'로 변환 될 수있는 경우에만 작동합니다. 맞습니까? 따라서 fallback 오버로드를 작성하거나 Obj conv (Obj const & o) {return o; }', 또는 자유 함수 템플릿을 작성하십시오 : 명시 적으로 형식화 된 오버로드보다 항상 일치해야합니다. – Useless

+0

아니요,'Obj '에'T'를 지정할 수 있으면 작동합니다. 어쨌든, 당신의 솔루션을 시도했지만 작동하지 않습니다 - "더 구체적인"과부하가 존재하더라도, 그것은 fallback'template '과부하를 맞추려고 시도합니다. –