2016-08-04 2 views
0

C++ 코드로 변환해야하는 다음 C# 코드가 있습니다. 나는 C++ Enum을 Attributes와 함께 수행하는 방법에 대해 조금은 찾아 보았지만 알아낼 수는 없다.C++과 같은 속성을 가진 enums C#

기본적으로 나는 C++에서 다음과 같은 단순화 된 C# 코드를 나타내는 방법이 필요합니다.이 코드는 특성이있는 열거 형을 사용합니다.

C# 코드 :

public class PSMNameAttribute : Attribute 
     { 
      public string PSMName; 
      public PSMNameAttribute(string _PSMName) { PSMName = _PSMName; } 
     } 

public class PSMNumberAttribute : Attribute 
     { 
      public string PSMNumber; 
      public PSMNumberAttribute(string _PSMNumber) { PSMNumber = _PSMNumber; } 
     } 

public class PSMNumberNameAttribute : Attribute 
     { 
      public string PSMNumberName; 
      public PSMNumberNameAttribute(string _PSMNumberName) { PSMNumberName = _PSMNumberName; } 
     } 


public enum ShippingMethodsTypes 
     { 
      [PSMName("ErrorScriptMed")] 
      [PSMNumber("-5")] 
      [PSMNumberName("-5 ErrorScriptMed")] 
      ErrorScriptMed = -5     
      , 
      [PSMName("SpecialHandling")] 
      [PSMNumber("-1")] 
      [PSMNumberName("-1 SpecialHandling")] 
      SpecialHandling = -1     
      , 
      [PSMName("Error")] 
      [PSMNumber("0")] 
      [PSMNumberName("0 Error")] 
      Error = 0       
     } 

이이 같은 수행 할 수 없습니다 :

enum Category{ 
    unknown = -1, meat, poultry, seafood, dairy, vegetable,fruit, grain, sweet 
}; 

typedef struct { 
    float calories; // calories 
    float carbonhydrates; // grams 
    float fat; // grams 
    float cholesterol; // grams 
    float sodium; // grams 
    float protein; // grams 
    Category category ; 
}Food; 

내가 열거를 사용하여 구조체 값을 호출 얼마나 그렇다면

?

+0

"열거를 통해이 속성을 검색 할 수 있습니다 : 그러나 나는 당신이 문자열 배열/벡터 /지도 나처럼 열거하는 사람들의 조합을 유지함으로써이 문제를 해결 수 있다고 생각 애트리뷰트 "는 C++에서 아무 것도 의미하지 않으므로 원하는 것을 설명해야합니다. –

+0

@PeteBecker 귀하의 repsonse 주셔서 감사합니다. C++에서 열거 형을 계속 사용하여 Error = 0 등의 기본 사항을 나타내지 만 열거 형 오류의 경우 해당 속성에서 액세스 할 수있는 특성을 추가로 배치해야합니다. 기본적으로 Error.PSNNumberName = "0 Error" – bing281

답변

1

boost::variant 몇 방문자 멋지게 그것을 해결해야

#include <boost/variant.hpp> 
#include <iostream> 

struct get_number : boost::static_visitor<int> { 
    template<class Method> int operator()(const Method& m) const { return number(m); } 
}; 

struct get_name : boost::static_visitor<std::string> { 
    template<class Method> const std::string operator()(const Method& m) const { return name(m); } 
}; 

struct ShippingMethodMed {}; 
static constexpr int number(ShippingMethodMed) { return -5; } 
static std::string name(ShippingMethodMed) { return "ErrorScriptMedMed"; } 

struct ShippingMethodSpecialHandling { }; 
static constexpr int number(ShippingMethodSpecialHandling) { return -10; } 
static std::string name(ShippingMethodSpecialHandling) { return "SpecialHandling"; } 

struct ShippingMethodError {}; 
static constexpr int number(ShippingMethodError) { return 0; } 
static std::string name(ShippingMethodError) { return "Error"; } 

using ShippingMethod = boost::variant<ShippingMethodMed, ShippingMethodSpecialHandling, ShippingMethodError>; 

int number(ShippingMethod const& sm) { 
    return boost::apply_visitor(get_number(), sm); 
} 

std::string name(ShippingMethod const& sm) { 
    return boost::apply_visitor(get_name(), sm); 
} 

std::string number_name(ShippingMethod const& sm) { 
    return std::to_string(number(sm)) + " " + name(sm); 
} 


int main() 
{ 
    ShippingMethod m = ShippingMethodError(); 

    std::cout << number(m) << std::endl; 
    std::cout << name(m) << std::endl; 
    std::cout << number_name(m) << std::endl; 

    m = ShippingMethodSpecialHandling(); 

    std::cout << number(m) << std::endl; 
    std::cout << name(m) << std::endl; 
    std::cout << number_name(m) << std::endl; 
} 
+0

나는 당신이 가고있는 곳을 좋아하지만 불행히도 이것은 다른 많은 프로그램들에 의해 호출되는 DLL에 있기 때문에 열거 형에서 벗어날 수 없다. enum을 유지하면서 enum에 속성을 추가해야합니다. – bing281

1

이 혼자 열거 형과 수 없습니다.

enum test 
{ 
    first = 0, 
    second, // = 1 
    // etc... 
}; 

const char* attributes[] = 
{ 
    "first attribute", 
    "second attribute", 
}; 

은 다음과

const char* firstattribute = attributes[test::first]; 
+0

나는이 일이 가능할 것이라고 믿는다. 나는 당신의 대답과 도움을 주셔서 감사합니다 :) – bing281