Mbed OS Reference
Loading...
Searching...
No Matches
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
25namespace mbed {
26namespace nfc {
27namespace ndef {
28
29/**
30 * @addtogroup nfc
31 * @{
32 */
33
34/**
35 * Parse a record.
36 */
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
50protected:
51 /**
52 * Protected non virtual destructor.
53 * RecordParser subclasses are not meant to be destroyed as RecordParser's.
54 */
56
57private:
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 */
73template<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
122protected:
123 /**
124 * Protected non virtual destructor.
125 */
127
128private:
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 */
159
160private:
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.
virtual void on_record_parsed(const ParsingResult &object_parsed, const RecordID &id)=0
Called when a record has been parsed and converted into a value_type.
virtual bool parse(const Record &record)
~GenericRecordParser()
Protected non virtual destructor.
GenericRecordParser()
Construct a record parser.
void set_delegate(Delegate *delegate)
Set the delegate that processes record parser.
Represent a record.
Definition Record.h:149
RecordID id
ID of the record.
Definition Record.h:195
~RecordParserChain()
Destroy a parser chain.
RecordParserChain()
Construct a parser chain.
void set_next_parser(RecordParser *parser)
Add a parser at the end of the parser list.
bool parse(const Record &record)
Parse a record.
RecordParser()
Construct a record parser.
virtual bool parse(const Record &record)=0
Parse the record in input.
~RecordParser()
Protected non virtual destructor.