首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否将枚举TagLib::ID3v2::AttachedPictureFrame::Type转换为字符串?

是否将枚举TagLib::ID3v2::AttachedPictureFrame::Type转换为字符串?
EN

Stack Overflow用户
提问于 2020-09-29 03:18:30
回答 1查看 36关注 0票数 0

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

代码语言:javascript
复制
ID3v2::AttachedPictureFrame::Type t = picFrame->type();

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

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

EN

回答 1

Stack Overflow用户

发布于 2020-09-29 03:43:25

TagLib::ID3v2::AttachedPictureFrame的文档说明Type是该类中定义的枚举:

枚举类型{其他= 0x00,FileIcon = 0x01,OtherFileIcon = 0x02,FrontCover = 0x03,BackCover = 0x04,LeafletPage = 0x05,媒体= 0x06,LeadArtist = 0x07,艺术家= 0x08,指挥家= 0x09,乐队= 0x0A,作曲家= 0x0B,词作者= 0x0C,RecordingLocation = 0x0D,DuringRecording = 0x0E,DuringPerformance = 0x0F,MovieScreenCapture = 0x10,ColouredFish = 0x11,插图= 0x12,BandLogo = 0x13,PublisherLogo = 0x14 }

没有自动转换来将enum的符号转换为std::string,反之亦然。另外,正如您注意到的,TagLib::ID3v2::AttachedPictureFrame类没有提供这样的功能。

为此,您需要实现助手函数:

代码语言:javascript
复制
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中提到的,也可以使用字典来做到这一点,例如:

代码语言:javascript
复制
 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.");
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64108305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档