Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RecordParser.h Source File

RecordParser.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef NFC_NDEF_RECORDPARSER_H_
00018 #define NFC_NDEF_RECORDPARSER_H_
00019 
00020 #include <stddef.h>
00021 
00022 #include "nfc/ndef/Record.h"
00023 
00024 namespace mbed {
00025 namespace nfc {
00026 namespace ndef {
00027 
00028 /**
00029  * @addtogroup nfc
00030  * @{
00031  */
00032 
00033 /**
00034  * Parse a record.
00035  */
00036 struct RecordParser {
00037     /**
00038      * Construct a record parser.
00039      */
00040     RecordParser() : _next_parser(NULL) { }
00041 
00042     /**
00043      * Parse the record in input.
00044      * @param record The NDEF record to parse.
00045      * @return true if decoding has succeeded and false otherwise.
00046      */
00047     virtual bool parse(const Record &record) = 0;
00048 
00049 protected:
00050     /**
00051      * Protected non virtual destructor.
00052      * RecordParser subclasses are not meant to be destroyed as RecordParser's.
00053      */
00054     ~RecordParser() { }
00055 
00056 private:
00057     friend class RecordParserChain;
00058     RecordParser *_next_parser;
00059 };
00060 
00061 
00062 /**
00063  * GenericRecordParser.
00064  *
00065  * @tparam ParserImplementation the implementation type of the parser.
00066  * It must provides A decoding function named do_parse that accept a const
00067  * reference to a record and a reference to the type parsed and return a boolean
00068  * status that indicates the result of the parsing operation.
00069  *
00070  * @tparam ParsingResult Type produced by the parsing operation when successful.
00071  */
00072 template<typename ParserImplementation, typename ParsingResult>
00073 struct GenericRecordParser : public RecordParser {
00074 
00075     /**
00076      * Handle that receives parsed values.
00077      */
00078     struct Delegate {
00079         /**
00080          * Called when a record has been parsed and converted into a value_type.
00081          *
00082          * @param object_parsed The record in its parsed form.
00083          * @param id The RecordId associated with the object parsed.
00084          */
00085         virtual void on_record_parsed(const ParsingResult &object_parsed, const RecordID  &id) = 0;
00086 
00087     protected:
00088         ~Delegate() { }
00089     };
00090 
00091     /**
00092      * Construct a record parser.
00093      */
00094     GenericRecordParser() : _delegate(NULL) { }
00095 
00096     /**
00097      * @see RecordParser::parse
00098      */
00099     virtual bool parse (const Record &record)
00100     {
00101         ParsingResult parsed_value;
00102         if (static_cast<ParserImplementation *>(this)->do_parse(record, parsed_value)) {
00103             if (_delegate) {
00104                 _delegate->on_record_parsed(parsed_value, record.id);
00105             }
00106             return true;
00107         }
00108         return false;
00109     }
00110 
00111     /**
00112      * Set the delegate that processes record parser.
00113      *
00114      * @param delegate The delegate to set.
00115      */
00116     void set_delegate(Delegate *delegate)
00117     {
00118         _delegate = delegate;
00119     }
00120 
00121 protected:
00122     /**
00123      * Protected non virtual destructor.
00124      */
00125     ~GenericRecordParser() { }
00126 
00127 private:
00128     Delegate *_delegate;
00129 };
00130 
00131 
00132 /**
00133  * Record parser chain.
00134  */
00135 struct RecordParserChain {
00136     /**
00137      * Construct a parser chain.
00138      */
00139     RecordParserChain() : _parsers(NULL) { }
00140 
00141     /**
00142      * Destroy a parser chain.
00143      */
00144     ~RecordParserChain() { }
00145 
00146     /**
00147      * Parse a record.
00148      * @param record The record to parse.
00149      * @return true if the record has been parsed and false otherwise.
00150      */
00151     bool parse(const Record &record);
00152 
00153     /**
00154      * Add a parser at the end of the parser list.
00155      * @param parser The parser to add.
00156      */
00157     void set_next_parser(RecordParser *parser);
00158 
00159 private:
00160     RecordParser *_parsers;
00161 };
00162 
00163 /**
00164  * @}
00165  */
00166 
00167 } // namespace ndef
00168 } // namespace nfc
00169 } // namespace mbed
00170 
00171 
00172 #endif /* NFC_NDEF_RECORDPARSER_H_ */