comet-cpp 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/comet-html +69 -0
- data/bin/comet-make +53 -0
- data/bin/comet-new +62 -0
- data/bin/comet-web +14 -0
- data/lib/comet-html/generator.rb +220 -0
- data/lib/comet-html/header-generator.rb +145 -0
- data/lib/comet-html/parser-binding.rb +42 -0
- data/lib/comet-html/parser-class.rb +168 -0
- data/lib/comet-html/parser-context.rb +61 -0
- data/lib/comet-html/parser-reference.rb +98 -0
- data/lib/comet-html/parser-repeater.rb +58 -0
- data/lib/comet-html/parser-slot.rb +108 -0
- data/lib/comet-html/source-generator.rb +285 -0
- data/lib/comet-html/utils.rb +88 -0
- data/lib/guard/comet-html.rb +32 -0
- data/lib/guard/comet.rb +36 -0
- data/vendor/project/Gemfile +4 -0
- data/vendor/project/Guardfile +5 -0
- data/vendor/project/app/application.hpp +29 -0
- data/vendor/project/app/collections/.gitkeep +0 -0
- data/vendor/project/app/controllers/.gitkeep +0 -0
- data/vendor/project/app/main.cpp +6 -0
- data/vendor/project/app/models/.gitkeep +0 -0
- data/vendor/project/app/routes.cpp +7 -0
- data/vendor/project/app/views/layouts/.gitkeep +0 -0
- data/vendor/project/public/index.html +7 -0
- data/vendor/src/anchorable_element.hpp +79 -0
- data/vendor/src/append_semantics.hpp +73 -0
- data/vendor/src/bindable.cpp +99 -0
- data/vendor/src/bindable.hpp +106 -0
- data/vendor/src/cheerp_parse_cookie_values.cpp +58 -0
- data/vendor/src/comment_element.cpp +11 -0
- data/vendor/src/comment_element.hpp +17 -0
- data/vendor/src/cookies.cpp +94 -0
- data/vendor/src/cookies.hpp +60 -0
- data/vendor/src/custom_element.hpp +61 -0
- data/vendor/src/datatree.cpp +198 -0
- data/vendor/src/datatree.hpp +233 -0
- data/vendor/src/document.cpp +62 -0
- data/vendor/src/document.hpp +31 -0
- data/vendor/src/element.cpp +358 -0
- data/vendor/src/element.hpp +138 -0
- data/vendor/src/events.hpp +76 -0
- data/vendor/src/exception.cpp +13 -0
- data/vendor/src/exception.hpp +11 -0
- data/vendor/src/from_string.cpp +99 -0
- data/vendor/src/from_string.hpp +37 -0
- data/vendor/src/globals.cpp +6 -0
- data/vendor/src/globals.hpp +15 -0
- data/vendor/src/http.cpp +93 -0
- data/vendor/src/http.hpp +72 -0
- data/vendor/src/lexical_cast.hpp +51 -0
- data/vendor/src/local_storage.cpp +75 -0
- data/vendor/src/local_storage.hpp +53 -0
- data/vendor/src/mvc/collection.hpp +154 -0
- data/vendor/src/mvc/controller.hpp +59 -0
- data/vendor/src/mvc/id_type.hpp +9 -0
- data/vendor/src/mvc/layout.hpp +44 -0
- data/vendor/src/mvc/model.cpp +89 -0
- data/vendor/src/mvc/model.hpp +50 -0
- data/vendor/src/object.cpp +71 -0
- data/vendor/src/object.hpp +298 -0
- data/vendor/src/parse_cookie_values.hpp +12 -0
- data/vendor/src/promise.cpp +50 -0
- data/vendor/src/promise.hpp +43 -0
- data/vendor/src/repeater.hpp +116 -0
- data/vendor/src/router.cpp +62 -0
- data/vendor/src/router.hpp +34 -0
- data/vendor/src/router_base.hpp +107 -0
- data/vendor/src/signal.hpp +150 -0
- data/vendor/src/slot_element.hpp +61 -0
- data/vendor/src/url.cpp +19 -0
- data/vendor/src/url.hpp +15 -0
- data/vendor/src/window.cpp +22 -0
- data/vendor/src/window.hpp +24 -0
- metadata +134 -0
@@ -0,0 +1,138 @@
|
|
1
|
+
#ifndef COMET_ELEMENT_HPP
|
2
|
+
# define COMET_ELEMENT_HPP
|
3
|
+
|
4
|
+
# include <cheerp/client.h>
|
5
|
+
# include <cheerp/clientlib.h>
|
6
|
+
# include <map>
|
7
|
+
# include "object.hpp"
|
8
|
+
# include "events.hpp"
|
9
|
+
# include "append_semantics.hpp"
|
10
|
+
|
11
|
+
namespace Comet
|
12
|
+
{
|
13
|
+
class Element : public ObjectImpl<client::HTMLElement>
|
14
|
+
{
|
15
|
+
public:
|
16
|
+
std::shared_ptr<JavascriptEvents> events;
|
17
|
+
|
18
|
+
Element();
|
19
|
+
Element(const client::String& str, const std::map<std::string, std::string>& attrs = {});
|
20
|
+
Element(client::HTMLElement*);
|
21
|
+
|
22
|
+
Element& attr(const std::map<std::string, std::string>& attrs);
|
23
|
+
Element& attr(const std::map<std::string, std::wstring>& attrs);
|
24
|
+
Element& attr(const std::map<std::string, client::String*>& attrs);
|
25
|
+
Element& attr(const std::string& name, const std::string& value);
|
26
|
+
std::string attr(const std::string& name) const;
|
27
|
+
|
28
|
+
Element& css(const std::string& name, const std::string& value);
|
29
|
+
Element& css(const std::map<std::string, std::string>& attrs);
|
30
|
+
std::map<std::string, std::string> css() const;
|
31
|
+
|
32
|
+
Element& html(client::String* content) { (*this)->set_innerHTML(content); return *this; }
|
33
|
+
Element& html(const std::string& content) { _html<std::string>(content); return *this; }
|
34
|
+
Element& html(const std::wstring& content) { _html<std::wstring>(content); return *this; }
|
35
|
+
Element& text(client::String* content) { (*this)->set_textContent(content); return *this; }
|
36
|
+
Element& text(const std::string& content) { _text<std::string>(content); return *this; }
|
37
|
+
Element& text(const std::wstring& content) { _text<std::wstring>(content); return *this; }
|
38
|
+
Element& value(client::String* val) { static_cast<client::HTMLInputElement*>(**this)->set_value(val); return *this; }
|
39
|
+
Element& value(const std::string& val) { _value<std::string>(val); return *this; }
|
40
|
+
Element& value(const std::wstring& val) { _value<std::wstring>(val); return *this; }
|
41
|
+
template<typename T>
|
42
|
+
Element& value(const T val) { std::stringstream stream; stream << val; _value<std::string>(stream.str()); return *this; }
|
43
|
+
Element& checked(bool val) { static_cast<client::HTMLInputElement*>(**this)->set_checked(val); return *this; }
|
44
|
+
|
45
|
+
std::string html() const { return Comet::Object((*this)->get_innerHTML()); }
|
46
|
+
std::string tagName() const { return Comet::Object((*this)->get_tagName()); }
|
47
|
+
|
48
|
+
template<typename T>
|
49
|
+
T value()
|
50
|
+
{
|
51
|
+
T var;
|
52
|
+
std::stringstream stream;
|
53
|
+
stream << get_value();
|
54
|
+
stream >> var;
|
55
|
+
return var;
|
56
|
+
}
|
57
|
+
|
58
|
+
template<>
|
59
|
+
std::wstring value<std::wstring>()
|
60
|
+
{
|
61
|
+
auto* input_el = static_cast<client::HTMLInputElement*>(**this);
|
62
|
+
auto* client_string = input_el->get_value();
|
63
|
+
|
64
|
+
return to_wstring(client_string);
|
65
|
+
}
|
66
|
+
|
67
|
+
inline Element& inner(const std::vector<Element>& els) { return operator>(els); }
|
68
|
+
inline Element& inner(const std::vector<Element*>& els) { return operator>(els); }
|
69
|
+
inline Element& inner(const std::vector<std::shared_ptr<Element> > els) { return operator>(els); }
|
70
|
+
|
71
|
+
template<typename LIST>
|
72
|
+
Element& operator>(const LIST& list)
|
73
|
+
{
|
74
|
+
ElementListAppender<LIST, is_array_container<LIST>::value>::append_list(list, **this);
|
75
|
+
return *this;
|
76
|
+
}
|
77
|
+
|
78
|
+
Element& empty();
|
79
|
+
|
80
|
+
void append_to(client::HTMLElement* el);
|
81
|
+
void append_to(Element& el);
|
82
|
+
void insert_before(client::HTMLElement* el);
|
83
|
+
void insert_before(Element& el);
|
84
|
+
void insert_after(client::HTMLElement* el);
|
85
|
+
void insert_after(Element& el);
|
86
|
+
void destroy();
|
87
|
+
|
88
|
+
bool is_attached() const;
|
89
|
+
bool has_parent() const;
|
90
|
+
Element get_parent();
|
91
|
+
Element get_next();
|
92
|
+
|
93
|
+
std::vector<Element> find(const std::string& selector);
|
94
|
+
Element find_one(const std::string& selector);
|
95
|
+
bool contains(const client::HTMLElement*);
|
96
|
+
void each(std::function<bool (Element&)>);
|
97
|
+
|
98
|
+
void toggle_class(const std::string& str, bool set);
|
99
|
+
void add_class(const std::string& str);
|
100
|
+
void remove_class(const std::string& str);
|
101
|
+
bool has_class(const std::string& str) const;
|
102
|
+
|
103
|
+
bool has_attribute(const std::string& key) const;
|
104
|
+
std::string get_attribute(const std::string& key) const;
|
105
|
+
void remove_attribute(const std::string& key);
|
106
|
+
|
107
|
+
bool is_visible() const;
|
108
|
+
Element& visible(bool, const std::string& display = "");
|
109
|
+
void set_visible(bool val) { visible(val); }
|
110
|
+
|
111
|
+
std::string get_inner_html() const;
|
112
|
+
std::string get_text() const;
|
113
|
+
std::string get_value() const;
|
114
|
+
bool get_checked() const { return static_cast<client::HTMLInputElement*>(**this)->get_checked(); }
|
115
|
+
|
116
|
+
private:
|
117
|
+
template<typename TYPE>
|
118
|
+
void _html(const TYPE& content)
|
119
|
+
{
|
120
|
+
(*this)->set_innerHTML(content.c_str());
|
121
|
+
}
|
122
|
+
|
123
|
+
template<typename TYPE>
|
124
|
+
void _text(const TYPE& content)
|
125
|
+
{
|
126
|
+
(*this)->set_textContent(content.c_str());
|
127
|
+
}
|
128
|
+
|
129
|
+
template<typename TYPE>
|
130
|
+
void _value(const TYPE& val)
|
131
|
+
{
|
132
|
+
static_cast<client::HTMLInputElement*>(**this)->set_value(val.c_str());
|
133
|
+
}
|
134
|
+
};
|
135
|
+
}
|
136
|
+
|
137
|
+
#endif
|
138
|
+
|
@@ -0,0 +1,76 @@
|
|
1
|
+
#ifndef COMET_EVENTS_HPP
|
2
|
+
# define COMET_EVENTS_HPP
|
3
|
+
|
4
|
+
# include <cheerp/clientlib.h>
|
5
|
+
# include <map>
|
6
|
+
|
7
|
+
namespace Comet
|
8
|
+
{
|
9
|
+
class JavascriptEvents
|
10
|
+
{
|
11
|
+
client::EventTarget* target;
|
12
|
+
std::map<std::string, client::EventListener*> listeners;
|
13
|
+
public:
|
14
|
+
JavascriptEvents(client::EventTarget* value) : target(value)
|
15
|
+
{
|
16
|
+
}
|
17
|
+
|
18
|
+
JavascriptEvents(const JavascriptEvents& copy)
|
19
|
+
{
|
20
|
+
target = copy.target;
|
21
|
+
}
|
22
|
+
|
23
|
+
~JavascriptEvents()
|
24
|
+
{
|
25
|
+
clear();
|
26
|
+
}
|
27
|
+
|
28
|
+
JavascriptEvents& operator=(const JavascriptEvents& copy)
|
29
|
+
{
|
30
|
+
target = copy.target;
|
31
|
+
return *this;
|
32
|
+
}
|
33
|
+
|
34
|
+
template<typename CALLBACK>
|
35
|
+
void on(const std::string& event, CALLBACK callback)
|
36
|
+
{
|
37
|
+
auto listener = cheerp::Callback(callback);
|
38
|
+
|
39
|
+
if (listeners.count(event)) off(event);
|
40
|
+
target->addEventListener(event.c_str(), listener);
|
41
|
+
listeners.emplace(event, listener);
|
42
|
+
}
|
43
|
+
|
44
|
+
void off(const std::string& event)
|
45
|
+
{
|
46
|
+
auto it = listeners.find(event);
|
47
|
+
|
48
|
+
if (it != listeners.end())
|
49
|
+
{
|
50
|
+
target->removeEventListener(it->first.c_str(), it->second);
|
51
|
+
listeners.erase(it);
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
void clear()
|
56
|
+
{
|
57
|
+
disconnect();
|
58
|
+
listeners.clear();
|
59
|
+
}
|
60
|
+
|
61
|
+
void connect()
|
62
|
+
{
|
63
|
+
disconnect();
|
64
|
+
for (auto listener : listeners)
|
65
|
+
target->addEventListener(listener.first.c_str(), listener.second);
|
66
|
+
}
|
67
|
+
|
68
|
+
void disconnect()
|
69
|
+
{
|
70
|
+
for (auto listener : listeners)
|
71
|
+
target->removeEventListener(listener.first.c_str(), listener.second);
|
72
|
+
}
|
73
|
+
};
|
74
|
+
}
|
75
|
+
|
76
|
+
#endif
|
@@ -0,0 +1,99 @@
|
|
1
|
+
#include "from_string.hpp"
|
2
|
+
#include "exception.hpp"
|
3
|
+
#include <map>
|
4
|
+
#include <climits>
|
5
|
+
#include <algorithm>
|
6
|
+
|
7
|
+
#define declare_std_converter(type, func) \
|
8
|
+
template<> type from_string<type>(const std::string& str) \
|
9
|
+
{ \
|
10
|
+
return std::func(str); \
|
11
|
+
}
|
12
|
+
|
13
|
+
namespace Comet
|
14
|
+
{
|
15
|
+
declare_std_converter(int, stoi)
|
16
|
+
declare_std_converter(double, stod)
|
17
|
+
declare_std_converter(long double, stold)
|
18
|
+
declare_std_converter(long, stol)
|
19
|
+
declare_std_converter(unsigned long, stoul)
|
20
|
+
declare_std_converter(long long, stoll)
|
21
|
+
declare_std_converter(unsigned long long, stoull)
|
22
|
+
declare_std_converter(float, stof)
|
23
|
+
|
24
|
+
template<> unsigned int from_string<unsigned int>(const std::string& str)
|
25
|
+
{
|
26
|
+
unsigned long value = std::stoul(str);
|
27
|
+
|
28
|
+
if (value > UINT_MAX)
|
29
|
+
raise(std::out_of_range(str));
|
30
|
+
return value;
|
31
|
+
}
|
32
|
+
|
33
|
+
template<> short from_string<short>(const std::string& str)
|
34
|
+
{
|
35
|
+
int value = std::stoi(str);
|
36
|
+
|
37
|
+
if (value > SHRT_MAX || value < SHRT_MIN)
|
38
|
+
raise(std::out_of_range(str));
|
39
|
+
return value;
|
40
|
+
}
|
41
|
+
|
42
|
+
template<> unsigned short from_string<unsigned short>(const std::string& str)
|
43
|
+
{
|
44
|
+
unsigned long value = std::stoul(str);
|
45
|
+
|
46
|
+
if (value > USHRT_MAX)
|
47
|
+
raise(std::out_of_range(str));
|
48
|
+
return value;
|
49
|
+
}
|
50
|
+
|
51
|
+
template<> char from_string<char>(const std::string& str)
|
52
|
+
{
|
53
|
+
if (str.length() > 1)
|
54
|
+
raise(std::out_of_range(str));
|
55
|
+
return *str.begin();
|
56
|
+
}
|
57
|
+
|
58
|
+
template<> unsigned char from_string<unsigned char>(const std::string& str)
|
59
|
+
{
|
60
|
+
if (str.length() > 1)
|
61
|
+
raise(std::out_of_range(str));
|
62
|
+
return *str.begin();
|
63
|
+
}
|
64
|
+
|
65
|
+
template<> bool from_string<bool>(const std::string& str)
|
66
|
+
{
|
67
|
+
static const std::map<std::string, bool> translation_table {
|
68
|
+
{"true", true}, {"TRUE", true}, {"True", true}, {"t", true},
|
69
|
+
{"false", false}, {"FALSE", false}, {"False", false}, {"f", false}
|
70
|
+
};
|
71
|
+
auto it = translation_table.find(str);
|
72
|
+
|
73
|
+
if (it == translation_table.end())
|
74
|
+
{
|
75
|
+
bool is_number = false;
|
76
|
+
|
77
|
+
if (!str.empty())
|
78
|
+
{
|
79
|
+
auto it = str.begin();
|
80
|
+
|
81
|
+
if (*it == '-')
|
82
|
+
++it;
|
83
|
+
is_number = std::find_if(it, str.end(), [](unsigned char c) { return !std::isdigit(c); }) == str.end();
|
84
|
+
}
|
85
|
+
if (!is_number)
|
86
|
+
{
|
87
|
+
std::string what = "cannot cast '" + str + "' to boolean";
|
88
|
+
raise(std::runtime_error(what.c_str()));
|
89
|
+
}
|
90
|
+
return std::stold(str) > 0;
|
91
|
+
}
|
92
|
+
return it->second;
|
93
|
+
}
|
94
|
+
|
95
|
+
template<> std::string from_string<std::string>(const std::string& str)
|
96
|
+
{
|
97
|
+
return str;
|
98
|
+
}
|
99
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#ifndef COMET_FROM_STRING_HPP
|
2
|
+
# define COMET_FROM_STRING_HPP
|
3
|
+
|
4
|
+
# include <string>
|
5
|
+
# include <sstream>
|
6
|
+
|
7
|
+
namespace Comet
|
8
|
+
{
|
9
|
+
template<typename TYPE>
|
10
|
+
TYPE from_string(const std::string& str)
|
11
|
+
{
|
12
|
+
std::stringstream stream;
|
13
|
+
TYPE value;
|
14
|
+
|
15
|
+
stream << str;
|
16
|
+
stream >> value;
|
17
|
+
return value;
|
18
|
+
}
|
19
|
+
|
20
|
+
template<> int from_string<int>(const std::string& str);
|
21
|
+
template<> unsigned int from_string<unsigned int>(const std::string& str);
|
22
|
+
template<> double from_string<double>(const std::string& str);
|
23
|
+
template<> long double from_string<long double>(const std::string& str);
|
24
|
+
template<> long from_string<long>(const std::string& str);
|
25
|
+
template<> unsigned long from_string<unsigned long>(const std::string& str);
|
26
|
+
template<> long long from_string<long long>(const std::string& str);
|
27
|
+
template<> unsigned long long from_string<unsigned long long>(const std::string& str);
|
28
|
+
template<> float from_string<float>(const std::string& str);
|
29
|
+
template<> short from_string<short>(const std::string& str);
|
30
|
+
template<> unsigned short from_string<unsigned short>(const std::string& str);
|
31
|
+
template<> char from_string<char>(const std::string& str);
|
32
|
+
template<> unsigned char from_string<unsigned char>(const std::string& str);
|
33
|
+
template<> bool from_string<bool>(const std::string& str);
|
34
|
+
template<> std::string from_string<std::string>(const std::string& str);
|
35
|
+
}
|
36
|
+
|
37
|
+
#endif
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef COMET_GLOBALS_HPP
|
2
|
+
# define COMET_GLOBALS_HPP
|
3
|
+
|
4
|
+
# include "element.hpp"
|
5
|
+
# include "document.hpp"
|
6
|
+
# include "window.hpp"
|
7
|
+
|
8
|
+
namespace Comet
|
9
|
+
{
|
10
|
+
extern Element body;
|
11
|
+
extern Window window;
|
12
|
+
extern Document document;
|
13
|
+
}
|
14
|
+
|
15
|
+
#endif
|
data/vendor/src/http.cpp
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
#include "http.hpp"
|
2
|
+
|
3
|
+
using namespace std;
|
4
|
+
using namespace Comet;
|
5
|
+
using namespace Comet::Http;
|
6
|
+
|
7
|
+
/*
|
8
|
+
* Response
|
9
|
+
*/
|
10
|
+
Response::Response()
|
11
|
+
{
|
12
|
+
}
|
13
|
+
|
14
|
+
void Response::initialize(client::XMLHttpRequest* xhr)
|
15
|
+
{
|
16
|
+
status = xhr->get_status();
|
17
|
+
if (xhr->get_responseText())
|
18
|
+
{
|
19
|
+
_has_body = true;
|
20
|
+
body = xhr->get_responseText();
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
DataTree Response::get_response_data() const
|
25
|
+
{
|
26
|
+
return DataTree(get_response_object());
|
27
|
+
}
|
28
|
+
|
29
|
+
Comet::Object Response::get_response_object() const
|
30
|
+
{
|
31
|
+
if (_has_body)
|
32
|
+
return Comet::Object::from_json(body);
|
33
|
+
return Comet::Object();
|
34
|
+
}
|
35
|
+
|
36
|
+
/*
|
37
|
+
* Request
|
38
|
+
*/
|
39
|
+
Request::Request(const string& m, const string& p) : method(m), path(p)
|
40
|
+
{
|
41
|
+
response = make_shared<Response>();
|
42
|
+
xhr = new client::XMLHttpRequest;
|
43
|
+
xhr->open(method.c_str(), path.c_str());
|
44
|
+
}
|
45
|
+
|
46
|
+
void Request::set_headers(const map<string, string> headers)
|
47
|
+
{
|
48
|
+
for (const auto header : headers)
|
49
|
+
xhr->setRequestHeader(header.first.c_str(), header.second.c_str());
|
50
|
+
}
|
51
|
+
|
52
|
+
void Request::set_body(const std::string& value)
|
53
|
+
{
|
54
|
+
set_body(Comet::String(value));
|
55
|
+
}
|
56
|
+
|
57
|
+
void Request::set_body(const std::wstring& value)
|
58
|
+
{
|
59
|
+
set_body(Comet::String(value));
|
60
|
+
}
|
61
|
+
|
62
|
+
void Request::set_body(Comet::String value)
|
63
|
+
{
|
64
|
+
body = value;
|
65
|
+
}
|
66
|
+
|
67
|
+
static client::EventListener* request_on_load(
|
68
|
+
client::XMLHttpRequest* xhr,
|
69
|
+
shared_ptr<Response> response,
|
70
|
+
function<void()> resolve,
|
71
|
+
function<void()> reject)
|
72
|
+
{
|
73
|
+
return cheerp::Callback([xhr, response, resolve, reject]()
|
74
|
+
{
|
75
|
+
if ((int)xhr->get_readyState() == 4)
|
76
|
+
{
|
77
|
+
response->initialize(xhr);
|
78
|
+
resolve();
|
79
|
+
}
|
80
|
+
});
|
81
|
+
}
|
82
|
+
|
83
|
+
Promise Request::send()
|
84
|
+
{
|
85
|
+
return Promise([this](function<void()> resolve, function<void()> reject)
|
86
|
+
{
|
87
|
+
xhr->set_onload(request_on_load(xhr, response, resolve, reject));
|
88
|
+
if (body->get_length() > 0)
|
89
|
+
xhr->send(*body);
|
90
|
+
else
|
91
|
+
xhr->send();
|
92
|
+
});
|
93
|
+
}
|
data/vendor/src/http.hpp
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
#ifndef CRAILS_FRONT_HTTP_HPP
|
2
|
+
# define CRAILS_FRONT_HTTP_HPP
|
3
|
+
|
4
|
+
#include "promise.hpp"
|
5
|
+
#include "datatree.hpp"
|
6
|
+
|
7
|
+
namespace Comet
|
8
|
+
{
|
9
|
+
namespace Http
|
10
|
+
{
|
11
|
+
class Request;
|
12
|
+
|
13
|
+
class Response
|
14
|
+
{
|
15
|
+
friend class Request;
|
16
|
+
|
17
|
+
bool _has_body = false;
|
18
|
+
client::String* body;
|
19
|
+
double status = 0;
|
20
|
+
public:
|
21
|
+
Response();
|
22
|
+
|
23
|
+
void initialize(client::XMLHttpRequest* xhr);
|
24
|
+
|
25
|
+
bool has_body() const { return _has_body; }
|
26
|
+
bool ok() const { return status >= 200 && status < 300; }
|
27
|
+
double get_status() const { return status; }
|
28
|
+
|
29
|
+
Comet::Object get_response_object() const;
|
30
|
+
DataTree get_response_data() const;
|
31
|
+
std::string get_response_text() const { return std::string(*body); }
|
32
|
+
std::wstring get_response_wide_text() const { return to_wstring(body); }
|
33
|
+
};
|
34
|
+
|
35
|
+
class Request
|
36
|
+
{
|
37
|
+
const std::string method, path;
|
38
|
+
Comet::String body;
|
39
|
+
std::shared_ptr<Response> response;
|
40
|
+
client::XMLHttpRequest* xhr;
|
41
|
+
|
42
|
+
public:
|
43
|
+
Request(const std::string& m, const std::string& p);
|
44
|
+
|
45
|
+
typedef Comet::Promise PromiseType;
|
46
|
+
typedef std::shared_ptr<PromiseType> PromisePtr;
|
47
|
+
typedef std::shared_ptr<Request> Ptr;
|
48
|
+
|
49
|
+
static Ptr get (const std::string& path) { return make("get", path); }
|
50
|
+
static Ptr post (const std::string& path) { return make("post", path); }
|
51
|
+
static Ptr put (const std::string& path) { return make("put", path); }
|
52
|
+
static Ptr patch (const std::string& path) { return make("patch", path); }
|
53
|
+
static Ptr _delete(const std::string& path) { return make("delete", path); }
|
54
|
+
|
55
|
+
static Ptr make(const std::string& method, const std::string& path)
|
56
|
+
{
|
57
|
+
return std::make_shared<Request>(method, path);
|
58
|
+
}
|
59
|
+
|
60
|
+
std::shared_ptr<Response> get_response() const { return response; }
|
61
|
+
|
62
|
+
void set_headers(const std::map<std::string, std::string> value);
|
63
|
+
void set_body(const std::string& value);
|
64
|
+
void set_body(const std::wstring& value);
|
65
|
+
void set_body(Comet::String value);
|
66
|
+
|
67
|
+
Comet::Promise send();
|
68
|
+
};
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
#endif
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#ifndef COMET_LEXICAL_CAST_HPP
|
2
|
+
# define COMET_LEXICAL_CAST_HPP
|
3
|
+
|
4
|
+
# include <string>
|
5
|
+
# include <sstream>
|
6
|
+
# include "from_string.hpp"
|
7
|
+
|
8
|
+
#include <iostream>
|
9
|
+
|
10
|
+
namespace Comet
|
11
|
+
{
|
12
|
+
template<typename OUTPUT, typename INPUT, bool SAME = std::is_same<OUTPUT, INPUT>::value >
|
13
|
+
struct lexical_caster
|
14
|
+
{
|
15
|
+
static inline OUTPUT _(INPUT input)
|
16
|
+
{
|
17
|
+
std::stringstream stream;
|
18
|
+
OUTPUT output;
|
19
|
+
|
20
|
+
stream << input;
|
21
|
+
stream >> output;
|
22
|
+
return output;
|
23
|
+
}
|
24
|
+
};
|
25
|
+
|
26
|
+
template<typename OUTPUT, typename INPUT>
|
27
|
+
struct lexical_caster<OUTPUT, INPUT, true>
|
28
|
+
{
|
29
|
+
static inline OUTPUT _(INPUT input) { return input; }
|
30
|
+
};
|
31
|
+
|
32
|
+
template<typename OUTPUT>
|
33
|
+
struct lexical_caster<OUTPUT, std::string, false>
|
34
|
+
{
|
35
|
+
static inline OUTPUT _(const std::string& input) { return Comet::from_string<OUTPUT>(input); }
|
36
|
+
};
|
37
|
+
|
38
|
+
template<typename INPUT>
|
39
|
+
struct lexical_caster<std::string, INPUT, false>
|
40
|
+
{
|
41
|
+
static inline std::string _(INPUT input) { return std::to_string(input); }
|
42
|
+
};
|
43
|
+
|
44
|
+
template<typename OUTPUT, typename INPUT>
|
45
|
+
OUTPUT lexical_cast(INPUT input)
|
46
|
+
{
|
47
|
+
return lexical_caster<OUTPUT, INPUT, std::is_same<OUTPUT, INPUT>::value >::_(input);
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
#endif
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#include "local_storage.hpp"
|
2
|
+
#include <cheerp/client.h>
|
3
|
+
|
4
|
+
using namespace std;
|
5
|
+
using namespace Comet;
|
6
|
+
|
7
|
+
LocalStorage::LocalStorage(client::Storage* storage) :
|
8
|
+
storage(storage)
|
9
|
+
{
|
10
|
+
initialize();
|
11
|
+
}
|
12
|
+
|
13
|
+
LocalStorage::LocalStorage()
|
14
|
+
{
|
15
|
+
storage = client::window.get_localStorage();
|
16
|
+
initialize();
|
17
|
+
}
|
18
|
+
|
19
|
+
LocalStorage::~LocalStorage()
|
20
|
+
{
|
21
|
+
client::window.removeEventListener("storage", event_listener);
|
22
|
+
}
|
23
|
+
|
24
|
+
void LocalStorage::initialize()
|
25
|
+
{
|
26
|
+
event_listener = cheerp::Callback([this](client::Event* _event)
|
27
|
+
{
|
28
|
+
auto* event = static_cast<client::StorageEvent*>(_event);
|
29
|
+
|
30
|
+
if (event->get_storageArea() == storage)
|
31
|
+
on_change.trigger(Change(event));
|
32
|
+
});
|
33
|
+
client::window.addEventListener("storage", event_listener);
|
34
|
+
}
|
35
|
+
|
36
|
+
LocalStorage::Change::Change(client::StorageEvent* event)
|
37
|
+
{
|
38
|
+
key = (string)(*event->get_key());
|
39
|
+
if (event->get_oldValue())
|
40
|
+
old_value = (string)(*static_cast<client::String*>(event->get_oldValue()));
|
41
|
+
if (event->get_newValue())
|
42
|
+
new_value = (string)(*static_cast<client::String*>(event->get_newValue()));
|
43
|
+
}
|
44
|
+
|
45
|
+
template<>
|
46
|
+
void LocalStorage::set_item<const string&>(const string& key, const string& value)
|
47
|
+
{
|
48
|
+
storage->setItem(key.c_str(), value.c_str());
|
49
|
+
}
|
50
|
+
|
51
|
+
template<>
|
52
|
+
void LocalStorage::set_item<string>(const string& key, string value)
|
53
|
+
{
|
54
|
+
set_item<const string&>(key, value);
|
55
|
+
}
|
56
|
+
|
57
|
+
template<>
|
58
|
+
string LocalStorage::get_item<string>(const string& key) const
|
59
|
+
{
|
60
|
+
client::Object* object = storage->getItem(key.c_str());
|
61
|
+
|
62
|
+
if (object)
|
63
|
+
return (string)(*static_cast<client::String*>(object));
|
64
|
+
return "";
|
65
|
+
}
|
66
|
+
|
67
|
+
void LocalStorage::remove_item(const string& key)
|
68
|
+
{
|
69
|
+
storage->removeItem(key.c_str());
|
70
|
+
}
|
71
|
+
|
72
|
+
void LocalStorage::clear()
|
73
|
+
{
|
74
|
+
storage->clear();
|
75
|
+
}
|