<commit_before>#include <cassert>

#include "operators.h"

namespace ad {

static void AddBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative();
    rhs->derivative() += val.derivative();
}

Var operator+(const Var& v1, const Var& v2) {
    return v1.graph()->CreateNode(v1.value() + v2.value(), v1, v2, AddBackprop);
}

static void SubBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative();
    rhs->derivative() -= val.derivative();
}

Var operator-(const Var& v1, const Var& v2) {
    return v1.graph()->CreateNode(v1.value() - v2.value(), v1, v2, SubBackprop);
}

Var operator-(double a, const Var& v1) {
    Eigen::MatrixXd coeff(v1.value().rows(), v1.value().cols());
    coeff.setConstant(a);
    Var coeff_var = v1.graph()->CreateParam(coeff);
    return v1.graph()->CreateNode(
            a - v1.value().array(), coeff_var, v1, SubBackprop);
}

Var operator-(const Var& v1, double a) {
    Eigen::MatrixXd coeff(v1.value().rows(), v1.value().cols());
    coeff.setConstant(a);
    Var coeff_var = v1.graph()->CreateParam(coeff);
    return v1.graph()->CreateNode(
            v1.value().array() - a, v1, coeff_var, SubBackprop);
}

static void MulBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() +=  val.derivative() * rhs->value().transpose();
    rhs->derivative() +=  lhs->value().transpose() * val.derivative();
}

Var operator*(const Var& v1, const Var& v2) {
    return v1.graph()->CreateNode(v1.value() * v2.value(), v1, v2, MulBackprop);
}

static void CoeffMulBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() +=  val.derivative() * rhs->value()(0, 0);
}

Var operator*(double a, const Var& v1) {
    Eigen::MatrixXd coeff(1, 1);
    coeff << a;
    Var coeff_var = v1.graph()->CreateParam(coeff);
    return v1.graph()->CreateNode(v1.value() * a, v1, coeff_var, CoeffMulBackprop);
}

Var operator*(const Var& v1, double a) {
    Eigen::MatrixXd coeff(1, 1);
    coeff << a;
    Var coeff_var = v1.graph()->CreateParam(coeff);
    return v1.graph()->CreateNode(v1.value() * a, v1, coeff_var, CoeffMulBackprop);
}

static void ReluBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    const double* a = lhs->value().data();
    double* db = val.derivative().data();
    for (int i = 0; i < lhs->derivative().size(); ++i) {
        da[i] += a[i] > 0 ? db[i] : 0;
    }
}

Var Relu(const Var& v1) {
    return v1.graph()->CreateNode(
            v1.value().array().max(0), v1, no_operand, ReluBackprop);
}

static void SquareBackprop(Var& val, Var* lhs, Var*) {
    lhs->derivative() += 2 * val.derivative() * lhs->value();
}

Var Square(const Var& v1) {
    return v1.graph()->CreateNode(
            v1.value() * v1.value(), v1, no_operand, SquareBackprop);
}

static void EltSquareBackprop(Var& val, Var* lhs, Var*) {
    lhs->derivative() += 2 * val.derivative().cwiseProduct(lhs->value());
}

Var EltSquare(const Var& v1) {
    return v1.graph()->CreateNode(
            v1.value().cwiseProduct(v1.value()), v1, no_operand, EltSquareBackprop);
}

static void EltwiseMulBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative().cwiseProduct(rhs->value());
    rhs->derivative() += val.derivative().cwiseProduct(lhs->value());
}

Var operator^(const Var& v1, const Var& v2) {
    return v1.graph()->CreateNode(
            v1.value().cwiseProduct(v2.value()), v1, v2, EltwiseMulBackprop);
}

static void LogBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    double* dx = val.derivative().data();
    const double* a = lhs->value().data();
    for (int i = 0; i < val.value().size(); ++i) {
        da[i] += dx[i] / a[i];
    }
}

Var Log(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = log(src_ptr[i]);
    }
    return x.graph()->CreateNode(res, x, no_operand, LogBackprop);
}

static void NLogBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    double* dx = val.derivative().data();
    const double* a = lhs->value().data();
    for (int i = 0, size = val.value().size(); i < size; ++i) {
        da[i] += -dx[i] / (a[i]);
    }
}

Var NLog(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = -log(src_ptr[i]);
    }
    return x.graph()->CreateNode(res, x, no_operand, NLogBackprop);
}

Var CrossEntropy(const Var& h, const Var& y) {
    return Sum(y ^ NLog(h));
}

static Eigen::MatrixXd Softmax(const Eigen::MatrixXd& x) {
    Eigen::MatrixXd res(x.rows(), x.cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.data();
    double total = 0;
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = exp(src_ptr[i]);
        total += dst_ptr[i];
    }

    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] /= total;
    }
    return res;
}

static void SoftmaxLossBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative().cwiseProduct(
            Softmax(lhs->value()) - rhs->value());
}

Var SoftmaxLoss(Var h, Var y) {
    auto res = Softmax(h.value());

    double* d = res.data();
    double* dy = y.value().data();
    for (size_t i = 0, len = res.size(); i < len; ++i) {
        if (dy[i] != 0.0) {
            d[i] = -dy[i] * log(d[i]);
        }
    }
    return h.graph()->CreateNode(res, h, y, SoftmaxLossBackprop);
}

static void ExpBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    const double* dx = val.derivative().data();
    const double* a = lhs->value().data();
    for (int i = 0; i < val.value().size(); ++i) {
        da[i] += dx[i] * exp(a[i]);
    }
}

Var Exp(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = exp(src_ptr[i]);
    }
    return x.graph()->CreateNode(res, x, no_operand, ExpBackprop);
}

static void SoftmaxBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    const double* dx = val.derivative().data();
    const double* a = val.value().data();
    size_t len = val.value().size();
    for (size_t i = 0; i < len; ++i) {
        double d = 0;
        for (size_t j = 0; j < len; ++j) {
            d += a[i] * (((i == j) ? 1 : 0) - a[j]) * dx[j];
        }
        da[i] += d;
    }
}

Var Softmax(const Var& x) {
    return x.graph()->CreateNode(
            Softmax(x.value()), x, no_operand, SoftmaxBackprop);
}

static void SigmoidBackprop(Var& val, Var* lhs, Var*) {
    const Eigen::MatrixXd& a = val.value();
    lhs->derivative() += val.derivative()
        .cwiseProduct((a.array() * (1.0 - a.array())).matrix());
}

Var Sigmoid(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = 1.0 / (1 + exp(-src_ptr[i]));
    }

    return x.graph()->CreateNode(res, x, no_operand, SigmoidBackprop);
}

static void SumBackprop(Var& val, Var* lhs, Var*) {
    lhs->derivative().array() += (double)val.derivative()(0, 0);
}

Var Sum(const Var& a) {
    Eigen::MatrixXd res(1, 1);
    res << a.value().sum();
    return a.graph()->CreateNode(res, a, no_operand, SumBackprop);
}

static void MeanBackprop(Var& val, Var* lhs, Var*) {
    lhs->derivative().array() +=
        (double)val.derivative()(0, 0) / val.value().size();
}

Var Mean(const Var& a) {
    Eigen::MatrixXd res(1, 1);
    res << a.value().sum() / a.value().size();
    return a.graph()->CreateNode(res, a, no_operand, MeanBackprop);
}

Var MSE(const Var& h, const Var& y) {
    return Mean(EltSquare(h - y));
}

Var SSE(const Var& h, const Var& y) {
    return Sum(EltSquare(h - y));
}

static void NthColBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative().col(rhs->value()(0, 0)) += val.derivative();
}

Var NthCol(const Var& w, int n) {
    Eigen::MatrixXd n_mat(1, 1);
    n_mat << n;
    Var n_var = w.graph()->CreateParam(n_mat);
    return w.graph()->CreateNode(w.value().col(n), w, n_var, NthColBackprop);
}

static void TanhBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    const double* dx = val.derivative().data();
    const double* a = lhs->value().data();
    for (int i = 0; i < val.value().size(); ++i) {
        double denom = std::cosh(2 * a[i]) + 1;
        denom = denom * denom;
        if (std::isfinite(denom)) {
            double num = std::cosh(a[i]);
            num = 4 * num * num;
            double res = num / denom;
            da[i] += dx[i] * res;
        }
    }
}

Var Tanh(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = std::tanh(src_ptr[i]);
    }

    return x.graph()->CreateNode(res, x, no_operand, TanhBackprop);
}

static void ColAppendBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative().block(0, 0, lhs->derivative().rows(), 1);
    rhs->derivative() += val.derivative().block(
            lhs->derivative().rows(), 0, rhs->derivative().rows(), 1);
}

Var ColAppend(Var x, Var y) {
    if (x.value().cols() != 1 || y.value().cols() != 1) {
        throw std::runtime_error("cannot append not-a-column-vectors");
    }

    Eigen::MatrixXd cated(x.value().rows() + y.value().rows(), 1);
    cated << x.value(), y.value();
    return x.graph()->CreateNode(cated, x, y, ColAppendBackprop);
}

}
<commit_msg>Allow scalar * vector<commit_after>#include <cassert>

#include "operators.h"

namespace ad {

static void AddBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative();
    rhs->derivative() += val.derivative();
}

Var operator+(const Var& v1, const Var& v2) {
    return v1.graph()->CreateNode(v1.value() + v2.value(), v1, v2, AddBackprop);
}

static void SubBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative();
    rhs->derivative() -= val.derivative();
}

Var operator-(const Var& v1, const Var& v2) {
    return v1.graph()->CreateNode(v1.value() - v2.value(), v1, v2, SubBackprop);
}

Var operator-(double a, const Var& v1) {
    Eigen::MatrixXd coeff(v1.value().rows(), v1.value().cols());
    coeff.setConstant(a);
    Var coeff_var = v1.graph()->CreateParam(coeff);
    return v1.graph()->CreateNode(
            a - v1.value().array(), coeff_var, v1, SubBackprop);
}

Var operator-(const Var& v1, double a) {
    Eigen::MatrixXd coeff(v1.value().rows(), v1.value().cols());
    coeff.setConstant(a);
    Var coeff_var = v1.graph()->CreateParam(coeff);
    return v1.graph()->CreateNode(
            v1.value().array() - a, v1, coeff_var, SubBackprop);
}

static void MulBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() +=  val.derivative() * rhs->value().transpose();
    rhs->derivative() +=  lhs->value().transpose() * val.derivative();
}

static void CoeffMulBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() +=  val.derivative() * rhs->value()(0, 0);
    rhs->derivatuve() += val.derivative().transpose() * lhs->value();
}

Var operator*(const Var& v1, const Var& v2) {
    if (v2.value().rows() == 1 && v2.value().cols() == 1) {
        return v2 * v1;
    }

    if (v1.value().rows() == 1 && v1.value().cols() == 1) {
        return v1.graph()->CreateNode(
                v2.value() * v1.value()(0, 0), v2, v1, CoeffMulBackprop);
    }

    return v1.graph()->CreateNode(v1.value() * v2.value(), v1, v2, MulBackprop);
}

Var operator*(double a, const Var& v1) {
    Eigen::MatrixXd coeff(1, 1);
    coeff << a;
    Var coeff_var = v1.graph()->CreateParam(coeff);
    return v1.graph()->CreateNode(v1.value() * a, v1, coeff_var, CoeffMulBackprop);
}

Var operator*(const Var& v1, double a) {
    Eigen::MatrixXd coeff(1, 1);
    coeff << a;
    Var coeff_var = v1.graph()->CreateParam(coeff);
    return v1.graph()->CreateNode(v1.value() * a, v1, coeff_var, CoeffMulBackprop);
}

static void ReluBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    const double* a = lhs->value().data();
    double* db = val.derivative().data();
    for (int i = 0; i < lhs->derivative().size(); ++i) {
        da[i] += a[i] > 0 ? db[i] : 0;
    }
}

Var Relu(const Var& v1) {
    return v1.graph()->CreateNode(
            v1.value().array().max(0), v1, no_operand, ReluBackprop);
}

static void SquareBackprop(Var& val, Var* lhs, Var*) {
    lhs->derivative() += 2 * val.derivative() * lhs->value();
}

Var Square(const Var& v1) {
    return v1.graph()->CreateNode(
            v1.value() * v1.value(), v1, no_operand, SquareBackprop);
}

static void EltSquareBackprop(Var& val, Var* lhs, Var*) {
    lhs->derivative() += 2 * val.derivative().cwiseProduct(lhs->value());
}

Var EltSquare(const Var& v1) {
    return v1.graph()->CreateNode(
            v1.value().cwiseProduct(v1.value()), v1, no_operand, EltSquareBackprop);
}

static void EltwiseMulBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative().cwiseProduct(rhs->value());
    rhs->derivative() += val.derivative().cwiseProduct(lhs->value());
}

Var operator^(const Var& v1, const Var& v2) {
    return v1.graph()->CreateNode(
            v1.value().cwiseProduct(v2.value()), v1, v2, EltwiseMulBackprop);
}

static void LogBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    double* dx = val.derivative().data();
    const double* a = lhs->value().data();
    for (int i = 0; i < val.value().size(); ++i) {
        da[i] += dx[i] / a[i];
    }
}

Var Log(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = log(src_ptr[i]);
    }
    return x.graph()->CreateNode(res, x, no_operand, LogBackprop);
}

static void NLogBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    double* dx = val.derivative().data();
    const double* a = lhs->value().data();
    for (int i = 0, size = val.value().size(); i < size; ++i) {
        da[i] += -dx[i] / (a[i]);
    }
}

Var NLog(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = -log(src_ptr[i]);
    }
    return x.graph()->CreateNode(res, x, no_operand, NLogBackprop);
}

Var CrossEntropy(const Var& h, const Var& y) {
    return Sum(y ^ NLog(h));
}

static Eigen::MatrixXd Softmax(const Eigen::MatrixXd& x) {
    Eigen::MatrixXd res(x.rows(), x.cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.data();
    double total = 0;
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = exp(src_ptr[i]);
        total += dst_ptr[i];
    }

    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] /= total;
    }
    return res;
}

static void SoftmaxLossBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative().cwiseProduct(
            Softmax(lhs->value()) - rhs->value());
}

Var SoftmaxLoss(Var h, Var y) {
    auto res = Softmax(h.value());

    double* d = res.data();
    double* dy = y.value().data();
    for (size_t i = 0, len = res.size(); i < len; ++i) {
        if (dy[i] != 0.0) {
            d[i] = -dy[i] * log(d[i]);
        }
    }
    return h.graph()->CreateNode(res, h, y, SoftmaxLossBackprop);
}

static void ExpBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    const double* dx = val.derivative().data();
    const double* a = lhs->value().data();
    for (int i = 0; i < val.value().size(); ++i) {
        da[i] += dx[i] * exp(a[i]);
    }
}

Var Exp(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = exp(src_ptr[i]);
    }
    return x.graph()->CreateNode(res, x, no_operand, ExpBackprop);
}

static void SoftmaxBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    const double* dx = val.derivative().data();
    const double* a = val.value().data();
    size_t len = val.value().size();
    for (size_t i = 0; i < len; ++i) {
        double d = 0;
        for (size_t j = 0; j < len; ++j) {
            d += a[i] * (((i == j) ? 1 : 0) - a[j]) * dx[j];
        }
        da[i] += d;
    }
}

Var Softmax(const Var& x) {
    return x.graph()->CreateNode(
            Softmax(x.value()), x, no_operand, SoftmaxBackprop);
}

static void SigmoidBackprop(Var& val, Var* lhs, Var*) {
    const Eigen::MatrixXd& a = val.value();
    lhs->derivative() += val.derivative()
        .cwiseProduct((a.array() * (1.0 - a.array())).matrix());
}

Var Sigmoid(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = 1.0 / (1 + exp(-src_ptr[i]));
    }

    return x.graph()->CreateNode(res, x, no_operand, SigmoidBackprop);
}

static void SumBackprop(Var& val, Var* lhs, Var*) {
    lhs->derivative().array() += (double)val.derivative()(0, 0);
}

Var Sum(const Var& a) {
    Eigen::MatrixXd res(1, 1);
    res << a.value().sum();
    return a.graph()->CreateNode(res, a, no_operand, SumBackprop);
}

static void MeanBackprop(Var& val, Var* lhs, Var*) {
    lhs->derivative().array() +=
        (double)val.derivative()(0, 0) / val.value().size();
}

Var Mean(const Var& a) {
    Eigen::MatrixXd res(1, 1);
    res << a.value().sum() / a.value().size();
    return a.graph()->CreateNode(res, a, no_operand, MeanBackprop);
}

Var MSE(const Var& h, const Var& y) {
    return Mean(EltSquare(h - y));
}

Var SSE(const Var& h, const Var& y) {
    return Sum(EltSquare(h - y));
}

static void NthColBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative().col(rhs->value()(0, 0)) += val.derivative();
}

Var NthCol(const Var& w, int n) {
    Eigen::MatrixXd n_mat(1, 1);
    n_mat << n;
    Var n_var = w.graph()->CreateParam(n_mat);
    return w.graph()->CreateNode(w.value().col(n), w, n_var, NthColBackprop);
}

static void TanhBackprop(Var& val, Var* lhs, Var*) {
    double* da = lhs->derivative().data();
    const double* dx = val.derivative().data();
    const double* a = lhs->value().data();
    for (int i = 0; i < val.value().size(); ++i) {
        double denom = std::cosh(2 * a[i]) + 1;
        denom = denom * denom;
        if (std::isfinite(denom)) {
            double num = std::cosh(a[i]);
            num = 4 * num * num;
            double res = num / denom;
            da[i] += dx[i] * res;
        }
    }
}

Var Tanh(const Var& x) {
    Eigen::MatrixXd res(x.value().rows(), x.value().cols());
    double* dst_ptr = res.data();
    const double* src_ptr = x.value().data();
    for (int i = 0; i < res.size(); ++i) {
        dst_ptr[i] = std::tanh(src_ptr[i]);
    }

    return x.graph()->CreateNode(res, x, no_operand, TanhBackprop);
}

static void ColAppendBackprop(Var& val, Var* lhs, Var* rhs) {
    lhs->derivative() += val.derivative().block(0, 0, lhs->derivative().rows(), 1);
    rhs->derivative() += val.derivative().block(
            lhs->derivative().rows(), 0, rhs->derivative().rows(), 1);
}

Var ColAppend(Var x, Var y) {
    if (x.value().cols() != 1 || y.value().cols() != 1) {
        throw std::runtime_error("cannot append not-a-column-vectors");
    }

    Eigen::MatrixXd cated(x.value().rows() + y.value().rows(), 1);
    cated << x.value(), y.value();
    return x.graph()->CreateNode(cated, x, y, ColAppendBackprop);
}

}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: slidebitmap.hxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: rt $ $Date: 2004-11-26 19:20:53 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _SLIDESHOW_SLIDEBITMAP_HXX
#define _SLIDESHOW_SLIDEBITMAP_HXX

#ifndef _COM_SUN_STAR_UNO_REFERENCE_HXX_
#include <com/sun/star/uno/Reference.hxx>
#endif
#ifndef _CPPCANVAS_CANVAS_HXX
#include <cppcanvas/canvas.hxx>
#endif
#ifndef _CPPCANVAS_BITMAP_HXX
#include <cppcanvas/bitmap.hxx>
#endif

#ifndef _BGFX_POINT_B2DPOINT_HXX
#include <basegfx/point/b2dpoint.hxx>
#endif
#ifndef _BGFX_POLYGON_B2DPOLYPOLYGON_HXX
#include <basegfx/polygon/b2dpolypolygon.hxx>
#endif

#ifndef BOOST_SHARED_PTR_HPP_INCLUDED
#include <boost/shared_ptr.hpp>
#endif

namespace drafts { namespace com { namespace sun { namespace star { namespace rendering
{
    class XBitmap;
} } } } }

/* Definition of SlideBitmap class */

namespace presentation
{
    namespace internal
    {

        /** Little wrapper encapsulating an XBitmap

            This is to insulate us from changes to the preferred
            transport format for bitmaps (using a sole XBitmap here is
            a hack, since it is not guaranteed to work, or to work
            without data loss, across different canvases). And since
            we don't want to revert to a VCL Bitmap here, have to wait
            until basegfx bitmap tooling is ready.

            TODO(F2): Add support for Canvas-independent bitmaps
            here. Then, Slide::getInitialSlideBitmap and
            Slide::getFinalSlideBitmap must also be adapted (they no
            longer need a Canvas ptr, which is actually a hack now).
         */
        class SlideBitmap
        {
        public:
            SlideBitmap( const ::cppcanvas::BitmapSharedPtr& rBitmap );

            bool                draw( const ::cppcanvas::CanvasSharedPtr& rCanvas ) const;
            ::basegfx::B2ISize  getSize() const;
            void                move( const ::basegfx::B2DPoint& rNewPos );
            void                clip( const ::basegfx::B2DPolyPolygon& rClipPoly );

        private:
            // default: disabled copy/assignment
            SlideBitmap(const SlideBitmap&);
            SlideBitmap& operator=( const SlideBitmap& );

            ::basegfx::B2DPoint                                     maOutputPos;
            ::basegfx::B2DPolyPolygon                               maClipPoly;

            // TODO(Q2): Remove UNO bitmap as the transport medium
            ::com::sun::star::uno::Reference<
                ::drafts::com::sun::star::rendering::XBitmap >      mxBitmap;
        };

        typedef ::boost::shared_ptr< ::presentation::internal::SlideBitmap > SlideBitmapSharedPtr;

    }
}

#endif /* _SLIDESHOW_SLIDEBITMAP_HXX */
<commit_msg>INTEGRATION: CWS presfixes01 (1.2.6); FILE MERGED 2005/02/16 11:17:50 fs 1.2.6.1: #i42558# drafts.com.sun.star.drawing/rendering/geometry moved to com.sun.star.*<commit_after>/*************************************************************************
 *
 *  $RCSfile: slidebitmap.hxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: vg $ $Date: 2005-03-10 13:56:05 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _SLIDESHOW_SLIDEBITMAP_HXX
#define _SLIDESHOW_SLIDEBITMAP_HXX

#ifndef _COM_SUN_STAR_UNO_REFERENCE_HXX_
#include <com/sun/star/uno/Reference.hxx>
#endif
#ifndef _CPPCANVAS_CANVAS_HXX
#include <cppcanvas/canvas.hxx>
#endif
#ifndef _CPPCANVAS_BITMAP_HXX
#include <cppcanvas/bitmap.hxx>
#endif

#ifndef _BGFX_POINT_B2DPOINT_HXX
#include <basegfx/point/b2dpoint.hxx>
#endif
#ifndef _BGFX_POLYGON_B2DPOLYPOLYGON_HXX
#include <basegfx/polygon/b2dpolypolygon.hxx>
#endif

#ifndef BOOST_SHARED_PTR_HPP_INCLUDED
#include <boost/shared_ptr.hpp>
#endif

namespace com { namespace sun { namespace star { namespace rendering
{
    class XBitmap;
} } } }

/* Definition of SlideBitmap class */

namespace presentation
{
    namespace internal
    {

        /** Little wrapper encapsulating an XBitmap

            This is to insulate us from changes to the preferred
            transport format for bitmaps (using a sole XBitmap here is
            a hack, since it is not guaranteed to work, or to work
            without data loss, across different canvases). And since
            we don't want to revert to a VCL Bitmap here, have to wait
            until basegfx bitmap tooling is ready.

            TODO(F2): Add support for Canvas-independent bitmaps
            here. Then, Slide::getInitialSlideBitmap and
            Slide::getFinalSlideBitmap must also be adapted (they no
            longer need a Canvas ptr, which is actually a hack now).
         */
        class SlideBitmap
        {
        public:
            SlideBitmap( const ::cppcanvas::BitmapSharedPtr& rBitmap );

            bool                draw( const ::cppcanvas::CanvasSharedPtr& rCanvas ) const;
            ::basegfx::B2ISize  getSize() const;
            void                move( const ::basegfx::B2DPoint& rNewPos );
            void                clip( const ::basegfx::B2DPolyPolygon& rClipPoly );

        private:
            // default: disabled copy/assignment
            SlideBitmap(const SlideBitmap&);
            SlideBitmap& operator=( const SlideBitmap& );

            ::basegfx::B2DPoint                                     maOutputPos;
            ::basegfx::B2DPolyPolygon                               maClipPoly;

            // TODO(Q2): Remove UNO bitmap as the transport medium
            ::com::sun::star::uno::Reference<
                ::com::sun::star::rendering::XBitmap >      mxBitmap;
        };

        typedef ::boost::shared_ptr< ::presentation::internal::SlideBitmap > SlideBitmapSharedPtr;

    }
}

#endif /* _SLIDESHOW_SLIDEBITMAP_HXX */
<|endoftext|>


<commit_before>/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <af/array.h>
#include <af/defines.h>
#include <af/arith.h>
#include <af/data.h>
#include <ArrayInfo.hpp>
#include <optypes.hpp>
#include <implicit.hpp>
#include <err_common.hpp>
#include <handle.hpp>
#include <backend.hpp>

#include <arith.hpp>
#include <logic.hpp>

using namespace detail;
using af::dim4;

template<typename T, af_op_t op>
static inline af_array arithOp(const af_array lhs, const af_array rhs,
                               const dim4 &odims)
{
    af_array res = getHandle(arithOp<T, op>(castArray<T>(lhs), castArray<T>(rhs), odims));
    return res;
}

template<af_op_t op>
static af_err af_arith(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        const af_dtype otype = implicit(lhs, rhs);

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (otype) {
        case f32: res = arithOp<float  , op>(lhs, rhs, odims); break;
        case f64: res = arithOp<double , op>(lhs, rhs, odims); break;
        case c32: res = arithOp<cfloat , op>(lhs, rhs, odims); break;
        case c64: res = arithOp<cdouble, op>(lhs, rhs, odims); break;
        case s32: res = arithOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = arithOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = arithOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = arithOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = arithOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = arithOp<uintl  , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, otype);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

template<af_op_t op>
static af_err af_arith_real(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        const af_dtype otype = implicit(lhs, rhs);

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (otype) {
        case f32: res = arithOp<float  , op>(lhs, rhs, odims); break;
        case f64: res = arithOp<double , op>(lhs, rhs, odims); break;
        case s32: res = arithOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = arithOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = arithOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = arithOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = arithOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = arithOp<uintl  , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, otype);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

af_err af_add(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_add_t>(out, lhs, rhs, batchMode);
}

af_err af_mul(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_mul_t>(out, lhs, rhs, batchMode);
}

af_err af_sub(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_sub_t>(out, lhs, rhs, batchMode);
}

af_err af_div(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_div_t>(out, lhs, rhs, batchMode);
}

af_err af_maxof(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_max_t>(out, lhs, rhs, batchMode);
}

af_err af_minof(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_min_t>(out, lhs, rhs, batchMode);
}

af_err af_rem(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith_real<af_rem_t>(out, lhs, rhs, batchMode);
}

af_err af_mod(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith_real<af_mod_t>(out, lhs, rhs, batchMode);
}

af_err af_pow(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);
        if (linfo.isComplex() || rinfo.isComplex()) {
            AF_ERROR("Powers of Complex numbers not supported", AF_ERR_NOT_SUPPORTED);
        }
    } CATCHALL;

    return af_arith_real<af_pow_t>(out, lhs, rhs, batchMode);
}

af_err af_atan2(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {

        const af_dtype type = implicit(lhs, rhs);

        if (type != f32 && type != f64) {
            AF_ERROR("Only floating point arrays are supported for atan2 ",
                     AF_ERR_NOT_SUPPORTED);
        }

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (type) {
        case f32: res = arithOp<float , af_atan2_t>(lhs, rhs, odims); break;
        case f64: res = arithOp<double, af_atan2_t>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

af_err af_hypot(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {

        const af_dtype type = implicit(lhs, rhs);

        if (type != f32 && type != f64) {
            AF_ERROR("Only floating point arrays are supported for hypot ",
                     AF_ERR_NOT_SUPPORTED);
        }

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (type) {
        case f32: res = arithOp<float , af_hypot_t>(lhs, rhs, odims); break;
        case f64: res = arithOp<double, af_hypot_t>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

template<typename T, af_op_t op>
static inline af_array logicOp(const af_array lhs, const af_array rhs, const dim4 &odims)
{
    af_array res = getHandle(logicOp<T, op>(getArray<T>(lhs), getArray<T>(rhs), odims));
    return res;
}

template<af_op_t op>
static af_err af_logic(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        const af_dtype type = implicit(lhs, rhs);

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (type) {
        case f32: res = logicOp<float  , op>(lhs, rhs, odims); break;
        case f64: res = logicOp<double , op>(lhs, rhs, odims); break;
        case c32: res = logicOp<cfloat , op>(lhs, rhs, odims); break;
        case c64: res = logicOp<cdouble, op>(lhs, rhs, odims); break;
        case s32: res = logicOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = logicOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = logicOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = logicOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = logicOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = logicOp<uintl  , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

af_err af_eq(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_eq_t>(out, lhs, rhs, batchMode);
}

af_err af_neq(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_neq_t>(out, lhs, rhs, batchMode);
}

af_err af_gt(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_gt_t>(out, lhs, rhs, batchMode);
}

af_err af_ge(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_ge_t>(out, lhs, rhs, batchMode);
}

af_err af_lt(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_lt_t>(out, lhs, rhs, batchMode);
}

af_err af_le(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_le_t>(out, lhs, rhs, batchMode);
}

af_err af_and(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_and_t>(out, lhs, rhs, batchMode);
}

af_err af_or(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_or_t>(out, lhs, rhs, batchMode);
}

template<typename T, af_op_t op>
static inline af_array bitOp(const af_array lhs, const af_array rhs, const dim4 &odims)
{
    af_array res = getHandle(bitOp<T, op>(getArray<T>(lhs), getArray<T>(rhs), odims));
    return res;
}

template<af_op_t op>
static af_err af_bitwise(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        const af_dtype type = implicit(lhs, rhs);

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (type) {
        case s32: res = bitOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = bitOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = bitOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = bitOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = bitOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = bitOp<uintl  , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

af_err af_bitand(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitand_t>(out, lhs, rhs, batchMode);
}

af_err af_bitor(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitor_t>(out, lhs, rhs, batchMode);
}

af_err af_bitxor(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitxor_t>(out, lhs, rhs, batchMode);
}

af_err af_bitshiftl(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitshiftl_t>(out, lhs, rhs, batchMode);
}

af_err af_bitshiftr(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitshiftr_t>(out, lhs, rhs, batchMode);
}
<commit_msg>BUGFIX: Cast logical and bitwise operators to the right type<commit_after>/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <af/array.h>
#include <af/defines.h>
#include <af/arith.h>
#include <af/data.h>
#include <ArrayInfo.hpp>
#include <optypes.hpp>
#include <implicit.hpp>
#include <err_common.hpp>
#include <handle.hpp>
#include <backend.hpp>

#include <arith.hpp>
#include <logic.hpp>

using namespace detail;
using af::dim4;

template<typename T, af_op_t op>
static inline af_array arithOp(const af_array lhs, const af_array rhs,
                               const dim4 &odims)
{
    af_array res = getHandle(arithOp<T, op>(castArray<T>(lhs), castArray<T>(rhs), odims));
    return res;
}

template<af_op_t op>
static af_err af_arith(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        const af_dtype otype = implicit(lhs, rhs);

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (otype) {
        case f32: res = arithOp<float  , op>(lhs, rhs, odims); break;
        case f64: res = arithOp<double , op>(lhs, rhs, odims); break;
        case c32: res = arithOp<cfloat , op>(lhs, rhs, odims); break;
        case c64: res = arithOp<cdouble, op>(lhs, rhs, odims); break;
        case s32: res = arithOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = arithOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = arithOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = arithOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = arithOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = arithOp<uintl  , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, otype);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

template<af_op_t op>
static af_err af_arith_real(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        const af_dtype otype = implicit(lhs, rhs);

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (otype) {
        case f32: res = arithOp<float  , op>(lhs, rhs, odims); break;
        case f64: res = arithOp<double , op>(lhs, rhs, odims); break;
        case s32: res = arithOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = arithOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = arithOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = arithOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = arithOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = arithOp<uintl  , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, otype);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

af_err af_add(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_add_t>(out, lhs, rhs, batchMode);
}

af_err af_mul(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_mul_t>(out, lhs, rhs, batchMode);
}

af_err af_sub(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_sub_t>(out, lhs, rhs, batchMode);
}

af_err af_div(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_div_t>(out, lhs, rhs, batchMode);
}

af_err af_maxof(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_max_t>(out, lhs, rhs, batchMode);
}

af_err af_minof(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith<af_min_t>(out, lhs, rhs, batchMode);
}

af_err af_rem(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith_real<af_rem_t>(out, lhs, rhs, batchMode);
}

af_err af_mod(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_arith_real<af_mod_t>(out, lhs, rhs, batchMode);
}

af_err af_pow(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);
        if (linfo.isComplex() || rinfo.isComplex()) {
            AF_ERROR("Powers of Complex numbers not supported", AF_ERR_NOT_SUPPORTED);
        }
    } CATCHALL;

    return af_arith_real<af_pow_t>(out, lhs, rhs, batchMode);
}

af_err af_atan2(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {

        const af_dtype type = implicit(lhs, rhs);

        if (type != f32 && type != f64) {
            AF_ERROR("Only floating point arrays are supported for atan2 ",
                     AF_ERR_NOT_SUPPORTED);
        }

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (type) {
        case f32: res = arithOp<float , af_atan2_t>(lhs, rhs, odims); break;
        case f64: res = arithOp<double, af_atan2_t>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

af_err af_hypot(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {

        const af_dtype type = implicit(lhs, rhs);

        if (type != f32 && type != f64) {
            AF_ERROR("Only floating point arrays are supported for hypot ",
                     AF_ERR_NOT_SUPPORTED);
        }

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (type) {
        case f32: res = arithOp<float , af_hypot_t>(lhs, rhs, odims); break;
        case f64: res = arithOp<double, af_hypot_t>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

template<typename T, af_op_t op>
static inline af_array logicOp(const af_array lhs, const af_array rhs, const dim4 &odims)
{
    af_array res = getHandle(logicOp<T, op>(castArray<T>(lhs), castArray<T>(rhs), odims));
    return res;
}

template<af_op_t op>
static af_err af_logic(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        const af_dtype type = implicit(lhs, rhs);

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (type) {
        case f32: res = logicOp<float  , op>(lhs, rhs, odims); break;
        case f64: res = logicOp<double , op>(lhs, rhs, odims); break;
        case c32: res = logicOp<cfloat , op>(lhs, rhs, odims); break;
        case c64: res = logicOp<cdouble, op>(lhs, rhs, odims); break;
        case s32: res = logicOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = logicOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = logicOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = logicOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = logicOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = logicOp<uintl  , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

af_err af_eq(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_eq_t>(out, lhs, rhs, batchMode);
}

af_err af_neq(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_neq_t>(out, lhs, rhs, batchMode);
}

af_err af_gt(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_gt_t>(out, lhs, rhs, batchMode);
}

af_err af_ge(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_ge_t>(out, lhs, rhs, batchMode);
}

af_err af_lt(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_lt_t>(out, lhs, rhs, batchMode);
}

af_err af_le(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_le_t>(out, lhs, rhs, batchMode);
}

af_err af_and(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_and_t>(out, lhs, rhs, batchMode);
}

af_err af_or(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_logic<af_or_t>(out, lhs, rhs, batchMode);
}

template<typename T, af_op_t op>
static inline af_array bitOp(const af_array lhs, const af_array rhs, const dim4 &odims)
{
    af_array res = getHandle(bitOp<T, op>(castArray<T>(lhs), castArray<T>(rhs), odims));
    return res;
}

template<af_op_t op>
static af_err af_bitwise(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    try {
        const af_dtype type = implicit(lhs, rhs);

        ArrayInfo linfo = getInfo(lhs);
        ArrayInfo rinfo = getInfo(rhs);

        dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

        af_array res;
        switch (type) {
        case s32: res = bitOp<int    , op>(lhs, rhs, odims); break;
        case u32: res = bitOp<uint   , op>(lhs, rhs, odims); break;
        case u8 : res = bitOp<uchar  , op>(lhs, rhs, odims); break;
        case b8 : res = bitOp<char   , op>(lhs, rhs, odims); break;
        case s64: res = bitOp<intl   , op>(lhs, rhs, odims); break;
        case u64: res = bitOp<uintl  , op>(lhs, rhs, odims); break;
        default: TYPE_ERROR(0, type);
        }

        std::swap(*out, res);
    }
    CATCHALL;
    return AF_SUCCESS;
}

af_err af_bitand(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitand_t>(out, lhs, rhs, batchMode);
}

af_err af_bitor(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitor_t>(out, lhs, rhs, batchMode);
}

af_err af_bitxor(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitxor_t>(out, lhs, rhs, batchMode);
}

af_err af_bitshiftl(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitshiftl_t>(out, lhs, rhs, batchMode);
}

af_err af_bitshiftr(af_array *out, const af_array lhs, const af_array rhs, bool batchMode)
{
    return af_bitwise<af_bitshiftr_t>(out, lhs, rhs, batchMode);
}
<|endoftext|>


<commit_before><commit_msg>Be extra-careful about closing pipes; don't close stdout/stderr by mistake!<commit_after><|endoftext|>


<commit_before><commit_msg>FindPidsST corrected for incorrect subsampling, pid types changed to std::vectors<commit_after><|endoftext|>


<commit_before><commit_msg>INTEGRATION: CWS pchfix02 (1.4.92); FILE MERGED 2006/09/01 17:43:19 kaib 1.4.92.1: #i68856# Added header markers and pch files<commit_after><|endoftext|>


<commit_before>#include <QCoreApplication>

#include "./mindwavecontroller.h"
#include "./arduinointerface.h"

int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);

    if (app.arguments().size() != 3) {
        qDebug() << "Invalid Arguments provided";
        qDebug() << "Correct arguement syntax is [MindWaveSerialPort] [ArduinoSerialPort]";
        return 1;
    }

    MindWaveController controller1;
    if (controller1.initController(app.arguments().at(1))) {
        return 2;  // failed to open Serial Port with MindWave controller
    }

    ArduinoInterface arduino;
    if (arduino.init(app.arguments().at(2))) {
        return 3; // failed to connect to the arduino's serial port
    }

    // write BCI data to Arduino
    // changed the connected signal to change the output data written to the arduino
    arduino.connect(&controller1, &MindWaveController::attentionDataChanged,
                       [&](uint16_t data){
        arduino.write(QByteArray(1, data&0xFF));
    });

    return app.exec(); // start event loop
}
<commit_msg>added max speed limit to scalextric car speed<commit_after>#include <QCoreApplication>

#include "./mindwavecontroller.h"
#include "./arduinointerface.h"

const uint8_t maxSpeed= 70;

int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);

    if (app.arguments().size() != 3) {
        qDebug() << "Invalid Arguments provided";
        qDebug() << "Correct arguement syntax is [MindWaveSerialPort] [ArduinoSerialPort]";
        return 1;
    }

    MindWaveController controller1;
    if (controller1.initController(app.arguments().at(1))) {
        return 2;  // failed to open Serial Port with MindWave controller
    }

    ArduinoInterface arduino;
    if (arduino.init(app.arguments().at(2))) {
        return 3; // failed to connect to the arduino's serial port
    }

    // write BCI data to Arduino
    // changed the connected signal to change the output data written to the arduino
    arduino.connect(&controller1, &MindWaveController::attentionDataChanged,
                       [&](uint16_t data){
        QByteArray outputdata;
        outputdata.append(0x10);

        if (data > maxSpeed) {
            outputdata.append(maxSpeed);
        } else {
            outputdata.append(data&0xFF);
        }

        qDebug() << "Player Code: " << static_cast<uint8_t>(outputdata.at(0))
                 << "Player Level: "<< static_cast<uint8_t>(outputdata.at(1));

        arduino.write(outputdata);
    });

    return app.exec(); // start event loop
}
<|endoftext|>


<commit_before>#ifndef __PERFMON_HPP__
#define __PERFMON_HPP__

#include <string>
#include <map>
#include "config/alloc.hpp"
#include "utils.hpp"

// Horrible hack because we define fail() as a macro
#pragma push_macro("fail")
#undef fail
#include <sstream>
#pragma pop_macro("fail")

class perfmon_watcher_t;
class perfmon_fsm_t;

typedef std::basic_string<char, std::char_traits<char>, gnew_alloc<char> > std_string_t;

typedef std::map<std_string_t, std_string_t, std::less<std_string_t>,
    gnew_alloc<std::pair<std_string_t, std_string_t> > > perfmon_stats_t;

struct perfmon_callback_t {
    virtual void on_perfmon_stats() = 0;
};

class perfmon_controller_t {
    friend class perfmon_watcher_t;
    friend class one_perfmon_fsm_t;
    friend class perfmon_fsm_t;

public:
    perfmon_controller_t();
    ~perfmon_controller_t();

    bool get_stats(perfmon_stats_t *dest, perfmon_callback_t *cb);

    static perfmon_controller_t *controller;

private:
    typedef std::map<std_string_t, perfmon_watcher_t*, std::less<std_string_t>,
        gnew_alloc<std::pair<std_string_t, perfmon_watcher_t*> > > var_map_t;
    var_map_t *var_maps[MAX_CPUS];
};

class perfmon_watcher_t :
    public home_cpu_mixin_t
{
public:
    perfmon_watcher_t(const char *name);
    ~perfmon_watcher_t();
    virtual std_string_t get_value() = 0;

private:
    const char *name;
};

template<class var_t>
class perfmon_var_t :
    public perfmon_watcher_t
{
public:
    perfmon_var_t(const char *name, var_t *var)
        : perfmon_watcher_t(name), var(var) { }

    std_string_t get_value() {
        std::basic_stringstream<char, std::char_traits<char>, gnew_alloc<char> > s;
        s << *var;
        return s.str();
    }

    var_t *var;
};

#endif /* __PERFMON_HPP__ */
<commit_msg>Removing push_macro/pop_macro because they dont work on all versions of GCC we want. Adding an even shittier fix for now<commit_after>#ifndef __PERFMON_HPP__
#define __PERFMON_HPP__

#include <string>
#include <map>
#include "config/alloc.hpp"
#include "utils.hpp"

// Horrible hack because we define fail() as a macro
#undef fail
#include <sstream>
#define fail(...) _fail(__FILE__, __LINE__, __VA_ARGS__)

class perfmon_watcher_t;
class perfmon_fsm_t;

typedef std::basic_string<char, std::char_traits<char>, gnew_alloc<char> > std_string_t;

typedef std::map<std_string_t, std_string_t, std::less<std_string_t>,
    gnew_alloc<std::pair<std_string_t, std_string_t> > > perfmon_stats_t;

struct perfmon_callback_t {
    virtual void on_perfmon_stats() = 0;
};

class perfmon_controller_t {
    friend class perfmon_watcher_t;
    friend class one_perfmon_fsm_t;
    friend class perfmon_fsm_t;

public:
    perfmon_controller_t();
    ~perfmon_controller_t();

    bool get_stats(perfmon_stats_t *dest, perfmon_callback_t *cb);

    static perfmon_controller_t *controller;

private:
    typedef std::map<std_string_t, perfmon_watcher_t*, std::less<std_string_t>,
        gnew_alloc<std::pair<std_string_t, perfmon_watcher_t*> > > var_map_t;
    var_map_t *var_maps[MAX_CPUS];
};

class perfmon_watcher_t :
    public home_cpu_mixin_t
{
public:
    perfmon_watcher_t(const char *name);
    ~perfmon_watcher_t();
    virtual std_string_t get_value() = 0;

private:
    const char *name;
};

template<class var_t>
class perfmon_var_t :
    public perfmon_watcher_t
{
public:
    perfmon_var_t(const char *name, var_t *var)
        : perfmon_watcher_t(name), var(var) { }

    std_string_t get_value() {
        std::basic_stringstream<char, std::char_traits<char>, gnew_alloc<char> > s;
        s << *var;
        return s.str();
    }

    var_t *var;
};

#endif /* __PERFMON_HPP__ */
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: sfxresid.cxx,v $
 *
 *  $Revision: 1.6 $
 *
 *  last change: $Author: rt $ $Date: 2007-04-26 10:08:44 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sfx2.hxx"

#ifndef GCC
#endif

#ifndef _TOOLS_SIMPLERESMGR_HXX_
#include <tools/simplerm.hxx>
#endif

#include "sfxresid.hxx"
#include "app.hxx"

// -----------------------------------------------------------------------

static ResMgr* pMgr=NULL;

SfxResId::SfxResId( USHORT nId ) :

    ResId( nId, *GetResMgr() )
{
}

//============================================================================
//
// SfxSimpleResId Implementation.
//
//============================================================================

SfxSimpleResId::SfxSimpleResId(USHORT nID):
    m_sText( SFX_APP()->GetSimpleResManager()->ReadString(nID) )
{}

ResMgr* SfxResId::GetResMgr()
{
    if ( !pMgr )
    {
        pMgr = SfxApplication::CreateResManager("sfx");
    }

    return pMgr;
}

void SfxResId::DeleteResMgr()
{
    DELETEZ( pMgr );
}

<commit_msg>INTEGRATION: CWS vgbugs07 (1.6.32); FILE MERGED 2007/06/04 13:34:43 vg 1.6.32.1: #i76605# Remove -I .../inc/module hack introduced by hedaburemove01<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: sfxresid.cxx,v $
 *
 *  $Revision: 1.7 $
 *
 *  last change: $Author: hr $ $Date: 2007-06-27 23:04:28 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sfx2.hxx"

#ifndef GCC
#endif

#ifndef _TOOLS_SIMPLERESMGR_HXX_
#include <tools/simplerm.hxx>
#endif

#include "sfxresid.hxx"
#include <sfx2/app.hxx>

// -----------------------------------------------------------------------

static ResMgr* pMgr=NULL;

SfxResId::SfxResId( USHORT nId ) :

    ResId( nId, *GetResMgr() )
{
}

//============================================================================
//
// SfxSimpleResId Implementation.
//
//============================================================================

SfxSimpleResId::SfxSimpleResId(USHORT nID):
    m_sText( SFX_APP()->GetSimpleResManager()->ReadString(nID) )
{}

ResMgr* SfxResId::GetResMgr()
{
    if ( !pMgr )
    {
        pMgr = SfxApplication::CreateResManager("sfx");
    }

    return pMgr;
}

void SfxResId::DeleteResMgr()
{
    DELETEZ( pMgr );
}

<|endoftext|>


<commit_before>/*
 * Static.hpp
 *
 *  Created on: Jan 13, 2011
 *      Author: Peter Goodman
 *     Version: $Id$
 *
 * Copyright 2011 Peter Goodman, all rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef FLTL_STATIC_HPP_
#define FLTL_STATIC_HPP_

#include <new>

namespace fltl { namespace mpl {

    namespace detail {

        template <typename T>
        class StaticValue {
        public:
            inline static T default_value(void) throw() {
                static T val;
                static bool is_initialized(false);
                if(!is_initialized) {
                    is_initialized = true;
                    new (&val) T;
                }
                return val;
            }
        };

        template <typename T>
        class StaticValue<T *> {
        public:
            inline static T *default_value(void) throw() {
                return 0;
            }
        };

        template <>
        class StaticValue<char> {
        public:
            inline static char default_value(void) throw() {
                return '\0';
            }
        };

        template <>
        class StaticValue<unsigned char> {
        public:
            inline static unsigned char default_value(void) throw() {
                return static_cast<unsigned char>(0U);
            }
        };

        template <>
        class StaticValue<short> {
        public:
            inline static short default_value(void) throw() {
                return 0;
            }
        };

        template <>
        class StaticValue<unsigned short> {
        public:
            inline static unsigned short default_value(void) throw() {
                return 0U;
            }
        };

        template <>
        class StaticValue<int> {
        public:
            inline static int default_value(void) throw() {
                return 0;
            }
        };

        template <>
        class StaticValue<unsigned> {
        public:
            inline static unsigned default_value(void) throw() {
                return 0U;
            }
        };

        template <>
        class StaticValue<long> {
        public:
            inline static long default_value(void) throw() {
                return 0L;
            }
        };

        template <>
        class StaticValue<unsigned long> {
        public:
            inline static unsigned long default_value(void) throw() {
                return 0UL;
            }
        };

        template <>
        class StaticValue<float> {
        public:
            inline static float default_value(void) throw() {
                return 0.0;
            }
        };

        template <>
        class StaticValue<double> {
        public:
            inline static double default_value(void) throw() {
                return 0.0;
            }
        };
    }

    /// produces a value of a given type
    template <typename T>
    class Static {
    public:
        static const T VALUE;
    };

    template <typename T>
    const T Static<T>::VALUE(detail::StaticValue<T>::default_value());
}}

#endif /* FLTL_STATIC_HPP_ */
<commit_msg>working on compiling with vc++<commit_after>/*
 * Static.hpp
 *
 *  Created on: Jan 13, 2011
 *      Author: Peter Goodman
 *     Version: $Id$
 *
 * Copyright 2011 Peter Goodman, all rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef FLTL_STATIC_HPP_
#define FLTL_STATIC_HPP_

#include <new>

namespace fltl { namespace mpl {

    namespace detail {

        template <typename T>
        class StaticValue {
        public:
            inline static T &default_value(void) throw() {
                static T val;
                static bool is_initialized(false);
                if(!is_initialized) {
                    is_initialized = true;
                    new (&val) T;
                }
                return val;
            }
        };

        template <typename T>
        class StaticValue<T *> {
        public:
            inline static T *&default_value(void) throw() {
                static T *val(0);
                return val;
            }
        };

        template <>
        class StaticValue<char> {
        public:
            inline static char &default_value(void) throw() {
                static char val('\0');
                return val;
            }
        };

        template <>
        class StaticValue<unsigned char> {
        public:
            inline static unsigned char &default_value(void) throw() {
                static unsigned char val(0U);
                return val;
            }
        };

        template <>
        class StaticValue<short> {
        public:
            inline static short &default_value(void) throw() {
                static short val(0);
                return val;
            }
        };

        template <>
        class StaticValue<unsigned short> {
        public:
            inline static unsigned short &default_value(void) throw() {
                static unsigned short val(0U);
                return val;
            }
        };

        template <>
        class StaticValue<int> {
        public:
            inline static int &default_value(void) throw() {
                static int val(0);
                return val;
            }
        };

        template <>
        class StaticValue<unsigned> {
        public:
            inline static unsigned &default_value(void) throw() {
                static unsigned val(0U);
                return val;
            }
        };

        template <>
        class StaticValue<long> {
        public:
            inline static long &default_value(void) throw() {
                static long val(0L);
                return val;
            }
        };

        template <>
        class StaticValue<unsigned long> {
        public:
            inline static unsigned long &default_value(void) throw() {
                static unsigned long val(0UL);
                return val;
            }
        };

        template <>
        class StaticValue<float> {
        public:
            inline static float &default_value(void) throw() {
                static float val(0.0);
                return val;
            }
        };

        template <>
        class StaticValue<double> {
        public:
            inline static double &default_value(void) throw() {
                static double val(0.0);
                return val;
            }
        };
    }

    /// produces a value of a given type
    template <typename T>
    class Static {
    public:
        static const T &VALUE;
    };

    template <typename T>
    const T &Static<T>::VALUE(detail::StaticValue<T>::default_value());
}}

#endif /* FLTL_STATIC_HPP_ */
<|endoftext|>


<commit_before>/**
 * This file is part of the "libfnord" project
 *   Copyright (c) 2015 Paul Asmuth
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <fnord-msg/MessageEncoder.h>

namespace fnord {
namespace msg {

void MessageEncoder::encode(
    const MessageObject& msg,
    const MessageSchema& schema,
    Buffer* buf) {
  Vector<Pair<uint32_t, uint64_t>> fields;
  util::BinaryMessageWriter header;
  util::BinaryMessageWriter body;

  encodeObject(msg, schema, &fields, &body);

  header.appendVarUInt(fields.size());
  for (const auto& f : fields) {
    header.appendVarUInt(f.first);
    header.appendVarUInt(f.second);
  }

  buf->append(header.data(), header.size());
  buf->append(body.data(), body.size());
}

void MessageEncoder::encodeObject(
    const MessageObject& msg,
    const MessageSchema& schema,
    Vector<Pair<uint32_t, uint64_t>>* fields,
    util::BinaryMessageWriter* data) {
  switch (schema.type(msg.id)) {

    case FieldType::OBJECT: {
      Vector<Pair<uint32_t, uint64_t>> obj_fields;
      for (const auto& o : msg.asObject()) {
        encodeObject(o, schema, &obj_fields, data);
      }

      fields->emplace_back(msg.id, data->size());
      for (const auto& f : obj_fields) {
        fields->emplace_back(f);
      }
      return;
    }

    case FieldType::STRING: {
      const auto& str = msg.asString();
      data->append(str.data(), str.size());
      break;
    }

    case FieldType::UINT32:
      data->appendVarUInt(msg.asUInt32());
      break;

    case FieldType::BOOLEAN:
      data->appendUInt8(msg.asBool() ? 1 : 0);
      break;

  }

  fields->emplace_back(msg.id, data->size());
}

} // namespace msg
} // namespace fnord

<commit_msg>better error message<commit_after>/**
 * This file is part of the "libfnord" project
 *   Copyright (c) 2015 Paul Asmuth
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <fnord-msg/MessageEncoder.h>

namespace fnord {
namespace msg {

void MessageEncoder::encode(
    const MessageObject& msg,
    const MessageSchema& schema,
    Buffer* buf) {
  Vector<Pair<uint32_t, uint64_t>> fields;
  util::BinaryMessageWriter header;
  util::BinaryMessageWriter body;

  encodeObject(msg, schema, &fields, &body);

  header.appendVarUInt(fields.size());
  for (const auto& f : fields) {
    header.appendVarUInt(f.first);
    header.appendVarUInt(f.second);
  }

  buf->append(header.data(), header.size());
  buf->append(body.data(), body.size());
}

void MessageEncoder::encodeObject(
    const MessageObject& msg,
    const MessageSchema& schema,
    Vector<Pair<uint32_t, uint64_t>>* fields,
    util::BinaryMessageWriter* data) {
  try {
    switch (schema.type(msg.id)) {

      case FieldType::OBJECT: {
        Vector<Pair<uint32_t, uint64_t>> obj_fields;
        for (const auto& o : msg.asObject()) {
          encodeObject(o, schema, &obj_fields, data);
        }

        fields->emplace_back(msg.id, data->size());
        for (const auto& f : obj_fields) {
          fields->emplace_back(f);
        }
        return;
      }

      case FieldType::STRING: {
        const auto& str = msg.asString();
        data->append(str.data(), str.size());
        break;
      }

      case FieldType::UINT32:
        data->appendVarUInt(msg.asUInt32());
        break;

      case FieldType::BOOLEAN:
        data->appendUInt8(msg.asBool() ? 1 : 0);
        break;

    }

    fields->emplace_back(msg.id, data->size());
  } catch (const std::exception& e) {
    RAISEF(
        kRuntimeError,
        "error while encoding field $0 ('$1'): $2",
        msg.id,
        schema.name(msg.id),
        e.what());
  }
}

} // namespace msg
} // namespace fnord

<|endoftext|>


<commit_before>#pragma once

#include <boost/thread.hpp>
#include <opencv2/core/core.hpp>

// Base class for output.  Derived classes are for writing
// AVI videos, zms (video + depth), plus whatever else we
// imagine in the future.
class MediaOut
{
   public:
		MediaOut(int frameSkip, int framesPerFile);
		virtual ~MediaOut();
		bool saveFrame(const cv::Mat &frame, const cv::Mat &depth);
		void sync(void);

   protected:
		// The base class calls these dervied classes to do the
		// heavy lifting.  They have to be implemented in the
		// base class as well, but hopefully those are never
		// called
		virtual bool openNext(void);
		virtual bool write(const cv::Mat &frame, const cv::Mat &depth);

		// Skip output frames if requested.  Skip is how many to
		// skip before writing next output frame, FrameCounter is how
		// many total frames seen.
		// Counter is used to split the output into multiple shorter
		// outputs
		int frameSkip_;
		int frameCounter_;
		int fileCounter_;
		int framesPerFile_;

   private:
		void writeThread(void);
		cv::Mat frame_;
		cv::Mat depth_;
		boost::mutex matLock_;
		boost::condition_variable frameCond_;
		bool frameReady_;
		bool writePending_;
		boost::thread thread_;
};
<commit_msg>Added framesomething<commit_after>#pragma once

#include <boost/thread.hpp>
#include <opencv2/core/core.hpp>

// Base class for output.  Derived classes are for writing
// AVI videos, zms (video + depth), plus whatever else we
// imagine in the future.
class MediaOut
{
   public:
		MediaOut(int frameSkip, int framesPerFile);
		virtual ~MediaOut();
		bool saveFrame(const cv::Mat &frame, const cv::Mat &depth);
		void sync(void);

   protected:
		// The base class calls these dervied classes to do the
		// heavy lifting.  They have to be implemented in the
		// base class as well, but hopefully those are never
		// called
		virtual bool openNext(int fileCounter);
		virtual bool write(const cv::Mat &frame, const cv::Mat &depth);

   private:
		// Skip output frames if requested.  Skip is how many to
		// skip before writing next output frame, FrameCounter is how
		// many total frames seen.
		// Counter is used to split the output into multiple shorter
		// outputs
		int frameSkip_;
		int frameCounter_;
		int fileCounter_;
		int framesPerFile_;
		int framesThisFile_;
		void writeThread(void);
		cv::Mat frame_;
		cv::Mat depth_;
		boost::mutex matLock_;
		boost::condition_variable frameCond_;
		bool frameReady_;
		bool writePending_;
		boost::thread thread_;
};
<|endoftext|>


<commit_before>/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "qmlmodelnodefacade.h"
#include "qmlmodelview.h"
#include <QtDebug>
namespace QmlDesigner {

QmlModelNodeFacade::QmlModelNodeFacade() : m_modelNode(ModelNode())
{}

QmlModelNodeFacade::QmlModelNodeFacade(const ModelNode &modelNode) : m_modelNode(modelNode)
{}

QmlModelNodeFacade::~QmlModelNodeFacade()
{}

bool QmlModelNodeFacade::isValid() const
{
    return modelNode().isValid() && qmlModelView() && qmlModelView()->hasInstanceForModelNode(modelNode()) && qmlModelView()->instanceForModelNode(modelNode()).isValid();
}

QmlModelView* QmlModelNodeFacade::qmlModelView() const
{
    return modelNode().view()->toQmlModelView();
}

bool QmlModelNodeFacade::isRootNode() const
{
    return modelNode().isRootNode();
}
} //QmlDesigner
<commit_msg>QmlDesigner.model: crashfix<commit_after>/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "qmlmodelnodefacade.h"
#include "qmlmodelview.h"
#include <QtDebug>
namespace QmlDesigner {

QmlModelNodeFacade::QmlModelNodeFacade() : m_modelNode(ModelNode())
{}

QmlModelNodeFacade::QmlModelNodeFacade(const ModelNode &modelNode) : m_modelNode(modelNode)
{}

QmlModelNodeFacade::~QmlModelNodeFacade()
{}

bool QmlModelNodeFacade::isValid() const
{
    return modelNode().isValid() && qmlModelView() && qmlModelView()->nodeInstanceView() && qmlModelView()->hasInstanceForModelNode(modelNode()) && qmlModelView()->instanceForModelNode(modelNode()).isValid();
}

QmlModelView* QmlModelNodeFacade::qmlModelView() const
{
    return modelNode().view()->toQmlModelView();
}

bool QmlModelNodeFacade::isRootNode() const
{
    return modelNode().isRootNode();
}
} //QmlDesigner
<|endoftext|>