forked from cpv-project/cpv-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormSerializer.hpp
40 lines (37 loc) · 987 Bytes
/
FormSerializer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include "../Http/HttpForm.hpp"
#include "../Utility/ObjectTrait.hpp"
namespace cpv {
/**
* The class used to serialize model to url encoded form packet.
* The model should contains a public function named `dumpForm` and
* takes `HttpForm&` as argument.
*/
template <class T, class = void /* for enable_if */>
class FormSerializer {
public:
/** Serialize model to form packet */
static Packet serialize(const T& model) {
using Trait = ObjectTrait<T>;
Packet p;
if constexpr (Trait::IsPointerLike) {
if (Trait::get(model) == nullptr) {
return p;
}
}
HttpForm form;
if constexpr (Trait::IsPointerLike) {
Trait::get(model)->dumpForm(form);
} else {
Trait::get(model).dumpForm(form);
}
form.buildUrlEncoded(p);
return p;
}
};
/** Convenient static function for FormSerializer */
template <class T>
static inline Packet serializeForm(const T& model) {
return FormSerializer<T>::serialize(model);
}
}