是否将枚举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.");
}
相关文章:
- 将内存分配返回值强制转换为 TYPE 数组
- C++ 嵌套类模板 错误 C2440 '=':无法从'type'转换为'same type'
- 为什么指针没有隐式转换为 <type> const*const
- 从整数到枚举的首选转换样式 QEvent::Type
- c++ C2440 错误无法从"const BWAPI::UpgradeType"转换为"const BWAPI::Type *"
- C++ 错误 C2662 无法将"this"指针从"const Type"转换为"Type &"
- 错误 C2440: 'type cast':无法从 'bool' 转换为 'CString'
- 无法将参数 1 从"cli::interior_ptr<Type>"转换为"CvCapture **"
- 将 MSVS 2010 项目转换为 MSVS 2012 RC,但出现错误"The C++ standard doesn't provide a hash for this type"
- 专用转换函数导致"ambiguous default type conversion"错误 (c++)
- vector::p ush_back 从 const Type* 转换为 type*
- 可以安全地使用静态强制转换,为每个实例使用唯一的虚拟 int type() 来提高性能
- 无法将参数 1 从"cli::array<Type> ^"转换为"无符号短整型"
- 无法从 Type* 转换为 Type&&
- 没有匹配的操作数[template]/无法从Type const*转换为Type
- C++ std::映射分配错误:无法在赋值中将' type **'转换为'type *'
- 错误 C2440 '=' : 无法从 'cli::array ^'<Type> 转换为 'wchar_t'
- 将Type**转换为void*,然后再返回到Type*
- 将 std::vector<const Type*> 转换为 const std::vector<T, A> &Vec
- 函数将变量Type转换为Type*,导致C++错误