本文主要是介绍webkit源码解读-FileList,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
FileList是文件组件的表示形式,是一个包装了系列File对象的对象。里面通过vector保持多个文件对象。
class FileList : public ScriptWrappable, public RefCounted<FileList> {
public:// 需要用create创建该类对象static Ref<FileList> create(){ // adoptRef是RefPtr中的函数,实现管理该对象的引用问题。这些不影响理解。return adoptRef(*new FileList);}static Ref<FileList> create(Vector<RefPtr<File>>&& files){ // adoptRef需要的是引用类型,需要要加*,WTFMove,WTF是web template framework简称,move是c++ std中提供的东西。return adoptRef(*new FileList(WTFMove(files)));}// 返回文件长度,简单调用vector的size实现unsigned length() const { return m_files.size(); }// 返回某个文件对象,见cpp文件WEBCORE_EXPORT File* item(unsigned index) const;// 判断文件列表是否为空,简单调用vector对象的isEmptybool isEmpty() const { return m_files.isEmpty(); }// 返回所有文件的路径,见cpp文件Vector<String> paths() const;private:// 不需要显示调用构造函数创建该类对象FileList();FileList(Vector<RefPtr<File>>&& files)// 初始化m_files变量: m_files(WTFMove(files)){ }// FileLists can only be changed by their owners.friend class DataTransfer;friend class FileInputType;// 追加文件void append(RefPtr<File>&& file) { m_files.append(WTFMove(file)); }// 删除所有文件void clear() { m_files.clear(); }// 是一个类似数组的vector,里面是一系列RefPtr对象,RefPtr对象负责管理File对象的引用问题。Vector<RefPtr<File>> m_files;
};
FileList.cpp
namespace WebCore {FileList::FileList()
{
}
// 返回某个文件对象
File* FileList::item(unsigned index) const
{if (index >= m_files.size())return 0;return m_files[index].get();
}
// 返回所有文件的路径
Vector<String> FileList::paths() const
{Vector<String> paths;for (unsigned i = 0; i < m_files.size(); ++i)paths.append(m_files[i]->path());return paths;
}} // namespace WebCore
这篇关于webkit源码解读-FileList的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!