Wednesday 15 July 2015

In c++11 passing a lambda function to scandir is probably impossible. What's the next best thing? -


I need an example method or lambda function or it is equivalent to passing the function as argument for the scanner. Is there any way to do this?

The thing I am trying to achieve is that the selection function (callback) is to see different parameters for each example of the class, which is called it. To make this thread-safe, or not very bad, I can not just store my parameters in a global variable, this is what are examples of class.

Here it will appear how it will work in the Lambda function in c ++ 11:

  myclass: getFilesMatching (char startChar) {... mParam = startChar ; Auto lfunc = [this] (constant straight dirent * dent) {return (* (DOT-> d_name) == mParam); }; MNumFiles = Scandinum ((* four *) full path, and amp; mfileList, lfunc, NULL); }   

This will get all the files whose names start with the specified character. I do not care if I pass the function in the local variable or in the example variable.

I hope Scandivores are self-threaded. Of course I can use simafar or mute x, but is this really necessary?

Of course this is just a simple example of the selection ceremony. What I really want to do is more complex.

I have to admit that I have no previous knowledge scandiver Or any of the related C functions but & lt; Dirent.h & gt; But what do I understand by reading through the document, it is an auxiliary work that tightens many calls to lower level APIs.

In these cases I like to create a C ++ cover with a C + + API with C ++ wrapper.

To ensure that it has been cleaned properly, start with the wrap around DIR : / P>

  Namespace CPP {struct DIR {:: DIR * dir; DIR (const std :: string and path): dir (opendir (path.c_str ())} {if (dir == 0) std :: runtime_error ("unable to open path"); } ~ DIR () {if (dir! = 0) Monkey (DIR); } Operator :: DIR * () {return DIR; }}; }   

scandir function can now be implemented like this:

  template & lt; Class filter, class compare & gt; Std :: vector & lt; Dirent & gt; Scandir (CRD: String and Path, Const Filter & Filter, Compare CRS and Compare) {CPP: DIR Dire (Path); Std :: vector & lt; Dirent & gt; Race; Dirent entry, * entry_ptr = & amp; Entry; While (:: readdir_r (dir, & amp; Entry, & amp; Entry _PTR) == 0) {if (entry_ptr == 0) break; If (filter (entry)) res.push_back (entry); } Std :: sort (start (res), end, res); Return ridge; }   

And call it like this:

  std :: string path = ...; ... auto filter = [] (constraint and entry) {return entry.d_name [0]! = '.' ;; }; Auto Comparison = [] (Constant & amp; LHS, Constant & amp; Rhs) {return std :: strcmp (lhs.d_name, rhs.d_name) & lt; 0; }; Auto listings = CPP :: Scandiner (route, filter, comparison); The implementation of thread-safe is done using   

readdir_r () , however, to include additional checks to report any errors returned by it needed.

Note: The above headers are required to include the following code:

  #include & lt; Dirent.h & gt; # Include & lt; Cstring & gt; # Include & lt; Algorithm & gt; # Include & lt; Iostream & gt; # Include & lt; Stdexcept & gt; # Include & lt; Vector & gt;    

No comments:

Post a Comment