jeudi 4 octobre 2018

composing multiple predicates for C++11 algorithms

I have read similar questions on stackoverflow such as this one. My particular use case seems a bit different: instead of applying all predicates all the time, I need to pick and combine them (&&, || ) in different ways, pending on user input: A best example I can think of is unix find, where a user can:

# find all files on condition:
# size more than 100K, modified 7 days ago, from user group abc 
find /path/to/somewhere -type f -size +100K -mtime +7 -group abc

Suppose I have the following predicates defined:

bool size_equal_to() { ... }
bool size_greater_to() { ... }
bool size_less_than() { ... }
bool type_is_file() { ... }
bool type_is_dir() { ... }
bool mtime_plus() { ... }

Composing such set of predicates in a lambda function to a container list (as suggested by the earlier question) is doable, but code structure is very messy. Any suggestions for better?

Aucun commentaire:

Enregistrer un commentaire