-
Notifications
You must be signed in to change notification settings - Fork 101
moved part of FileDataCache implementation into source file
#651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,12 @@ | |
| * Copyright (C) 2016-2023 simplecpp team | ||
| */ | ||
|
|
||
| // needs to be specified here otherwise _mingw.h will define it as 0x0601 | ||
| // causing FileIdInfo not to be available | ||
| #if defined(_WIN32) | ||
| # ifndef _WIN32_WINNT | ||
| # define _WIN32_WINNT 0x0602 | ||
| # endif | ||
| # ifndef NOMINMAX | ||
| # define NOMINMAX | ||
| # endif | ||
| # ifndef WIN32_LEAN_AND_MEAN | ||
| # define WIN32_LEAN_AND_MEAN | ||
| # endif | ||
| # include <windows.h> | ||
| # undef ERROR | ||
| #endif | ||
|
|
||
| #include "simplecpp.h" | ||
|
|
@@ -51,10 +45,19 @@ | |
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #ifdef _WIN32 | ||
| #if defined(_WIN32) | ||
| # ifndef NOMINMAX | ||
| # define NOMINMAX | ||
| # endif | ||
| # ifndef WIN32_LEAN_AND_MEAN | ||
| # define WIN32_LEAN_AND_MEAN | ||
| # endif | ||
| # include <windows.h> | ||
| # undef ERROR | ||
| # include <direct.h> | ||
| #else | ||
| # include <sys/stat.h> | ||
| # include <sys/types.h> | ||
| #endif | ||
|
|
||
| static bool isHex(const std::string &s) | ||
|
|
@@ -3075,6 +3078,65 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const | |
| return ""; | ||
| } | ||
|
|
||
| namespace { | ||
| struct FileID { | ||
| #ifdef _WIN32 | ||
| struct { | ||
| std::uint64_t VolumeSerialNumber; | ||
| struct { | ||
| std::uint64_t IdentifierHi; | ||
| std::uint64_t IdentifierLo; | ||
| } FileId; | ||
| } fileIdInfo; | ||
|
|
||
| bool operator==(const FileID &that) const noexcept { | ||
| return fileIdInfo.VolumeSerialNumber == that.fileIdInfo.VolumeSerialNumber && | ||
| fileIdInfo.FileId.IdentifierHi == that.fileIdInfo.FileId.IdentifierHi && | ||
| fileIdInfo.FileId.IdentifierLo == that.fileIdInfo.FileId.IdentifierLo; | ||
| } | ||
| #else | ||
| dev_t dev; | ||
| ino_t ino; | ||
|
|
||
| bool operator==(const FileID& that) const noexcept { | ||
| return dev == that.dev && ino == that.ino; | ||
| } | ||
| #endif | ||
| struct Hasher { | ||
| std::size_t operator()(const FileID &id) const { | ||
| #ifdef _WIN32 | ||
| return static_cast<std::size_t>(id.fileIdInfo.FileId.IdentifierHi ^ id.fileIdInfo.FileId.IdentifierLo ^ | ||
| id.fileIdInfo.VolumeSerialNumber); | ||
| #else | ||
| return static_cast<std::size_t>(id.dev) ^ static_cast<std::size_t>(id.ino); | ||
| #endif | ||
| } | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| struct simplecpp::FileDataCache::Impl | ||
| { | ||
| void clear() | ||
| { | ||
| mIdMap.clear(); | ||
| } | ||
|
|
||
| using id_map_type = std::unordered_map<FileID, FileData *, FileID::Hasher>; | ||
|
|
||
| id_map_type mIdMap; | ||
| }; | ||
|
|
||
| simplecpp::FileDataCache::FileDataCache() | ||
| : mImpl(new Impl) | ||
| {} | ||
|
|
||
| simplecpp::FileDataCache::~FileDataCache() = default; | ||
| simplecpp::FileDataCache::FileDataCache(FileDataCache &&) noexcept = default; | ||
| simplecpp::FileDataCache &simplecpp::FileDataCache::operator=(simplecpp::FileDataCache &&) noexcept = default; | ||
|
|
||
| static bool getFileId(const std::string &path, FileID &id); | ||
|
|
||
| std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDataCache::name_map_type::iterator &name_it, const simplecpp::DUI &dui, std::vector<std::string> &filenames, simplecpp::OutputList *outputList) | ||
| { | ||
| const std::string &path = name_it->first; | ||
|
|
@@ -3083,8 +3145,8 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat | |
| if (!getFileId(path, fileId)) | ||
| return {nullptr, false}; | ||
|
|
||
| const auto id_it = mIdMap.find(fileId); | ||
| if (id_it != mIdMap.end()) { | ||
| const auto id_it = mImpl->mIdMap.find(fileId); | ||
| if (id_it != mImpl->mIdMap.end()) { | ||
| name_it->second = id_it->second; | ||
| return {id_it->second, false}; | ||
| } | ||
|
|
@@ -3095,7 +3157,7 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat | |
| data->tokens.removeComments(); | ||
|
|
||
| name_it->second = data; | ||
| mIdMap.emplace(fileId, data); | ||
| mImpl->mIdMap.emplace(fileId, data); | ||
| mData.emplace_back(data); | ||
|
|
||
| return {data, true}; | ||
|
|
@@ -3147,7 +3209,14 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::get(const std:: | |
| return {nullptr, false}; | ||
| } | ||
|
|
||
| bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id) | ||
| void simplecpp::FileDataCache::clear() | ||
| { | ||
| mImpl->clear(); | ||
| mNameMap.clear(); | ||
| mData.clear(); | ||
| } | ||
|
|
||
| bool getFileId(const std::string &path, FileID &id) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it be
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. That should have been caught by the compiler or clang-tidy though.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am confused by your comments.. do you plan to add the static keyword or is there some reason to not add it?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is already |
||
| { | ||
| #ifdef _WIN32 | ||
| HANDLE hFile = CreateFileA(path.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah - I forward declared it so the code doesn't need to be moved.