- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一个实例方法或 lambda 函数,或者相当于将其作为 select 函数参数传递给 scandir。有什么办法可以做到这一点吗?
我想要实现的关键是让选择函数(回调)看到调用它的类的每个实例的不同参数。为了使这个线程安全,或者只是不那么丑陋,我不能只将参数存储在全局变量中,这就是类实例的用途。
如果它与 c++11 中的 lambda 函数一起使用,它会是什么样子:
myclass:getFilesMatching(char startChar)
{
...
mParam = startChar;
auto lfunc = [this] (const struct dirent * dent) { return (*(dent->d_name) == mParam); };
mNumFiles = scandir((char *)fullDirPath, &mfileList, lfunc, NULL);
}
这将获取名称以指定字符开头的所有文件。我不在乎是否将局部变量或实例变量传递给函数。
我希望 scandir 本身是线程安全的。当然,我可以使用信号量或互斥体,但这真的有必要吗?
当然这只是select函数的一个简单例子。我实际上想做的事情更复杂。
最佳答案
我必须承认我之前对scandir
一无所知。或任何相关的 C 函数。但根据我通过阅读 <dirent.h>
上的文档了解到的它是一个辅助函数,包装了对较低级别 APIS 的多个调用。
在这种情况下,我更喜欢创建一个 C++ 包装器,使用类似 C++ 的 API 来实现相同的功能。
从 DIR
周围的包装开始键入以确保其被正确清理:
namespace cpp {
struct DIR {
::DIR * dir;
DIR(const std::string & path) : dir(opendir(path.c_str()))
{
if(dir==0) throw std::runtime_error("Unable to open path");
}
~DIR() { if(dir!=0) closedir(dir); }
operator ::DIR * () { return dir; }
};
}
scandir
现在可以像这样实现函数:
template<class Filter, class Compare>
std::vector<dirent> scandir(const std::string & path, const Filter& filter, const Compare& compare) {
cpp::DIR dir(path);
std::vector<dirent> res;
dirent entry, *entry_ptr = &entry;
while( ::readdir_r(dir, &entry, &entry_ptr)==0 ) {
if( entry_ptr==0 ) break;
if( filter(entry) ) res.push_back(entry);
}
std::sort(begin(res), end(res), compare);
return res;
}
并这样调用它:
std::string path = ...;
...
auto filter = [] (const dirent& entry) { return entry.d_name[0]!='.'; };
auto compare = [](const dirent & lhs, const dirent & rhs) {
return std::strcmp(lhs.d_name, rhs.d_name)<0;
};
auto entries = cpp::scandir(path, filter, compare);
使用readdir_r()
使上面的实现是线程安全的,尽管应该添加更多检查来报告它返回的任何错误。
注意:上面的代码需要包含以下 header :
#include <dirent.h>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <vector>
关于c++11 - 在 c++11 中,将 lambda 函数传递给 scandir 可能是不可能的。下一个最好的事情是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17241347/
我有一个包含两个项目的解决方案,每个项目都生成一个单独的 dll,供另一个 Windows 应用程序使用。这些项目中的每一个都有一个名为 MyActions 的类,只有一个这样的方法 项目 1
我有一个包含两个项目的解决方案,每个项目都生成一个单独的 dll,供另一个 Windows 应用程序使用。这些项目中的每一个都有一个名为 MyActions 的类,只有一个这样的方法 项目 1
所以我在 if 语句中有这段代码如下 if (!inTime || !moment(inTime).format('m') % 15 === 0) { doSomething(); } 传入的 inT
像往常一样,我想做的比我知道的还多:-D 这就是我正在做的事情......我正在写一篇简历。 但是在简介中,我想要一个“长简介”和一个“短简介”按钮。 长传记显然会显示整个传记,但短传记会捕获列表中的
我正在使用物质。 js创建一个二维场景。我在场景中对一个物体施加力,这个物体撞击其他物体,但最终所有物体都因摩擦和能量损失而停止移动。 我需要以某种方式检测场景中的所有物体何时停止移动。我发现这样
谁能快速浏览一下这段代码,让我知道哪里出错了。 在模糊事件中,.textok 类加载正常,但 .textbad 类加载不正常。 .textok { color:#0F0; background
我的情况是这样的:我有一个项目,它使用了一些生成的代码。在生成的代码中,几乎所有文件中都硬编码了某个 URI。 因此,在某些时候我得到了两个生成的代码库:一个针对开发,另一个针对暂存。 我想通过 Gr
这是一个严肃的问题(见我的评论)。 问题很简单:Java 所做的所有 SEO 不友好的事情有哪些会导致您的网站在主要搜索引擎中的排名不如应有的好? 最佳答案 有一个与 JSESSIONID 相关的 s
我正在使用 PHP。我想完成 jQuery AJAX 进程,(完成进程并数据返回主页后)。 然后执行下一个 jQuery 操作。关于如何做到这一点有什么想法吗? $.ajax({ url: "pa
在释放内存之前,我要从 CPU 缓存中逐出内存范围。理想情况下,我只想放弃这些缓存行而不将它们保存到内存中。因为没有人会使用这些值,无论谁再次获得该内存范围(在 malloc()/new/_mm_ma
我不喜欢 jackson 。 我想使用 ajax,但要使用 Google Gson。 所以我试图弄清楚如何实现我自己的 HttpMessageConverter 以将其与 @ResponseBody
我是一名优秀的程序员,十分优秀!