Skip to content
Snippets Groups Projects
binary-op.hh 2.55 KiB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
/*
 * Copyright 2010,
 * François Bleibel,
 * Olivier Stasse,
Thomas Moulard's avatar
Thomas Moulard committed
 * CNRS/AIST
Thomas Moulard's avatar
Thomas Moulard committed
 */
Florent Lamiraux's avatar
Florent Lamiraux committed
#ifndef SOT_CORE_BINARYOP_HH
#define SOT_CORE_BINARYOP_HH

/* --------------------------------------------------------------------- */
/* --- INCLUDE --------------------------------------------------------- */
/* --------------------------------------------------------------------- */

/* Matrix */
#include <dynamic-graph/linear-algebra.h>

/* SOT */
#include <dynamic-graph/all-signals.h>
Guilhem Saurel's avatar
Guilhem Saurel committed
#include <dynamic-graph/entity.h>
Guilhem Saurel's avatar
Guilhem Saurel committed

Guilhem Saurel's avatar
Guilhem Saurel committed
#include <sot/core/flags.hh>
Guilhem Saurel's avatar
Guilhem Saurel committed
#include <sot/core/pool.hh>

/* STD */
#include <boost/function.hpp>
Guilhem Saurel's avatar
Guilhem Saurel committed
#include <string>
namespace dynamicgraph {
Guilhem Saurel's avatar
Guilhem Saurel committed
namespace sot {
Guilhem Saurel's avatar
Guilhem Saurel committed
/* --------------------------------------------------------------------- */
/* --- CLASS ----------------------------------------------------------- */
/* --------------------------------------------------------------------- */
Guilhem Saurel's avatar
Guilhem Saurel committed
template <typename Operator>
class BinaryOp : public Entity {
Guilhem Saurel's avatar
Guilhem Saurel committed
  Operator op;
  typedef typename Operator::Tin1 Tin1;
  typedef typename Operator::Tin2 Tin2;
  typedef typename Operator::Tout Tout;
  typedef BinaryOp<Operator> Self;

Guilhem Saurel's avatar
Guilhem Saurel committed
 public: /* --- CONSTRUCTION --- */
Guilhem Saurel's avatar
Guilhem Saurel committed
  static std::string getTypeIn1Name(void) { return Operator::nameTypeIn1(); }
  static std::string getTypeIn2Name(void) { return Operator::nameTypeIn2(); }
  static std::string getTypeOutName(void) { return Operator::nameTypeOut(); }
  static const std::string CLASS_NAME;
  virtual const std::string &getClassName() const { return CLASS_NAME; }
  std::string getDocString() const { return op.getDocString(); }

  BinaryOp(const std::string &name)
      : Entity(name),
        SIN1(NULL, BinaryOp::CLASS_NAME + "(" + name + ")::input(" +
                       getTypeIn1Name() + ")::sin1"),
        SIN2(NULL, CLASS_NAME + "(" + name + ")::input(" + getTypeIn2Name() +
                       ")::sin2"),
        SOUT(boost::bind(&Self::computeOperation, this, _1, _2), SIN1 << SIN2,
             CLASS_NAME + "(" + name + ")::output(" + getTypeOutName() +
                 ")::sout") {
    signalRegistration(SIN1 << SIN2 << SOUT);
    op.addSpecificCommands(*this, commandMap);
  }

  virtual ~BinaryOp(void){};

Guilhem Saurel's avatar
Guilhem Saurel committed
 public: /* --- SIGNAL --- */
Guilhem Saurel's avatar
Guilhem Saurel committed
  SignalPtr<Tin1, int> SIN1;
  SignalPtr<Tin2, int> SIN2;
  SignalTimeDependent<Tout, int> SOUT;

Guilhem Saurel's avatar
Guilhem Saurel committed
 protected:
Guilhem Saurel's avatar
Guilhem Saurel committed
  Tout &computeOperation(Tout &res, int time) {
    const Tin1 &x1 = SIN1(time);
    const Tin2 &x2 = SIN2(time);
    op(x1, x2, res);
    return res;
  }
};
Guilhem Saurel's avatar
Guilhem Saurel committed
}  // namespace sot
}  // namespace dynamicgraph
Guilhem Saurel's avatar
Guilhem Saurel committed
#endif  // #ifndef SOT_CORE_BINARYOP_HH