Arduino KVComm  master
Key-Value pair communication library for Arduino
KV_Parser.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #if !defined(ARDUINO) || defined(DOXYGEN)
4 
5 #include <KVComm/KV_Types.hpp> // KV_Type
6 
7 #include <cstdint> // uint8_t, uint32_t
8 #include <map> // std::map
9 #include <stdexcept> // std::logic_error, std::out_of_range, std::length_error
10 #include <string> // std::string
11 #include <vector> // std::vector
12 
15 
30 class KV_Parser {
31  private:
33  struct strcmp {
34  bool operator()(const char *a, const char *b) const;
35  };
37  using map_t = std::map<const char *, KV, strcmp>;
38 
40 
41  public:
54  KV_Parser(const uint8_t *buffer, size_t length)
55  : parseResult{parse(buffer, length)} {}
56 
67  bool contains(const char *key) const {
68  return parseResult.find(key) != parseResult.end();
69  }
70 
80  KV getElement(const char *key) const { return parseResult.at(key); }
82  KV operator[](const char *key) const { return getElement(key); }
84  map_t::iterator begin() { return parseResult.begin(); }
86  map_t::const_iterator begin() const { return parseResult.begin(); }
88  map_t::iterator end() { return parseResult.end(); }
90  map_t::const_iterator end() const { return parseResult.end(); }
91 
92  private:
104  map_t parse(const uint8_t *buffer, size_t length);
105 };
106 
108 
109 #endif // ARDUINO
KV_Parser::end
map_t::const_iterator end() const
End iterator over all entries in the dictionary.
Definition: KV_Parser.hpp:90
KV_Parser::parseResult
map_t parseResult
Definition: KV_Parser.hpp:39
KV_Parser::operator[]
KV operator[](const char *key) const
Get the element with the given key.
Definition: KV_Parser.hpp:82
KV_Iterator::KV
Definition: KV_Iterator.hpp:47
KV_Parser::begin
map_t::const_iterator begin() const
Begin iterator over all entries in the dictionary.
Definition: KV_Parser.hpp:86
KV_Parser::parse
map_t parse(const uint8_t *buffer, size_t length)
Parse the buffer for key-value pairs, save their keys, type, and size in an std::map,...
Definition: KV_Parser.cpp:9
KV_Parser::strcmp
Functor to compare the map keys.
Definition: KV_Parser.hpp:33
KV_Parser::begin
map_t::iterator begin()
Begin iterator over all entries in the dictionary.
Definition: KV_Parser.hpp:84
KV_Parser::map_t
std::map< const char *, KV, strcmp > map_t
Definition: KV_Parser.hpp:37
KV_Types.hpp
KV_Type type definitions for fundamental types (int::_t, uint::_t, float, double, bool,...
KV_Parser::strcmp::operator()
bool operator()(const char *a, const char *b) const
Definition: KV_Parser.cpp:18
KV_Parser::contains
bool contains(const char *key) const
Check if the dictionary contains an element with the given key.
Definition: KV_Parser.hpp:67
KV_Parser
A parser for dictionaries generated by the KV_Builder.
Definition: KV_Parser.hpp:30
KV_Parser::KV_Parser
KV_Parser(const uint8_t *buffer, size_t length)
Parse a raw buffer into a new KV_Parser.
Definition: KV_Parser.hpp:54
KV_Parser::getElement
KV getElement(const char *key) const
Get the element with the given key.
Definition: KV_Parser.hpp:80
KV_Parser::end
map_t::iterator end()
End iterator over all entries in the dictionary.
Definition: KV_Parser.hpp:88