【问题标题】:Convert enum TagLib::ID3v2::AttachedPictureFrame::Type to string?将枚举 TagLib::ID3v2::AttachedPictureFrame::Type 转换为字符串?
【发布时间】:2020-09-28 19:18:29
【问题描述】:

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

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

我得到例如“FrontCover (3)”,但我没有找到任何解决方案将其转换为字符串以保存值。

我还需要一个解决方案,如何设置ID3v2::AttachedPictureFrame::Type t 将其写入 APIC 标签。

【问题讨论】:

    标签: c++ taglib


    【解决方案1】:

    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
    }
    

    没有将enums 符号转换为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 在他们的comment 中提到的,也可以使用字典来做到这一点,例如:

     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.");
     }
    

    【讨论】:

    • 您可以创建一个字典来将值映射到字符串表示形式,这样您就可以定义一次并用于两个方向。
    • @UriyaHarpeness 是的,这是另一种方式。您为什么不写另一个答案来演示该技术?
    • 对不起,通过我的电话接听...但我已经使用了几次 dict 来双向翻译对象和字符串。
    • @UriyaHarpeness 我已经采纳了您的想法并将其添加到我的答案中。请注意,不使用 std::map 是有意的,因为可以避免不必要的开销。
    • 谢谢,我搞定了。但我不明白为什么它这么复杂。在我看来,如果可以只获取类型信息的数值,那会容易得多。
    猜你喜欢
    • 2010-10-03
    • 1970-01-01
    • 2014-03-18
    • 2015-06-10
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多