drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
HttpViewData.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <drogon/exports.h>
18#include <trantor/utils/Logger.h>
19#include <trantor/utils/MsgBuffer.h>
20#include <sstream>
21#include <string>
22#include <unordered_map>
23#include <stdarg.h>
24#include <stdio.h>
25#include <type_traits>
26#include <any>
27#include <string_view>
28
29namespace drogon
30{
32class DROGON_EXPORT HttpViewData
33{
34 public:
37 template <typename T>
38 const T &get(const std::string &key) const
39 {
40 static const T nullVal = T();
41 auto it = viewData_.find(key);
42 if (it != viewData_.end())
43 {
44 if (typeid(T) == it->second.type())
45 {
46 return *(std::any_cast<T>(&(it->second)));
47 }
48 else
49 {
50 LOG_ERROR << "Bad type";
51 }
52 }
53 return nullVal;
54 }
55
57 void insert(const std::string &key, std::any &&obj)
58 {
59 viewData_[key] = std::move(obj);
60 }
61
62 void insert(const std::string &key, const std::any &obj)
63 {
64 viewData_[key] = obj;
65 }
66
69 template <typename T>
70 void insertAsString(const std::string &key, T &&val)
71 {
72 std::stringstream ss;
73 ss << val;
74 viewData_[key] = ss.str();
75 }
76
78 void insertFormattedString(const std::string &key, const char *format, ...)
79 {
80 std::string strBuffer;
81 strBuffer.resize(128);
82 va_list ap, backup_ap;
83 va_start(ap, format);
84 va_copy(backup_ap, ap);
85 auto result = vsnprintf((char *)strBuffer.data(),
86 strBuffer.size(),
87 format,
88 backup_ap);
89 va_end(backup_ap);
90 if ((result >= 0) &&
91 (static_cast<std::string::size_type>(result) < strBuffer.size()))
92 {
93 strBuffer.resize(static_cast<std::string::size_type>(result));
94 }
95 else
96 {
97 while (true)
98 {
99 if (result < 0)
100 {
101 // Older snprintf() behavior. Just try doubling the buffer
102 // size
103 strBuffer.resize(strBuffer.size() * 2);
104 }
105 else
106 {
107 strBuffer.resize(result + 1);
108 }
109
110 va_copy(backup_ap, ap);
111 result = vsnprintf((char *)strBuffer.data(),
112 strBuffer.size(),
113 format,
114 backup_ap);
115 va_end(backup_ap);
116
117 if ((result >= 0) &&
118 ((std::string::size_type)result < strBuffer.size()))
119 {
120 strBuffer.resize(result);
121 break;
122 }
123 }
124 }
125 va_end(ap);
126 viewData_[key] = std::move(strBuffer);
127 }
128
130 std::any &operator[](const std::string &key) const
131 {
132 return viewData_[key];
133 }
134
136
145 static std::string htmlTranslate(const char *str, size_t length);
146
147 static std::string htmlTranslate(const std::string_view &str)
148 {
149 return htmlTranslate(str.data(), str.length());
150 }
151
152 static bool needTranslation(const std::string_view &str)
153 {
154 for (auto const &c : str)
155 {
156 switch (c)
157 {
158 case '"':
159 case '&':
160 case '<':
161 case '>':
162 return true;
163 default:
164 continue;
165 }
166 }
167 return false;
168 }
169
170 protected:
171 using ViewDataMap = std::unordered_map<std::string, std::any>;
172 mutable ViewDataMap viewData_;
173};
174
175} // namespace drogon
This class represents the data set displayed in views.
Definition HttpViewData.h:33
const T & get(const std::string &key) const
Definition HttpViewData.h:38
std::any & operator[](const std::string &key) const
Get the 'any' object by the key parameter.
Definition HttpViewData.h:130
void insertAsString(const std::string &key, T &&val)
Definition HttpViewData.h:70
static std::string htmlTranslate(const char *str, size_t length)
Translate some special characters to HTML format.
void insert(const std::string &key, std::any &&obj)
Insert an item identified by the key parameter into the data set;.
Definition HttpViewData.h:57
void insertFormattedString(const std::string &key, const char *format,...)
Insert a formatted string identified by the key parameter.
Definition HttpViewData.h:78
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23