<commit_before>#include <requests/Context.hpp>
#include <requests/Utils.hpp>
#include <requests/Exception.hpp>

using namespace requests;

Context::Context(IOService &service, const Url &url, Method method, const StringMap &data)
    : Context(service, url, method, data, UserCallback())
{
}

Context::Context(IOService &service, const Url &url, Method method, const StringMap &data, const UserCallback &callback)
        : sock_(service),
          url_(url),
          callback_(callback),
          method_(method)
    {
        std::ostream reqStream(&requestBuff_);

        if (method_ == Method::Get)
        {
            url_.addQueries(data);

            reqStream << "GET " << url_.pathAndQueries() << " HTTP/1.1\r\n";
            reqStream << "Host: " << url_.host() << "\r\n";
            reqStream << "Accept: */*\r\n";
            reqStream << "Connection: close\r\n\r\n";
        }
        else if (method_ == Method::Post)
        {
            auto requestBody = urlEncode(data);
            auto length = std::to_string(requestBody.size());

            reqStream << "POST " << url_.path() << " HTTP/1.1\r\n";
            reqStream << "Host: " << url_.host() << "\r\n";
            reqStream << "Accept: */*\r\n";
            reqStream << "Content-Type: application/x-www-form-urlencoded\r\n";
            reqStream << "Content-Length: " << length << "\r\n";
            reqStream << "Connection: close\r\n\r\n";

            reqStream << requestBody;
        }
    }
<commit_msg>adjust style<commit_after>#include <requests/Context.hpp>
#include <requests/Utils.hpp>
#include <requests/Exception.hpp>

using namespace requests;

Context::Context(IOService &service, const Url &url, Method method, const StringMap &data)
    : Context(service, url, method, data, UserCallback())
{
}

Context::Context(IOService &service, const Url &url, Method method, const StringMap &data, const UserCallback &callback)
    : sock_(service),
      url_(url),
      callback_(callback),
      method_(method)
{
    std::ostream reqStream(&requestBuff_);

    if (method_ == Method::Get)
    {
        url_.addQueries(data);

        reqStream << "GET " << url_.pathAndQueries() << " HTTP/1.1\r\n";
        reqStream << "Host: " << url_.host() << "\r\n";
        reqStream << "Accept: */*\r\n";
        reqStream << "Connection: close\r\n\r\n";
    }
    else if (method_ == Method::Post)
    {
        auto requestBody = urlEncode(data);
        auto length = std::to_string(requestBody.size());

        reqStream << "POST " << url_.path() << " HTTP/1.1\r\n";
        reqStream << "Host: " << url_.host() << "\r\n";
        reqStream << "Accept: */*\r\n";
        reqStream << "Content-Type: application/x-www-form-urlencoded\r\n";
        reqStream << "Content-Length: " << length << "\r\n";
        reqStream << "Connection: close\r\n\r\n";

        reqStream << requestBody;
    }
}
<|endoftext|>


<commit_before>/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/

#include "tensorflow/core/common_runtime/optimization_registry.h"
#include "tensorflow/core/util/dump_graph.h"

namespace tensorflow {

// static
OptimizationPassRegistry* OptimizationPassRegistry::Global() {
  static OptimizationPassRegistry* global_optimization_registry =
      new OptimizationPassRegistry;
  return global_optimization_registry;
}

void OptimizationPassRegistry::Register(
    Grouping grouping, int phase, std::unique_ptr<GraphOptimizationPass> pass) {
  groups_[grouping][phase].push_back(std::move(pass));
}

Status OptimizationPassRegistry::RunGrouping(
    Grouping grouping, const GraphOptimizationPassOptions& options) {
  auto group = groups_.find(grouping);
  if (group != groups_.end()) {
    for (auto& phase : group->second) {
      VLOG(1) << "Running optimization phase " << phase.first;
      for (auto& pass : phase.second) {
        VLOG(1) << "Running optimization pass: " << pass->name();
        Status s = pass->Run(options);
        if (!s.ok()) return s;
        if (VLOG_IS_ON(1)) {
          if (options.graph) {
            DumpGraphToFile(
                strings::StrCat(
                    "after_phase_", phase.first, "_", pass->name(), "_",
                    reinterpret_cast<uintptr_t>((*options.graph).get())),
                **options.graph, options.flib_def);
          }
          if (options.partition_graphs) {
            for (auto& part : *options.partition_graphs) {
              DumpGraphToFile(
                  strings::StrCat(
                      "after_phase_", phase.first, "_", pass->name(),
                      "_partition_", part.first, "_",
                      reinterpret_cast<uintptr_t>(part.second.get())),
                  *part.second, options.flib_def);
            }
          }
        }
      }
    }
  }
  return Status::OK();
}

void OptimizationPassRegistry::LogGrouping(Grouping grouping, int vlog_level) {
  auto group = groups_.find(grouping);
  if (group != groups_.end()) {
    for (auto& phase : group->second) {
      for (auto& pass : phase.second) {
        VLOG(vlog_level) << "Registered optimization pass grouping " << grouping
                         << " phase " << phase.first << ": " << pass->name();
      }
    }
  }
}

void OptimizationPassRegistry::LogAllGroupings(int vlog_level) {
  for (auto group = groups_.begin(); group != groups_.end(); ++group) {
    LogGrouping(group->first, vlog_level);
  }
}

}  // namespace tensorflow
<commit_msg>[TF] Include group number in name of optimization_registry_dumps.<commit_after>/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/

#include "tensorflow/core/common_runtime/optimization_registry.h"
#include "tensorflow/core/util/dump_graph.h"

namespace tensorflow {

// static
OptimizationPassRegistry* OptimizationPassRegistry::Global() {
  static OptimizationPassRegistry* global_optimization_registry =
      new OptimizationPassRegistry;
  return global_optimization_registry;
}

void OptimizationPassRegistry::Register(
    Grouping grouping, int phase, std::unique_ptr<GraphOptimizationPass> pass) {
  groups_[grouping][phase].push_back(std::move(pass));
}

Status OptimizationPassRegistry::RunGrouping(
    Grouping grouping, const GraphOptimizationPassOptions& options) {
  auto group = groups_.find(grouping);
  if (group != groups_.end()) {
    for (auto& phase : group->second) {
      VLOG(1) << "Running optimization phase " << phase.first;
      for (auto& pass : phase.second) {
        VLOG(1) << "Running optimization pass: " << pass->name();
        Status s = pass->Run(options);
        if (!s.ok()) return s;
        if (VLOG_IS_ON(1)) {
          if (options.graph) {
            DumpGraphToFile(strings::StrCat("after_group_", grouping, "_phase_",
                                            phase.first, "_", pass->name(), "_",
                                            reinterpret_cast<uintptr_t>(
                                                (*options.graph).get())),
                            **options.graph, options.flib_def);
          }
          if (options.partition_graphs) {
            for (auto& part : *options.partition_graphs) {
              DumpGraphToFile(
                  strings::StrCat(
                      "after_group_", grouping, "_phase_", phase.first, "_",
                      pass->name(), "_partition_", part.first, "_",
                      reinterpret_cast<uintptr_t>(part.second.get())),
                  *part.second, options.flib_def);
            }
          }
        }
      }
    }
  }
  return Status::OK();
}

void OptimizationPassRegistry::LogGrouping(Grouping grouping, int vlog_level) {
  auto group = groups_.find(grouping);
  if (group != groups_.end()) {
    for (auto& phase : group->second) {
      for (auto& pass : phase.second) {
        VLOG(vlog_level) << "Registered optimization pass grouping " << grouping
                         << " phase " << phase.first << ": " << pass->name();
      }
    }
  }
}

void OptimizationPassRegistry::LogAllGroupings(int vlog_level) {
  for (auto group = groups_.begin(); group != groups_.end(); ++group) {
    LogGrouping(group->first, vlog_level);
  }
}

}  // namespace tensorflow
<|endoftext|>


<commit_before>// Copyright 2014 Open Source Robotics Foundation, 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.

#ifndef RCLCPP__FUNCTION_TRAITS_HPP_
#define RCLCPP__FUNCTION_TRAITS_HPP_

#include <memory>
#include <tuple>

namespace rclcpp
{

namespace function_traits

{

/* NOTE(esteve):
 * We support service callbacks that can optionally take the request id,
 * which should be possible with two overloaded create_service methods,
 * but unfortunately std::function's constructor on VS2015 is too greedy,
 * so we need a mechanism for checking the arity and the type of each argument
 * in a callback function.
 * See http://blogs.msdn.com/b/vcblog/archive/2015/06/19/c-11-14-17-features-in-vs-2015-rtm.aspx
 */

// Remove the first item in a tuple
template<typename T>
struct tuple_tail;

template<typename Head, typename ... Tail>
struct tuple_tail<std::tuple<Head, Tail ...>>
{
  using type = std::tuple<Tail ...>;
};

// std::function
template<typename FunctionT>
struct function_traits
{
  using arguments = typename tuple_tail<
      typename function_traits<decltype( & FunctionT::operator())>::arguments>::type;

  static constexpr std::size_t arity = std::tuple_size<arguments>::value;

  template<std::size_t N>
  using argument_type = typename std::tuple_element<N, arguments>::type;
};

// Free functions
template<typename ReturnTypeT, typename ... Args>
struct function_traits<ReturnTypeT(Args ...)>
{
  using arguments = std::tuple<Args ...>;

  static constexpr std::size_t arity = std::tuple_size<arguments>::value;

  template<std::size_t N>
  using argument_type = typename std::tuple_element<N, arguments>::type;
};

// Function pointers
template<typename ReturnTypeT, typename ... Args>
struct function_traits<ReturnTypeT (*)(Args ...)>: function_traits<ReturnTypeT(Args ...)>
{};

// Lambdas
template<typename ClassT, typename ReturnTypeT, typename ... Args>
struct function_traits<ReturnTypeT (ClassT::*)(Args ...) const>
  : function_traits<ReturnTypeT(ClassT &, Args ...)>
{};

template<typename FunctionT>
struct function_traits<FunctionT &>: function_traits<FunctionT>
{};

template<typename FunctionT>
struct function_traits<FunctionT &&>: function_traits<FunctionT>
{};

/* NOTE(esteve):
 * VS2015 does not support expression SFINAE, so we're using this template to evaluate
 * the arity of a function.
 */
template<std::size_t Arity, typename FunctorT>
struct arity_comparator : std::integral_constant<
    bool, (Arity == function_traits<FunctorT>::arity)>{};

template<typename FunctorT, typename ... Args>
struct check_arguments : std::is_same<
    typename function_traits<FunctorT>::arguments,
    std::tuple<Args ...>
  >
{};

template<typename FunctorAT, typename FunctorBT>
struct same_arguments : std::is_same<
    typename function_traits<FunctorAT>::arguments,
    typename function_traits<FunctorBT>::arguments
  >
{};

}  // namespace function_traits

}  // namespace rclcpp

#endif  // RCLCPP__FUNCTION_TRAITS_HPP_
<commit_msg>Fix uncrustify warning<commit_after>// Copyright 2014 Open Source Robotics Foundation, 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.

#ifndef RCLCPP__FUNCTION_TRAITS_HPP_
#define RCLCPP__FUNCTION_TRAITS_HPP_

#include <memory>
#include <tuple>

namespace rclcpp
{

namespace function_traits
{

/* NOTE(esteve):
 * We support service callbacks that can optionally take the request id,
 * which should be possible with two overloaded create_service methods,
 * but unfortunately std::function's constructor on VS2015 is too greedy,
 * so we need a mechanism for checking the arity and the type of each argument
 * in a callback function.
 * See http://blogs.msdn.com/b/vcblog/archive/2015/06/19/c-11-14-17-features-in-vs-2015-rtm.aspx
 */

// Remove the first item in a tuple
template<typename T>
struct tuple_tail;

template<typename Head, typename ... Tail>
struct tuple_tail<std::tuple<Head, Tail ...>>
{
  using type = std::tuple<Tail ...>;
};

// std::function
template<typename FunctionT>
struct function_traits
{
  using arguments = typename tuple_tail<
      typename function_traits<decltype( & FunctionT::operator())>::arguments>::type;

  static constexpr std::size_t arity = std::tuple_size<arguments>::value;

  template<std::size_t N>
  using argument_type = typename std::tuple_element<N, arguments>::type;
};

// Free functions
template<typename ReturnTypeT, typename ... Args>
struct function_traits<ReturnTypeT(Args ...)>
{
  using arguments = std::tuple<Args ...>;

  static constexpr std::size_t arity = std::tuple_size<arguments>::value;

  template<std::size_t N>
  using argument_type = typename std::tuple_element<N, arguments>::type;
};

// Function pointers
template<typename ReturnTypeT, typename ... Args>
struct function_traits<ReturnTypeT (*)(Args ...)>: function_traits<ReturnTypeT(Args ...)>
{};

// Lambdas
template<typename ClassT, typename ReturnTypeT, typename ... Args>
struct function_traits<ReturnTypeT (ClassT::*)(Args ...) const>
  : function_traits<ReturnTypeT(ClassT &, Args ...)>
{};

template<typename FunctionT>
struct function_traits<FunctionT &>: function_traits<FunctionT>
{};

template<typename FunctionT>
struct function_traits<FunctionT &&>: function_traits<FunctionT>
{};

/* NOTE(esteve):
 * VS2015 does not support expression SFINAE, so we're using this template to evaluate
 * the arity of a function.
 */
template<std::size_t Arity, typename FunctorT>
struct arity_comparator : std::integral_constant<
    bool, (Arity == function_traits<FunctorT>::arity)>{};

template<typename FunctorT, typename ... Args>
struct check_arguments : std::is_same<
    typename function_traits<FunctorT>::arguments,
    std::tuple<Args ...>
  >
{};

template<typename FunctorAT, typename FunctorBT>
struct same_arguments : std::is_same<
    typename function_traits<FunctorAT>::arguments,
    typename function_traits<FunctorBT>::arguments
  >
{};

}  // namespace function_traits

}  // namespace rclcpp

#endif  // RCLCPP__FUNCTION_TRAITS_HPP_
<|endoftext|>


<commit_before>#include <stan/math/rev.hpp>
#include <test/unit/util.hpp>

#include <gtest/gtest.h>

TEST(MathFunRev, to_vector_var_value) {
  constexpr Eigen::Index n = 100;
  Eigen::MatrixXd a_val = Eigen::MatrixXd::Random(n, n);
  stan::math::var_value<Eigen::MatrixXd> a(a_val);
  const auto& tmp = stan::math::to_vector(a);
  EXPECT_TRUE((stan::is_col_vector<decltype(tmp.val())>::value));
  EXPECT_MATRIX_EQ(tmp.val(), Eigen::Map<Eigen::VectorXd>(a_val.data(), a.rows() * a.cols()));
}
<commit_msg>adds check that adj is referenced w/o a copy<commit_after>#include <stan/math/rev.hpp>
#include <test/unit/util.hpp>

#include <gtest/gtest.h>

TEST(MathFunRev, to_vector_var_value) {
  constexpr Eigen::Index n = 100;
  Eigen::MatrixXd a_val = Eigen::MatrixXd::Random(n, n);
  stan::math::var_value<Eigen::MatrixXd> a(a_val);
  auto&& tmp = stan::math::to_vector(a);
  EXPECT_TRUE((stan::is_col_vector<decltype(tmp.val())>::value));
  EXPECT_MATRIX_EQ(tmp.val(), Eigen::Map<Eigen::VectorXd>(a_val.data(), a.rows() * a.cols()));
  tmp.adj().array() += 1.0;
  EXPECT_MATRIX_EQ(tmp.adj(), Eigen::Map<Eigen::VectorXd>(a.vi_->adj_.data(), a.rows() * a.cols()));
}
<|endoftext|>


<commit_before>/*
Copyright (C) 2016 iNuron NV

This file is part of Open vStorage Open Source Edition (OSE), as available from

  http://www.openvstorage.org and
  http://www.openvstorage.com.

This file is free software; you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License v3 (GNU AGPLv3)
as published by the Free Software Foundation, in version 3 as it comes
in the <LICENSE.txt> file of the Open vStorage OSE distribution.

Open vStorage is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY of any kind.
*/

#include "llio.h"
#include "proxy_protocol.h"
#include "snappy.h"
#include "stuff.h"
#include <boost/optional/optional_io.hpp>

namespace alba {
namespace llio {

using namespace proxy_protocol;
template <> void from(message &m, EncodingScheme &es) {
  uint8_t version;
  from(m, version);
  if (version != 1) {
    throw deserialisation_exception("unexpected EncodingScheme version");
  }

  from(m, es.k);
  from(m, es.m);
  from(m, es.w);
}

void from(message &m, std::unique_ptr<Compression> &p) {
  uint8_t type;
  from(m, type);
  Compression *r;
  switch (type) {
  case 1: {
    r = new NoCompression();
  }; break;
  case 2: {
    r = new SnappyCompression();
  }; break;
  case 3: {
    r = new BZip2Compression();
  }; break;
  case 4: {
    r = new TestCompression();
  }; break;
  default: {
      ALBA_LOG(WARNING, "unknown compression type " << (int)type);
      throw deserialisation_exception("unknown compression type"); };
  }
  p.reset(r);
}

void from(message &m, chaining_mode_t &mode) {
  uint8_t type;
  from(m, type);
  switch (type) {
  case 1: {
    mode = chaining_mode_t::CBC;
  }; break;
  case 2: {
    mode = chaining_mode_t::CTR;
  }; break;
  default: { throw deserialisation_exception("unknown chaining_mode"); };
  }
}

void from(message &m, key_length_t &kl) {
  uint8_t type;
  from(m, type);
  switch (type) {
  case 1: {
    kl = key_length_t::L256;
  } break;
  default: { throw deserialisation_exception("unknown key_length"); };
  }
}

void from(message &m, proxy_protocol::algo_t &algo) {
  uint8_t type;
  from(m, type);
  switch (type) {
  case 1: {
    algo = proxy_protocol::algo_t::AES;
  }; break;
  default: { throw deserialisation_exception("unknown algo"); };
  }
}
void from(message &m, AlgoWithKey &awk) {

  // ALBA_LOG(DEBUG, "pos = " << m.get_pos());
  // alba::stuff::dump_buffer(std::cout, m.current(16), 16);

  // AES   CTR  KEY_LENGTH  x length of key  key bytes
  // 01    02   01         01 20 00 00 00    fa c8
  from(m, awk.algo);
  from(m, awk.mode);
  from(m, awk.key_length);
  uint8_t x;
  from (m,x);
  from(m, awk.key);
  if (awk.key.size() != 32){
      throw deserialisation_exception("key length != 32");
  }
}

void from(message &m, std::unique_ptr<EncryptInfo> &p) {
  uint8_t type;
  from(m, type);
  EncryptInfo *r;
  switch (type) {
  case 1: {
    r = new NoEncryption();
  }; break;
  case 2: {
    AlgoWithKey *awk = new AlgoWithKey();
    from(m, *awk);
    r = awk;
  }; break;
  default: {
      ALBA_LOG(WARNING, "unknown encryption scheme: type=" << type);
      throw deserialisation_exception("unknown encryption scheme)"); };
  }
  p.reset(r);
}

template <> void from(message &m, Manifest &mf) {
  uint8_t version;
  from(m, version);
  if (version != 1) {
    throw deserialisation_exception("unexpecteded Manifest version");
  }

  std::string compressed;
  from(m, compressed);

  std::string real;
  snappy::Uncompress(compressed.data(), compressed.size(), &real);
  std::vector<char> buffer(real.begin(), real.end());
  message m2(buffer);
  from(m2, mf.name);
  from(m2, mf.object_id);

  std::vector<uint32_t> chunk_sizes;
  from(m2, mf.chunk_sizes);

  uint8_t version2;
  from(m2, version2);
  if (version2 != 1) {
    throw deserialisation_exception("unexpected version2");
  }

  from(m2, mf.encoding_scheme);

  from(m2, mf.compression);

  from(m2, mf.encrypt_info);
  from(m2, mf.checksum);
  from(m2, mf.size);
  uint8_t layout_tag;
  from(m2, layout_tag);
  if (layout_tag != 1) {
    throw deserialisation_exception("unexpected layout tag");
  }
  from(m2, mf.fragment_locations);

  uint8_t layout_tag2;
  from(m2, layout_tag2);
  if (layout_tag2 != 1) {
    throw deserialisation_exception("unexpected layout tag 2");
  }

  // from(m2, mf.fragment_checksums); // TODO: how to this via the layout based
  // template ?
  // iso this:

  uint32_t n_chunks;
  from(m2, n_chunks);

  for (uint32_t i = 0; i < n_chunks; i++) {
    uint32_t n_fragments;
    from(m2, n_fragments);
    std::vector<std::shared_ptr<alba::Checksum>> chunk(n_fragments);
    for (int32_t f = n_fragments - 1; f >= 0; --f) {
      alba::Checksum *p;
      from(m2, p);
      std::shared_ptr<alba::Checksum> sp(p);
      chunk[f] = sp;
    };
    mf.fragment_checksums.push_back(chunk);
  }

  uint8_t layout_tag3;
  from(m2, layout_tag3);
  if (layout_tag3 != 1) {
    throw deserialisation_exception("unexpected layout tag 3");
  }

  from(m2, mf.fragment_packed_sizes);

  from(m2, mf.version_id);
  from(m2, mf.max_disks_per_node);
  from(m2, mf.timestamp);
}

template <>
void from(message &m, ManifestWithNamespaceId &mfid) {
  from(m, (Manifest &)mfid);
  from(m, mfid.namespace_id);
}
}

namespace proxy_protocol {

std::ostream &operator<<(std::ostream &os, const EncodingScheme &scheme) {
  os << "EncodingScheme{k=" << scheme.k << ", m=" << scheme.m
     << ", w=" << (int)scheme.w << "}";

  return os;
}

std::ostream &operator<<(std::ostream &os, const compressor_t &compressor) {
  switch (compressor) {
  case compressor_t::NO_COMPRESSION:
    os << "NO_COMPRESSION";
    break;
  case compressor_t::SNAPPY:
    os << "SNAPPY";
    break;
  case compressor_t::BZIP2:
    os << "BZIP2"; break;
  case compressor_t::TEST:
    os << "TEST"; break;
  };
  return os;
}

std::ostream &operator <<(std::ostream &os, const algo_t &algo){
    switch(algo){
    case algo_t::AES:{
        os <<"AES";
    }
    };
    return os;
}

std::ostream &operator <<(std::ostream &os, const chaining_mode_t &mode){
    switch(mode){
    case chaining_mode_t::CBC:{
        os <<"CBC";
    };break;
    case chaining_mode_t::CTR:{
        os << "CTR";
    };break;
    };
    return os;
}

std::ostream &operator<<(std::ostream &os, const Compression &c) {
  c.print(os);
  return os;
}

std::ostream &operator<<(std::ostream &os, const encryption_t &encryption) {
  switch (encryption) {
  case encryption_t::NO_ENCRYPTION:
    os << "NO_ENCRYPTION";
    break;
  case encryption_t::ALGO_WITH_KEY:
    os << "ALGO_WITH_KEY";
  default:
    os << "?encryption?";
  };
  return os;
}

std::ostream &operator<<(std::ostream &os, const EncryptInfo &info) {
  info.print(os);
  return os;
}

std::ostream &operator<<(std::ostream &os, const fragment_location_t &f) {
  os << "(" << f.first // boost knows how
     << ", " << f.second << ")";
  return os;
}

void dump_string(std::ostream &os, const std::string &s) {
  const char *bytes = s.data();
  const int size = s.size();
  stuff::dump_buffer(os, bytes, size);
}
std::ostream &operator<<(std::ostream &os, const Manifest &mf) {
  using alba::stuff::operator<<;
  os << "{"
     << "name = `";
  dump_string(os, mf.name);
  os << "`, " << std::endl;
  os << "  object_id = `";
  dump_string(os, mf.object_id);
  os << "`, " << std::endl

     << "  encoding_scheme = " << mf.encoding_scheme << "," << std::endl
     << "  compression = " << *mf.compression << "," << std::endl
     << "  encryptinfo = " << *mf.encrypt_info << "," // dangerous
     << "  chunk_sizes = " << mf.chunk_sizes << "," << std::endl
     << "  size = " << mf.size << std::endl
     << std::endl
     << "  checksum= " << *mf.checksum << "," << std::endl
     << "  fragment_locations = " << mf.fragment_locations << "," << std::endl
     << "  fragment_checksums = " << mf.fragment_checksums << "," << std::endl
     << "  fragment_packed_sizes = [" << std::endl;

  for (const std::vector<uint32_t> &c : mf.fragment_packed_sizes) {
    os << c << "," << std::endl;
  }
  os << "  ], ";
  os << std::endl
     << "  version_id = " << mf.version_id << "," << std::endl
     << "  timestamp = " << mf.timestamp // TODO: decent formatting?
     << "}";
  return os;
}

std::ostream &operator<<(std::ostream &os,
                         const ManifestWithNamespaceId &mfid) {
  os << "{" << (Manifest &)mfid << ", namespace_id = " << mfid.namespace_id
     << "} ";
  return os;
}
}
}
<commit_msg>temporarily disabling AlgoWithKey<commit_after>/*
Copyright (C) 2016 iNuron NV

This file is part of Open vStorage Open Source Edition (OSE), as available from

  http://www.openvstorage.org and
  http://www.openvstorage.com.

This file is free software; you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License v3 (GNU AGPLv3)
as published by the Free Software Foundation, in version 3 as it comes
in the <LICENSE.txt> file of the Open vStorage OSE distribution.

Open vStorage is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY of any kind.
*/

#include "llio.h"
#include "proxy_protocol.h"
#include "snappy.h"
#include "stuff.h"
#include <boost/optional/optional_io.hpp>

namespace alba {
namespace llio {

using namespace proxy_protocol;
template <> void from(message &m, EncodingScheme &es) {
  uint8_t version;
  from(m, version);
  if (version != 1) {
    throw deserialisation_exception("unexpected EncodingScheme version");
  }

  from(m, es.k);
  from(m, es.m);
  from(m, es.w);
}

void from(message &m, std::unique_ptr<Compression> &p) {
  uint8_t type;
  from(m, type);
  Compression *r;
  switch (type) {
  case 1: {
    r = new NoCompression();
  }; break;
  case 2: {
    r = new SnappyCompression();
  }; break;
  case 3: {
    r = new BZip2Compression();
  }; break;
  case 4: {
    r = new TestCompression();
  }; break;
  default: {
      ALBA_LOG(WARNING, "unknown compression type " << (int)type);
      throw deserialisation_exception("unknown compression type"); };
  }
  p.reset(r);
}

void from(message &m, chaining_mode_t &mode) {
  uint8_t type;
  from(m, type);
  switch (type) {
  case 1: {
    mode = chaining_mode_t::CBC;
  }; break;
  case 2: {
    mode = chaining_mode_t::CTR;
  }; break;
  default: { throw deserialisation_exception("unknown chaining_mode"); };
  }
}

void from(message &m, key_length_t &kl) {
  uint8_t type;
  from(m, type);
  switch (type) {
  case 1: {
    kl = key_length_t::L256;
  } break;
  default: { throw deserialisation_exception("unknown key_length"); };
  }
}

void from(message &m, proxy_protocol::algo_t &algo) {
  uint8_t type;
  from(m, type);
  switch (type) {
  case 1: {
    algo = proxy_protocol::algo_t::AES;
  }; break;
  default: { throw deserialisation_exception("unknown algo"); };
  }
}
void from(message &m, AlgoWithKey &awk) {

  // ALBA_LOG(DEBUG, "pos = " << m.get_pos());
  // alba::stuff::dump_buffer(std::cout, m.current(16), 16);

  // AES   CTR  KEY_LENGTH  x length of key  key bytes
  // 01    02   01         01 20 00 00 00    fa c8
  from(m, awk.algo);
  from(m, awk.mode);
  from(m, awk.key_length);
  uint8_t x;
  from (m,x);
  from(m, awk.key);
  if (awk.key.size() != 32){
      throw deserialisation_exception("key length != 32");
  }
}

void from(message &m, std::unique_ptr<EncryptInfo> &p) {
  uint8_t type;
  from(m, type);
  EncryptInfo *r;
  switch (type) {
  case 1: {
    r = new NoEncryption();
  }; break;
  case 2: {
    // comment for now until the serialisation has been cleared out

    /* AlgoWithKey *awk = new AlgoWithKey();
    from(m, *awk);
    r = awk; */

    throw deserialisation_exception("AlgoWithKey not yet supported");

  }; break;
  default: {
      ALBA_LOG(WARNING, "unknown encryption scheme: type=" << type);
      throw deserialisation_exception("unknown encryption scheme)"); };
  }
  p.reset(r);
}

template <> void from(message &m, Manifest &mf) {
  uint8_t version;
  from(m, version);
  if (version != 1) {
    throw deserialisation_exception("unexpecteded Manifest version");
  }

  std::string compressed;
  from(m, compressed);

  std::string real;
  snappy::Uncompress(compressed.data(), compressed.size(), &real);
  std::vector<char> buffer(real.begin(), real.end());
  message m2(buffer);
  from(m2, mf.name);
  from(m2, mf.object_id);

  std::vector<uint32_t> chunk_sizes;
  from(m2, mf.chunk_sizes);

  uint8_t version2;
  from(m2, version2);
  if (version2 != 1) {
    throw deserialisation_exception("unexpected version2");
  }

  from(m2, mf.encoding_scheme);

  from(m2, mf.compression);

  from(m2, mf.encrypt_info);
  from(m2, mf.checksum);
  from(m2, mf.size);
  uint8_t layout_tag;
  from(m2, layout_tag);
  if (layout_tag != 1) {
    throw deserialisation_exception("unexpected layout tag");
  }
  from(m2, mf.fragment_locations);

  uint8_t layout_tag2;
  from(m2, layout_tag2);
  if (layout_tag2 != 1) {
    throw deserialisation_exception("unexpected layout tag 2");
  }

  // from(m2, mf.fragment_checksums); // TODO: how to this via the layout based
  // template ?
  // iso this:

  uint32_t n_chunks;
  from(m2, n_chunks);

  for (uint32_t i = 0; i < n_chunks; i++) {
    uint32_t n_fragments;
    from(m2, n_fragments);
    std::vector<std::shared_ptr<alba::Checksum>> chunk(n_fragments);
    for (int32_t f = n_fragments - 1; f >= 0; --f) {
      alba::Checksum *p;
      from(m2, p);
      std::shared_ptr<alba::Checksum> sp(p);
      chunk[f] = sp;
    };
    mf.fragment_checksums.push_back(chunk);
  }

  uint8_t layout_tag3;
  from(m2, layout_tag3);
  if (layout_tag3 != 1) {
    throw deserialisation_exception("unexpected layout tag 3");
  }

  from(m2, mf.fragment_packed_sizes);

  from(m2, mf.version_id);
  from(m2, mf.max_disks_per_node);
  from(m2, mf.timestamp);
}

template <>
void from(message &m, ManifestWithNamespaceId &mfid) {
  from(m, (Manifest &)mfid);
  from(m, mfid.namespace_id);
}
}

namespace proxy_protocol {

std::ostream &operator<<(std::ostream &os, const EncodingScheme &scheme) {
  os << "EncodingScheme{k=" << scheme.k << ", m=" << scheme.m
     << ", w=" << (int)scheme.w << "}";

  return os;
}

std::ostream &operator<<(std::ostream &os, const compressor_t &compressor) {
  switch (compressor) {
  case compressor_t::NO_COMPRESSION:
    os << "NO_COMPRESSION";
    break;
  case compressor_t::SNAPPY:
    os << "SNAPPY";
    break;
  case compressor_t::BZIP2:
    os << "BZIP2"; break;
  case compressor_t::TEST:
    os << "TEST"; break;
  };
  return os;
}

std::ostream &operator <<(std::ostream &os, const algo_t &algo){
    switch(algo){
    case algo_t::AES:{
        os <<"AES";
    }
    };
    return os;
}

std::ostream &operator <<(std::ostream &os, const chaining_mode_t &mode){
    switch(mode){
    case chaining_mode_t::CBC:{
        os <<"CBC";
    };break;
    case chaining_mode_t::CTR:{
        os << "CTR";
    };break;
    };
    return os;
}

std::ostream &operator<<(std::ostream &os, const Compression &c) {
  c.print(os);
  return os;
}

std::ostream &operator<<(std::ostream &os, const encryption_t &encryption) {
  switch (encryption) {
  case encryption_t::NO_ENCRYPTION:
    os << "NO_ENCRYPTION";
    break;
  case encryption_t::ALGO_WITH_KEY:
    os << "ALGO_WITH_KEY";
  default:
    os << "?encryption?";
  };
  return os;
}

std::ostream &operator<<(std::ostream &os, const EncryptInfo &info) {
  info.print(os);
  return os;
}

std::ostream &operator<<(std::ostream &os, const fragment_location_t &f) {
  os << "(" << f.first // boost knows how
     << ", " << f.second << ")";
  return os;
}

void dump_string(std::ostream &os, const std::string &s) {
  const char *bytes = s.data();
  const int size = s.size();
  stuff::dump_buffer(os, bytes, size);
}
std::ostream &operator<<(std::ostream &os, const Manifest &mf) {
  using alba::stuff::operator<<;
  os << "{"
     << "name = `";
  dump_string(os, mf.name);
  os << "`, " << std::endl;
  os << "  object_id = `";
  dump_string(os, mf.object_id);
  os << "`, " << std::endl

     << "  encoding_scheme = " << mf.encoding_scheme << "," << std::endl
     << "  compression = " << *mf.compression << "," << std::endl
     << "  encryptinfo = " << *mf.encrypt_info << "," // dangerous
     << "  chunk_sizes = " << mf.chunk_sizes << "," << std::endl
     << "  size = " << mf.size << std::endl
     << std::endl
     << "  checksum= " << *mf.checksum << "," << std::endl
     << "  fragment_locations = " << mf.fragment_locations << "," << std::endl
     << "  fragment_checksums = " << mf.fragment_checksums << "," << std::endl
     << "  fragment_packed_sizes = [" << std::endl;

  for (const std::vector<uint32_t> &c : mf.fragment_packed_sizes) {
    os << c << "," << std::endl;
  }
  os << "  ], ";
  os << std::endl
     << "  version_id = " << mf.version_id << "," << std::endl
     << "  timestamp = " << mf.timestamp // TODO: decent formatting?
     << "}";
  return os;
}

std::ostream &operator<<(std::ostream &os,
                         const ManifestWithNamespaceId &mfid) {
  os << "{" << (Manifest &)mfid << ", namespace_id = " << mfid.namespace_id
     << "} ";
  return os;
}
}
}
<|endoftext|>


<commit_before>/*  Sirikata
 *  SpaceNodeConnection.cpp
 *
 *  Copyright (c) 2009, Ewen Cheslack-Postava
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of Sirikata nor the names of its contributors may
 *    be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sirikata/oh/SpaceNodeConnection.hpp>
#include <sirikata/core/trace/Trace.hpp>
#include <sirikata/core/network/StreamFactory.hpp>
#include <sirikata/core/network/Stream.hpp>
#include <sirikata/core/util/PluginManager.hpp>
#include <sirikata/core/options/CommonOptions.hpp>
#include <sirikata/core/network/IOStrandImpl.hpp>

using namespace Sirikata;
using namespace Sirikata::Network;

namespace Sirikata {

SpaceNodeConnection::SpaceNodeConnection(ObjectHostContext* ctx, Network::IOStrand* ioStrand, TimeProfiler::Stage* handle_read_stage, OptionSet *streamOptions, const SpaceID& spaceid, ServerID sid, const Network::Address& addr, ConnectionEventCallback ccb, ReceiveCallback rcb)
 : mContext(ctx),
   mHandleReadStage(handle_read_stage),
   mSpace(spaceid),
   mServer(sid),
   socket(Sirikata::Network::StreamFactory::getSingleton().getConstructor(GetOptionValue<String>("ohstreamlib"))(&ioStrand->service(),streamOptions)),
   mAddr(addr),
   mConnecting(false),
   receive_queue(GetOptionValue<int32>("object-host-receive-buffer"), std::tr1::bind(&ObjectMessage::size, std::tr1::placeholders::_1)),
   mConnectCB(ccb),
   mReceiveCB(rcb)
{
    static Sirikata::PluginManager sPluginManager;
    static int tcpSstLoaded=(sPluginManager.load(GetOptionValue<String>("ohstreamlib")),0);
}

SpaceNodeConnection::~SpaceNodeConnection() {
    delete socket;
}

bool SpaceNodeConnection::push(const ObjectMessage& msg) {
    TIMESTAMP_START(tstamp, (&msg));

    std::string data;
    msg.serialize(&data);

    // Try to push to the network
    bool success = socket->send(
        //Sirikata::MemoryReference(&(data[0]), data.size()),
        Sirikata::MemoryReference(data),
        Sirikata::Network::ReliableOrdered
                                );
    if (success) {
        TIMESTAMP_END(tstamp, Trace::OH_HIT_NETWORK);
    }
    else {
        TIMESTAMP_END(tstamp, Trace::OH_DROPPED_AT_SEND);
        TRACE_DROP(OH_DROPPED_AT_SEND);
    }

    return success;
}

ObjectMessage* SpaceNodeConnection::pull() {
    return receive_queue.pull();
}

bool SpaceNodeConnection::empty() {
    return receive_queue.empty();
}

void SpaceNodeConnection::handleRead(Chunk& chunk, const Sirikata::Network::Stream::PauseReceiveCallback& pause) {
    mHandleReadStage->started();

    // Parse
    ObjectMessage* msg = new ObjectMessage();
    bool parse_success = msg->ParseFromArray(&(*chunk.begin()), chunk.size());
    assert(parse_success == true);

    TIMESTAMP_START(tstamp, msg);

    // Mark as received
    TIMESTAMP_END(tstamp, Trace::OH_NET_RECEIVED);

    // NOTE: We can't record drops here or we incur a lot of overhead in parsing...
    bool pushed = receive_queue.push(msg);

    if (pushed) {
        // handleConnectionRead() could be called from any thread/strand. Everything that is not
        // thread safe that could result from a new message needs to happen in the main strand,
        // so just post the whole handler there.
        if (receive_queue.wentNonEmpty())
            mReceiveCB(this);
    }
    else {
        TIMESTAMP_END(tstamp, Trace::OH_DROPPED_AT_RECEIVE_QUEUE);
        TRACE_DROP(OH_DROPPED_AT_RECEIVE_QUEUE);
    }

    mHandleReadStage->finished();

    // No matter what, we've "handled" the data, either for real or by dropping.
}

void SpaceNodeConnection::connect() {
    using std::tr1::placeholders::_1;
    using std::tr1::placeholders::_2;

    mConnecting = true;

    socket->connect(mAddr,
        &Sirikata::Network::Stream::ignoreSubstreamCallback,
        mContext->mainStrand->wrap( std::tr1::bind(&SpaceNodeConnection::handleConnectionEvent, this, _1, _2) ),
        std::tr1::bind(&SpaceNodeConnection::handleRead, this, _1, _2),
        &Sirikata::Network::Stream::ignoreReadySendCallback
    );
}

void SpaceNodeConnection::handleConnectionEvent(
    const Network::Stream::ConnectionStatus status,
    const std::string&reason)
{
    mConnecting = false;

    invokeAndClearCallbacks( status == Network::Stream::Connected );
    mConnectCB(status, reason);
}

void SpaceNodeConnection::invokeAndClearCallbacks(bool connected) {
    SpaceNodeConnection* param = (connected ? this : NULL);
    for(ConnectionCallbackList::iterator it = mConnectCallbacks.begin(); it != mConnectCallbacks.end(); it++)
        (*it)(param);
    mConnectCallbacks.clear();
}

void SpaceNodeConnection::shutdown() {
    socket->close();
}

}
<commit_msg>Fix leak of message when it is dropped.<commit_after>/*  Sirikata
 *  SpaceNodeConnection.cpp
 *
 *  Copyright (c) 2009, Ewen Cheslack-Postava
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of Sirikata nor the names of its contributors may
 *    be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sirikata/oh/SpaceNodeConnection.hpp>
#include <sirikata/core/trace/Trace.hpp>
#include <sirikata/core/network/StreamFactory.hpp>
#include <sirikata/core/network/Stream.hpp>
#include <sirikata/core/util/PluginManager.hpp>
#include <sirikata/core/options/CommonOptions.hpp>
#include <sirikata/core/network/IOStrandImpl.hpp>

using namespace Sirikata;
using namespace Sirikata::Network;

namespace Sirikata {

SpaceNodeConnection::SpaceNodeConnection(ObjectHostContext* ctx, Network::IOStrand* ioStrand, TimeProfiler::Stage* handle_read_stage, OptionSet *streamOptions, const SpaceID& spaceid, ServerID sid, const Network::Address& addr, ConnectionEventCallback ccb, ReceiveCallback rcb)
 : mContext(ctx),
   mHandleReadStage(handle_read_stage),
   mSpace(spaceid),
   mServer(sid),
   socket(Sirikata::Network::StreamFactory::getSingleton().getConstructor(GetOptionValue<String>("ohstreamlib"))(&ioStrand->service(),streamOptions)),
   mAddr(addr),
   mConnecting(false),
   receive_queue(GetOptionValue<int32>("object-host-receive-buffer"), std::tr1::bind(&ObjectMessage::size, std::tr1::placeholders::_1)),
   mConnectCB(ccb),
   mReceiveCB(rcb)
{
    static Sirikata::PluginManager sPluginManager;
    static int tcpSstLoaded=(sPluginManager.load(GetOptionValue<String>("ohstreamlib")),0);
}

SpaceNodeConnection::~SpaceNodeConnection() {
    delete socket;
}

bool SpaceNodeConnection::push(const ObjectMessage& msg) {
    TIMESTAMP_START(tstamp, (&msg));

    std::string data;
    msg.serialize(&data);

    // Try to push to the network
    bool success = socket->send(
        //Sirikata::MemoryReference(&(data[0]), data.size()),
        Sirikata::MemoryReference(data),
        Sirikata::Network::ReliableOrdered
                                );
    if (success) {
        TIMESTAMP_END(tstamp, Trace::OH_HIT_NETWORK);
    }
    else {
        TIMESTAMP_END(tstamp, Trace::OH_DROPPED_AT_SEND);
        TRACE_DROP(OH_DROPPED_AT_SEND);
    }

    return success;
}

ObjectMessage* SpaceNodeConnection::pull() {
    return receive_queue.pull();
}

bool SpaceNodeConnection::empty() {
    return receive_queue.empty();
}

void SpaceNodeConnection::handleRead(Chunk& chunk, const Sirikata::Network::Stream::PauseReceiveCallback& pause) {
    mHandleReadStage->started();

    // Parse
    ObjectMessage* msg = new ObjectMessage();
    bool parse_success = msg->ParseFromArray(&(*chunk.begin()), chunk.size());
    assert(parse_success == true);

    TIMESTAMP_START(tstamp, msg);

    // Mark as received
    TIMESTAMP_END(tstamp, Trace::OH_NET_RECEIVED);

    // NOTE: We can't record drops here or we incur a lot of overhead in parsing...
    bool pushed = receive_queue.push(msg);

    if (pushed) {
        // handleConnectionRead() could be called from any thread/strand. Everything that is not
        // thread safe that could result from a new message needs to happen in the main strand,
        // so just post the whole handler there.
        if (receive_queue.wentNonEmpty())
            mReceiveCB(this);
    }
    else {
        TIMESTAMP_END(tstamp, Trace::OH_DROPPED_AT_RECEIVE_QUEUE);
        TRACE_DROP(OH_DROPPED_AT_RECEIVE_QUEUE);
        delete msg;
    }

    mHandleReadStage->finished();

    // No matter what, we've "handled" the data, either for real or by dropping.
}

void SpaceNodeConnection::connect() {
    using std::tr1::placeholders::_1;
    using std::tr1::placeholders::_2;

    mConnecting = true;

    socket->connect(mAddr,
        &Sirikata::Network::Stream::ignoreSubstreamCallback,
        mContext->mainStrand->wrap( std::tr1::bind(&SpaceNodeConnection::handleConnectionEvent, this, _1, _2) ),
        std::tr1::bind(&SpaceNodeConnection::handleRead, this, _1, _2),
        &Sirikata::Network::Stream::ignoreReadySendCallback
    );
}

void SpaceNodeConnection::handleConnectionEvent(
    const Network::Stream::ConnectionStatus status,
    const std::string&reason)
{
    mConnecting = false;

    invokeAndClearCallbacks( status == Network::Stream::Connected );
    mConnectCB(status, reason);
}

void SpaceNodeConnection::invokeAndClearCallbacks(bool connected) {
    SpaceNodeConnection* param = (connected ? this : NULL);
    for(ConnectionCallbackList::iterator it = mConnectCallbacks.begin(); it != mConnectCallbacks.end(); it++)
        (*it)(param);
    mConnectCallbacks.clear();
}

void SpaceNodeConnection::shutdown() {
    socket->close();
}

}
<|endoftext|>


<commit_before>//
//  video-coder.cpp
//  ndnrtc
//
//  Copyright 2013 Regents of the University of California
//  For licensing details see the LICENSE file.
//
//  Author:  Peter Gusev
//  Created: 8/21/13
//

// #define USE_VP9

#include <boost/thread.hpp>
#include <webrtc/modules/video_coding/codecs/vp8/include/vp8.h>
#include <webrtc/modules/video_coding/codecs/vp9/include/vp9.h>
#include <webrtc/modules/video_coding/include/video_coding.h>
#include <webrtc/modules/video_coding/include/video_codec_interface.h>
#include <webrtc/modules/video_coding/codec_database.h>

#include "video-coder.hpp"
#include "threading-capability.hpp"

using namespace std;
using namespace ndnlog;
using namespace ndnrtc;
using namespace webrtc;

// this class is needed in order to synchronously dispatch all
// frame scaling (see - FrameScaler) calls on the same thread.
// this is WebRTC requirement - all frame scaling calls must be
// executed on the same thread throughout lifecycle
class ScalerThread : public ThreadingCapability
{
public:
    static ScalerThread *getSharedInstance()
    {
        static ScalerThread scalerThread;
        return &scalerThread;
    }

    void perform(boost::function<void(void)> block)
    {
        performOnMyThread(block);
    }

private:
    ScalerThread() { startMyThread(); }
    ~ScalerThread() { stopMyThread(); }
};

//********************************************************************************
char* plotCodec(webrtc::VideoCodec codec)
{
    static char msg[256];

    sprintf(msg, "\t\tMax Framerate:\t%d\n \
            \tStart Bitrate:\t%d\n \
            \tMin Bitrate:\t%d\n \
            \tMax Bitrate:\t%d\n \
            \tTarget Bitrate:\t%d\n \
            \tWidth:\t%d\n \
            \tHeight:\t%d",
            codec.maxFramerate,
            codec.startBitrate,
            codec.minBitrate,
            codec.maxBitrate,
            codec.targetBitrate,
            codec.width,
            codec.height);

    return msg;
}

//******************************************************************************
#pragma mark - static
webrtc::VideoCodec VideoCoder::codecFromSettings(const VideoCoderParams &settings)
{
    webrtc::VideoCodec codec;

    // setup default params first
#ifdef USE_VP9
    webrtc::VCMCodecDataBase::Codec(webrtc::kVideoCodecVP9, &codec);
#else
    webrtc::VCMCodecDataBase::Codec(webrtc::kVideoCodecVP8, &codec);
#endif

    // dropping frames
#ifdef USE_VP9
    codec.VP9()->resilience = 1;
    codec.VP9()->frameDroppingOn = settings.dropFramesOn_;
    codec.VP9()->keyFrameInterval = settings.gop_;
#else
    codec.VP8()->resilience = kResilientStream;
    codec.VP8()->frameDroppingOn = settings.dropFramesOn_;
    codec.VP8()->keyFrameInterval = settings.gop_;
#endif

    // customize parameteres if possible
    codec.maxFramerate = (int)settings.codecFrameRate_;
    codec.startBitrate = settings.startBitrate_;
    codec.minBitrate = 100;
    codec.maxBitrate = settings.maxBitrate_;
    codec.targetBitrate = settings.startBitrate_;
    codec.width = settings.encodeWidth_;
    codec.height = settings.encodeHeight_;

    return codec;
}

//******************************************************************************
FrameScaler::FrameScaler(unsigned int dstWidth, unsigned int dstHeight):
srcWidth_(0), srcHeight_(0),
dstWidth_(dstWidth), dstHeight_(dstHeight)
{
    initScaledFrame();
}

const WebRtcVideoFrame&
FrameScaler::operator()(const WebRtcVideoFrame& frame)
{
    // if (srcWidth_ != frame.width() || srcHeight_ != frame.height())
    // {
    //     srcWidth_ = frame.width(); srcHeight_ = frame.height();
    //     scaler_.Set(srcWidth_, srcHeight_, dstWidth_, dstHeight_,
    //         webrtc::kI420, webrtc::kI420, webrtc::kScaleBilinear);
    // }

    // try do scaling on this thread. if throws, do it old-fashioned way
    // ScalerThread::getSharedInstance()->perform([&res, &frame, this](){
    //     scaledFrameBuffer_->ScaleFrom(frame);
    // });

    scaledFrameBuffer_->ScaleFrom(*(frame.video_frame_buffer()));

    return WebRtcVideoFrame(scaledFrameBuffer_, frame.rotation(), frame.timestamp_us());
}

void FrameScaler::initScaledFrame()
{
    int stride_y = dstWidth_;
    int stride_uv = (dstWidth_ + 1) / 2;
    int target_width = dstWidth_;
    int target_height = dstHeight_;

    scaledFrameBuffer_ = I420Buffer::Create(target_width,
                                            abs(target_height),
                                            stride_y,
                                            stride_uv, stride_uv);

    if (!scaledFrameBuffer_) throw std::runtime_error("failed to allocate scaled frame");
}

//********************************************************************************
#pragma mark - construction/destruction
VideoCoder::VideoCoder(const VideoCoderParams& coderParams, IEncoderDelegate* delegate,
    KeyEnforcement keyEnforcement) :
NdnRtcComponent(),
coderParams_(coderParams),
delegate_(delegate),
gopCounter_(0),
codec_(VideoCoder::codecFromSettings(coderParams_)),
codecSpecificInfo_(nullptr),
keyEnforcement_(keyEnforcement),
#ifdef USE_VP9
    encoder_(VP9Encoder::Create())
#else
    encoder_(VP8Encoder::Create())
#endif
{
    assert(delegate_);
    description_ = "coder";
    keyFrameType_.push_back(webrtc::kVideoFrameKey);

    if (!encoder_.get())
        throw std::runtime_error("Error creating encoder");

    encoder_->RegisterEncodeCompleteCallback(this);
    int maxPayload = 1440;

    if (encoder_->InitEncode(&codec_, boost::thread::hardware_concurrency(), maxPayload) != WEBRTC_VIDEO_CODEC_OK)
        throw std::runtime_error("Can't initialize encoder");

    LogInfoC
    << "initialized. max payload " << maxPayload
    << " parameters: " << plotCodec(codec_) << endl;
}

//********************************************************************************
#pragma mark - public
void VideoCoder::onRawFrame(const WebRtcVideoFrame &frame)
{
    LogTraceC << "⤹ encoding ○" << endl;

    if (frame.width() != coderParams_.encodeWidth_ ||
            frame.height() != coderParams_.encodeHeight_)
    {
        std::stringstream ss;
        ss << "Frame size (" << frame.width() << "x" << frame.height()
            << ") does not equal encoding resolution ("
            << coderParams_.encodeWidth_ << "x" << coderParams_.encodeHeight_
            << ")" << std::endl;
        throw std::runtime_error(ss.str());
    }

    encodeComplete_ = false;
    delegate_->onEncodingStarted();

    int err;
    if (gopCounter_%coderParams_.gop_ == 0)
    {
        err = encoder_->Encode(frame, codecSpecificInfo_, &keyFrameType_);
        if (keyEnforcement_ == KeyEnforcement::EncoderDefined) gopCounter_ = 1;
    }
    else
        err = encoder_->Encode(frame, codecSpecificInfo_, NULL);

    if (keyEnforcement_ == KeyEnforcement::Timed) gopCounter_++;

    if (!encodeComplete_)
    {
        LogTraceC << " dropped ✕" << endl;
        delegate_->onDroppedFrame();
    }

    if (err != WEBRTC_VIDEO_CODEC_OK)
        LogErrorC <<"can't encode frame due to error " << err << std::endl;
}

//********************************************************************************
#pragma mark - interfaces realization - EncodedImageCallback
webrtc::EncodedImageCallback::Result
VideoCoder::OnEncodedImage(const EncodedImage& encodedImage,
    const CodecSpecificInfo* codecSpecificInfo,
    const RTPFragmentationHeader* fragmentation)
{
    encodeComplete_ = true;
    if (keyEnforcement_ == KeyEnforcement::Gop) gopCounter_++;

    LogTraceC << "⤷ encoded ●" << std::endl;

    /*
    LogInfoC << "encoded"
    << " type " << ((encodedImage._frameType == webrtc::kKeyFrame) ? "KEY":((encodedImage._frameType == webrtc::kSkipFrame)?"SKIP":"DELTA"))
    << " complete " << encodedImage._completeFrame << "\n"
    << " has Received SLI " << codecSpecificInfo->codecSpecific.VP8.hasReceivedSLI
    << " picture Id SLI " << (int)codecSpecificInfo->codecSpecific.VP8.pictureIdSLI
    << " has Received RPSI " << codecSpecificInfo->codecSpecific.VP8.hasReceivedRPSI
    << " picture Id RPSI " << codecSpecificInfo->codecSpecific.VP8.pictureIdRPSI
    << " picture Id " << codecSpecificInfo->codecSpecific.VP8.pictureId
    << " non Reference " << codecSpecificInfo->codecSpecific.VP8.nonReference
    << " temporalIdx " << (int)codecSpecificInfo->codecSpecific.VP8.temporalIdx
    << " layerSync " << codecSpecificInfo->codecSpecific.VP8.layerSync
    << " tl0PicIdx " << codecSpecificInfo->codecSpecific.VP8.tl0PicIdx
    << " keyIdx " << (int)codecSpecificInfo->codecSpecific.VP8.keyIdx
    << std::endl;
    */
    delegate_->onEncodedFrame(encodedImage);
    return Result(Result::OK);
}
<commit_msg>removed commented code<commit_after>//
//  video-coder.cpp
//  ndnrtc
//
//  Copyright 2013 Regents of the University of California
//  For licensing details see the LICENSE file.
//
//  Author:  Peter Gusev
//  Created: 8/21/13
//

// #define USE_VP9

#include <boost/thread.hpp>
#include <webrtc/modules/video_coding/codecs/vp8/include/vp8.h>
#include <webrtc/modules/video_coding/codecs/vp9/include/vp9.h>
#include <webrtc/modules/video_coding/include/video_coding.h>
#include <webrtc/modules/video_coding/include/video_codec_interface.h>
#include <webrtc/modules/video_coding/codec_database.h>

#include "video-coder.hpp"
#include "threading-capability.hpp"

using namespace std;
using namespace ndnlog;
using namespace ndnrtc;
using namespace webrtc;

// this class is needed to synchronously dispatch all
// frame scaling (see - FrameScaler) calls on the same thread.
// this is WebRTC requirement - all frame scaling calls must be
// executed on the same thread throughout lifecycle
class ScalerThread : public ThreadingCapability
{
public:
    static ScalerThread *getSharedInstance()
    {
        static ScalerThread scalerThread;
        return &scalerThread;
    }

    void perform(boost::function<void(void)> block)
    {
        performOnMyThread(block);
    }

private:
    ScalerThread() { startMyThread(); }
    ~ScalerThread() { stopMyThread(); }
};

//********************************************************************************
char* plotCodec(webrtc::VideoCodec codec)
{
    static char msg[256];

    sprintf(msg, "\t\tMax Framerate:\t%d\n \
            \tStart Bitrate:\t%d\n \
            \tMin Bitrate:\t%d\n \
            \tMax Bitrate:\t%d\n \
            \tTarget Bitrate:\t%d\n \
            \tWidth:\t%d\n \
            \tHeight:\t%d",
            codec.maxFramerate,
            codec.startBitrate,
            codec.minBitrate,
            codec.maxBitrate,
            codec.targetBitrate,
            codec.width,
            codec.height);

    return msg;
}

//******************************************************************************
#pragma mark - static
webrtc::VideoCodec VideoCoder::codecFromSettings(const VideoCoderParams &settings)
{
    webrtc::VideoCodec codec;

    // setup default params first
#ifdef USE_VP9
    webrtc::VCMCodecDataBase::Codec(webrtc::kVideoCodecVP9, &codec);
#else
    webrtc::VCMCodecDataBase::Codec(webrtc::kVideoCodecVP8, &codec);
#endif

    // dropping frames
#ifdef USE_VP9
    codec.VP9()->resilience = 1;
    codec.VP9()->frameDroppingOn = settings.dropFramesOn_;
    codec.VP9()->keyFrameInterval = settings.gop_;
#else
    codec.VP8()->resilience = kResilientStream;
    codec.VP8()->frameDroppingOn = settings.dropFramesOn_;
    codec.VP8()->keyFrameInterval = settings.gop_;
#endif

    // customize parameteres if possible
    codec.maxFramerate = (int)settings.codecFrameRate_;
    codec.startBitrate = settings.startBitrate_;
    codec.minBitrate = 100;
    codec.maxBitrate = settings.maxBitrate_;
    codec.targetBitrate = settings.startBitrate_;
    codec.width = settings.encodeWidth_;
    codec.height = settings.encodeHeight_;

    return codec;
}

//******************************************************************************
FrameScaler::FrameScaler(unsigned int dstWidth, unsigned int dstHeight):
srcWidth_(0), srcHeight_(0),
dstWidth_(dstWidth), dstHeight_(dstHeight)
{
    initScaledFrame();
}

const WebRtcVideoFrame&
FrameScaler::operator()(const WebRtcVideoFrame& frame)
{
    // try do scaling on this thread. if throws, do it old-fashioned way
    // ScalerThread::getSharedInstance()->perform([&res, &frame, this](){
    //     scaledFrameBuffer_->ScaleFrom(frame);
    // });

    scaledFrameBuffer_->ScaleFrom(*(frame.video_frame_buffer()));

    return WebRtcVideoFrame(scaledFrameBuffer_, frame.rotation(), frame.timestamp_us());
}

void FrameScaler::initScaledFrame()
{
    int stride_y = dstWidth_;
    int stride_uv = (dstWidth_ + 1) / 2;
    int target_width = dstWidth_;
    int target_height = dstHeight_;

    scaledFrameBuffer_ = I420Buffer::Create(target_width,
                                            abs(target_height),
                                            stride_y,
                                            stride_uv, stride_uv);

    if (!scaledFrameBuffer_) throw std::runtime_error("failed to allocate scaled frame");
}

//********************************************************************************
#pragma mark - construction/destruction
VideoCoder::VideoCoder(const VideoCoderParams& coderParams, IEncoderDelegate* delegate,
    KeyEnforcement keyEnforcement) :
NdnRtcComponent(),
coderParams_(coderParams),
delegate_(delegate),
gopCounter_(0),
codec_(VideoCoder::codecFromSettings(coderParams_)),
codecSpecificInfo_(nullptr),
keyEnforcement_(keyEnforcement),
#ifdef USE_VP9
    encoder_(VP9Encoder::Create())
#else
    encoder_(VP8Encoder::Create())
#endif
{
    assert(delegate_);
    description_ = "coder";
    keyFrameType_.push_back(webrtc::kVideoFrameKey);

    if (!encoder_.get())
        throw std::runtime_error("Error creating encoder");

    encoder_->RegisterEncodeCompleteCallback(this);
    int maxPayload = 1440;

    if (encoder_->InitEncode(&codec_, boost::thread::hardware_concurrency(), maxPayload) != WEBRTC_VIDEO_CODEC_OK)
        throw std::runtime_error("Can't initialize encoder");

    LogInfoC
    << "initialized. max payload " << maxPayload
    << " parameters: " << plotCodec(codec_) << endl;
}

//********************************************************************************
#pragma mark - public
void VideoCoder::onRawFrame(const WebRtcVideoFrame &frame)
{
    LogTraceC << "⤹ encoding ○" << endl;

    if (frame.width() != coderParams_.encodeWidth_ ||
            frame.height() != coderParams_.encodeHeight_)
    {
        std::stringstream ss;
        ss << "Frame size (" << frame.width() << "x" << frame.height()
            << ") does not equal encoding resolution ("
            << coderParams_.encodeWidth_ << "x" << coderParams_.encodeHeight_
            << ")" << std::endl;
        throw std::runtime_error(ss.str());
    }

    encodeComplete_ = false;
    delegate_->onEncodingStarted();

    int err;
    if (gopCounter_%coderParams_.gop_ == 0)
    {
        err = encoder_->Encode(frame, codecSpecificInfo_, &keyFrameType_);
        if (keyEnforcement_ == KeyEnforcement::EncoderDefined) gopCounter_ = 1;
    }
    else
        err = encoder_->Encode(frame, codecSpecificInfo_, NULL);

    if (keyEnforcement_ == KeyEnforcement::Timed) gopCounter_++;

    if (!encodeComplete_)
    {
        LogTraceC << " dropped ✕" << endl;
        delegate_->onDroppedFrame();
    }

    if (err != WEBRTC_VIDEO_CODEC_OK)
        LogErrorC <<"can't encode frame due to error " << err << std::endl;
}

//********************************************************************************
#pragma mark - interfaces realization - EncodedImageCallback
webrtc::EncodedImageCallback::Result
VideoCoder::OnEncodedImage(const EncodedImage& encodedImage,
    const CodecSpecificInfo* codecSpecificInfo,
    const RTPFragmentationHeader* fragmentation)
{
    encodeComplete_ = true;
    if (keyEnforcement_ == KeyEnforcement::Gop) gopCounter_++;

    LogTraceC << "⤷ encoded ●" << std::endl;

    /*
    LogInfoC << "encoded"
    << " type " << ((encodedImage._frameType == webrtc::kKeyFrame) ? "KEY":((encodedImage._frameType == webrtc::kSkipFrame)?"SKIP":"DELTA"))
    << " complete " << encodedImage._completeFrame << "\n"
    << " has Received SLI " << codecSpecificInfo->codecSpecific.VP8.hasReceivedSLI
    << " picture Id SLI " << (int)codecSpecificInfo->codecSpecific.VP8.pictureIdSLI
    << " has Received RPSI " << codecSpecificInfo->codecSpecific.VP8.hasReceivedRPSI
    << " picture Id RPSI " << codecSpecificInfo->codecSpecific.VP8.pictureIdRPSI
    << " picture Id " << codecSpecificInfo->codecSpecific.VP8.pictureId
    << " non Reference " << codecSpecificInfo->codecSpecific.VP8.nonReference
    << " temporalIdx " << (int)codecSpecificInfo->codecSpecific.VP8.temporalIdx
    << " layerSync " << codecSpecificInfo->codecSpecific.VP8.layerSync
    << " tl0PicIdx " << codecSpecificInfo->codecSpecific.VP8.tl0PicIdx
    << " keyIdx " << (int)codecSpecificInfo->codecSpecific.VP8.keyIdx
    << std::endl;
    */
    delegate_->onEncodedFrame(encodedImage);
    return Result(Result::OK);
}
<|endoftext|>


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

#ifndef _SWBASESH_HXX
#include "basesh.hxx"
#endif

#ifndef INCLUDED_SWDLLAPI_H
#include "swdllapi.h"
#endif

class SfxItemSet;
class SwWrtShell;

SW_DLLPUBLIC void ItemSetToTableParam( const SfxItemSet& rSet, SwWrtShell &rSh );

extern const USHORT __FAR_DATA aUITableAttrRange[];
SW_DLLPUBLIC const USHORT* SwuiGetUITableAttrRange();

class SwTableShell: public SwBaseShell
{
public:
    SFX_DECL_INTERFACE(SW_TABSHELL);
    TYPEINFO();

    void    Execute(SfxRequest &);
    void    GetState(SfxItemSet &);
    void    GetFrmBorderState(SfxItemSet &rSet);
    void    GetLineStyleState(SfxItemSet &rSet);
    void    ExecTableStyle(SfxRequest& rReq);

    void    ExecNumberFormat(SfxRequest& rReq);

            SwTableShell(SwView &rView);
};

#endif
<commit_msg>INTEGRATION: CWS ooo19126 (1.2.596); FILE MERGED 2005/09/05 13:45:44 rt 1.2.596.1: #i54170# Change license header: remove SISSL<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: tabsh.hxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-09 10:08:00 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
#ifndef _SWTABSH_HXX
#define _SWTABSH_HXX

#ifndef _SWBASESH_HXX
#include "basesh.hxx"
#endif

#ifndef INCLUDED_SWDLLAPI_H
#include "swdllapi.h"
#endif

class SfxItemSet;
class SwWrtShell;

SW_DLLPUBLIC void ItemSetToTableParam( const SfxItemSet& rSet, SwWrtShell &rSh );

extern const USHORT __FAR_DATA aUITableAttrRange[];
SW_DLLPUBLIC const USHORT* SwuiGetUITableAttrRange();

class SwTableShell: public SwBaseShell
{
public:
    SFX_DECL_INTERFACE(SW_TABSHELL);
    TYPEINFO();

    void    Execute(SfxRequest &);
    void    GetState(SfxItemSet &);
    void    GetFrmBorderState(SfxItemSet &rSet);
    void    GetLineStyleState(SfxItemSet &rSet);
    void    ExecTableStyle(SfxRequest& rReq);

    void    ExecNumberFormat(SfxRequest& rReq);

            SwTableShell(SwView &rView);
};

#endif
<|endoftext|>


<commit_before>#pragma once
#include <algorithm>
#include <initializer_list>
#include <stdexcept>
#include <utility>
#include <vector>

#include "base/helpers.hpp"
#include "is_equal_to_zero.hpp"
#include "maths.hpp"
#include "random.hpp"

struct maximal_element_search_tag {};
struct prime_modulo_calculations_tag {};

template<typename T>
class Matrix {
public:
	using value_type = T;
	using size_type = std::size_t;

	using vector_container_type = std::vector<value_type>;
	using matrix_container_type = std::vector<vector_container_type>;

	Matrix(const size_type rows_cnt, const size_type cols_cnt, const value_type mod = 1000000007) :
			rows_cnt_(rows_cnt),
			cols_cnt_(cols_cnt),
			data_(rows_cnt, vector_container_type(cols_cnt, 0)),
			mod_(mod)
	{}

	explicit Matrix(const std::initializer_list<std::initializer_list<value_type>>& initializer_list, const value_type mod = 1000000007) :
			rows_cnt_(initializer_list.size()),
			cols_cnt_(0),
			mod_(mod)
	{
		data_.reserve(initializer_list.size());
		for (const auto& row : initializer_list) {
			if (data_.empty()) {
				cols_cnt_ = row.size();
			} else {
				if (cols_cnt_ != row.size()) {
					throw std::out_of_range("Matrix<T> initializer list must have rows of the same size");
				}
			}
			data_.emplace_back(row);
		}
	}

	template<typename U>
	Matrix(const Matrix<U>& matrix) :
			rows_cnt_(matrix.rows_cnt()),
			cols_cnt_(matrix.cols_cnt()),
			data_(matrix.rows_cnt(), vector_container_type(matrix.cols_cnt()))
	{
		for (size_type i = 0; i < rows_cnt_; ++i) {
			for (size_type j = 0; j < cols_cnt_; ++j) {
				data_[i][j] = static_cast<value_type>(matrix[i][j]);
			}
		}
	}

	size_type get_matrix_rank() const;
	size_type get_matrix_rank(maximal_element_search_tag) const;
	size_type get_matrix_rank(prime_modulo_calculations_tag, const value_type& mod) const;

	void swap_rows(const size_type i, const size_type j) {
		data_[i].swap(data_[j]);
	}

	void swap_cols(const size_type i, const size_type j) {
		for (auto& row : data_) {
			std::swap(row[i], row[j]);
		}
	}

	void shuffle() {
		shuffle_rows();
		shuffle_cols();
	}

	void shuffle_cols() {
		matrix_container_type tmp(rows_cnt_, cols_cnt_);
		std::vector<size_type> indices(cols_cnt_);
		for (size_type i = 0; i < cols_cnt_; ++i) {
			indices[i] = i;
		}
		std::shuffle(indices.begin(), indices.end(), Random::random_engine());
		for (size_type j = 0; j < cols_cnt_; ++j) {
			size_type index = indices[j];
			for (size_type row = 0; row < rows_cnt_; ++row) {
				tmp[row][j] = data_[row][index];
			}
		}
		data_.swap(tmp);
	}

	void shuffle_rows() {
		matrix_container_type tmp;
		tmp.reserve(rows_cnt_);
		std::vector<size_type> indices(rows_cnt_);
		for (size_type i = 0; i < rows_cnt_; ++i) {
			indices[i] = i;
		}
		std::shuffle(indices.begin(), indices.end(), Random::random_engine());
		for (const auto& index : indices) {
			tmp.emplace_back(data_[index]);
		}
		data_.swap(tmp);
	}

	typename matrix_container_type::iterator begin() {
		return data_.begin();
	}

	typename matrix_container_type::const_iterator begin() const {
		return data_.begin();
	}

	typename matrix_container_type::iterator end() {
		return data_.end();
	}

	typename matrix_container_type::const_iterator end() const {
		return data_.end();
	}

	vector_container_type get_column(const size_type index) const {
		vector_container_type column;
		column.reserve(rows_cnt_);
		for (const auto& row : data_) {
			column.emplace_back(row[index]);
		}
		return column;
	}

	vector_container_type& operator [](const size_type index) {
		return data_[index];
	}

	const vector_container_type& operator [](const size_type index) const {
		return data_[index];
	}

	constexpr size_type rows_cnt() const {
		return rows_cnt_;
	}

	constexpr size_type cols_cnt() const {
		return cols_cnt_;
	}

	Matrix operator *(const Matrix& rhs) const {
		Matrix result(rows_cnt_, rhs.cols_cnt_);
		for (size_type i = 0; i < rows_cnt_; i++) {
			for (size_type k = 0; k < rhs.cols_cnt_; k++) {
				for (size_type j = 0; j < rhs.rows_cnt_; j++) {
					result[i][k] += data_[i][j] * rhs[j][k];
				}
			}
		}
		return result;
	}

	Matrix& operator *=(const Matrix& rhs) {
		Matrix result = operator *(rhs);
		swap(result);
		return *this;
	}

	template<typename U>
	Matrix binpow(U b) const {
		static_assert(std::is_integral<U>::value, "Degree must be integral. For real degree use pow.");
		Matrix ret = identity_matrix(rows_cnt_, cols_cnt_);
		Matrix a = *this;
		while (b != 0) {
			if ((b & 1) != 0) {
				ret *= a;
			}
			a *= a;
			b >>= 1;
		}
		return ret;
	}

	void swap(Matrix& rhs) {
		data_.swap(rhs.data_);
		std::swap(mod_, rhs.mod_);
		std::swap(rows_cnt_, rhs.rows_cnt_);
		std::swap(cols_cnt_, rhs.cols_cnt_);
	}

	static Matrix identity_matrix(const size_type rows_cnt, const size_type cols_cnt) {
		Matrix result(rows_cnt, cols_cnt);
		for (size_type i = 0; i < std::min(rows_cnt, cols_cnt); ++i) {
			result[i][i] = 1;
		}
		return result;
	}

private:
	size_type rows_cnt_;
	size_type cols_cnt_;
	value_type mod_;

	matrix_container_type data_;
};

template <typename T>
typename Matrix<T>::size_type Matrix<T>::get_matrix_rank() const {
	Matrix<long double> tmp(*this);
	std::vector<bool> used(rows_cnt_, false);
	size_type rank = cols_cnt_;
	for (size_type col = 0; col < cols_cnt_; ++col) {
		size_type best_row = std::numeric_limits<size_type>::max();
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (!used[row] && !is_equal_to_zero(tmp[row][col])) {
				best_row = row;
				break;
			}
		}
		if (best_row == std::numeric_limits<size_type>::max()) {
			--rank;
			continue;
		}
		used[best_row] = true;
		for (size_type j = col + 1; j < cols_cnt_; ++j) {
			tmp[best_row][j] /= tmp[best_row][col];
		}
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (used[row] || is_equal_to_zero(tmp[row][col])) {
				continue;
			}
			for (size_type j = col + 1; j < cols_cnt_; ++j) {
				tmp[row][j] -= tmp[best_row][j] * tmp[row][col];
			}
		}
	}
	return rank;
}

template <typename T>
typename Matrix<T>::size_type Matrix<T>::get_matrix_rank(maximal_element_search_tag) const {
	Matrix<long double> tmp(*this);
	std::vector<bool> used(rows_cnt_, false);
	size_type rank = 0;
	for (size_type col = 0; col < cols_cnt_; ++col) {
		value_type max_value = EPS;
		size_type best_row = std::numeric_limits<size_type>::max();
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (!used[row] && umax(max_value, abs(tmp[row][col]))) {
				best_row = row;
			}
		}
		if (best_row == std::numeric_limits<size_type>::max()) {
			continue;
		}
		++rank;
		used[best_row] = true;
		for (size_type j = col + 1; j < cols_cnt_; ++j) {
			tmp[best_row][j] /= tmp[best_row][col];
		}
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (used[row] || is_equal_to_zero(tmp[row][col])) {
				continue;
			}
			for (size_type j = col + 1; j < cols_cnt_; ++j) {
				tmp[row][j] -= tmp[best_row][j] * tmp[row][col];
			}
		}
	}
	return rank;
}

template <typename T>
typename Matrix<T>::size_type Matrix<T>::get_matrix_rank(prime_modulo_calculations_tag, const value_type& mod) const {
	static_assert(std::is_integral<value_type>::value, "Integral type required to process calculations by prime modulo");
	Matrix<long long> tmp(*this);
	for (auto& row : tmp) {
		for (auto& col : row) {
			if (col < 0) {
				col += mod;
			}
		}
	}
	std::vector<bool> used(rows_cnt_, false);
	size_type rank = cols_cnt_;
	for (size_type col = 0; col < cols_cnt_; ++col) {
		size_type best_row = std::numeric_limits<size_type>::max();
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (!used[row] && !is_equal_to_zero(tmp[row][col])) {
				best_row = row;
				break;
			}
		}
		if (best_row == std::numeric_limits<size_type>::max()) {
			--rank;
			continue;
		}
		used[best_row] = true;
		for (size_type j = col + 1; j < cols_cnt_; ++j) {
			mul_mod(tmp[best_row][j], inverseElement(tmp[best_row][col], mod), mod);
		}
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (used[row] || is_equal_to_zero(tmp[row][col])) {
				continue;
			}
			for (size_type j = col + 1; j < cols_cnt_; ++j) {
				long long x = tmp[best_row][j];
				mul_mod(x, tmp[row][col], mod);
				sub_mod(tmp[row][j], x, mod);
			}
		}
	}
	return rank;
}
<commit_msg>Fix -Wreorder warning in Matrix<>.<commit_after>#pragma once
#include <algorithm>
#include <initializer_list>
#include <stdexcept>
#include <utility>
#include <vector>

#include "base/helpers.hpp"
#include "is_equal_to_zero.hpp"
#include "maths.hpp"
#include "random.hpp"

struct maximal_element_search_tag {};
struct prime_modulo_calculations_tag {};

template<typename T>
class Matrix {
public:
	using value_type = T;
	using size_type = std::size_t;

	using vector_container_type = std::vector<value_type>;
	using matrix_container_type = std::vector<vector_container_type>;

	Matrix(const size_type rows_cnt, const size_type cols_cnt, const value_type mod = 1000000007) :
			rows_cnt_(rows_cnt),
			cols_cnt_(cols_cnt),
			data_(rows_cnt, vector_container_type(cols_cnt, 0)),
			mod_(mod)
	{}

	explicit Matrix(const std::initializer_list<std::initializer_list<value_type>>& initializer_list, const value_type mod = 1000000007) :
			rows_cnt_(initializer_list.size()),
			cols_cnt_(0),
			mod_(mod)
	{
		data_.reserve(initializer_list.size());
		for (const auto& row : initializer_list) {
			if (data_.empty()) {
				cols_cnt_ = row.size();
			} else {
				if (cols_cnt_ != row.size()) {
					throw std::out_of_range("Matrix<T> initializer list must have rows of the same size");
				}
			}
			data_.emplace_back(row);
		}
	}

	template<typename U>
	Matrix(const Matrix<U>& matrix) :
			rows_cnt_(matrix.rows_cnt()),
			cols_cnt_(matrix.cols_cnt()),
			data_(matrix.rows_cnt(), vector_container_type(matrix.cols_cnt()))
	{
		for (size_type i = 0; i < rows_cnt_; ++i) {
			for (size_type j = 0; j < cols_cnt_; ++j) {
				data_[i][j] = static_cast<value_type>(matrix[i][j]);
			}
		}
	}

	size_type get_matrix_rank() const;
	size_type get_matrix_rank(maximal_element_search_tag) const;
	size_type get_matrix_rank(prime_modulo_calculations_tag, const value_type& mod) const;

	void swap_rows(const size_type i, const size_type j) {
		data_[i].swap(data_[j]);
	}

	void swap_cols(const size_type i, const size_type j) {
		for (auto& row : data_) {
			std::swap(row[i], row[j]);
		}
	}

	void shuffle() {
		shuffle_rows();
		shuffle_cols();
	}

	void shuffle_cols() {
		matrix_container_type tmp(rows_cnt_, cols_cnt_);
		std::vector<size_type> indices(cols_cnt_);
		for (size_type i = 0; i < cols_cnt_; ++i) {
			indices[i] = i;
		}
		std::shuffle(indices.begin(), indices.end(), Random::random_engine());
		for (size_type j = 0; j < cols_cnt_; ++j) {
			size_type index = indices[j];
			for (size_type row = 0; row < rows_cnt_; ++row) {
				tmp[row][j] = data_[row][index];
			}
		}
		data_.swap(tmp);
	}

	void shuffle_rows() {
		matrix_container_type tmp;
		tmp.reserve(rows_cnt_);
		std::vector<size_type> indices(rows_cnt_);
		for (size_type i = 0; i < rows_cnt_; ++i) {
			indices[i] = i;
		}
		std::shuffle(indices.begin(), indices.end(), Random::random_engine());
		for (const auto& index : indices) {
			tmp.emplace_back(data_[index]);
		}
		data_.swap(tmp);
	}

	typename matrix_container_type::iterator begin() {
		return data_.begin();
	}

	typename matrix_container_type::const_iterator begin() const {
		return data_.begin();
	}

	typename matrix_container_type::iterator end() {
		return data_.end();
	}

	typename matrix_container_type::const_iterator end() const {
		return data_.end();
	}

	vector_container_type get_column(const size_type index) const {
		vector_container_type column;
		column.reserve(rows_cnt_);
		for (const auto& row : data_) {
			column.emplace_back(row[index]);
		}
		return column;
	}

	vector_container_type& operator [](const size_type index) {
		return data_[index];
	}

	const vector_container_type& operator [](const size_type index) const {
		return data_[index];
	}

	constexpr size_type rows_cnt() const {
		return rows_cnt_;
	}

	constexpr size_type cols_cnt() const {
		return cols_cnt_;
	}

	Matrix operator *(const Matrix& rhs) const {
		Matrix result(rows_cnt_, rhs.cols_cnt_);
		for (size_type i = 0; i < rows_cnt_; i++) {
			for (size_type k = 0; k < rhs.cols_cnt_; k++) {
				for (size_type j = 0; j < rhs.rows_cnt_; j++) {
					result[i][k] += data_[i][j] * rhs[j][k];
				}
			}
		}
		return result;
	}

	Matrix& operator *=(const Matrix& rhs) {
		Matrix result = operator *(rhs);
		swap(result);
		return *this;
	}

	template<typename U>
	Matrix binpow(U b) const {
		static_assert(std::is_integral<U>::value, "Degree must be integral. For real degree use pow.");
		Matrix ret = identity_matrix(rows_cnt_, cols_cnt_);
		Matrix a = *this;
		while (b != 0) {
			if ((b & 1) != 0) {
				ret *= a;
			}
			a *= a;
			b >>= 1;
		}
		return ret;
	}

	void swap(Matrix& rhs) {
		data_.swap(rhs.data_);
		std::swap(mod_, rhs.mod_);
		std::swap(rows_cnt_, rhs.rows_cnt_);
		std::swap(cols_cnt_, rhs.cols_cnt_);
	}

	static Matrix identity_matrix(const size_type rows_cnt, const size_type cols_cnt) {
		Matrix result(rows_cnt, cols_cnt);
		for (size_type i = 0; i < std::min(rows_cnt, cols_cnt); ++i) {
			result[i][i] = 1;
		}
		return result;
	}

private:
	size_type rows_cnt_;
	size_type cols_cnt_;
	matrix_container_type data_;

	value_type mod_;
};

template <typename T>
typename Matrix<T>::size_type Matrix<T>::get_matrix_rank() const {
	Matrix<long double> tmp(*this);
	std::vector<bool> used(rows_cnt_, false);
	size_type rank = cols_cnt_;
	for (size_type col = 0; col < cols_cnt_; ++col) {
		size_type best_row = std::numeric_limits<size_type>::max();
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (!used[row] && !is_equal_to_zero(tmp[row][col])) {
				best_row = row;
				break;
			}
		}
		if (best_row == std::numeric_limits<size_type>::max()) {
			--rank;
			continue;
		}
		used[best_row] = true;
		for (size_type j = col + 1; j < cols_cnt_; ++j) {
			tmp[best_row][j] /= tmp[best_row][col];
		}
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (used[row] || is_equal_to_zero(tmp[row][col])) {
				continue;
			}
			for (size_type j = col + 1; j < cols_cnt_; ++j) {
				tmp[row][j] -= tmp[best_row][j] * tmp[row][col];
			}
		}
	}
	return rank;
}

template <typename T>
typename Matrix<T>::size_type Matrix<T>::get_matrix_rank(maximal_element_search_tag) const {
	Matrix<long double> tmp(*this);
	std::vector<bool> used(rows_cnt_, false);
	size_type rank = 0;
	for (size_type col = 0; col < cols_cnt_; ++col) {
		value_type max_value = EPS;
		size_type best_row = std::numeric_limits<size_type>::max();
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (!used[row] && umax(max_value, abs(tmp[row][col]))) {
				best_row = row;
			}
		}
		if (best_row == std::numeric_limits<size_type>::max()) {
			continue;
		}
		++rank;
		used[best_row] = true;
		for (size_type j = col + 1; j < cols_cnt_; ++j) {
			tmp[best_row][j] /= tmp[best_row][col];
		}
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (used[row] || is_equal_to_zero(tmp[row][col])) {
				continue;
			}
			for (size_type j = col + 1; j < cols_cnt_; ++j) {
				tmp[row][j] -= tmp[best_row][j] * tmp[row][col];
			}
		}
	}
	return rank;
}

template <typename T>
typename Matrix<T>::size_type Matrix<T>::get_matrix_rank(prime_modulo_calculations_tag, const value_type& mod) const {
	static_assert(std::is_integral<value_type>::value, "Integral type required to process calculations by prime modulo");
	Matrix<long long> tmp(*this);
	for (auto& row : tmp) {
		for (auto& col : row) {
			if (col < 0) {
				col += mod;
			}
		}
	}
	std::vector<bool> used(rows_cnt_, false);
	size_type rank = cols_cnt_;
	for (size_type col = 0; col < cols_cnt_; ++col) {
		size_type best_row = std::numeric_limits<size_type>::max();
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (!used[row] && !is_equal_to_zero(tmp[row][col])) {
				best_row = row;
				break;
			}
		}
		if (best_row == std::numeric_limits<size_type>::max()) {
			--rank;
			continue;
		}
		used[best_row] = true;
		for (size_type j = col + 1; j < cols_cnt_; ++j) {
			mul_mod(tmp[best_row][j], inverseElement(tmp[best_row][col], mod), mod);
		}
		for (size_type row = 0; row < rows_cnt_; ++row) {
			if (used[row] || is_equal_to_zero(tmp[row][col])) {
				continue;
			}
			for (size_type j = col + 1; j < cols_cnt_; ++j) {
				long long x = tmp[best_row][j];
				mul_mod(x, tmp[row][col], mod);
				sub_mod(tmp[row][j], x, mod);
			}
		}
	}
	return rank;
}
<|endoftext|>