OmniSciDB  04ee39c94c
RegexSupport.h
Go to the documentation of this file.
1 #ifndef REGEX_SUPPORT_H
2 #define REGEX_SUPPORT_H
3 
4 #include "boost/regex.hpp"
5 
6 template <typename COMMAND_LIST_TYPE,
7  typename CONTEXT_TYPE,
8  typename REGEX_PRESENT_LAMBDA>
9 bool on_valid_regex_present(COMMAND_LIST_TYPE const& command_list,
10  CONTEXT_TYPE& client_context,
11  REGEX_PRESENT_LAMBDA on_present_op,
12  std::ostream& output_stream = std::cout) {
13  try {
14  if (command_list.size() > 1) {
15  boost::regex filter_expression(command_list[1], boost::regex_constants::no_except);
16  if (filter_expression.status() == 0) { // On successful regex
17  on_present_op(filter_expression, client_context);
18  return true;
19  } else {
20  output_stream << "Malformed regular expression: " << command_list[1] << std::endl;
21  }
22  }
23  } catch (std::runtime_error& re) { // Handles cases outlined in boost regex bad
24  // expressions test case, to avoid blow ups
25  output_stream << "Invalid regular expression: " << command_list[1] << std::endl;
26  }
27 
28  return false;
29 }
30 
31 std::function<bool(std::string const&)> yield_default_filter_function(
32  boost::regex& filter_expression) {
33  return [&filter_expression](std::string const& input) -> bool {
34  boost::smatch results;
35  return !boost::regex_match(input, results, filter_expression);
36  };
37 }
38 
39 template <ThriftService SERVICE_TYPE,
40  typename CLIENT_CONTEXT_TYPE,
41  typename DETAIL_PROCESSOR>
42 void for_all_return_names(CLIENT_CONTEXT_TYPE& context,
43  DETAIL_PROCESSOR detail_processor) {
44  thrift_op<SERVICE_TYPE>(context, [&](CLIENT_CONTEXT_TYPE& lambda_context) {
45  for (auto name_val : lambda_context.names_return) {
46  detail_processor(name_val, lambda_context);
47  }
48  });
49 }
50 
51 template <ThriftService THRIFT_SERVICE, typename COMMAND_LIST_TYPE, typename CONTEXT_TYPE>
52 void returned_list_regex(COMMAND_LIST_TYPE const& command_list,
53  CONTEXT_TYPE& context,
54  std::ostream& output_stream = std::cout) {
55  // clang-format off
56  auto did_execute = on_valid_regex_present(command_list, context, [&output_stream](boost::regex& filter_expression, CONTEXT_TYPE& context_param) {
57  auto filter_function = yield_default_filter_function(filter_expression);
58  auto result_processor = [filter_function, &output_stream](std::string const& element_name, CONTEXT_TYPE& context_param) {
59  if (!filter_function(element_name)) {
60  output_stream << element_name << '\n';
61  }
62  };
63 
64  for_all_return_names<THRIFT_SERVICE>(context_param, result_processor);
65  });
66  // clang-format on
67 
68  if (!did_execute) { // Run classic mode instead
69  for_all_return_names<THRIFT_SERVICE>(
70  context,
71  [&output_stream](std::string const& element_name, CONTEXT_TYPE& context_param) {
72  output_stream << element_name << '\n';
73  });
74  }
75 
76  output_stream.flush();
77 }
78 
79 #endif
void returned_list_regex(COMMAND_LIST_TYPE const &command_list, CONTEXT_TYPE &context, std::ostream &output_stream=std::cout)
Definition: RegexSupport.h:52
bool on_valid_regex_present(COMMAND_LIST_TYPE const &command_list, CONTEXT_TYPE &client_context, REGEX_PRESENT_LAMBDA on_present_op, std::ostream &output_stream=std::cout)
Definition: RegexSupport.h:9
void for_all_return_names(CLIENT_CONTEXT_TYPE &context, DETAIL_PROCESSOR detail_processor)
Definition: RegexSupport.h:42
std::function< bool(std::string const &)> yield_default_filter_function(boost::regex &filter_expression)
Definition: RegexSupport.h:31
ThriftService
Definition: ThriftService.h:4