Mistake on this page?
Report an issue in GitHub or email us
RecordParser.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef NFC_NDEF_RECORDPARSER_H_
18 #define NFC_NDEF_RECORDPARSER_H_
19 
20 #include <stddef.h>
21 
22 #include "nfc/ndef/Record.h"
23 
24 namespace mbed {
25 namespace nfc {
26 namespace ndef {
27 
28 /**
29  * @addtogroup nfc
30  * @{
31  */
32 
33 /**
34  * Parse a record.
35  */
36 struct RecordParser {
37  /**
38  * Construct a record parser.
39  */
40  RecordParser() : _next_parser(NULL) { }
41 
42  /**
43  * Parse the record in input.
44  * @param record The NDEF record to parse.
45  * @return true if decoding has succeeded and false otherwise.
46  */
47  virtual bool parse(const Record &record) = 0;
48 
49 protected:
50  /**
51  * Protected non virtual destructor.
52  * RecordParser subclasses are not meant to be destroyed as RecordParser's.
53  */
55 
56 private:
57  friend class RecordParserChain;
58  RecordParser *_next_parser;
59 };
60 
61 
62 /**
63  * GenericRecordParser.
64  *
65  * @tparam ParserImplementation the implementation type of the parser.
66  * It must provides A decoding function named do_parse that accept a const
67  * reference to a record and a reference to the type parsed and return a boolean
68  * status that indicates the result of the parsing operation.
69  *
70  * @tparam ParsingResult Type produced by the parsing operation when successful.
71  */
72 template<typename ParserImplementation, typename ParsingResult>
74 
75  /**
76  * Handle that receives parsed values.
77  */
78  struct Delegate {
79  /**
80  * Called when a record has been parsed and converted into a value_type.
81  *
82  * @param object_parsed The record in its parsed form.
83  * @param id The RecordId associated with the object parsed.
84  */
85  virtual void on_record_parsed(const ParsingResult &object_parsed, const RecordID &id) = 0;
86 
87  protected:
88  ~Delegate() { }
89  };
90 
91  /**
92  * Construct a record parser.
93  */
94  GenericRecordParser() : _delegate(NULL) { }
95 
96  /**
97  * @see RecordParser::parse
98  */
99  virtual bool parse(const Record &record)
100  {
101  ParsingResult parsed_value;
102  if (static_cast<ParserImplementation *>(this)->do_parse(record, parsed_value)) {
103  if (_delegate) {
104  _delegate->on_record_parsed(parsed_value, record.id);
105  }
106  return true;
107  }
108  return false;
109  }
110 
111  /**
112  * Set the delegate that processes record parser.
113  *
114  * @param delegate The delegate to set.
115  */
116  void set_delegate(Delegate *delegate)
117  {
118  _delegate = delegate;
119  }
120 
121 protected:
122  /**
123  * Protected non virtual destructor.
124  */
126 
127 private:
128  Delegate *_delegate;
129 };
130 
131 
132 /**
133  * Record parser chain.
134  */
136  /**
137  * Construct a parser chain.
138  */
139  RecordParserChain() : _parsers(NULL) { }
140 
141  /**
142  * Destroy a parser chain.
143  */
145 
146  /**
147  * Parse a record.
148  * @param record The record to parse.
149  * @return true if the record has been parsed and false otherwise.
150  */
151  bool parse(const Record &record);
152 
153  /**
154  * Add a parser at the end of the parser list.
155  * @param parser The parser to add.
156  */
157  void set_next_parser(RecordParser *parser);
158 
159 private:
160  RecordParser *_parsers;
161 };
162 
163 /**
164  * @}
165  */
166 
167 } // namespace ndef
168 } // namespace nfc
169 } // namespace mbed
170 
171 
172 #endif /* NFC_NDEF_RECORDPARSER_H_ */
Handle that receives parsed values.
Definition: RecordParser.h:78
~RecordParser()
Protected non virtual destructor.
Definition: RecordParser.h:54
void set_delegate(Delegate *delegate)
Set the delegate that processes record parser.
Definition: RecordParser.h:116
virtual bool parse(const Record &record)=0
Parse the record in input.
Represent a record.
Definition: Record.h:148
RecordParserChain()
Construct a parser chain.
Definition: RecordParser.h:139
~RecordParserChain()
Destroy a parser chain.
Definition: RecordParser.h:144
RecordID id
ID of the record.
Definition: Record.h:194
GenericRecordParser()
Construct a record parser.
Definition: RecordParser.h:94
RecordParser()
Construct a record parser.
Definition: RecordParser.h:40
virtual bool parse(const Record &record)
Definition: RecordParser.h:99
~GenericRecordParser()
Protected non virtual destructor.
Definition: RecordParser.h:125
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.