ContentGenerator.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #pragma once
  5. #include "processor-private.h"
  6. #include "../utils/utils.h"
  7. #define __merge_body(a, b) a ## b
  8. #define __merge(a, b) __merge_body(a, b)
  9. #define __uniqueVarName(name) __merge(name, __LINE__)
  10. #define __string_fic(a) #a
  11. #define __string_fic_r(a) __string_fic(a)
  12. #define ContentGeneratorDefine(cond, eval) const static ContentGenerator __uniqueVarName(AutoContentGenerator_Line_)(__string_fic_r(__uniqueVarName(AutoRegistered) __FILE_NAME__), [] (auto request) { cond; }, [] (auto request) { eval; });
  13. using namespace std;
  14. namespace xc {
  15. namespace processor {
  16. /*输入一个请求,判断此生成器是否响应此请求*/
  17. typedef function<bool (RequestData)> RequestCheckBlock;
  18. /*为此请求生成响应,响应需要使用new,在发送完成或HTTP服务器不再需要时自动调用delete销毁*/
  19. typedef function<ResponseData* (RequestData)> ContentGenerateBlock;
  20. /*请定义为static*/
  21. class ContentGenerator {
  22. public:
  23. ContentGenerator(string name, RequestCheckBlock checker, ContentGenerateBlock generator);
  24. bool matchRequest(RequestData request);
  25. ResponseData *generateResponse(RequestData request);
  26. string getName();
  27. private:
  28. string name;
  29. RequestCheckBlock checker;
  30. ContentGenerateBlock generator;
  31. };
  32. }
  33. }