Skip to main content

include/sleepy_discord/common_return_types.h

Namespaces​

Name
SleepyDiscord

Classes​

Name
structSleepyDiscord::StandardResponse
structSleepyDiscord::BooleanResponse
structSleepyDiscord::ObjectResponse
structSleepyDiscord::ArrayResponseWrapper
structSleepyDiscord::StringResponse

Types​

Name
typedef BooleanResponseBoolResponse
template <class Type >
using json::ArrayWrapper< Type, ArrayResponseWrapper >
ArrayResponse
using StringResponseVoidResponse

Functions​

Name
template <ErrorCode Code>
const BooleanResponse::Callback
SuccessCodeFn()
const BooleanResponse::CallbackEmptyRespFn()
const BooleanResponse::CallbackStandardRespFn()

Types Documentation​

typedef BoolResponse​

typedef BooleanResponse SleepyDiscord::BoolResponse;

using ArrayResponse​

template <class Type >
using SleepyDiscord::ArrayResponse = typedef json::ArrayWrapper<Type, ArrayResponseWrapper>;

using VoidResponse​

using SleepyDiscord::VoidResponse = typedef StringResponse;

Functions Documentation​

function SuccessCodeFn​

template <ErrorCode Code>
inline const BooleanResponse::Callback SuccessCodeFn()

function EmptyRespFn​

inline const BooleanResponse::Callback EmptyRespFn()

function StandardRespFn​

inline const BooleanResponse::Callback StandardRespFn()

Source code​

#pragma once
#include <functional>
#include "http.h"
#include "json_wrapper.h"
#include "error.h"

namespace SleepyDiscord {
struct StandardResponse : Response { //This is here for possiable future use
explicit StandardResponse(const Response& response) : Response(response) {}
};

struct BooleanResponse : public StandardResponse {
public:
using StandardResponse::StandardResponse;
using Callback = std::function<bool(const Response& response)>;
using Type = bool;
BooleanResponse(const Response& response, const Callback callback) :
StandardResponse(response), wasSuccessful(callback) { }

inline operator Type() const {
return wasSuccessful(*this) || !error();
}

Type operator*() const {
return operator Type();
}

inline Type cast() {
return operator Type();
}

//this isn't a function so that we can override it during construction.
//this isn't a virtual function because then we need lots of child classes
//this isn't used in a template because then the user would have to write the right error handling function
const Callback wasSuccessful = [](const Response& /*response*/) { return true; };
};

typedef BooleanResponse BoolResponse;

template<ErrorCode Code>
inline const BooleanResponse::Callback SuccessCodeFn() {
return [](const Response& response) {return response.statusCode == Code; };
}

inline const BooleanResponse::Callback EmptyRespFn() {
return SuccessCodeFn<NO_CONTENT>();
}

inline const BooleanResponse::Callback StandardRespFn() {
return SuccessCodeFn<OK>();
}


template<class _Type>
struct ObjectResponse : public StandardResponse {
using StandardResponse::StandardResponse;
using Type = _Type;

operator Type() { //to do use references instead of pointers
return error() ? Type() : Type(text);
}

Type& operator*() const {
return operator Type();
}

inline Type cast() {
return operator Type();
}

inline bool cast(Type& value) {
if (error())
return false;
rapidjson::Document doc;
rapidjson::ParseResult isOK =
doc.Parse(text.c_str(), text.length());
if (!isOK)
return false;
value = Type(doc);
return true;
}
};


struct ArrayResponseWrapper : public StandardResponse {
using StandardResponse::StandardResponse;
using Type = std::string;
inline operator const std::string&() const {
return text;
}
inline rapidjson::Document getDoc() {
rapidjson::Document arr; //ARR, I'm a pirate
arr.Parse(text.data(), text.length());
return arr;
}
template<class Callback>
inline rapidjson::ParseResult getDoc(Callback& callback) {
rapidjson::Document arr;
rapidjson::ParseResult isOK =
arr.Parse(text.data(), text.length());
if (isOK) callback(arr);
return isOK;
}
};

template <class Type>
using ArrayResponse = json::ArrayWrapper<Type, ArrayResponseWrapper>;

struct StringResponse : public StandardResponse {
using StandardResponse::StandardResponse;
using Type = std::string;
inline operator const Type&() const {
return text;
}
};

using VoidResponse = StringResponse;
}

Updated on 13 April 2022 at 18:39:59 UTC