簡體   English   中英

將枚舉 TagLib::ID3v2::AttachedPictureFrame::Type 轉換為字符串?

[英]Convert enum TagLib::ID3v2::AttachedPictureFrame::Type to string?

我正在使用 c++ 的 taglib。 如果我使用以下代碼:

ID3v2::AttachedPictureFrame::Type t = picFrame->type();

我得到例如“FrontCover (3)”,但我沒有找到任何將其轉換為字符串以保存值的解決方案。

我還需要一個解決方案,如何設置ID3v2::AttachedPictureFrame::Type t以將其寫入 APIC 標記。

TagLib::ID3v2::AttachedPictureFrame的文檔說Type是在該類中定義的枚舉:

 enum Type { Other = 0x00, FileIcon = 0x01, OtherFileIcon = 0x02, FrontCover = 0x03, BackCover = 0x04, LeafletPage = 0x05, Media = 0x06, LeadArtist = 0x07, Artist = 0x08, Conductor = 0x09, Band = 0x0A, Composer = 0x0B, Lyricist = 0x0C, RecordingLocation = 0x0D, DuringRecording = 0x0E, DuringPerformance = 0x0F, MovieScreenCapture = 0x10, ColouredFish = 0x11, Illustration = 0x12, BandLogo = 0x13, PublisherLogo = 0x14 }

沒有將enum符號轉換為std::string的自動轉換,反之亦然。 此外,您還注意到TagLib::ID3v2::AttachedPictureFrame類沒有提供此類功能。
您需要為此實現輔助函數:

std::string attachedPictureFrameType2String(AttachedPictureFrame::Type type) {
    switch(type) {
    case AttachedPictureFrame::Other: return "Other";
    case AttachedPictureFrame::FileIcon: return "FileIcon";
    case AttachedPictureFrame::OtherFileIcon: return "OtherFileIcon";
    case AttachedPictureFrame::FrontCover: return "FrontCover";
    // All the other enum values ...
    }
}

AttachedPictureFrame::Type string2AttachedPictureFrameType(const std::string& s) {
    if(s == "Other") return AttachedPictureFrame::Other;
    if(s == "FileIcon") return AttachedPictureFrame::FileIcon;
    if(s == "OtherFileIcon") return AttachedPictureFrame::OtherFileIcon;
    if(s == "FrontCover") return AttachedPictureFrame::FrontCover;
    // All the other valid string values ...
    throw std::exception("Invalid string");
}

正如@Uriya Harpeness在他們的評論中提到的,也可以使用字典來做到這一點,例如:

 using std::pair<AttachedPictureFrame::Type,std::string> = TypeDictItem;
 using std::vector<TypeDictItem> = TypeDict;
 const TypeDict pictureFrameTypeDict = { 
       std::make_pair(AttachedPictureFrame::Other,"Other")
     , std::make_pair(AttachedPictureFrame::FileIcon,"FileIcon")
     , std::make_pair(AttachedPictureFrame::OtherFileIcon,"OtherFileIcon")
     , std::make_pair(AttachedPictureFrame::FrontCover,"FrontCover")
     // ...
 };

 std::string attachedPictureFrameType2String(AttachedPictureFrame::Type type) {
     auto found = std::find_if(std::begin(pictureFrameTypeDict),
         std::end(pictureFrameTypeDict), 
         [type](const TypeDictItem& i) { return i.first == type; } );
     if(found != pictureFrameTypeDict.end()) {
          return (*found).second;
     }
     throw std::exception("Unexpected enum value.");
 }

 AttachedPictureFrame::Type string2AttachedPictureFrameType(const std::string s) {
     auto found = std::find_if(std::begin(pictureFrameTypeDict),
         std::end(pictureFrameTypeDict), 
         [s](const TypeDictItem& i) { return i.second == s; } );
     if(found != pictureFrameTypeDict.end()) {
          return (*found).first;
     }
     throw std::exception("Invalid string.");
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM