<commit_before>#ifndef ZXTK_MODULES_Z80_IMPLEMENTATION_INCLUDE_GUARD
#define ZXTK_MODULES_Z80_IMPLEMENTATION_INCLUDE_GUARD

#include <zxtk/misc/zxtk_config.hpp>
#include <zxtk/misc/zxtk_types.hpp>
#include <zxtk/modules/register_set.hpp>
#include <zxtk/modules/memory.hpp>

namespace zxtk {
    namespace cpu {
        namespace impl {
            template <typename M = memory::Memory, typename R = register_set::Z80_register_set>
            struct Default_cpu_impl {
                // I'm not using any sort of decode logic as a jump table is faster - the modern day equivalent to jp (hl) is faster than jp (hl), some bit decode stuff, and another jp, and a pointer decode (on register access)
                // I'm not using a table for memory, obviously!
                using memory_type = M;
                using register_type = R;
                                        // oct (hex)
                void nop()              // 000 (00)
                {
#ifdef ZXTK_Z80_CORRECT_TIMING
#pragma NOSUCHPRAGMA NOTE: Correct timing feature has not been implemented yet, falling back to incorrect time. This will cause multicolour programs, and some others, to not work correctly
#endif
                    ++r.pc();
                    clock(4); // NOTE: Not the c function! See protected part of class definition
                    // How about a syntax like this?
                    // clock(m1);
                }
                void ld_bc_nn()         // 001 (01)
                {
                    r.bc() = m.g16(++r.pc());
                    r.pc() += 2;
                    clock(10);
                    // clock(m1,mem,mem);
                }
                void ld_addr_bc_a()     // 002 (02)
                {
                    m.r8(r.bc()) = r.a();
                    ++r.pc();
                    clock(7);
                    // clock(m1,mem);
                }
                void inc_bc()           // 003 (03)
                {
                    ++r.bc();
                    ++r.pc();
                    clock(6);
                    // clock(m1_6);
                }
                void inc_b()            // 004 (04)
                {
                    ++r.b();
                    // flagaffect (inc,r.b(),254);
                    // ^~~~~~~~~~ any ideas for a better name for this function?
                    // The 254 is 11111110, or all the flags this instruction affects
                    ++r.pc();
                    clock(4);
                    // clock(m1);
                }
                void dec_b()            // 005 (05)
                {
                    --r.b();
                    // flagaffect (dec,r.b(),254);
                    ++r.pc();
                    clock(4);
                    // clock(m1);
                }
                void ld_b_n()           // 006 (06)
                {
                    r.b() = m.g8(++r.pc());
                    ++r.pc();
                    clock(7);
                    // clock(m1,mem);
                }
                void rlca()             // 007 (07)
                {
                    // flagand (196);
                    // flagcalc (1);
                    // TODO: We need a Flag class for all this
                    bool tmp = r.f() & 1;
                    r.f() &= 254 | (((r.a() & 128) == 128)?1:0);
                    r.a() <<= 1;
                    r.a() |= tmp;
                    ++r.pc();
                    clock(4);
                }
                void ex_af_alt_af()     // 010 (08)
                {
                    r.ex_af_af();
                    ++r.pc();
                    clock(4);
                }
                void add_hl_bc()        // 011 (09)
                {
                    r.hl() += r.bc();
                    // Flags: C,N,H
                    ++r.pc();
                    clock(11);
                }
                void ld_a_addr_bc()     // 012 (0A)
                {
                    r.a() = m.g8(r.bc());
                    ++r.pc();
                    clock(7);
                }
                void dec_bc()           // 013 (0B)
                {
                    --r.bc();
                    ++r.pc();
                    clock(6);
                    // clock(m1_6);
                }
                void inc_c()            // 014 (0C)
                {
                    ++r.c();
                    // flagaffect (inc,r.c(),254);
                    // ^~~~~~~~~~ any ideas for a better name for this function?
                    // The 254 is 11111110, or all the flags this instruction affects
                    ++r.pc();
                    clock(4);
                    // clock(m1);
                }
                void dec_c()            // 015 (0D)
                {
                    --r.c();
                    // flagaffect (dec,r.c(),254);
                    ++r.pc();
                    clock(4);
                    // clock(m1);
                }
                void ld_c_n()           // 016 (0E)
                {
                    r.c() = m.g8(++r.pc());
                    ++r.pc();
                    clock(7);
                    // clock(m1,mem);
                }
                void rrca()             // 017 (0F)
                {
                    // flagand (196);
                    // flagcalc (1);
                    // TODO: We need a Flag class for all this
                    bool tmp = r.f() & 1;
                    r.f() &= 254 | r.a() & 1;
                    r.a() >>= 1;
                    r.a() |= ((tmp==1)?128:0);
                    ++r.pc();
                    clock(4);
                }
            protected:
                R r;
                M m; // Ummm, should the CPU own the memory? TODO
                types::cycle diff_cycle (types::cycle, types::cycle) {return 0;} // TODO
                types::cycle cycle {0}; // NOTE: It feels wrong making this and the next declaration a member of this class (and not its base). Any ideas on how to do this better?
                types::cycle mcycle() { /*...*/ return 0;} // TODO
                void clock(types::cycle i)
                    {
#ifdef ZXTK_THREADS_TRUE
                        if (diff_cycle(mcycle(),cycle)>i)
                            cycle += i;
#else
#error Single thread excectuion has not been implemented yet
// TODO: Implement single thread excectuion
#endif
                    }
            };
        }
    }
}
#endif
<commit_msg>Make the cpu so it doesn't own its memory<commit_after>#ifndef ZXTK_MODULES_Z80_IMPLEMENTATION_INCLUDE_GUARD
#define ZXTK_MODULES_Z80_IMPLEMENTATION_INCLUDE_GUARD

#include <zxtk/misc/zxtk_config.hpp>
#include <zxtk/misc/zxtk_types.hpp>
#include <zxtk/modules/register_set.hpp>
#include <zxtk/modules/memory.hpp>

namespace zxtk {
    namespace cpu {
        namespace impl {
            template <typename M = memory::Memory, typename R = register_set::Z80_register_set>
            // In the future, the memory type will be a memory client type, so
            // timing can be implemented correctly
            struct Default_cpu_impl {
                Default_cpu_impl(M& memory) :m{memory} {}
                // I'm not using any sort of decode logic as a jump table is faster - the modern day equivalent to jp (hl) is faster than jp (hl), some bit decode stuff, and another jp, and a pointer decode (on register access)
                // I'm not using a table for memory, obviously!
                using memory_type = M;
                using register_type = R;
                                        // oct (hex)
                void nop()              // 000 (00)
                {
#ifdef ZXTK_Z80_CORRECT_TIMING
#pragma NOSUCHPRAGMA NOTE: Correct timing feature has not been implemented yet, falling back to incorrect time. This will cause multicolour programs, and some others, to not work correctly
#endif
                    ++r.pc();
                    clock(4); // NOTE: Not the c function! See protected part of class definition
                    // How about a syntax like this?
                    // clock(m1);
                }
                void ld_bc_nn()         // 001 (01)
                {
                    r.bc() = m.g16(++r.pc());
                    r.pc() += 2;
                    clock(10);
                    // clock(m1,mem,mem);
                }
                void ld_addr_bc_a()     // 002 (02)
                {
                    m.r8(r.bc()) = r.a();
                    ++r.pc();
                    clock(7);
                    // clock(m1,mem);
                }
                void inc_bc()           // 003 (03)
                {
                    ++r.bc();
                    ++r.pc();
                    clock(6);
                    // clock(m1_6);
                }
                void inc_b()            // 004 (04)
                {
                    ++r.b();
                    // flagaffect (inc,r.b(),254);
                    // ^~~~~~~~~~ any ideas for a better name for this function?
                    // The 254 is 11111110, or all the flags this instruction affects
                    ++r.pc();
                    clock(4);
                    // clock(m1);
                }
                void dec_b()            // 005 (05)
                {
                    --r.b();
                    // flagaffect (dec,r.b(),254);
                    ++r.pc();
                    clock(4);
                    // clock(m1);
                }
                void ld_b_n()           // 006 (06)
                {
                    r.b() = m.g8(++r.pc());
                    ++r.pc();
                    clock(7);
                    // clock(m1,mem);
                }
                void rlca()             // 007 (07)
                {
                    // flagand (196);
                    // flagcalc (1);
                    // TODO: We need a Flag class for all this
                    bool tmp = r.f() & 1;
                    r.f() &= 254 | (((r.a() & 128) == 128)?1:0);
                    r.a() <<= 1;
                    r.a() |= tmp;
                    ++r.pc();
                    clock(4);
                }
                void ex_af_alt_af()     // 010 (08)
                {
                    r.ex_af_af();
                    ++r.pc();
                    clock(4);
                }
                void add_hl_bc()        // 011 (09)
                {
                    r.hl() += r.bc();
                    // Flags: C,N,H
                    ++r.pc();
                    clock(11);
                }
                void ld_a_addr_bc()     // 012 (0A)
                {
                    r.a() = m.g8(r.bc());
                    ++r.pc();
                    clock(7);
                }
                void dec_bc()           // 013 (0B)
                {
                    --r.bc();
                    ++r.pc();
                    clock(6);
                    // clock(m1_6);
                }
                void inc_c()            // 014 (0C)
                {
                    ++r.c();
                    // flagaffect (inc,r.c(),254);
                    // ^~~~~~~~~~ any ideas for a better name for this function?
                    // The 254 is 11111110, or all the flags this instruction affects
                    ++r.pc();
                    clock(4);
                    // clock(m1);
                }
                void dec_c()            // 015 (0D)
                {
                    --r.c();
                    // flagaffect (dec,r.c(),254);
                    ++r.pc();
                    clock(4);
                    // clock(m1);
                }
                void ld_c_n()           // 016 (0E)
                {
                    r.c() = m.g8(++r.pc());
                    ++r.pc();
                    clock(7);
                    // clock(m1,mem);
                }
                void rrca()             // 017 (0F)
                {
                    // flagand (196);
                    // flagcalc (1);
                    // TODO: We need a Flag class for all this
                    bool tmp = r.f() & 1;
                    r.f() &= 254 | r.a() & 1;
                    r.a() >>= 1;
                    r.a() |= ((tmp==1)?128:0);
                    ++r.pc();
                    clock(4);
                }
            protected:
                R r;
                M& m;
                types::cycle diff_cycle (types::cycle, types::cycle) {return 0;} // TODO
                types::cycle cycle {0}; // NOTE: It feels wrong making this and the next declaration a member of this class (and not its base). Any ideas on how to do this better?
                types::cycle mcycle() { /*...*/ return 0;} // TODO
                void clock(types::cycle i)
                    {
#ifdef ZXTK_THREADS_TRUE
                        if (diff_cycle(mcycle(),cycle)>i)
                            cycle += i;
#else
#error Single thread excectuion has not been implemented yet
// TODO: Implement single thread excectuion
#endif
                    }
            };
        }
    }
}
#endif
<|endoftext|>


<commit_before>#include "doofit/plotting/Plot/Plot.h"

// STL
#include <string>
#include <sstream>
#include <vector>

// boost
#include <boost/regex.hpp>

// ROOT
#include "TIterator.h"

// from RooFit
#include "RooArgList.h"
#include "RooAbsRealLValue.h"
#include "RooAbsData.h"
#include "RooAbsPdf.h"
#include "RooPlot.h"

// from Project
#include "doocore/io/MsgStream.h"
#include "doocore/lutils/lutils.h"
#include "doofit/plotting/Plot/PlotConfig.h"

using namespace ROOT;
using namespace RooFit;
using namespace doocore::io;

namespace doofit {
namespace plotting {

Plot::Plot(const PlotConfig& cfg_plot, const RooAbsRealLValue& dimension, const RooAbsData& dataset, const RooArgList& pdfs, const std::string& plot_name)
: config_plot_(cfg_plot),
  dimension_(dimension),
  datasets_(),
  plot_name_(plot_name)
{
  datasets_.push_back(&dataset);
  pdf_ = dynamic_cast<RooAbsPdf*>(pdfs.first());

  if (&dimension_ == NULL) {
    serr << "Plot::Plot(): Dimension is invalid." << endmsg;
    throw 1;
  }
  if (datasets_.front() == NULL) {
    serr << "Plot::Plot(): Dataset is invalid." << endmsg;
    throw 1;
  }
  if (plot_name_ == "") {
    plot_name_ = dimension_.GetName();
  }

  for (int i=1; i<pdfs.getSize(); ++i) {
    RooAbsArg* sub_arg = pdfs.at(i);
    const RooAbsPdf* sub_pdf = dynamic_cast<RooAbsPdf*>(sub_arg);

    if (sub_pdf != NULL) {
      components_.push_back(RooArgSet(*sub_pdf));
    }
  }
}

Plot::Plot(const PlotConfig& cfg_plot, const RooAbsRealLValue& dimension, const RooAbsData& dataset, const RooAbsPdf& pdf, const std::vector<std::string>& components, const std::string& plot_name)
: config_plot_(cfg_plot),
  dimension_(dimension),
  datasets_(),
  plot_name_(plot_name)
{
  datasets_.push_back(&dataset);
  pdf_ = &pdf;

  if (pdf_ == NULL) {
    serr << "Plot::Plot(): Main PDF is invalid." << endmsg;
    throw 1;
  }
  if (&dimension_ == NULL) {
    serr << "Plot::Plot(): Dimension is invalid." << endmsg;
    throw 1;
  }
  if (datasets_.front() == NULL) {
    serr << "Plot::Plot(): Dataset is invalid." << endmsg;
    throw 1;
  }
  if (plot_name_ == "") {
    plot_name_ = dimension_.GetName();
  }

  // iterate over sub PDFs and match supplied regular expressions
  RooArgSet nodes;
  pdf.branchNodeServerList(&nodes);

  for (std::vector<std::string>::const_iterator it = components.begin();
       it != components.end(); ++it) {
    boost::regex r(*it);
    components_.push_back(RooArgSet());

    TIterator* it_nodes = nodes.createIterator();
    RooAbsArg* node = NULL;

    while ((node = dynamic_cast<RooAbsArg*>(it_nodes->Next()))) {
      RooAbsPdf* pdf_node = dynamic_cast<RooAbsPdf*>(node);
      if (pdf_node != NULL) {
        std::string pdf_name = pdf_node->GetName();

        // exclude resolution models generated by RooFit and match the rest
        if (pdf_name.find("_conv_") == -1 && regex_match(pdf_name,r)) {
          components_.back().add(*pdf_node);
        }
      }
    }
    delete it_nodes;
  }
}

void Plot::PlotHandler(ScaleType sc_y, std::string suffix) const {
  if (suffix == "") suffix = "_log";

  std::string plot_name = plot_name_;

  std::stringstream log_plot_name_sstr;
  log_plot_name_sstr << plot_name << suffix;
  std::string log_plot_name = log_plot_name_sstr.str();

  std::stringstream pull_plot_sstr;
  pull_plot_sstr << plot_name << "_pull";
  std::string pull_plot_name = pull_plot_sstr.str();

  std::stringstream log_pull_plot_sstr;
  log_pull_plot_sstr << plot_name << "_pull" << suffix;
  std::string log_pull_plot_name = log_pull_plot_sstr.str();

  sinfo << "Plotting " << dimension_.GetName() << " into " << config_plot_.plot_directory() << plot_name << endmsg;

  doocore::lutils::setStyle("LHCb");

  RooCmdArg range_arg;
  if (!dimension_.hasMin() && !dimension_.hasMax()) {
    double min, max;

    // ugly const_cast because RooFit is stupid (RooDataSet::getRange needs non-const RooRealVar)
    RooRealVar* dimension_non_const = const_cast<RooRealVar*>(dynamic_cast<const RooRealVar*>(&dimension_));
    datasets_.front()->getRange(*dimension_non_const, min, max);

    double min_t, max_t;
    for (std::vector<const RooAbsData*>::const_iterator it = datasets_.begin()+1;
         it != datasets_.end(); ++it) {
      (*it)->getRange(*dimension_non_const, min_t, max_t);
      if (min_t < min) min = min_t;
      if (max_t > max) max = max_t;
    }

    range_arg = Range(min, max);
  }
  RooPlot* plot_frame = dimension_.frame(range_arg);

  for (std::vector<const RooAbsData*>::const_iterator it = datasets_.begin();
       it != datasets_.end(); ++it) {
    (*it)->plotOn(plot_frame/*, Rescale(1.0/(*it)->sumEntries())*/);
  }

  config_plot_.OnDemandOpenPlotStack();
  if (pdf_ != NULL) {
    RooPlot* plot_frame_pull = dimension_.frame(range_arg);
    for (std::vector<const RooAbsData*>::const_iterator it = datasets_.begin();
         it != datasets_.end(); ++it) {
      (*it)->plotOn(plot_frame_pull);
    }

    // I feel so tupid doing this but apparently RooFit leaves me no other way...
    RooCmdArg arg1, arg2, arg3, arg4, arg5, arg6, arg7;
    if (plot_args_.size() > 0) arg1 = plot_args_[0];
    if (plot_args_.size() > 1) arg2 = plot_args_[1];
    if (plot_args_.size() > 2) arg3 = plot_args_[2];
    if (plot_args_.size() > 3) arg4 = plot_args_[3];
    if (plot_args_.size() > 4) arg5 = plot_args_[4];
    if (plot_args_.size() > 5) arg6 = plot_args_[5];
    if (plot_args_.size() > 6) arg7 = plot_args_[6];

    int i=1;
    for (std::vector<RooArgSet>::const_iterator it = components_.begin();
         it != components_.end(); ++it) {
      pdf_->plotOn(plot_frame, Components(*it), LineColor(config_plot_.GetPdfLineColor(i)), LineStyle(config_plot_.GetPdfLineStyle(i)), arg1, arg2, arg3, arg4, arg5, arg6, arg7);
      pdf_->plotOn(plot_frame_pull, Components(*it), LineColor(config_plot_.GetPdfLineColor(i)), LineStyle(config_plot_.GetPdfLineStyle(i)), arg1, arg2, arg3, arg4, arg5, arg6, arg7);
      ++i;
    }

    pdf_->plotOn(plot_frame, LineColor(config_plot_.GetPdfLineColor(0)), LineStyle(config_plot_.GetPdfLineStyle(0)), arg1, arg2, arg3, arg4, arg5, arg6, arg7);
    pdf_->plotOn(plot_frame_pull, LineColor(config_plot_.GetPdfLineColor(0)), LineStyle(config_plot_.GetPdfLineStyle(0)), arg1, arg2, arg3, arg4, arg5, arg6, arg7);
    plot_frame_pull->SetMinimum(0.5);
    plot_frame_pull->SetMaximum(1.3*plot_frame_pull->GetMaximum());

    if (sc_y == kLinear || sc_y == kBoth) {
      doocore::lutils::PlotResiduals(pull_plot_name, plot_frame_pull, &dimension_, NULL, config_plot_.plot_directory(), true, false);
      doocore::lutils::PlotResiduals("AllPlots", plot_frame_pull, &dimension_, NULL, config_plot_.plot_directory(), true, false);
    }
    if (sc_y == kLogarithmic || sc_y == kBoth) {
      doocore::lutils::PlotResiduals(log_pull_plot_name, plot_frame_pull, &dimension_, NULL, config_plot_.plot_directory(), true, true);
      doocore::lutils::PlotResiduals("AllPlots", plot_frame_pull, &dimension_, NULL, config_plot_.plot_directory(), true, true);
    }

    delete plot_frame_pull;
  }
  plot_frame->SetMinimum(0.5);
  plot_frame->SetMaximum(1.3*plot_frame->GetMaximum());
  if (sc_y == kLinear || sc_y == kBoth) {
    doocore::lutils::PlotSimple(plot_name, plot_frame, &dimension_, config_plot_.plot_directory(), false);
    doocore::lutils::PlotSimple("AllPlots", plot_frame, &dimension_, config_plot_.plot_directory(), false);
  }
  if (sc_y == kLogarithmic || sc_y == kBoth) {
    doocore::lutils::PlotSimple(log_plot_name, plot_frame, &dimension_, config_plot_.plot_directory(), true);
    doocore::lutils::PlotSimple("AllPlots", plot_frame, &dimension_, config_plot_.plot_directory(), true);
  }

  delete plot_frame;
}

Plot::~Plot() {}

} // namespace plotting
} // namespace doofit
<commit_msg>Plotting:    * removing plotting of unnecessary components<commit_after>#include "doofit/plotting/Plot/Plot.h"

// STL
#include <string>
#include <sstream>
#include <vector>

// boost
#include <boost/regex.hpp>

// ROOT
#include "TIterator.h"

// from RooFit
#include "RooArgList.h"
#include "RooAbsRealLValue.h"
#include "RooAbsData.h"
#include "RooAbsPdf.h"
#include "RooPlot.h"

// from Project
#include "doocore/io/MsgStream.h"
#include "doocore/lutils/lutils.h"
#include "doofit/plotting/Plot/PlotConfig.h"

using namespace ROOT;
using namespace RooFit;
using namespace doocore::io;

namespace doofit {
namespace plotting {

Plot::Plot(const PlotConfig& cfg_plot, const RooAbsRealLValue& dimension, const RooAbsData& dataset, const RooArgList& pdfs, const std::string& plot_name)
: config_plot_(cfg_plot),
  dimension_(dimension),
  datasets_(),
  plot_name_(plot_name)
{
  datasets_.push_back(&dataset);
  pdf_ = dynamic_cast<RooAbsPdf*>(pdfs.first());

  if (&dimension_ == NULL) {
    serr << "Plot::Plot(): Dimension is invalid." << endmsg;
    throw 1;
  }
  if (datasets_.front() == NULL) {
    serr << "Plot::Plot(): Dataset is invalid." << endmsg;
    throw 1;
  }
  if (plot_name_ == "") {
    plot_name_ = dimension_.GetName();
  }

  for (int i=1; i<pdfs.getSize(); ++i) {
    RooAbsArg* sub_arg = pdfs.at(i);
    const RooAbsPdf* sub_pdf = dynamic_cast<RooAbsPdf*>(sub_arg);

    if (sub_pdf != NULL) {
      components_.push_back(RooArgSet(*sub_pdf));
    }
  }
}

Plot::Plot(const PlotConfig& cfg_plot, const RooAbsRealLValue& dimension, const RooAbsData& dataset, const RooAbsPdf& pdf, const std::vector<std::string>& components, const std::string& plot_name)
: config_plot_(cfg_plot),
  dimension_(dimension),
  datasets_(),
  plot_name_(plot_name)
{
  datasets_.push_back(&dataset);
  pdf_ = &pdf;

  if (pdf_ == NULL) {
    serr << "Plot::Plot(): Main PDF is invalid." << endmsg;
    throw 1;
  }
  if (&dimension_ == NULL) {
    serr << "Plot::Plot(): Dimension is invalid." << endmsg;
    throw 1;
  }
  if (datasets_.front() == NULL) {
    serr << "Plot::Plot(): Dataset is invalid." << endmsg;
    throw 1;
  }
  if (plot_name_ == "") {
    plot_name_ = dimension_.GetName();
  }

  // iterate over sub PDFs and match supplied regular expressions
  RooArgSet nodes;
  pdf.branchNodeServerList(&nodes);

  for (std::vector<std::string>::const_iterator it = components.begin();
       it != components.end(); ++it) {
    boost::regex r(*it);
    components_.push_back(RooArgSet());

    TIterator* it_nodes = nodes.createIterator();
    RooAbsArg* node = NULL;

    while ((node = dynamic_cast<RooAbsArg*>(it_nodes->Next()))) {
      RooAbsPdf* pdf_node = dynamic_cast<RooAbsPdf*>(node);
      if (pdf_node != NULL) {
        std::string pdf_name = pdf_node->GetName();

        // exclude resolution models generated by RooFit and match the rest
        if (pdf_name.find("_conv_") == -1 && regex_match(pdf_name,r)) {
          components_.back().add(*pdf_node);
        }
      }
    }
    delete it_nodes;
  }
}

void Plot::PlotHandler(ScaleType sc_y, std::string suffix) const {
  if (suffix == "") suffix = "_log";

  std::string plot_name = plot_name_;

  std::stringstream log_plot_name_sstr;
  log_plot_name_sstr << plot_name << suffix;
  std::string log_plot_name = log_plot_name_sstr.str();

  std::stringstream pull_plot_sstr;
  pull_plot_sstr << plot_name << "_pull";
  std::string pull_plot_name = pull_plot_sstr.str();

  std::stringstream log_pull_plot_sstr;
  log_pull_plot_sstr << plot_name << "_pull" << suffix;
  std::string log_pull_plot_name = log_pull_plot_sstr.str();

  sinfo << "Plotting " << dimension_.GetName() << " into " << config_plot_.plot_directory() << plot_name << endmsg;

  doocore::lutils::setStyle("LHCb");

  RooCmdArg range_arg;
  if (!dimension_.hasMin() && !dimension_.hasMax()) {
    double min, max;

    // ugly const_cast because RooFit is stupid (RooDataSet::getRange needs non-const RooRealVar)
    RooRealVar* dimension_non_const = const_cast<RooRealVar*>(dynamic_cast<const RooRealVar*>(&dimension_));
    datasets_.front()->getRange(*dimension_non_const, min, max);

    double min_t, max_t;
    for (std::vector<const RooAbsData*>::const_iterator it = datasets_.begin()+1;
         it != datasets_.end(); ++it) {
      (*it)->getRange(*dimension_non_const, min_t, max_t);
      if (min_t < min) min = min_t;
      if (max_t > max) max = max_t;
    }

    range_arg = Range(min, max);
  }
  RooPlot* plot_frame = dimension_.frame(range_arg);

  for (std::vector<const RooAbsData*>::const_iterator it = datasets_.begin();
       it != datasets_.end(); ++it) {
    (*it)->plotOn(plot_frame/*, Rescale(1.0/(*it)->sumEntries())*/);
  }

  config_plot_.OnDemandOpenPlotStack();
  if (pdf_ != NULL) {
    RooPlot* plot_frame_pull = dimension_.frame(range_arg);
    for (std::vector<const RooAbsData*>::const_iterator it = datasets_.begin();
         it != datasets_.end(); ++it) {
      (*it)->plotOn(plot_frame_pull);
    }

    // I feel so tupid doing this but apparently RooFit leaves me no other way...
    RooCmdArg arg1, arg2, arg3, arg4, arg5, arg6, arg7;
    if (plot_args_.size() > 0) arg1 = plot_args_[0];
    if (plot_args_.size() > 1) arg2 = plot_args_[1];
    if (plot_args_.size() > 2) arg3 = plot_args_[2];
    if (plot_args_.size() > 3) arg4 = plot_args_[3];
    if (plot_args_.size() > 4) arg5 = plot_args_[4];
    if (plot_args_.size() > 5) arg6 = plot_args_[5];
    if (plot_args_.size() > 6) arg7 = plot_args_[6];

    int i=1;
    for (std::vector<RooArgSet>::const_iterator it = components_.begin();
         it != components_.end(); ++it) {
      if (it->getSize() > 0) {
        pdf_->plotOn(plot_frame, Components(*it), LineColor(config_plot_.GetPdfLineColor(i)), LineStyle(config_plot_.GetPdfLineStyle(i)), arg1, arg2, arg3, arg4, arg5, arg6, arg7);
        pdf_->plotOn(plot_frame_pull, Components(*it), LineColor(config_plot_.GetPdfLineColor(i)), LineStyle(config_plot_.GetPdfLineStyle(i)), arg1, arg2, arg3, arg4, arg5, arg6, arg7);
        ++i;
      }
    }

    pdf_->plotOn(plot_frame, LineColor(config_plot_.GetPdfLineColor(0)), LineStyle(config_plot_.GetPdfLineStyle(0)), arg1, arg2, arg3, arg4, arg5, arg6, arg7);
    pdf_->plotOn(plot_frame_pull, LineColor(config_plot_.GetPdfLineColor(0)), LineStyle(config_plot_.GetPdfLineStyle(0)), arg1, arg2, arg3, arg4, arg5, arg6, arg7);
    plot_frame_pull->SetMinimum(0.5);
    plot_frame_pull->SetMaximum(1.3*plot_frame_pull->GetMaximum());

    if (sc_y == kLinear || sc_y == kBoth) {
      doocore::lutils::PlotResiduals(pull_plot_name, plot_frame_pull, &dimension_, NULL, config_plot_.plot_directory(), true, false);
      doocore::lutils::PlotResiduals("AllPlots", plot_frame_pull, &dimension_, NULL, config_plot_.plot_directory(), true, false);
    }
    if (sc_y == kLogarithmic || sc_y == kBoth) {
      doocore::lutils::PlotResiduals(log_pull_plot_name, plot_frame_pull, &dimension_, NULL, config_plot_.plot_directory(), true, true);
      doocore::lutils::PlotResiduals("AllPlots", plot_frame_pull, &dimension_, NULL, config_plot_.plot_directory(), true, true);
    }

    delete plot_frame_pull;
  }
  plot_frame->SetMinimum(0.5);
  plot_frame->SetMaximum(1.3*plot_frame->GetMaximum());
  if (sc_y == kLinear || sc_y == kBoth) {
    doocore::lutils::PlotSimple(plot_name, plot_frame, &dimension_, config_plot_.plot_directory(), false);
    doocore::lutils::PlotSimple("AllPlots", plot_frame, &dimension_, config_plot_.plot_directory(), false);
  }
  if (sc_y == kLogarithmic || sc_y == kBoth) {
    doocore::lutils::PlotSimple(log_plot_name, plot_frame, &dimension_, config_plot_.plot_directory(), true);
    doocore::lutils::PlotSimple("AllPlots", plot_frame, &dimension_, config_plot_.plot_directory(), true);
  }

  delete plot_frame;
}

Plot::~Plot() {}

} // namespace plotting
} // namespace doofit
<|endoftext|>


<commit_before>#ifndef REDI_WORLD_BIOME_HPP
#define REDI_WORLD_BIOME_HPP

#include <cstdint>

namespace redi
{

enum class Biome : std::uint8_t
{
  Ocean = 0,
  Plains = 1,
  Desert = 2,
  ExtremeHills = 3,
  Forest = 4
};

} // namespace redi

#endif // REDI_WORLD_BIOME_HPP<commit_msg>Update biome.hpp<commit_after>#ifndef REDI_WORLD_BIOME_HPP
#define REDI_WORLD_BIOME_HPP

#include <cstdint>

namespace redi
{

enum class Biome : std::uint8_t
{
  Ocean = 0,
  Plains = 1,
  Desert = 2,
  ExtremeHills = 3,
  Forest = 4,
  Taiga = 5,
  Swampland = 6,
  River = 7,
  Hell = 8,
  Sky = 9,
  FrozenOcean = 10,
  FrozenRiver = 11,
  IceFlats = 12,
  IceMountains = 13,
  MushroomIsland = 14,
  MushroomIslandShore = 15,
  Beaches = 16,
  DesertHills = 17,
  ForestHills = 18,
  TaigaHills = 19,
  SmallerExtremeHills = 20,
  Jungle = 21,
  JungleHills = 22,
  JungleEdge = 23,
  DeepOcean = 24,
  StoneBeach = 25,
  ColdBeach = 26,
  BirchForest = 27,
  BirchForestHills = 28,
  RoofedForest = 29,
  TaigaCold = 30,
  TaigaColdHills = 31,
  RedwoodTaiga = 32,
  RedwoodTaigaHills = 33,
  ExtremeHillsWithTrees = 34,
  Savanna = 35,
  SavannaRock = 36,
  Mesa = 37,
  MesaRock = 38,
  MesaClearRock = 39,
  Void = 127,
  MutatedPlains = 129,
  MutatedDesert = 130,
  MutatedExtremeHills = 131,
  MutatedForest = 132,
  MutatedTaiga = 133,
  MutatedSwampland = 134,
  MutatedIceFlats = 140,
  MutatedJungle = 149,
  MutatedJungleEdge = 151,
  MutatedBirchForest = 155,
  MutatedBirchForestHills = 156,
  MutatedRoofedForest = 157,
  MutatedTaigaCold = 158,
  MutatedRedwoodTaiga = 160,
  MutatedRedwoodTaigaHills = 161,
  MutatedExtremeHillsWithTrees = 162,
  MutatedSavanna = 163,
  MutatedSavannaRock = 164,
  MutatedMesa = 165,
  MutatedMesaRock = 166,
  MutatedMesaClearRock = 167
};

} // namespace redi

#endif // REDI_WORLD_BIOME_HPP
<|endoftext|>


<commit_before>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */

#include "AnimationChildWindow.hxx"

#include "app.hrc"
#include "animobjs.hxx"
#include "animobjs.hrc"
#include <sfx2/app.hxx>
#include <sfx2/childwin.hxx>
#include <sfx2/dockwin.hxx>

namespace sd {

SFX_IMPL_DOCKINGWINDOW_WITHID(AnimationChildWindow, SID_ANIMATION_OBJECTS)

/*************************************************************************
|*
|* Ableitung vom SfxChildWindow als "Behaelter" fuer Animator
|*
\************************************************************************/

AnimationChildWindow::AnimationChildWindow(
    ::Window* _pParent,
    sal_uInt16 nId,
    SfxBindings* pBindings,
    SfxChildWinInfo* pInfo )
    : SfxChildWindow( _pParent, nId )
{
    AnimationWindow* pAnimWin = new AnimationWindow(
        pBindings, this, _pParent, SdResId( FLT_WIN_ANIMATION ) );
    pWindow = pAnimWin;

    eChildAlignment = SFX_ALIGN_NOALIGNMENT;

    pAnimWin->Initialize( pInfo );

    SetHideNotDelete( sal_True );
}

} // end of namespace sd

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
<commit_msg>sd/source/ui/dlg/AnimationChildWindow.cxx comment translation and cleanup<commit_after>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */

#include "AnimationChildWindow.hxx"

#include "app.hrc"
#include "animobjs.hxx"
#include "animobjs.hrc"
#include <sfx2/app.hxx>
#include <sfx2/childwin.hxx>
#include <sfx2/dockwin.hxx>

namespace sd {

SFX_IMPL_DOCKINGWINDOW_WITHID(AnimationChildWindow, SID_ANIMATION_OBJECTS)

/**
 * Derivative from SfxChildWindow as "container" for animator
 */
AnimationChildWindow::AnimationChildWindow(
    ::Window* _pParent,
    sal_uInt16 nId,
    SfxBindings* pBindings,
    SfxChildWinInfo* pInfo )
    : SfxChildWindow( _pParent, nId )
{
    AnimationWindow* pAnimWin = new AnimationWindow(
        pBindings, this, _pParent, SdResId( FLT_WIN_ANIMATION ) );
    pWindow = pAnimWin;

    eChildAlignment = SFX_ALIGN_NOALIGNMENT;

    pAnimWin->Initialize( pInfo );

    SetHideNotDelete( sal_True );
}

} // end of namespace sd

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
<|endoftext|>


<commit_before>/***************************************************************************
 *   Copyright (C) 2006 by Till Adam <adam@kde.org>                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "akonadi.h"
#include "akonadiconnection.h"
#include "serveradaptor.h"

#include "cachecleaner.h"
#include "cachepolicymanager.h"
#include "storage/datastore.h"
#include "notificationmanager.h"
#include "resourcemanager.h"
#include "tracer.h"
#include "xdgbasedirs.h"
#include "xesammanager.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QSettings>
#include <QtCore/QTimer>

#include <unistd.h>
#include <stdlib.h>

using namespace Akonadi;

static AkonadiServer *s_instance = 0;

AkonadiServer::AkonadiServer( QObject* parent )
#ifdef Q_OS_WIN
    : QTcpServer( parent )
#else
    : KLocalSocketServer( parent )
#endif
    , mCacheCleaner( 0 )
    , mDatabaseProcess( 0 )
{
    QSettings settings( XdgBaseDirs::akonadiServerConfigFile(), QSettings::IniFormat );
    if ( settings.value( QLatin1String("General/Driver"), QLatin1String( "QMYSQL" ) ).toString() == QLatin1String( "QMYSQL" )
         && settings.value( QLatin1String( "QMYSQL/StartServer" ), true ).toBool() )
      startDatabaseProcess();

    s_instance = this;

    const QString connectionSettingsFile = XdgBaseDirs::akonadiConnectionConfigFile(
    XdgBaseDirs::WriteOnly );

    QSettings connectionSettings( connectionSettingsFile, QSettings::IniFormat );

#ifdef Q_OS_WIN
    int port = settings.value( QLatin1String( "Connection/Port" ), 4444 ).toInt();
    if ( !listen( QHostAddress::LocalHost, port ) )
      qFatal("Unable to listen on port %d", port);

    connectionSettings.setValue( QLatin1String( "Data/Method" ), QLatin1String( "TCP" ) );
    connectionSettings.setValue( QLatin1String( "Data/Address" ), serverAddress().toString() );
    connectionSettings.setValue( QLatin1String( "Data/Port" ), serverPort() );
#else
    const QString defaultSocketDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi" ) );
    QString socketDir = settings.value( QLatin1String( "Connection/SocketDirectory" ), defaultSocketDir ).toString();
    if ( socketDir[0] != QLatin1Char( '/' ) )
    {
      QDir::home().mkdir( socketDir );
      socketDir = QDir::homePath() + QLatin1Char( '/' ) + socketDir;
    }

    const QString socketFile = socketDir + QLatin1String( "/akonadiserver.socket" );
    unlink( socketFile.toUtf8().constData() );
    if ( !listen( socketFile ) )
      qFatal("Unable to listen on Unix socket '%s'", socketFile.toLocal8Bit().data() );

    connectionSettings.setValue( QLatin1String( "Data/Method" ), QLatin1String( "UnixPath" ) );
    connectionSettings.setValue( QLatin1String( "Data/UnixPath" ), socketFile );
#endif

    // initialize the database
    DataStore *db = DataStore::self();
    if ( !db->database().isOpen() )
      qFatal("Unable to open database.");
    if ( !db->init() )
      qFatal("Unable to initialize database.");

    NotificationManager::self();
    Tracer::self();
    ResourceManager::self();
    new CachePolicyManager( this );
    if ( settings.value( QLatin1String( "Cache/EnableCleaner" ), true ).toBool() ) {
      mCacheCleaner = new CacheCleaner( this );
      mCacheCleaner->start( QThread::IdlePriority );
    }

    mXesamManager = new XesamManager( this );

    new ServerAdaptor( this );
    QDBusConnection::sessionBus().registerObject( QLatin1String( "/Server" ), this );

    char* dbusAddress = getenv( "DBUS_SESSION_BUS_ADDRESS" );
    if (dbusAddress != 0)
    {
      connectionSettings.setValue( QLatin1String( "DBUS/Address" ), QLatin1String( dbusAddress ) );
    }
}

AkonadiServer::~AkonadiServer()
{
}

void AkonadiServer::quit()
{
    if ( mCacheCleaner ) {
        mCacheCleaner->quit();
        mCacheCleaner->wait();
        delete mCacheCleaner;
    }
    delete mXesamManager;
    mXesamManager = 0;

    for ( int i = 0; i < mConnections.count(); ++i ) {
      if ( mConnections[ i ] ) {
        mConnections[ i ]->quit();
        mConnections[ i ]->wait();
      }
    }
    // execute the deleteLater() calls for the threads so they free their db connections
    // and the following db termination will work
    QCoreApplication::instance()->processEvents();

    if ( mDatabaseProcess )
      stopDatabaseProcess();

    QSettings settings( XdgBaseDirs::akonadiServerConfigFile(), QSettings::IniFormat );
    const QString connectionSettingsFile = XdgBaseDirs::akonadiConnectionConfigFile( XdgBaseDirs::WriteOnly );

#ifndef Q_OS_WIN
    QSettings connectionSettings( connectionSettingsFile, QSettings::IniFormat );
    const QString defaultSocketDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi" ) );
    const QString socketDir = settings.value( QLatin1String( "Connection/SocketDirectory" ), defaultSocketDir ).toString();

    if ( !QDir::home().remove( socketDir + QLatin1String( "/akonadiserver.socket" ) ) )
        qWarning("Failed to remove Unix socket");
#endif
    if ( !QDir::home().remove( connectionSettingsFile ) )
        qWarning("Failed to remove runtime connection config file");

    QTimer::singleShot( 0, this, SLOT( doQuit() ) );
}

void AkonadiServer::doQuit()
{
    QCoreApplication::exit();
}

void AkonadiServer::incomingConnection( int socketDescriptor )
{
    QPointer<AkonadiConnection> thread = new AkonadiConnection( socketDescriptor, this );
    connect( thread, SIGNAL( finished() ), thread, SLOT( deleteLater() ) );
    mConnections.append( thread );
    thread->start();
}

AkonadiServer * AkonadiServer::instance()
{
    if ( !s_instance )
        s_instance = new AkonadiServer();
    return s_instance;
}

void AkonadiServer::startDatabaseProcess()
{
#ifdef MYSQLD_EXECUTABLE
  const QString mysqldPath = QLatin1String( MYSQLD_EXECUTABLE );
#else
  Q_ASSERT_X( false, "AkonadiServer::startDatabaseProcess()",
              "mysqld was not found during compile time, you need to start a MySQL server yourself first and configure Akonadi accordingly" );
#endif

  // create the database directories if they don't exists
  const QString dataDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi/db_data" ) );
  const QString akDir   = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi/" ) );
  const QString miscDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi/db_misc" ) );

  // generate config file
  const QString globalConfig = XdgBaseDirs::findResourceFile( "config", QLatin1String( "akonadi/mysql-global.conf" ) );
  const QString localConfig  = XdgBaseDirs::findResourceFile( "config", QLatin1String( "akonadi/mysql-local.conf" ) );
  const QString actualConfig = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi" ) ) + QLatin1String("/mysql.conf");
  if ( globalConfig.isEmpty() )
    qFatal("Where is my MySQL config file??");
  QFile globalFile( globalConfig );
  QFile actualFile( actualConfig );
  if ( globalFile.open( QFile::ReadOnly ) && actualFile.open( QFile::WriteOnly ) ) {
    actualFile.write( globalFile.readAll() );
    if ( !localConfig.isEmpty() ) {
      QFile localFile( localConfig );
      if ( localFile.open( QFile::ReadOnly ) ) {
        actualFile.write( localFile.readAll() );
        localFile.close();
      }
    }
    actualFile.close();
    globalFile.close();
  } else {
    qFatal("What did you do to my MySQL config file??");
  }

  if ( dataDir.isEmpty() )
    qFatal("Akonadi server was not able not create database data directory");

  if ( akDir.isEmpty() )
    qFatal("Akonadi server was not able not create database log directory");

  if ( miscDir.isEmpty() )
    qFatal("Akonadi server was not able not create database misc directory");

  // synthesize the mysqld command
  QStringList arguments;
  arguments << QString::fromLatin1( "--defaults-file=%1/mysql.conf" ).arg( akDir );
  arguments << QString::fromLatin1( "--datadir=%1/" ).arg( dataDir );
  arguments << QString::fromLatin1( "--socket=%1/mysql.socket" ).arg( miscDir );

  mDatabaseProcess = new QProcess( this );
  mDatabaseProcess->start( mysqldPath, arguments );
  if ( !mDatabaseProcess->waitForStarted() )
    qFatal( "Could not start database server '%s'", qPrintable( mysqldPath ) );

  QSqlDatabase db = QSqlDatabase::addDatabase( QLatin1String( "QMYSQL" ), QLatin1String( "initConnection" ) );
  db.setConnectOptions( QString::fromLatin1( "UNIX_SOCKET=%1/mysql.socket" ).arg( miscDir ) );

  bool opened = false;
  for ( int i = 0; i < 10; ++i ) {
    opened = db.open();
    if ( opened )
      break;

    sleep( 1 );
  }

  if ( opened ) {
    QSqlQuery query( db );
    if ( !query.exec( QLatin1String( "USE DATABASE akonadi" ) ) )
      query.exec( QLatin1String( "CREATE DATABASE akonadi" ) );
  }

  QSqlDatabase::removeDatabase( QLatin1String( "initConnection" ) );
}

void AkonadiServer::stopDatabaseProcess()
{
  mDatabaseProcess->terminate();
  mDatabaseProcess->waitForFinished();
}

#include "akonadi.moc"
<commit_msg>oops, also build when mysqld is not found<commit_after>/***************************************************************************
 *   Copyright (C) 2006 by Till Adam <adam@kde.org>                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "akonadi.h"
#include "akonadiconnection.h"
#include "serveradaptor.h"

#include "cachecleaner.h"
#include "cachepolicymanager.h"
#include "storage/datastore.h"
#include "notificationmanager.h"
#include "resourcemanager.h"
#include "tracer.h"
#include "xdgbasedirs.h"
#include "xesammanager.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QSettings>
#include <QtCore/QTimer>

#include <unistd.h>
#include <stdlib.h>

using namespace Akonadi;

static AkonadiServer *s_instance = 0;

AkonadiServer::AkonadiServer( QObject* parent )
#ifdef Q_OS_WIN
    : QTcpServer( parent )
#else
    : KLocalSocketServer( parent )
#endif
    , mCacheCleaner( 0 )
    , mDatabaseProcess( 0 )
{
    QSettings settings( XdgBaseDirs::akonadiServerConfigFile(), QSettings::IniFormat );
    if ( settings.value( QLatin1String("General/Driver"), QLatin1String( "QMYSQL" ) ).toString() == QLatin1String( "QMYSQL" )
         && settings.value( QLatin1String( "QMYSQL/StartServer" ), true ).toBool() )
      startDatabaseProcess();

    s_instance = this;

    const QString connectionSettingsFile = XdgBaseDirs::akonadiConnectionConfigFile(
    XdgBaseDirs::WriteOnly );

    QSettings connectionSettings( connectionSettingsFile, QSettings::IniFormat );

#ifdef Q_OS_WIN
    int port = settings.value( QLatin1String( "Connection/Port" ), 4444 ).toInt();
    if ( !listen( QHostAddress::LocalHost, port ) )
      qFatal("Unable to listen on port %d", port);

    connectionSettings.setValue( QLatin1String( "Data/Method" ), QLatin1String( "TCP" ) );
    connectionSettings.setValue( QLatin1String( "Data/Address" ), serverAddress().toString() );
    connectionSettings.setValue( QLatin1String( "Data/Port" ), serverPort() );
#else
    const QString defaultSocketDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi" ) );
    QString socketDir = settings.value( QLatin1String( "Connection/SocketDirectory" ), defaultSocketDir ).toString();
    if ( socketDir[0] != QLatin1Char( '/' ) )
    {
      QDir::home().mkdir( socketDir );
      socketDir = QDir::homePath() + QLatin1Char( '/' ) + socketDir;
    }

    const QString socketFile = socketDir + QLatin1String( "/akonadiserver.socket" );
    unlink( socketFile.toUtf8().constData() );
    if ( !listen( socketFile ) )
      qFatal("Unable to listen on Unix socket '%s'", socketFile.toLocal8Bit().data() );

    connectionSettings.setValue( QLatin1String( "Data/Method" ), QLatin1String( "UnixPath" ) );
    connectionSettings.setValue( QLatin1String( "Data/UnixPath" ), socketFile );
#endif

    // initialize the database
    DataStore *db = DataStore::self();
    if ( !db->database().isOpen() )
      qFatal("Unable to open database.");
    if ( !db->init() )
      qFatal("Unable to initialize database.");

    NotificationManager::self();
    Tracer::self();
    ResourceManager::self();
    new CachePolicyManager( this );
    if ( settings.value( QLatin1String( "Cache/EnableCleaner" ), true ).toBool() ) {
      mCacheCleaner = new CacheCleaner( this );
      mCacheCleaner->start( QThread::IdlePriority );
    }

    mXesamManager = new XesamManager( this );

    new ServerAdaptor( this );
    QDBusConnection::sessionBus().registerObject( QLatin1String( "/Server" ), this );

    char* dbusAddress = getenv( "DBUS_SESSION_BUS_ADDRESS" );
    if (dbusAddress != 0)
    {
      connectionSettings.setValue( QLatin1String( "DBUS/Address" ), QLatin1String( dbusAddress ) );
    }
}

AkonadiServer::~AkonadiServer()
{
}

void AkonadiServer::quit()
{
    if ( mCacheCleaner ) {
        mCacheCleaner->quit();
        mCacheCleaner->wait();
        delete mCacheCleaner;
    }
    delete mXesamManager;
    mXesamManager = 0;

    for ( int i = 0; i < mConnections.count(); ++i ) {
      if ( mConnections[ i ] ) {
        mConnections[ i ]->quit();
        mConnections[ i ]->wait();
      }
    }
    // execute the deleteLater() calls for the threads so they free their db connections
    // and the following db termination will work
    QCoreApplication::instance()->processEvents();

    if ( mDatabaseProcess )
      stopDatabaseProcess();

    QSettings settings( XdgBaseDirs::akonadiServerConfigFile(), QSettings::IniFormat );
    const QString connectionSettingsFile = XdgBaseDirs::akonadiConnectionConfigFile( XdgBaseDirs::WriteOnly );

#ifndef Q_OS_WIN
    QSettings connectionSettings( connectionSettingsFile, QSettings::IniFormat );
    const QString defaultSocketDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi" ) );
    const QString socketDir = settings.value( QLatin1String( "Connection/SocketDirectory" ), defaultSocketDir ).toString();

    if ( !QDir::home().remove( socketDir + QLatin1String( "/akonadiserver.socket" ) ) )
        qWarning("Failed to remove Unix socket");
#endif
    if ( !QDir::home().remove( connectionSettingsFile ) )
        qWarning("Failed to remove runtime connection config file");

    QTimer::singleShot( 0, this, SLOT( doQuit() ) );
}

void AkonadiServer::doQuit()
{
    QCoreApplication::exit();
}

void AkonadiServer::incomingConnection( int socketDescriptor )
{
    QPointer<AkonadiConnection> thread = new AkonadiConnection( socketDescriptor, this );
    connect( thread, SIGNAL( finished() ), thread, SLOT( deleteLater() ) );
    mConnections.append( thread );
    thread->start();
}

AkonadiServer * AkonadiServer::instance()
{
    if ( !s_instance )
        s_instance = new AkonadiServer();
    return s_instance;
}

void AkonadiServer::startDatabaseProcess()
{
#ifdef MYSQLD_EXECUTABLE
  const QString mysqldPath = QLatin1String( MYSQLD_EXECUTABLE );
#else
  const QString mysqldPath;
  Q_ASSERT_X( false, "AkonadiServer::startDatabaseProcess()",
              "mysqld was not found during compile time, you need to start a MySQL server yourself first and configure Akonadi accordingly" );
#endif

  // create the database directories if they don't exists
  const QString dataDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi/db_data" ) );
  const QString akDir   = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi/" ) );
  const QString miscDir = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi/db_misc" ) );

  // generate config file
  const QString globalConfig = XdgBaseDirs::findResourceFile( "config", QLatin1String( "akonadi/mysql-global.conf" ) );
  const QString localConfig  = XdgBaseDirs::findResourceFile( "config", QLatin1String( "akonadi/mysql-local.conf" ) );
  const QString actualConfig = XdgBaseDirs::saveDir( "data", QLatin1String( "akonadi" ) ) + QLatin1String("/mysql.conf");
  if ( globalConfig.isEmpty() )
    qFatal("Where is my MySQL config file??");
  QFile globalFile( globalConfig );
  QFile actualFile( actualConfig );
  if ( globalFile.open( QFile::ReadOnly ) && actualFile.open( QFile::WriteOnly ) ) {
    actualFile.write( globalFile.readAll() );
    if ( !localConfig.isEmpty() ) {
      QFile localFile( localConfig );
      if ( localFile.open( QFile::ReadOnly ) ) {
        actualFile.write( localFile.readAll() );
        localFile.close();
      }
    }
    actualFile.close();
    globalFile.close();
  } else {
    qFatal("What did you do to my MySQL config file??");
  }

  if ( dataDir.isEmpty() )
    qFatal("Akonadi server was not able not create database data directory");

  if ( akDir.isEmpty() )
    qFatal("Akonadi server was not able not create database log directory");

  if ( miscDir.isEmpty() )
    qFatal("Akonadi server was not able not create database misc directory");

  // synthesize the mysqld command
  QStringList arguments;
  arguments << QString::fromLatin1( "--defaults-file=%1/mysql.conf" ).arg( akDir );
  arguments << QString::fromLatin1( "--datadir=%1/" ).arg( dataDir );
  arguments << QString::fromLatin1( "--socket=%1/mysql.socket" ).arg( miscDir );

  mDatabaseProcess = new QProcess( this );
  mDatabaseProcess->start( mysqldPath, arguments );
  if ( !mDatabaseProcess->waitForStarted() )
    qFatal( "Could not start database server '%s'", qPrintable( mysqldPath ) );

  QSqlDatabase db = QSqlDatabase::addDatabase( QLatin1String( "QMYSQL" ), QLatin1String( "initConnection" ) );
  db.setConnectOptions( QString::fromLatin1( "UNIX_SOCKET=%1/mysql.socket" ).arg( miscDir ) );

  bool opened = false;
  for ( int i = 0; i < 10; ++i ) {
    opened = db.open();
    if ( opened )
      break;

    sleep( 1 );
  }

  if ( opened ) {
    QSqlQuery query( db );
    if ( !query.exec( QLatin1String( "USE DATABASE akonadi" ) ) )
      query.exec( QLatin1String( "CREATE DATABASE akonadi" ) );
  }

  QSqlDatabase::removeDatabase( QLatin1String( "initConnection" ) );
}

void AkonadiServer::stopDatabaseProcess()
{
  mDatabaseProcess->terminate();
  mDatabaseProcess->waitForFinished();
}

#include "akonadi.moc"
<|endoftext|>


<commit_before>#include "BlobCreator.hpp"
#include <iostream>

String BlobCreator::GetCommand() const
{
	return "blob";
}

void BlobCreator::PrintHelp() const
{
	std::cout << "Creates a blob from specified directory(-ies). Usage:\n";
	std::cout << GetCommand() << L" <output-blob-file> <directory> [<directory>]...\n";
}

void BlobCreator::Run(const std::vector<String>& arguments)
{
	if(arguments.size() < 2)
		THROW("Must be at least 2 arguments for command");

	fileSystem = Platform::FileSystem::GetNativeFileSystem();
	builder = NEW(Data::BlobFileSystemBuilder(fileSystem->SaveStream(arguments[0])));

	for(size_t i = 1; i < arguments.size(); ++i)
		AddDirectory(arguments[i]);

	builder->Finalize();
}

void BlobCreator::AddDirectory(const String& directory, const String& namePrefix)
{
	std::vector<String> entries;
	fileSystem->GetAllDirectoryEntries(directory, entries);
	for(size_t i = 0; i < entries.size(); ++i)
		builder->AddFileStream(entries[i], fileSystem->LoadStream(entries[i]));
}
<commit_msg>use native file system only for output file in archi's blob creator<commit_after>#include "BlobCreator.hpp"
#include <iostream>

String BlobCreator::GetCommand() const
{
	return "blob";
}

void BlobCreator::PrintHelp() const
{
	std::cout << "Creates a blob from specified directory(-ies). Usage:\n";
	std::cout << GetCommand() << L" <output-blob-file> <directory> [<directory>]...\n";
}

void BlobCreator::Run(const std::vector<String>& arguments)
{
	if(arguments.size() < 2)
		THROW("Must be at least 2 arguments for command");

	fileSystem = NEW(Platform::FileSystem(""));
	builder = NEW(Data::BlobFileSystemBuilder(Platform::FileSystem::GetNativeFileSystem()->SaveStream(arguments[0])));

	for(size_t i = 1; i < arguments.size(); ++i)
		AddDirectory(arguments[i]);

	builder->Finalize();
}

void BlobCreator::AddDirectory(const String& directory, const String& namePrefix)
{
	std::vector<String> entries;
	fileSystem->GetAllDirectoryEntries(directory, entries);
	for(size_t i = 0; i < entries.size(); ++i)
		builder->AddFileStream(entries[i], fileSystem->LoadStream(entries[i]));
}
<|endoftext|>


<commit_before>/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 VoltDB Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "storage/persistenttable.h"
#include "storage/ElasticScanner.h"

namespace voltdb
{

/**
 * Constructor.
 */
ElasticScanner::ElasticScanner(PersistentTable &table) :
#ifdef DEBUG
    m_table(table),
#endif
    m_blockMap(table.m_data),
    m_tupleSize(table.getTupleLength()),
    m_blockIterator(m_blockMap.begin()),
    m_blockEnd(m_blockMap.end()),
    m_currentBlockPtr(NULL),
    m_tuplePtr(NULL),
    m_tupleIndex(0),
    m_scanComplete(false)
{}

ElasticScanner::~ElasticScanner()
{}

/**
 * Internal method that handles transitions between blocks and
 * returns true as long as tuples are available.
 */
bool ElasticScanner::continueScan() {
    if (!m_scanComplete) {
        // First block or end of block?
        if (m_currentBlockPtr == NULL || m_tupleIndex >= m_currentBlockPtr->unusedTupleBoundry()) {
            // No more blocks?
            m_scanComplete = (m_blockIterator == m_blockEnd);
            if (!m_scanComplete) {
                // Shift to the next block.
                m_tuplePtr = m_blockIterator.key();
                m_currentBlockPtr = m_blockIterator.data();
                m_scannedBlocks.insert(m_currentBlockPtr);
                assert(m_currentBlockPtr->address() == m_tuplePtr);
                m_blockIterator.data() = TBPtr();
                m_tupleIndex = 0;
                m_blockIterator++;
            }
        }
    }
    return !m_scanComplete;
}

/**
 * Get the next tuple or return false if none is available.
 */
bool ElasticScanner::next(TableTuple &out)
{
    bool found = false;
    while (!found && continueScan()) {
        assert(m_currentBlockPtr != NULL);
        // Sanity checks.
        assert(m_tuplePtr < m_currentBlockPtr.get()->address() + m_table.getTableAllocationSize());
        assert(m_tuplePtr < m_currentBlockPtr.get()->address() + (m_tupleSize * m_table.getTuplesPerBlock()));
        assert (out.sizeInValues() == m_table.columnCount());
        // Grab the tuple pointer.
        out.move(m_tuplePtr);
        // Shift to the next tuple in block.
        // continueScan() will check if it's the last one in the block.
        m_tupleIndex++;
        m_tuplePtr += m_tupleSize;
        // The next active/non-dirty tuple is return-worthy.
        found = out.isActive() && !out.isDirty();
    }
    return found;
}

/**
 * Block compaction hook.
 */
void ElasticScanner::notifyBlockWasCompactedAway(TBPtr block) {
    if (!m_scanComplete && m_blockIterator != m_blockEnd) {
        TBPtr nextBlock = m_blockIterator.data();
        if (nextBlock == block) {
            // The next block was compacted away.
            m_blockIterator++;
            if (m_blockIterator != m_blockEnd) {
                // There is a block to skip to.
                TBPtr newNextBlock = m_blockIterator.data();
                m_blockMap.erase(block->address());
                m_blockIterator = m_blockMap.find(newNextBlock->address());
                m_blockEnd = m_blockMap.end();
                assert(m_blockIterator != m_blockMap.end());
            }
            else {
                // There isn't a block to skip to, so we're done.
                m_blockMap.erase(block->address());
                m_blockIterator = m_blockMap.end();
                m_blockEnd = m_blockMap.end();
            }
        } else {
            // Some random block was compacted away.
            // Remove it and regenerate the iterator.
            m_blockMap.erase(block->address());
            m_blockIterator = m_blockMap.find(nextBlock->address());
            m_blockEnd = m_blockMap.end();
            assert(m_blockIterator != m_blockMap.end());
        }
    }
}

} // namespace voltdb
<commit_msg>Pull fix for ElasticScanner from master.<commit_after>/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 VoltDB Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "storage/persistenttable.h"
#include "storage/ElasticScanner.h"

namespace voltdb
{

/**
 * Constructor.
 */
ElasticScanner::ElasticScanner(PersistentTable &table) :
    m_table(table),
    m_blockMap(m_table.m_data),
    m_tupleSize(table.getTupleLength()),
    m_blockIterator(m_blockMap.begin()),
    m_blockEnd(m_blockMap.end()),
    m_currentBlockPtr(NULL),
    m_tuplePtr(NULL),
    m_tupleIndex(0),
    m_scanComplete(false)
{}

ElasticScanner::~ElasticScanner()
{}

/**
 * Internal method that handles transitions between blocks and
 * returns true as long as tuples are available.
 */
bool ElasticScanner::continueScan() {
    if (!m_scanComplete) {
        // First block or end of block?
        if (m_currentBlockPtr == NULL || m_tupleIndex >= m_currentBlockPtr->unusedTupleBoundry()) {
            // No more blocks?
            m_scanComplete = (m_blockIterator == m_blockEnd);
            if (!m_scanComplete) {
                // Shift to the next block.
                m_tuplePtr = m_blockIterator.key();
                m_currentBlockPtr = m_blockIterator.data();
                m_scannedBlocks.insert(m_currentBlockPtr);
                assert(m_currentBlockPtr->address() == m_tuplePtr);
                m_blockIterator.data() = TBPtr();
                m_tupleIndex = 0;
                m_blockIterator++;
            }
        }
    }
    return !m_scanComplete;
}

/**
 * Get the next tuple or return false if none is available.
 */
bool ElasticScanner::next(TableTuple &out)
{
    bool found = false;
    while (!found && continueScan()) {
        assert(m_currentBlockPtr != NULL);
        // Sanity checks.
        assert(m_tuplePtr < m_currentBlockPtr.get()->address() + m_table.getTableAllocationSize());
        assert(m_tuplePtr < m_currentBlockPtr.get()->address() + (m_tupleSize * m_table.getTuplesPerBlock()));
        assert (out.sizeInValues() == m_table.columnCount());
        // Grab the tuple pointer.
        out.move(m_tuplePtr);
        // Shift to the next tuple in block.
        // continueScan() will check if it's the last one in the block.
        m_tupleIndex++;
        m_tuplePtr += m_tupleSize;
        // The next active/non-dirty tuple is return-worthy.
        found = out.isActive() && !out.isDirty();
    }
    return found;
}

/**
 * Block compaction hook.
 */
void ElasticScanner::notifyBlockWasCompactedAway(TBPtr block) {
    if (!m_scanComplete && m_blockIterator != m_blockEnd) {
        TBPtr nextBlock = m_blockIterator.data();
        if (nextBlock == block) {
            // The next block was compacted away.
            m_blockIterator++;
            if (m_blockIterator != m_blockEnd) {
                // There is a block to skip to.
                TBPtr newNextBlock = m_blockIterator.data();
                m_blockMap.erase(block->address());
                m_blockIterator = m_blockMap.find(newNextBlock->address());
                m_blockEnd = m_blockMap.end();
                assert(m_blockIterator != m_blockMap.end());
            }
            else {
                // There isn't a block to skip to, so we're done.
                m_blockMap.erase(block->address());
                m_blockIterator = m_blockMap.end();
                m_blockEnd = m_blockMap.end();
            }
        } else {
            // Some random block was compacted away.
            // Remove it and regenerate the iterator.
            m_blockMap.erase(block->address());
            m_blockIterator = m_blockMap.find(nextBlock->address());
            m_blockEnd = m_blockMap.end();
            assert(m_blockIterator != m_blockMap.end());
        }
    }
}

} // namespace voltdb
<|endoftext|>


<commit_before>#include "MatrixHelper.h"

#include <algorithm>
#include <fstream>
#include <iterator>
#include <iostream>
#include <sstream>

using namespace std;

void MatrixHelper::writeMatrixTo(const string& filename, const Matrix<float>& matrix)
{
    ofstream file;
    file.exceptions(ios_base::failbit);
    file.open(filename);

    writeMatrixTo(file, matrix);
}

void MatrixHelper::writeMatrixTo(ostream& output, const Matrix<float>& matrix)
{
    output << matrix.rows() << " " << matrix.columns() << endl;

    for (size_t i=0; i<matrix.rows(); i++)
    {
        for (size_t j=0; j<matrix.columns(); j++)
        {
            if(j>0)
               output << " ";
            output << matrix(i, j);
        }
        output << endl;
    }
}

Matrix<float> MatrixHelper::readMatrixFrom(istream& stream)
{
    try
    {
        size_t rows, columns;
        stream >> rows;
        stream >> columns;
        string line;
        getline(stream, line);
        Matrix<float> matrix(rows, columns);
        fillMatrixFromStream(matrix, stream);
        return matrix;
    }
    catch (...)
    {
        cerr << "ERROR: Wrong matrices stream format." << endl;
        throw;
    }
}

Matrix<float> MatrixHelper::readMatrixFrom(const string& fileName)
{
    ifstream file;
    file.exceptions(ios_base::failbit);
    file.open(fileName);
    return readMatrixFrom(file);
}

pair<Matrix<float>, Matrix<float>> MatrixHelper::readMatrixPairFrom(std::istream& stream)
{
    pair<Matrix<float>, Matrix<float>> matrices;
    matrices.first = readMatrixFrom(stream);
    matrices.second = readMatrixFrom(stream);
    return matrices;
}

void MatrixHelper::fillMatrixFromStream(Matrix<float>& matrix, istream& stream)
{
    for (size_t i=0; i<matrix.rows() && stream.good(); i++)
    {
        string line;
        getline(stream, line);
        vector<float> values = getValuesIn(line);

        for (size_t j=0; j<matrix.columns() && j<values.size(); j++)
            matrix(i, j) = values[j];
    }
}

vector<float> MatrixHelper::getValuesIn(const string& line)
{
    istringstream stream(line);
    vector<float> result;
    copy(
            istream_iterator<float>(stream),
            istream_iterator<float>(),
            back_inserter<vector<float>>(result)
        );

    return result;
}

void MatrixHelper::fill(Matrix<float>& matrix, const function<float()>& generator)
{
    for(size_t y = 0; y<matrix.rows(); y++)
    {
        for(size_t x = 0; x<matrix.columns(); x++)
        {
            matrix(y, x) = generator();
        }
    }
}
<commit_msg>throw exception when MatrixFormat is invalid<commit_after>#include "MatrixHelper.h"

#include <algorithm>
#include <fstream>
#include <iterator>
#include <iostream>
#include <sstream>
#include <stdexcept>

using namespace std;

void MatrixHelper::writeMatrixTo(const string& filename, const Matrix<float>& matrix)
{
    ofstream file;
    file.exceptions(ios_base::failbit);
    file.open(filename);

    writeMatrixTo(file, matrix);
}

void MatrixHelper::writeMatrixTo(ostream& output, const Matrix<float>& matrix)
{
    output << matrix.rows() << " " << matrix.columns() << endl;

    for (size_t i=0; i<matrix.rows(); i++)
    {
        for (size_t j=0; j<matrix.columns(); j++)
        {
            if(j>0)
               output << " ";
            output << matrix(i, j);
        }
        output << endl;
    }
}

Matrix<float> MatrixHelper::readMatrixFrom(istream& stream)
{
    try
    {
        size_t rows, columns;

        stream >> rows;
        stream >> columns;

        if (stream.bad() || stream.fail())
            throw runtime_error("Failed to read matrix size from stream in " + string(__FILE__));

        string line;
        getline(stream, line);
        Matrix<float> matrix(rows, columns);
        fillMatrixFromStream(matrix, stream);

        return matrix;
    }
    catch (...)
    {
        cerr << "ERROR: Wrong matrices stream format." << endl;
        throw;
    }
}

Matrix<float> MatrixHelper::readMatrixFrom(const string& fileName)
{
    ifstream file;
    file.open(fileName);

    if (!file.is_open())
        throw runtime_error("Could not open file: " + fileName);

    return readMatrixFrom(file);
}

pair<Matrix<float>, Matrix<float>> MatrixHelper::readMatrixPairFrom(std::istream& stream)
{
    pair<Matrix<float>, Matrix<float>> matrices;
    matrices.first = readMatrixFrom(stream);
    matrices.second = readMatrixFrom(stream);
    return matrices;
}

void MatrixHelper::fillMatrixFromStream(Matrix<float>& matrix, istream& stream)
{
    for (size_t i=0; i<matrix.rows() && stream.good(); i++)
    {
        string line;
        getline(stream, line);

        if (stream.bad())
            throw runtime_error("Error reading matrix from stream in: " + string(__FILE__));

        vector<float> values = getValuesIn(line);

        for (size_t j=0; j<matrix.columns() && j<values.size(); j++)
            matrix(i, j) = values[j];
    }
}

vector<float> MatrixHelper::getValuesIn(const string& line)
{
    istringstream stream(line);
    vector<float> result;
    copy(
            istream_iterator<float>(stream),
            istream_iterator<float>(),
            back_inserter<vector<float>>(result)
        );

    return result;
}

void MatrixHelper::fill(Matrix<float>& matrix, const function<float()>& generator)
{
    for(size_t y = 0; y<matrix.rows(); y++)
    {
        for(size_t x = 0; x<matrix.columns(); x++)
        {
            matrix(y, x) = generator();
        }
    }
}
<|endoftext|>


<commit_before>//   Copyright 2014-2015 Quickstep Technologies LLC.
//   Copyright 2015 Pivotal Software, Inc.
//
//   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

// TODO(chasseur): Better error handling in place of asserts.

#include "tmb/native_net_client_message_bus.h"

#include <cassert>
#include <chrono>  // NOLINT(build/c++11)
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <string>
#include <utility>

#include "grpc++/client_context.h"
#include "grpc++/create_channel.h"
#include "grpc++/security/credentials.h"
#include "grpc++/support/status.h"
#include "grpc++/support/sync_stream.h"

#include "tmb/address.h"
#include "tmb/cancellation_token.h"
#include "tmb/id_typedefs.h"
#include "tmb/message_bus.h"
#include "tmb/message_style.h"
#include "tmb/tagged_message.h"
#include "tmb/priority.h"

#include "tmb/internal/container_pusher.h"
#include "tmb/internal/iterator_adapter.h"
#include "tmb/internal/tmb_net.grpc.pb.h"
#include "tmb/internal/tmb_net.pb.h"

namespace tmb {

void NativeNetClientMessageBus::AddServer(const std::string &hostname,
                                          const std::uint16_t port) {
  server_address_ = hostname;
  server_address_.push_back(':');
  server_address_.append(std::to_string(static_cast<unsigned>(port)));
}

bool NativeNetClientMessageBus::Initialize() {
  stub_ = internal::net::MessageBus::NewStub(
      grpc::CreateChannel(server_address_,
                          grpc::InsecureCredentials()));

  return (stub_.get() != nullptr);
}

void NativeNetClientMessageBus::ResetBus() {
  internal::net::EmptyMessage request;
  internal::net::EmptyMessage response;
  grpc::ClientContext context;

  grpc::Status status = stub_->ResetBus(&context, request, &response);
  assert(status.ok());
}

client_id NativeNetClientMessageBus::Connect() {
  internal::net::EmptyMessage request;
  internal::net::ConnectResponse response;
  grpc::ClientContext context;

  grpc::Status status = stub_->Connect(&context, request, &response);
  assert(status.ok());

  return response.client();
}

bool NativeNetClientMessageBus::Disconnect(const client_id client) {
  internal::net::DisconnectRequest request;
  request.set_client(client);

  internal::net::BoolStatus response;
  grpc::ClientContext context;

  grpc::Status status = stub_->Disconnect(&context, request, &response);
  assert(status.ok());

  return response.status();
}

bool NativeNetClientMessageBus::RegisterClientAsSender(
    const client_id sender_id,
    const message_type_id message_type) {
  internal::net::RegistrationRequest request;
  request.set_client(sender_id);
  request.set_message_type(message_type);

  internal::net::BoolStatus response;
  grpc::ClientContext context;

  grpc::Status status = stub_->RegisterClientAsSender(&context,
                                                      request,
                                                      &response);
  assert(status.ok());

  return response.status();
}

bool NativeNetClientMessageBus::RegisterClientAsReceiver(
    const client_id receiver_id,
    const message_type_id message_type) {
  internal::net::RegistrationRequest request;
  request.set_client(receiver_id);
  request.set_message_type(message_type);

  internal::net::BoolStatus response;
  grpc::ClientContext context;

  grpc::Status status = stub_->RegisterClientAsReceiver(&context,
                                                        request,
                                                        &response);
  assert(status.ok());

  return response.status();
}

MessageBus::SendStatus NativeNetClientMessageBus::Send(
    const client_id sender_id,
    const Address &destination_address,
    const MessageStyle &style,
    TaggedMessage &&message,  // NOLINT(whitespace/operators)
    const Priority priority,
    CancellationToken *cancellation_token) {
  internal::net::SendRequest request;
  request.set_sender(sender_id);
  request.set_send_to_all(destination_address.send_to_all_);
  for (const client_id recipient : destination_address.explicit_recipients_) {
    request.add_explicit_recipient(recipient);
  }
  request.set_broadcast(style.broadcast_);
  request.set_timeout(std::chrono::duration_cast<std::chrono::nanoseconds>(
      style.expiration_time_.time_since_epoch()).count());
  request.mutable_msg()->set_message_type(message.message_type());
  // TODO(chasseur): Protobuf API supports passing in an externally allocated
  // std::string to avoid a copy here, but we manage TaggedMessage's body at a
  // lower level than that using malloc/free plus our own version of the
  // short-string optimization. If message copying here and in NetServiceImpl
  // becomes a performance issue, we might consider changing the internal
  // representation of TaggedMessage to use a std::string so that we avoid a
  // copy into/out of protos.
  request.mutable_msg()->set_message_body(message.message(),
                                          message.message_bytes());
  request.set_priority(priority);
  request.set_cancellable(cancellation_token != nullptr);

  internal::net::SendResponse response;
  grpc::ClientContext context;

  grpc::Status status = stub_->Send(&context, request, &response);
  assert(status.ok());

  switch (response.status()) {
    case internal::net::SendResponse::OK:
      if (cancellation_token != nullptr) {
        cancellation_token->message_id_ = response.message_id();
      }
      return MessageBus::SendStatus::kOK;
    case internal::net::SendResponse::NO_RECEIVERS:
      return MessageBus::SendStatus::kNoReceivers;
    case internal::net::SendResponse::SENDER_NOT_CONNECTED:
      return MessageBus::SendStatus::kSenderNotConnected;
    case internal::net::SendResponse::SENDER_NOT_REGISTERED_FOR_MESSAGE_TYPE:
      return MessageBus::SendStatus::kSenderNotRegisteredForMessageType;
    case internal::net::SendResponse::RECEIVER_NOT_REGISTERED_FOR_MESSAGE_TYPE:
      return MessageBus::SendStatus::kReceiverNotRegisteredForMessageType;
    default:
      std::fprintf(
          stderr,
          "FATAL ERROR: Unrecognized value of status enum in SendResponse\n");
      std::exit(1);
  }
}

void NativeNetClientMessageBus::CancelMessage(
    const client_id sender_id,
    const CancellationToken &cancellation_token) {
  internal::net::DeleteOrCancelRequest request;
  request.set_client(sender_id);
  request.add_message_id(cancellation_token.message_id_);

  internal::net::EmptyMessage response;
  grpc::ClientContext context;

  grpc::Status status = stub_->SenderCancel(&context, request, &response);
  assert(status.ok());
}

std::size_t NativeNetClientMessageBus::CountQueuedMessagesForClient(
    const client_id receiver_id) {
  internal::net::CountQueuedMessagesRequest request;
  request.set_client(receiver_id);

  internal::net::CountQueuedMessagesResponse response;
  grpc::ClientContext context;

  grpc::Status status = stub_->CountQueuedMessages(&context,
                                                   request,
                                                   &response);
  assert(status.ok());

  return response.message_count();
}

std::size_t NativeNetClientMessageBus::ReceiveIfAvailableImpl(
    const client_id receiver_id,
    const Priority minimum_priority,
    const std::size_t max_messages,
    const bool delete_immediately,
    internal::ContainerPusher *pusher) {
  internal::net::ReceiveRequest request;
  request.set_receiver(receiver_id);
  request.set_minimum_priority(minimum_priority);
  request.set_maximum_messages(max_messages);
  request.set_delete_immediately(delete_immediately);

  internal::net::AnnotatedTmbMessage msg_proto;
  grpc::ClientContext context;

  std::unique_ptr<grpc::ClientReader<internal::net::AnnotatedTmbMessage>>
      reader(stub_->Receive(&context, request));
  std::size_t num_received = 0;
  while (reader->Read(&msg_proto)) {
    assert(msg_proto.has_tagged_message());
    ++num_received;

    AnnotatedMessage msg;
    msg.sender = msg_proto.sender();
    msg.send_time
        = std::chrono::time_point<std::chrono::high_resolution_clock>(
            std::chrono::nanoseconds(msg_proto.send_time()));
    msg.deletion_token.message_id = msg_proto.message_id();
    msg.tagged_message.set_message(
        msg_proto.tagged_message().message_body().c_str(),
        msg_proto.tagged_message().message_body().size(),
        msg_proto.tagged_message().message_type());

    pusher->Push(std::move(msg));
  }
  grpc::Status status = reader->Finish();
  assert(status.ok());

  return num_received;
}

void NativeNetClientMessageBus::DeleteImpl(
    const client_id receiver_id,
    internal::IteratorAdapter<const AnnotatedMessage> *adapter) {
  internal::net::DeleteOrCancelRequest request;
  request.set_client(receiver_id);
  while (adapter->Valid()) {
    request.add_message_id((*adapter)->deletion_token.message_id);
    adapter->Next();
  }

  internal::net::EmptyMessage response;
  grpc::ClientContext context;

  grpc::Status status = stub_->Delete(&context, request, &response);
  assert(status.ok());
}

void NativeNetClientMessageBus::CancelImpl(
    const client_id receiver_id,
    internal::IteratorAdapter<const AnnotatedMessage> *adapter) {
  internal::net::DeleteOrCancelRequest request;
  request.set_client(receiver_id);
  while (adapter->Valid()) {
    request.add_message_id((*adapter)->deletion_token.message_id);
    adapter->Next();
  }

  internal::net::EmptyMessage response;
  grpc::ClientContext context;

  grpc::Status status = stub_->ReceiverCancel(&context, request, &response);
  assert(status.ok());
}

}  // namespace tmb
<commit_msg>Replaced Breakage GRPC Credentials in Release 0.12.<commit_after>//   Copyright 2014-2015 Quickstep Technologies LLC.
//   Copyright 2015 Pivotal Software, Inc.
//
//   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

// TODO(chasseur): Better error handling in place of asserts.

#include "tmb/native_net_client_message_bus.h"

#include <cassert>
#include <chrono>  // NOLINT(build/c++11)
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <string>
#include <utility>

#include "grpc++/client_context.h"
#include "grpc++/create_channel.h"
#include "grpc++/security/credentials.h"
#include "grpc++/support/status.h"
#include "grpc++/support/sync_stream.h"

#include "tmb/address.h"
#include "tmb/cancellation_token.h"
#include "tmb/id_typedefs.h"
#include "tmb/message_bus.h"
#include "tmb/message_style.h"
#include "tmb/tagged_message.h"
#include "tmb/priority.h"

#include "tmb/internal/container_pusher.h"
#include "tmb/internal/iterator_adapter.h"
#include "tmb/internal/tmb_net.grpc.pb.h"
#include "tmb/internal/tmb_net.pb.h"

namespace tmb {

void NativeNetClientMessageBus::AddServer(const std::string &hostname,
                                          const std::uint16_t port) {
  server_address_ = hostname;
  server_address_.push_back(':');
  server_address_.append(std::to_string(static_cast<unsigned>(port)));
}

bool NativeNetClientMessageBus::Initialize() {
  stub_ = internal::net::MessageBus::NewStub(
      grpc::CreateChannel(server_address_,
                          grpc::InsecureChannelCredentials()));

  return (stub_.get() != nullptr);
}

void NativeNetClientMessageBus::ResetBus() {
  internal::net::EmptyMessage request;
  internal::net::EmptyMessage response;
  grpc::ClientContext context;

  grpc::Status status = stub_->ResetBus(&context, request, &response);
  assert(status.ok());
}

client_id NativeNetClientMessageBus::Connect() {
  internal::net::EmptyMessage request;
  internal::net::ConnectResponse response;
  grpc::ClientContext context;

  grpc::Status status = stub_->Connect(&context, request, &response);
  assert(status.ok());

  return response.client();
}

bool NativeNetClientMessageBus::Disconnect(const client_id client) {
  internal::net::DisconnectRequest request;
  request.set_client(client);

  internal::net::BoolStatus response;
  grpc::ClientContext context;

  grpc::Status status = stub_->Disconnect(&context, request, &response);
  assert(status.ok());

  return response.status();
}

bool NativeNetClientMessageBus::RegisterClientAsSender(
    const client_id sender_id,
    const message_type_id message_type) {
  internal::net::RegistrationRequest request;
  request.set_client(sender_id);
  request.set_message_type(message_type);

  internal::net::BoolStatus response;
  grpc::ClientContext context;

  grpc::Status status = stub_->RegisterClientAsSender(&context,
                                                      request,
                                                      &response);
  assert(status.ok());

  return response.status();
}

bool NativeNetClientMessageBus::RegisterClientAsReceiver(
    const client_id receiver_id,
    const message_type_id message_type) {
  internal::net::RegistrationRequest request;
  request.set_client(receiver_id);
  request.set_message_type(message_type);

  internal::net::BoolStatus response;
  grpc::ClientContext context;

  grpc::Status status = stub_->RegisterClientAsReceiver(&context,
                                                        request,
                                                        &response);
  assert(status.ok());

  return response.status();
}

MessageBus::SendStatus NativeNetClientMessageBus::Send(
    const client_id sender_id,
    const Address &destination_address,
    const MessageStyle &style,
    TaggedMessage &&message,  // NOLINT(whitespace/operators)
    const Priority priority,
    CancellationToken *cancellation_token) {
  internal::net::SendRequest request;
  request.set_sender(sender_id);
  request.set_send_to_all(destination_address.send_to_all_);
  for (const client_id recipient : destination_address.explicit_recipients_) {
    request.add_explicit_recipient(recipient);
  }
  request.set_broadcast(style.broadcast_);
  request.set_timeout(std::chrono::duration_cast<std::chrono::nanoseconds>(
      style.expiration_time_.time_since_epoch()).count());
  request.mutable_msg()->set_message_type(message.message_type());
  // TODO(chasseur): Protobuf API supports passing in an externally allocated
  // std::string to avoid a copy here, but we manage TaggedMessage's body at a
  // lower level than that using malloc/free plus our own version of the
  // short-string optimization. If message copying here and in NetServiceImpl
  // becomes a performance issue, we might consider changing the internal
  // representation of TaggedMessage to use a std::string so that we avoid a
  // copy into/out of protos.
  request.mutable_msg()->set_message_body(message.message(),
                                          message.message_bytes());
  request.set_priority(priority);
  request.set_cancellable(cancellation_token != nullptr);

  internal::net::SendResponse response;
  grpc::ClientContext context;

  grpc::Status status = stub_->Send(&context, request, &response);
  assert(status.ok());

  switch (response.status()) {
    case internal::net::SendResponse::OK:
      if (cancellation_token != nullptr) {
        cancellation_token->message_id_ = response.message_id();
      }
      return MessageBus::SendStatus::kOK;
    case internal::net::SendResponse::NO_RECEIVERS:
      return MessageBus::SendStatus::kNoReceivers;
    case internal::net::SendResponse::SENDER_NOT_CONNECTED:
      return MessageBus::SendStatus::kSenderNotConnected;
    case internal::net::SendResponse::SENDER_NOT_REGISTERED_FOR_MESSAGE_TYPE:
      return MessageBus::SendStatus::kSenderNotRegisteredForMessageType;
    case internal::net::SendResponse::RECEIVER_NOT_REGISTERED_FOR_MESSAGE_TYPE:
      return MessageBus::SendStatus::kReceiverNotRegisteredForMessageType;
    default:
      std::fprintf(
          stderr,
          "FATAL ERROR: Unrecognized value of status enum in SendResponse\n");
      std::exit(1);
  }
}

void NativeNetClientMessageBus::CancelMessage(
    const client_id sender_id,
    const CancellationToken &cancellation_token) {
  internal::net::DeleteOrCancelRequest request;
  request.set_client(sender_id);
  request.add_message_id(cancellation_token.message_id_);

  internal::net::EmptyMessage response;
  grpc::ClientContext context;

  grpc::Status status = stub_->SenderCancel(&context, request, &response);
  assert(status.ok());
}

std::size_t NativeNetClientMessageBus::CountQueuedMessagesForClient(
    const client_id receiver_id) {
  internal::net::CountQueuedMessagesRequest request;
  request.set_client(receiver_id);

  internal::net::CountQueuedMessagesResponse response;
  grpc::ClientContext context;

  grpc::Status status = stub_->CountQueuedMessages(&context,
                                                   request,
                                                   &response);
  assert(status.ok());

  return response.message_count();
}

std::size_t NativeNetClientMessageBus::ReceiveIfAvailableImpl(
    const client_id receiver_id,
    const Priority minimum_priority,
    const std::size_t max_messages,
    const bool delete_immediately,
    internal::ContainerPusher *pusher) {
  internal::net::ReceiveRequest request;
  request.set_receiver(receiver_id);
  request.set_minimum_priority(minimum_priority);
  request.set_maximum_messages(max_messages);
  request.set_delete_immediately(delete_immediately);

  internal::net::AnnotatedTmbMessage msg_proto;
  grpc::ClientContext context;

  std::unique_ptr<grpc::ClientReader<internal::net::AnnotatedTmbMessage>>
      reader(stub_->Receive(&context, request));
  std::size_t num_received = 0;
  while (reader->Read(&msg_proto)) {
    assert(msg_proto.has_tagged_message());
    ++num_received;

    AnnotatedMessage msg;
    msg.sender = msg_proto.sender();
    msg.send_time
        = std::chrono::time_point<std::chrono::high_resolution_clock>(
            std::chrono::nanoseconds(msg_proto.send_time()));
    msg.deletion_token.message_id = msg_proto.message_id();
    msg.tagged_message.set_message(
        msg_proto.tagged_message().message_body().c_str(),
        msg_proto.tagged_message().message_body().size(),
        msg_proto.tagged_message().message_type());

    pusher->Push(std::move(msg));
  }
  grpc::Status status = reader->Finish();
  assert(status.ok());

  return num_received;
}

void NativeNetClientMessageBus::DeleteImpl(
    const client_id receiver_id,
    internal::IteratorAdapter<const AnnotatedMessage> *adapter) {
  internal::net::DeleteOrCancelRequest request;
  request.set_client(receiver_id);
  while (adapter->Valid()) {
    request.add_message_id((*adapter)->deletion_token.message_id);
    adapter->Next();
  }

  internal::net::EmptyMessage response;
  grpc::ClientContext context;

  grpc::Status status = stub_->Delete(&context, request, &response);
  assert(status.ok());
}

void NativeNetClientMessageBus::CancelImpl(
    const client_id receiver_id,
    internal::IteratorAdapter<const AnnotatedMessage> *adapter) {
  internal::net::DeleteOrCancelRequest request;
  request.set_client(receiver_id);
  while (adapter->Valid()) {
    request.add_message_id((*adapter)->deletion_token.message_id);
    adapter->Next();
  }

  internal::net::EmptyMessage response;
  grpc::ClientContext context;

  grpc::Status status = stub_->ReceiverCancel(&context, request, &response);
  assert(status.ok());
}

}  // namespace tmb
<|endoftext|>