Class CallbackHolder

Synopsis

#include <orm_lib/inc/drogon/orm/SqlBinder.h>

template <typename Function>
class CallbackHolder : public CallbackHolderBase

Description

No description yet.

Inheritance

Ancestors: CallbackHolderBase

Methods

CallbackHolder
execCallback

Source

Lines 160-257 in orm_lib/inc/drogon/orm/SqlBinder.h.

template <typename Function>
class CallbackHolder : public CallbackHolderBase
{
  public:
    virtual void execCallback(const Result &result)
    {
        run(result);
    }

    CallbackHolder(Function &&function)
        : function_(std::forward<Function>(function))
    {
        static_assert(traits::isSqlCallback,
                      "Your sql callback function type is wrong!");
    }

  private:
    Function function_;
    using traits = FunctionTraits<Function>;
    template <std::size_t Index>
    using NthArgumentType = typename traits::template argument<Index>;
    static const size_t argumentCount = traits::arity;

    template <bool isStep = traits::isStepResultCallback>
    typename std::enable_if<isStep, void>::type run(const Result &result)
    {
        if (result.size() == 0)
        {
            run(nullptr, true);
            return;
        }
        for (auto const &row : result)
        {
            run(&row, false);
        }
        run(nullptr, true);
    }
    template <bool isStep = traits::isStepResultCallback>
    typename std::enable_if<!isStep, void>::type run(const Result &result)
    {
        static_assert(argumentCount == 0,
                      "Your sql callback function type is wrong!");
        function_(result);
    }
    template <typename... Values, std::size_t Boundary = argumentCount>
    typename std::enable_if<(sizeof...(Values) < Boundary), void>::type run(
        const Row *const row,
        bool isNull,
        Values &&... values)
    {
        // call this function recursively until parameter's count equals to the
        // count of target function parameters
        static_assert(
            CallbackArgTypeTraits<NthArgumentType<sizeof...(Values)>>::isValid,
            "your sql callback function argument type must be value "
            "type or "
            "const "
            "left-reference type");
        using ValueType =
            typename std::remove_cv<typename std::remove_reference<
                NthArgumentType<sizeof...(Values)>>::type>::type;
        ValueType value = ValueType();
        if (row && row->size() > sizeof...(Values))
        {
            // if(!VectorTypeTraits<ValueType>::isVector)
            //     value = (*row)[sizeof...(Values)].as<ValueType>();
            // else
            //     ; // value =
            //     (*row)[sizeof...(Values)].asArray<VectorTypeTraits<ValueType>::ItemsType>();
            value =
                makeValue<ValueType>((*row)[(Row::SizeType)sizeof...(Values)]);
        }

        run(row, isNull, std::forward<Values>(values)..., std::move(value));
    }
    template <typename... Values, std::size_t Boundary = argumentCount>
    typename std::enable_if<(sizeof...(Values) == Boundary), void>::type run(
        const Row *const,
        bool isNull,
        Values &&... values)
    {
        function_(isNull, std::move(values)...);
    }
    template <typename ValueType>
    typename std::enable_if<VectorTypeTraits<ValueType>::isVector,
                            ValueType>::type
    makeValue(const Field &field)
    {
        return field.asArray<typename VectorTypeTraits<ValueType>::ItemsType>();
    }
    template <typename ValueType>
    typename std::enable_if<!VectorTypeTraits<ValueType>::isVector,
                            ValueType>::type
    makeValue(const Field &field)
    {
        return field.as<ValueType>();
    }
};





Add Discussion as Guest

Log in to DocsForge