libcex  1.0.0
Modern C++11 embedded webserver library
plist.hpp
Go to the documentation of this file.
1 //*************************************************************************
2 // File plist.hpp
3 // Date 14.05.2018 - #1
4 // Copyright (c) 2018-2018 by Patrick Fial
5 //-------------------------------------------------------------------------
6 // Class PropertyList
7 //*************************************************************************
8 
9 #ifndef __PLIST_HPP__
10 #define __PLIST_HPP__
11 
17 //***************************************************************************
18 // includes
19 //***************************************************************************
20 
21 #include <unordered_map>
22 #include <string>
23 
24 namespace cex
25 {
26 
27 //***************************************************************************
28 // class Property
29 //***************************************************************************
36 class Property
37 {
38  public:
39 
41  Property(std::string value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
43  Property(std::string&& value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
45  Property(long value) : longValue(value), doubleValue(0), ptrValue(0) {}
47  Property(double value) : longValue(0), doubleValue(value), ptrValue(0) {}
49  Property(void* value) : longValue(0), doubleValue(0), ptrValue(value) {}
50 
52  std::string& getStringValue() { return stringValue; }
54  long getLongValue() { return longValue; }
56  double getDoubleValue() { return doubleValue; }
58  template<typename T> T* getObjectValue() { return (T*)ptrValue; }
59 
60  private:
61 
62  std::string stringValue;
63  long longValue;
64  double doubleValue;
65  void* ptrValue;
66 };
67 
68 //***************************************************************************
69 // class PropertyList
70 //***************************************************************************
77 {
78  public:
79 
81  Property* getProperty(std::string key)
82  {
83  std::shared_ptr<Property> res= entries[key];
84  return res ? res.get() : nullptr;
85  }
86 
88  template<typename T>
89  T* getObject(std::string key)
90  {
91  std::shared_ptr<Property> res= entries[key];
92  return res ? res.get()->getObjectValue<T>() : nullptr;
93  }
94 
96  long getLong(std::string key)
97  {
98  std::shared_ptr<Property> res= entries[key];
99  return res ? res.get()->getLongValue() : 0;
100  }
101 
103  double getDouble(std::string key)
104  {
105  std::shared_ptr<Property> res= entries[key];
106  return res ? res.get()->getDoubleValue() : 0;
107  }
108 
110  std::string getString(std::string key)
111  {
112  std::shared_ptr<Property> res= entries[key];
113  return res ? res.get()->getStringValue() : std::string();
114  }
115 
117  void set(std::string key, std::string value) { entries[key]= std::make_shared<Property>(value); }
119  void set(std::string key, std::string&& value) { entries[key]= std::make_shared<Property>(value); }
121  void set(std::string key, long value) { entries[key]= std::make_shared<Property>(value); }
123  void set(std::string key, double value) { entries[key]= std::make_shared<Property>(value); }
125  void set(std::string key, void* value) { entries[key]= std::make_shared<Property>(value); }
126 
128  bool has(std::string key) { return entries.count(key) > 0; }
129 
131  size_t remove(std::string key) { return entries.erase(key); }
132 
133  private:
134 
135  std::unordered_map<std::string, std::shared_ptr<Property>> entries;
136 };
137 
138 //***************************************************************************
139 } // namespace cex
140 
141 #endif // __PLIST_HPP_
Property(void *value)
Constructs a new property with a void* value.
Definition: plist.hpp:49
Property(std::string &&value)
Constructs a new property with a string value (moving the value)
Definition: plist.hpp:43
Property * getProperty(std::string key)
Retrieves the Property object of a given key.
Definition: plist.hpp:81
bool has(std::string key)
Checks if the list contains a given key.
Definition: plist.hpp:128
long getLong(std::string key)
Retrieves the long value of a given key.
Definition: plist.hpp:96
Describes a single property containing one or more typed values.
Definition: plist.hpp:36
long getLongValue()
Retrieves the long value of the property. If no long value was set, returns 0.
Definition: plist.hpp:54
std::string & getStringValue()
Retrieves the string value of the property. If no string value was set, returns an empty string objec...
Definition: plist.hpp:52
Property(std::string value)
Constructs a new property with a string value.
Definition: plist.hpp:41
std::string getString(std::string key)
Retrieves the string value of a given key.
Definition: plist.hpp:110
void set(std::string key, long value)
Sets the value of a key to a long value. Replaces previous values of a key.
Definition: plist.hpp:121
size_t remove(std::string key)
Removes a key from the list and returns the number of elements removed (0 or 1)
Definition: plist.hpp:131
void set(std::string key, std::string value)
Sets the value of a key to a string value. Replaces previous values of a key.
Definition: plist.hpp:117
T * getObject(std::string key)
Retrieves the value of a given key as a class-pointer value (of type T)
Definition: plist.hpp:89
double getDoubleValue()
Retrieves the double value of the property. If no double value was set, returns 0.
Definition: plist.hpp:56
void set(std::string key, double value)
Sets the value of a key to a double value. Replaces previous values of a key.
Definition: plist.hpp:123
void set(std::string key, void *value)
Sets the value of a key to a void* value. Replaces previous values of a key.
Definition: plist.hpp:125
Property(long value)
Constructs a new property with a long value.
Definition: plist.hpp:45
void set(std::string key, std::string &&value)
Sets the value of a key to a string value (moving the content). Replaces previous values of a key.
Definition: plist.hpp:119
A simple list of properties implemented using std::unordered_map
Definition: plist.hpp:76
T * getObjectValue()
Retrieves the void* value casted to the template type. If no void* was set, returns a null-pointer.
Definition: plist.hpp:58
Property(double value)
Constructs a new property with a double value.
Definition: plist.hpp:47
double getDouble(std::string key)
Retrieves the double value of a given key.
Definition: plist.hpp:103