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