rikitiki  v0.1.67
Build C++ web server modules that allow easy routing and deployment.
content_handler.h
1 #pragma once
2 
3 #include "content_types.h"
4 #include <vector>
5 #include <map>
6 #include <array>
7 #include <mxcomp/reflection.h>
8 namespace rikitiki {
9  class ConnContext;
10  struct Response;
11 
12  template <typename T>
13  struct ContentHandler_ {
14  static constexpr std::array<ContentType::t,1> ContentTypes() { return { { ContentType::DEFAULT } }; };
15  };
16 
17  struct OutProvider {
18  template<typename S, typename T>
19  static auto Make() -> void (*)(ConnContext&, S&) {
20  return [](ConnContext& ctx, S& s) {
21  T t;
22  t << s;
23  ctx << t;
24  };
25  }
26  };
27 
28  struct InProvider {
29 
30  template<typename S, typename T>
31  static auto Make() -> void (*)(ConnContext&, S&) {
32  return [](ConnContext& ctx, S& s) {
33  T t;
34  ctx >> t;
35  t >> s;
36  };
37  }
38  };
39 
40 
41  template <typename S, typename FProvider, typename... T>
42  struct TypeConversions {
43  typedef TypeConversions<S, FProvider, T...> thisType;
44  typedef TypeConversions<std::vector<S>, FProvider, T...> VectorType;
45  template <typename Th, typename...Tt>
46  struct HandlerAdders {
47  static void Add( thisType* _this){
50  }
51  };
52 
53  template <typename Th>
54  struct HandlerAdders<Th> {
55  static auto Add( thisType* _this) -> void {
56  for(auto contentType : ContentHandler_<Th>::ContentTypes()){
57  assert(contentType > ContentType::DEFAULT &&
58  contentType < ContentType::MAX &&
59  "Invalid content type value in specialized handler");
60  _this->handlers[contentType] = FProvider::template Make<S, Th> ();
61  }
62  }
63  };
64 
65  typedef void (*Setter)(ConnContext&, S& s);
66  std::vector<Setter> handlers;
67 
68  TypeConversions() {
69  handlers.resize(ContentType::MAX);
71  }
72 
73  static thisType& Instance() {
74  static thisType Instance;
75  return Instance;
76  }
77  };
78 
79  template<typename T, typename enable = void >
80  struct valid_conversions { };
81 
82 }
83 #include <rikitiki/jsoncpp/jsoncpp>
84 namespace rikitiki {
85  using namespace mxcomp;
86  template<typename T>
87  struct valid_conversions<T, typename std::enable_if< std::is_function <decltype(MetaClass_<T>::fields)>::value >::type > {
90  };
91 
92  template <typename T>
93  struct valid_conversions<std::vector<T>, typename std::enable_if< std::is_class<valid_conversions<T> >::value >::type > {
94  typedef typename valid_conversions<T>::In::VectorType In;
95  typedef typename valid_conversions<T>::Out::VectorType Out;
96  };
97 
98 
99 }