All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
ndn-regex-matcher-base.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_NDN_REGEX_MATCHER_BASE_HPP
24 #define NDN_NDN_REGEX_MATCHER_BASE_HPP
25 
26 // Depending if ndn-cpp-config.h defines NDN_CPP_HAVE_BOOST_REGEX or sets
27 // NDN_CPP_HAVE_STD_REGEX = 1, define the regex_lib namespace alias.
28 // Set NDN_CPP_HAVE_REGEX_LIB = 1 if regex_lib is defined.
29 
30 #include <ndn-cpp/ndn-cpp-config.h>
31 #if NDN_CPP_HAVE_STD_REGEX
32  #include <regex>
33  namespace ndn { namespace regex_lib = std; }
34  #define NDN_CPP_HAVE_REGEX_LIB 1
35 
36  // Only Boost needs the correction.
37  static const size_t NDN_REGEXP_MARK_COUNT_CORRECTION = 0;
38 #elif defined(NDN_CPP_HAVE_BOOST_REGEX)
39  #include <boost/regex.hpp>
40  #include <boost/version.hpp>
41  namespace ndn { namespace regex_lib = boost; }
42  #define NDN_CPP_HAVE_REGEX_LIB 1
43 
44  // Re: http://www.boost.org/users/history/version_1_56_0.html
45  // Correct the behavior of basic_regex<>::mark_count() to match existing
46  // documentation, basic_regex<>::subexpression(n) changed to match, see
47  // https://svn.boost.org/trac/boost/ticket/9227
48  static const size_t NDN_REGEXP_MARK_COUNT_CORRECTION =
49  #if BOOST_VERSION < 105600
50  1;
51  #else
52  0;
53  #endif
54 #else
55  #define NDN_CPP_HAVE_REGEX_LIB 0
56 #endif
57 
58 // Only compile if we set NDN_CPP_HAVE_REGEX_LIB.
59 #if NDN_CPP_HAVE_REGEX_LIB
60 
61 #include <string>
62 #include <ndn-cpp/name.hpp>
63 
64 namespace ndn {
65 
66 class NdnRegexBackrefManager;
67 
68 class NdnRegexMatcherBase {
69 public:
74  class Error : public std::exception {
75  public:
76  Error(const std::string& errorMessage) throw();
77 
78  virtual ~Error() throw();
79 
80  std::string
81  Msg() const { return errorMessage_; }
82 
83  virtual const char*
84  what() const throw();
85 
86  private:
87  const std::string errorMessage_;
88  };
89 
90  enum NdnRegexExprType {
91  NDN_REGEX_EXPR_TOP,
92  NDN_REGEX_EXPR_PATTERN_LIST,
93  NDN_REGEX_EXPR_REPEAT_PATTERN,
94  NDN_REGEX_EXPR_BACKREF,
95  NDN_REGEX_EXPR_COMPONENT_SET,
96  NDN_REGEX_EXPR_COMPONENT,
97  NDN_REGEX_EXPR_PSEUDO
98  };
99 
100  NdnRegexMatcherBase
101  (const std::string& expr, const NdnRegexExprType& type,
102  ptr_lib::shared_ptr<NdnRegexBackrefManager> backrefManager =
103  ptr_lib::shared_ptr<NdnRegexBackrefManager>());
104 
105  virtual
106  ~NdnRegexMatcherBase();
107 
108  virtual bool
109  match(const Name& name, size_t offset, size_t len);
110 
115  const std::vector<Name::Component>&
116  getMatchResult() const { return matchResult_; }
117 
118  const std::string&
119  getExpr() const { return expr_; }
120 
121 protected:
125  virtual void
126  compile() = 0;
127 
128 private:
129  bool
130  recursiveMatch(size_t matcherNo, const Name& name, size_t offset, size_t len);
131 
132 protected:
133  const std::string expr_;
134  const NdnRegexExprType type_;
135  ptr_lib::shared_ptr<NdnRegexBackrefManager> backrefManager_;
136  std::vector<ptr_lib::shared_ptr<NdnRegexMatcherBase> > matchers_;
137  std::vector<Name::Component> matchResult_;
138 };
139 
140 inline std::ostream&
141 operator<<(std::ostream& os, const NdnRegexMatcherBase& regex)
142 {
143  os << regex.getExpr();
144  return os;
145 }
146 
147 }
148 
149 #endif // NDN_CPP_HAVE_REGEX_LIB
150 
151 #endif