Go to the documentation of this file.00001 #ifndef CPR_MULTIPART_H
00002 #define CPR_MULTIPART_H
00003
00004 #include <cstdint>
00005 #include <initializer_list>
00006 #include <string>
00007 #include <type_traits>
00008 #include <vector>
00009
00010 #include "defines.h"
00011
00012 namespace cpr {
00013
00014 struct File {
00015 template <typename StringType>
00016 explicit File(StringType&& filepath)
00017 : filepath{CPR_FWD(filepath)} {}
00018 std::string filepath;
00019 };
00020
00021 struct Buffer {
00022 typedef const unsigned char* data_t;
00023
00024 template <typename Iterator, typename StringType>
00025 explicit Buffer(Iterator begin, Iterator end, StringType&& filename)
00026 : data{reinterpret_cast<data_t>(&(*begin))},
00027 datalen{static_cast<unsigned long>(std::distance(begin, end))},
00028 filename{CPR_FWD(filename)} {
00029 is_random_access_iterator(begin, end);
00030 static_assert(sizeof(*begin) == 1, "only byte buffers can be used");
00031 }
00032
00033 template <typename Iterator>
00034 typename std::enable_if<std::is_same<typename std::iterator_traits<Iterator>::iterator_category,
00035 std::random_access_iterator_tag>::value>::type
00036 is_random_access_iterator(Iterator , Iterator ) {}
00037
00038 data_t data;
00039 unsigned long datalen;
00040 std::string filename;
00041 };
00042
00043 struct Part {
00044 Part(const std::string& name, const std::string& value, const std::string& content_type = {})
00045 : name{name}, value{value}, content_type{content_type}, is_file{false},
00046 is_buffer{false} {}
00047 Part(const std::string& name, const std::int32_t& value, const std::string& content_type = {})
00048 : name{name}, value{std::to_string(value)}, content_type{content_type}, is_file{false},
00049 is_buffer{false} {}
00050 Part(const std::string& name, const File& file, const std::string& content_type = {})
00051 : name{name}, value{file.filepath}, content_type{content_type}, is_file{true},
00052 is_buffer{false} {}
00053 Part(const std::string& name, const Buffer& buffer, const std::string& content_type = {})
00054 : name{name}, value{buffer.filename}, content_type{content_type}, data{buffer.data},
00055 datalen{buffer.datalen}, is_file{false}, is_buffer{true} {}
00056
00057 std::string name;
00058 std::string value;
00059 std::string content_type;
00060 Buffer::data_t data;
00061 unsigned long datalen;
00062 bool is_file;
00063 bool is_buffer;
00064 };
00065
00066 class Multipart {
00067 public:
00068 Multipart(const std::initializer_list<Part>& parts);
00069
00070 std::vector<Part> parts;
00071 };
00072
00073 }
00074
00075 #endif