<commit_before>
#include "CommonHeaders.h"

#include "Builtins.h"
#include "CodeUnit.h"
#include "Function.h"
#include "Type.h"

namespace builtins {

static codeunit::CodeUnit* KERNEL = NULL;

void bootstrap()
{
   KERNEL = new codeunit::CodeUnit;

   // Create constant-generator function. This function takes a Type as
   // input, and gives a Function as output.
   Term* constFuncGenerator = codeunit::bootstrap_empty_term(KERNEL);
   function::initialize_data(constFuncGenerator);
   function::set_name(constFuncGenerator, "constant-generator");

   // Create constant-Type function. This function outputs a Type.
   Term* constType = codeunit::bootstrap_empty_term(KERNEL);
   constType->function = constFuncGenerator;
   function::initialize_data(constType);
   function::set_name(constType, "constant-Type");

   // Create Type type
   Term* typeType = codeunit::bootstrap_empty_term(KERNEL);
   typeType->function = constType;
   codeunit::bind_name(KERNEL, typeType, "Type");
   type::initialize_data_for_types(typeType);
   type::set_initialize_data_func(typeType, type::initialize_data_for_types);

   // Implant the Type type
   codeunit::set_input(KERNEL, constType, 0, typeType);
   function::set_input_type(constFuncGenerator, 0, typeType);
   function::set_output_type(constType, typeType);

   // Create constant-Function function, which outputs Functions.
   Term* constFuncFunc = codeunit::bootstrap_empty_term(KERNEL);
   constFuncFunc->function = constFuncGenerator;
   function::initialize_data(constFuncFunc);
   function::set_name(constFuncFunc, "constant-Function");

   // Create Function type
   Term* funcType = codeunit::bootstrap_empty_term(KERNEL);
   funcType->function = constFuncFunc;
   type::initialize_data_for_types(funcType);
   codeunit::bind_name(KERNEL, funcType, "Function");

   // Implant constant-Function
   codeunit::set_input(KERNEL, constFuncGenerator, 0, constFuncFunc);
   function::set_output_type(constFuncFunc, funcType);

   // Implant Function type
   codeunit::set_input(KERNEL, constFuncFunc, 0, funcType);
   function::set_output_type(constFuncGenerator, funcType);
}

}
<commit_msg>Fix stuff that Scons deleted<commit_after>
#include "CommonHeaders.h"

#include "Builtins.h"
#include "CodeUnit.h"
#include "Function.h"
#include "Type.h"

namespace builtins {

static codeunit::CodeUnit* KERNEL = NULL;
codeunit::CodeUnit* BUILTINS = NULL;

Term* CONST_GENERATOR = NULL;
Term* TYPE_TYPE = NULL;
Term* INT_TYPE = NULL;
Term* STRING_TYPE = NULL;

void bootstrap_kernel()
{
   KERNEL = new codeunit::CodeUnit;

   // Create constant-generator function. This function takes a Type as
   // input, and gives a Function as output.
   CONST_GENERATOR = codeunit::bootstrap_empty_term(KERNEL);
   function::initialize_data(CONST_GENERATOR);
   function::set_name(CONST_GENERATOR, "constant-generator");

   // Create constant-Type function. This function outputs a Type.
   Term* constType = codeunit::bootstrap_empty_term(KERNEL);
   constType->function = CONST_GENERATOR;
   function::initialize_data(constType);
   function::set_name(constType, "constant-Type");

   // Create Type type
   TYPE_TYPE = codeunit::bootstrap_empty_term(KERNEL);
   TYPE_TYPE->function = constType;
   codeunit::bind_name(KERNEL, TYPE_TYPE, "Type");
   type::initialize_data_for_types(TYPE_TYPE);
   type::set_initialize_data_func(TYPE_TYPE, type::initialize_data_for_types);

   // Implant the Type type
   codeunit::set_input(KERNEL, constType, 0, TYPE_TYPE);
   function::set_input_type(CONST_GENERATOR, 0, TYPE_TYPE);
   function::set_output_type(constType, TYPE_TYPE);

   // Create constant-Function function, which outputs Functions.
   Term* constFuncFunc = codeunit::bootstrap_empty_term(KERNEL);
   constFuncFunc->function = CONST_GENERATOR;
   function::initialize_data(constFuncFunc);
   function::set_name(constFuncFunc, "constant-Function");

   // Create Function type
   Term* funcType = codeunit::bootstrap_empty_term(KERNEL);
   funcType->function = constFuncFunc;
   type::initialize_data_for_types(funcType);
   codeunit::bind_name(KERNEL, funcType, "Function");

   // Implant constant-Function
   codeunit::set_input(KERNEL, CONST_GENERATOR, 0, constFuncFunc);
   function::set_output_type(constFuncFunc, funcType);

   // Implant Function type
   codeunit::set_input(KERNEL, constFuncFunc, 0, funcType);
   function::set_output_type(CONST_GENERATOR, funcType);
}

void bootstrap_builtins()
{
    // Create primitives

    // Create int type
    INT_TYPE = codeunit::create_term(BUILTINS, CONST_GENERATOR, TermList(TYPE_TYPE));
    type::set_initialize_data_func(INT_TYPE, int_type::initialize_data);
    type::set_to_string_func(INT_TYPE, int_type::to_string);

    // Create string type
    STRING_TYPE = codeunit::create_term(BUILTINS, CONST_GENERATOR, TermList(TYPE_TYPE));
    type::set_initialize_data_func(STRING_TYPE, string_type::initialize_data);
    type::set_to_string_func(STRINGINT_TYPE, string_type::to_string);

}

}
<|endoftext|>


<commit_before>/* Copyright (c) 2016 PaddlePaddle 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 <string.h>  // for strdup
#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>

#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/platform/cpu_helper.h"
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/fluid/string/split.h"
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/platform/cuda_device_guard.h"
#include "paddle/fluid/platform/dynload/cupti.h"
#endif
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/init.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/string/piece.h"

DECLARE_int32(paddle_num_threads);
DEFINE_int32(multiple_of_cupti_buffer_size, 1,
             "Multiple of the CUPTI device buffer size. If the timestamps have "
             "been dropped when you are profiling, try increasing this value.");

namespace paddle {
namespace platform {

void ParseCommandLineFlags(int argc, char **argv, bool remove) {
  google::ParseCommandLineFlags(&argc, &argv, remove);
}

}  // namespace platform
}  // namespace paddle

namespace paddle {
namespace framework {

#ifdef _WIN32
#define strdup _strdup
#endif

std::once_flag gflags_init_flag;
std::once_flag glog_init_flag;
std::once_flag p2p_init_flag;
std::once_flag glog_warning_once_flag;

bool InitGflags(std::vector<std::string> args) {
  bool successed = false;
  std::call_once(gflags_init_flag, [&]() {
    FLAGS_logtostderr = true;
    // NOTE(zhiqiu): dummy is needed, since the function
    // ParseNewCommandLineFlags in gflags.cc starts processing
    // commandline strings from idx 1.
    // The reason is, it assumes that the first one (idx 0) is
    // the filename of executable file.
    args.insert(args.begin(), "dummy");
    std::vector<char *> argv;
    std::string line;
    int argc = args.size();
    for (auto &arg : args) {
      argv.push_back(const_cast<char *>(arg.data()));
      line += arg;
      line += ' ';
    }
    VLOG(1) << "Before Parse: argc is " << argc
            << ", Init commandline: " << line;

    char **arr = argv.data();
    google::ParseCommandLineFlags(&argc, &arr, true);
    successed = true;

    VLOG(1) << "After Parse: argc is " << argc;
  });
  return successed;
}

void InitP2P(std::vector<int> devices) {
#ifdef PADDLE_WITH_CUDA
  std::call_once(p2p_init_flag, [&]() {
    int count = devices.size();
    for (int i = 0; i < count; ++i) {
      for (int j = 0; j < count; ++j) {
        if (devices[i] == devices[j]) continue;
        int can_acess = -1;
        PADDLE_ENFORCE_CUDA_SUCCESS(
            cudaDeviceCanAccessPeer(&can_acess, devices[i], devices[j]));
        if (can_acess != 1) {
          LOG(WARNING) << "Cannot enable P2P access from " << devices[i]
                       << " to " << devices[j];
        } else {
          platform::CUDADeviceGuard guard(devices[i]);
          cudaDeviceEnablePeerAccess(devices[j], 0);
        }
      }
    }
  });
#endif
}

void InitCupti() {
#ifdef PADDLE_WITH_CUPTI
  if (FLAGS_multiple_of_cupti_buffer_size == 1) return;
  size_t attrValue = 0, attrValueSize = sizeof(size_t);
#define MULTIPLY_ATTR_VALUE(attr)                                            \
  {                                                                          \
    PADDLE_ENFORCE_EQ(                                                       \
        !platform::dynload::cuptiActivityGetAttribute(attr, &attrValueSize,  \
                                                      &attrValue),           \
        true, platform::errors::Unavailable("Get cupti attribute failed.")); \
    attrValue *= FLAGS_multiple_of_cupti_buffer_size;                        \
    LOG(WARNING) << "Set " #attr " " << attrValue << " byte";                \
    PADDLE_ENFORCE_EQ(                                                       \
        !platform::dynload::cuptiActivitySetAttribute(attr, &attrValueSize,  \
                                                      &attrValue),           \
        true, platform::errors::Unavailable("Set cupti attribute failed.")); \
  }
  MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE);
  MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE_CDP);
#if CUDA_VERSION >= 9000
  MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_PROFILING_SEMAPHORE_POOL_SIZE);
#endif
#undef MULTIPLY_ATTR_VALUE
#endif
}

void InitDevices(bool init_p2p) {
  // CUPTI attribute should be set before any CUDA context is created (see CUPTI
  // documentation about CUpti_ActivityAttribute).
  InitCupti();
  /*Init all available devices by default */
  std::vector<int> devices;
#ifdef PADDLE_WITH_CUDA
  try {
    // use user specified GPUs in single-node multi-process mode.
    devices = platform::GetSelectedDevices();
  } catch (const std::exception &exp) {
    LOG(WARNING) << "Compiled with WITH_GPU, but no GPU found in runtime.";
  }
#endif
  InitDevices(init_p2p, devices);
}

void InitDevices(bool init_p2p, const std::vector<int> devices) {
  std::vector<platform::Place> places;

  for (size_t i = 0; i < devices.size(); ++i) {
    // In multi process multi gpu mode, we may have gpuid = 7
    // but count = 1.
    if (devices[i] < 0) {
      LOG(WARNING) << "Invalid devices id.";
      continue;
    }
    places.emplace_back(platform::CUDAPlace(devices[i]));
  }
  if (init_p2p) {
    InitP2P(devices);
  }
  places.emplace_back(platform::CPUPlace());
#ifdef PADDLE_WITH_CUDA
  places.emplace_back(platform::CUDAPinnedPlace());
#endif
  platform::DeviceContextPool::Init(places);

#ifndef PADDLE_WITH_MKLDNN
  platform::SetNumThreads(FLAGS_paddle_num_threads);
#endif

#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OSX__)
  if (platform::MayIUse(platform::avx)) {
#ifndef __AVX__
    LOG(WARNING) << "AVX is available, Please re-compile on local machine";
#endif
  }

// Throw some informations when CPU instructions mismatch.
#define AVX_GUIDE(compiletime, runtime)                                  \
  PADDLE_THROW(platform::errors::Unavailable(                            \
      "This version is compiled on higher instruction(" #compiletime     \
      ") system, you may encounter illegal instruction error running on" \
      " your local CPU machine. Please reinstall the " #runtime          \
      " version or compile from source code."))

#ifdef __AVX512F__
  if (!platform::MayIUse(platform::avx512f)) {
    if (platform::MayIUse(platform::avx2)) {
      AVX_GUIDE(AVX512, AVX2);
    } else if (platform::MayIUse(platform::avx)) {
      AVX_GUIDE(AVX512, AVX);
    } else {
      AVX_GUIDE(AVX512, NonAVX);
    }
  }
#endif

#ifdef __AVX2__
  if (!platform::MayIUse(platform::avx2)) {
    if (platform::MayIUse(platform::avx)) {
      AVX_GUIDE(AVX2, AVX);
    } else {
      AVX_GUIDE(AVX2, NonAVX);
    }
  }
#endif

#ifdef __AVX__
  if (!platform::MayIUse(platform::avx)) {
    AVX_GUIDE(AVX, NonAVX);
  }
#endif
#undef AVX_GUIDE

#endif
}

#ifndef _WIN32
void SignalHandle(const char *data, int size) {
  auto file_path = string::Sprintf("/tmp/paddle.%d.dump_info", ::getpid());
  try {
    // The signal is coming line by line but we print general guide just once
    std::call_once(glog_warning_once_flag, [&]() {
      LOG(WARNING) << "Warning: PaddlePaddle catches a failure signal, it may "
                      "not work properly\n";
      LOG(WARNING) << "You could check whether you killed PaddlePaddle "
                      "thread/process accidentally or report the case to "
                      "PaddlePaddle\n";
      LOG(WARNING) << "The detail failure signal is:\n\n";
    });

    LOG(WARNING) << std::string(data, size);
    std::ofstream dump_info;
    dump_info.open(file_path, std::ios::app);
    dump_info << std::string(data, size);
    dump_info.close();
  } catch (...) {
  }
}
#endif

void InitGLOG(const std::string &prog_name) {
  std::call_once(glog_init_flag, [&]() {
    // glog will not hold the ARGV[0] inside.
    // Use strdup to alloc a new string.
    google::InitGoogleLogging(strdup(prog_name.c_str()));
#ifndef _WIN32
    google::InstallFailureSignalHandler();
    google::InstallFailureWriter(&SignalHandle);
#endif
  });
}

}  // namespace framework
}  // namespace paddle
<commit_msg>Unified paddle error format when catch system signal (#25765)<commit_after>/* Copyright (c) 2016 PaddlePaddle 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 <string.h>  // for strdup
#include <algorithm>
#include <fstream>
#include <iostream>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>

#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/platform/cpu_helper.h"
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/fluid/string/split.h"
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/platform/cuda_device_guard.h"
#include "paddle/fluid/platform/dynload/cupti.h"
#endif
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/init.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/string/piece.h"

DECLARE_int32(paddle_num_threads);
DEFINE_int32(multiple_of_cupti_buffer_size, 1,
             "Multiple of the CUPTI device buffer size. If the timestamps have "
             "been dropped when you are profiling, try increasing this value.");

namespace paddle {
namespace platform {

void ParseCommandLineFlags(int argc, char **argv, bool remove) {
  google::ParseCommandLineFlags(&argc, &argv, remove);
}

}  // namespace platform
}  // namespace paddle

namespace paddle {
namespace framework {

#ifdef _WIN32
#define strdup _strdup
#endif

std::once_flag gflags_init_flag;
std::once_flag glog_init_flag;
std::once_flag p2p_init_flag;
std::once_flag glog_warning_once_flag;

bool InitGflags(std::vector<std::string> args) {
  bool successed = false;
  std::call_once(gflags_init_flag, [&]() {
    FLAGS_logtostderr = true;
    // NOTE(zhiqiu): dummy is needed, since the function
    // ParseNewCommandLineFlags in gflags.cc starts processing
    // commandline strings from idx 1.
    // The reason is, it assumes that the first one (idx 0) is
    // the filename of executable file.
    args.insert(args.begin(), "dummy");
    std::vector<char *> argv;
    std::string line;
    int argc = args.size();
    for (auto &arg : args) {
      argv.push_back(const_cast<char *>(arg.data()));
      line += arg;
      line += ' ';
    }
    VLOG(1) << "Before Parse: argc is " << argc
            << ", Init commandline: " << line;

    char **arr = argv.data();
    google::ParseCommandLineFlags(&argc, &arr, true);
    successed = true;

    VLOG(1) << "After Parse: argc is " << argc;
  });
  return successed;
}

void InitP2P(std::vector<int> devices) {
#ifdef PADDLE_WITH_CUDA
  std::call_once(p2p_init_flag, [&]() {
    int count = devices.size();
    for (int i = 0; i < count; ++i) {
      for (int j = 0; j < count; ++j) {
        if (devices[i] == devices[j]) continue;
        int can_acess = -1;
        PADDLE_ENFORCE_CUDA_SUCCESS(
            cudaDeviceCanAccessPeer(&can_acess, devices[i], devices[j]));
        if (can_acess != 1) {
          LOG(WARNING) << "Cannot enable P2P access from " << devices[i]
                       << " to " << devices[j];
        } else {
          platform::CUDADeviceGuard guard(devices[i]);
          cudaDeviceEnablePeerAccess(devices[j], 0);
        }
      }
    }
  });
#endif
}

void InitCupti() {
#ifdef PADDLE_WITH_CUPTI
  if (FLAGS_multiple_of_cupti_buffer_size == 1) return;
  size_t attrValue = 0, attrValueSize = sizeof(size_t);
#define MULTIPLY_ATTR_VALUE(attr)                                            \
  {                                                                          \
    PADDLE_ENFORCE_EQ(                                                       \
        !platform::dynload::cuptiActivityGetAttribute(attr, &attrValueSize,  \
                                                      &attrValue),           \
        true, platform::errors::Unavailable("Get cupti attribute failed.")); \
    attrValue *= FLAGS_multiple_of_cupti_buffer_size;                        \
    LOG(WARNING) << "Set " #attr " " << attrValue << " byte";                \
    PADDLE_ENFORCE_EQ(                                                       \
        !platform::dynload::cuptiActivitySetAttribute(attr, &attrValueSize,  \
                                                      &attrValue),           \
        true, platform::errors::Unavailable("Set cupti attribute failed.")); \
  }
  MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE);
  MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_DEVICE_BUFFER_SIZE_CDP);
#if CUDA_VERSION >= 9000
  MULTIPLY_ATTR_VALUE(CUPTI_ACTIVITY_ATTR_PROFILING_SEMAPHORE_POOL_SIZE);
#endif
#undef MULTIPLY_ATTR_VALUE
#endif
}

void InitDevices(bool init_p2p) {
  // CUPTI attribute should be set before any CUDA context is created (see CUPTI
  // documentation about CUpti_ActivityAttribute).
  InitCupti();
  /*Init all available devices by default */
  std::vector<int> devices;
#ifdef PADDLE_WITH_CUDA
  try {
    // use user specified GPUs in single-node multi-process mode.
    devices = platform::GetSelectedDevices();
  } catch (const std::exception &exp) {
    LOG(WARNING) << "Compiled with WITH_GPU, but no GPU found in runtime.";
  }
#endif
  InitDevices(init_p2p, devices);
}

void InitDevices(bool init_p2p, const std::vector<int> devices) {
  std::vector<platform::Place> places;

  for (size_t i = 0; i < devices.size(); ++i) {
    // In multi process multi gpu mode, we may have gpuid = 7
    // but count = 1.
    if (devices[i] < 0) {
      LOG(WARNING) << "Invalid devices id.";
      continue;
    }
    places.emplace_back(platform::CUDAPlace(devices[i]));
  }
  if (init_p2p) {
    InitP2P(devices);
  }
  places.emplace_back(platform::CPUPlace());
#ifdef PADDLE_WITH_CUDA
  places.emplace_back(platform::CUDAPinnedPlace());
#endif
  platform::DeviceContextPool::Init(places);

#ifndef PADDLE_WITH_MKLDNN
  platform::SetNumThreads(FLAGS_paddle_num_threads);
#endif

#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OSX__)
  if (platform::MayIUse(platform::avx)) {
#ifndef __AVX__
    LOG(WARNING) << "AVX is available, Please re-compile on local machine";
#endif
  }

// Throw some informations when CPU instructions mismatch.
#define AVX_GUIDE(compiletime, runtime)                                  \
  PADDLE_THROW(platform::errors::Unavailable(                            \
      "This version is compiled on higher instruction(" #compiletime     \
      ") system, you may encounter illegal instruction error running on" \
      " your local CPU machine. Please reinstall the " #runtime          \
      " version or compile from source code."))

#ifdef __AVX512F__
  if (!platform::MayIUse(platform::avx512f)) {
    if (platform::MayIUse(platform::avx2)) {
      AVX_GUIDE(AVX512, AVX2);
    } else if (platform::MayIUse(platform::avx)) {
      AVX_GUIDE(AVX512, AVX);
    } else {
      AVX_GUIDE(AVX512, NonAVX);
    }
  }
#endif

#ifdef __AVX2__
  if (!platform::MayIUse(platform::avx2)) {
    if (platform::MayIUse(platform::avx)) {
      AVX_GUIDE(AVX2, AVX);
    } else {
      AVX_GUIDE(AVX2, NonAVX);
    }
  }
#endif

#ifdef __AVX__
  if (!platform::MayIUse(platform::avx)) {
    AVX_GUIDE(AVX, NonAVX);
  }
#endif
#undef AVX_GUIDE

#endif
}

#ifndef _WIN32
// Description Quoted from
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
const struct {
  const char *name;
  const char *error_string;
} SignalErrorStrings[] = {
    {"SIGSEGV", "Segmentation fault"},
    {"SIGILL", "Illegal instruction"},
    {"SIGFPE", "Erroneous arithmetic operation"},
    {"SIGABRT", "Process abort signal"},
    {"SIGBUS", "Access to an undefined portion of a memory object"},
    {"SIGTERM", "Termination signal"},
};

bool StartsWith(const char *str, const char *prefix) {
  size_t len_prefix = strlen(prefix);
  size_t len_str = strlen(str);
  return len_str < len_prefix ? false : memcmp(prefix, str, len_prefix) == 0;
}

const char *ParseSignalErrorString(const std::string &str) {
  for (size_t i = 0;
       i < (sizeof(SignalErrorStrings) / sizeof(*(SignalErrorStrings))); ++i) {
    if (std::string::npos != str.find(SignalErrorStrings[i].name)) {
      return SignalErrorStrings[i].error_string;
    }
  }
  return "Unknown signal";
}

// Handle SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGBUS, and SIGTERM.
std::ostringstream signal_msg_dumper;
void SignalHandle(const char *data, int size) {
  try {
    // NOTE1: The glog FailureSignalHandler dumped messages
    //   are deal with line by line
    // NOTE2: we only deal with the time info ane signal info,
    //   the stack trace will generated by paddle self
    if (StartsWith(data, "*** Aborted at")) {
      signal_msg_dumper << "  [TimeInfo: " << std::string(data, size - 1)
                        << "]\n";
    } else if (StartsWith(data, "***")) {
      std::string signal_info(data, size - 1);
      std::string useless_substr("; stack trace:");
      size_t start_pos = signal_info.rfind(useless_substr);
      signal_info.replace(start_pos, useless_substr.length(), "");
      signal_msg_dumper << "  [SignalInfo: " << signal_info << "]\n";
      // NOTE3: Here does not throw an exception,
      // otherwise it will casue "terminate called recursively"
      auto exp = platform::EnforceNotMet(
          platform::errors::Fatal(
              "A serious error (%s) is detected by the operating system.",
              ParseSignalErrorString(signal_info)),
          __FILE__, __LINE__);
      std::cout << exp.what() << signal_msg_dumper.str() << std::endl;
    }
  } catch (...) {
    // Since the program has already triggered a system error,
    // no further processing is required here, glog FailureSignalHandler
    // will Kill program by the default signal handler
  }
}
#endif

void InitGLOG(const std::string &prog_name) {
  std::call_once(glog_init_flag, [&]() {
    // glog will not hold the ARGV[0] inside.
    // Use strdup to alloc a new string.
    google::InitGoogleLogging(strdup(prog_name.c_str()));
#ifndef _WIN32
    google::InstallFailureSignalHandler();
    google::InstallFailureWriter(&SignalHandle);
#endif
  });
}

}  // namespace framework
}  // namespace paddle
<|endoftext|>


<commit_before>//
// ogles_gpgpu project - GPGPU for mobile devices and embedded systems using OpenGL ES 2.0
//
// Author: Markus Konrad <post@mkonrad.net>, Winter 2014/2015
//         David Hirvonen
// http://www.mkonrad.net
//
// See LICENSE file in project repository root for the license.
//

#include "../common_includes.h"
#include "filter3x3.h"

using namespace std;
using namespace ogles_gpgpu;

Filter3x3Proc::Filter3x3Proc() {

}

void Filter3x3Proc::filterShaderSetup(const char *vShaderSrc, const char *fShaderSrc, GLenum target)
{
    // create shader object
    ProcBase::createShader(vShaderSrc, fShaderSrc, target);

    // get shader params
    shParamAPos = shader->getParam(ATTR, "position");
    shParamATexCoord = shader->getParam(ATTR, "inputTextureCoordinate");
    texelWidthUniform = shader->getParam(UNIF, "texelWidth");
    texelHeightUniform = shader->getParam(UNIF, "texelHeight");

    Tools::checkGLErr(getProcName(), "filterShaderSetup");
}

void Filter3x3Proc::getUniforms() {

}

void Filter3x3Proc::setUniforms() {
    // Set texel width/height uniforms:
    glUniform1f(texelWidthUniform, (1.0f/ float(outFrameW)));
    glUniform1f(texelHeightUniform, (1.0f/ float(outFrameH)));
}

<commit_msg>formatting<commit_after>//
// ogles_gpgpu project - GPGPU for mobile devices and embedded systems using OpenGL ES 2.0
//
// Author: Markus Konrad <post@mkonrad.net>, Winter 2014/2015
//         David Hirvonen
// http://www.mkonrad.net
//
// See LICENSE file in project repository root for the license.
//

#include "../common_includes.h"
#include "filter3x3.h"

using namespace std;
using namespace ogles_gpgpu;

Filter3x3Proc::Filter3x3Proc()
{

}

void Filter3x3Proc::filterShaderSetup(const char *vShaderSrc, const char *fShaderSrc, GLenum target)
{
    // create shader object
    ProcBase::createShader(vShaderSrc, fShaderSrc, target);

    // get shader params
    shParamAPos = shader->getParam(ATTR, "position");
    shParamATexCoord = shader->getParam(ATTR, "inputTextureCoordinate");
    texelWidthUniform = shader->getParam(UNIF, "texelWidth");
    texelHeightUniform = shader->getParam(UNIF, "texelHeight");

    Tools::checkGLErr(getProcName(), "filterShaderSetup");
}

void Filter3x3Proc::getUniforms()
{

}

void Filter3x3Proc::setUniforms()
{
    // Set texel width/height uniforms:
    glUniform1f(texelWidthUniform, (1.0f/ float(outFrameW)));
    glUniform1f(texelHeightUniform, (1.0f/ float(outFrameH)));
}

<|endoftext|>


<commit_before>#pragma once

#include <vector>
#include <string>
#include <set>
#include <memory>
#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <random>
#include <cmath>
#include <ctime>

namespace bandit{

typedef unsigned int uint;

extern const double pi;
extern const double e;

//RNG engine
extern std::mt19937 randomEngine;

template<class T>
int vectorMaxIndex(const std::vector<T> &elems){
  int m=0;
  T mv=elems[0];
  for(unsigned int i=0;i<elems.size();++i){
    if(elems[i]>mv){
      mv=elems[i];
      m=i;
    }
  }
  return m;
}

template<class T>
T vectorMax(std::vector<T> &elems){
  T mv=elems[0];
  for(unsigned int i=0;i<elems.size();++i){
    if(elems[i]>mv){
      mv=elems[i];
    }
  }
  return mv;
}

template<class T>
T vectorSum(const std::vector<T> &elems){
  T s=0;
  for(uint i=0;i<elems.size();++i){
    s+=elems[i];
  }
  return s;
}

std::string itos(int number);
std::string dtos(double number);

} //namespace
<commit_msg>Update util.hpp<commit_after>#pragma once

#include <vector>
#include <string>
#include <set>
#include <memory>
#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <random>
#include <cmath>
#include <ctime>

namespace bandit{

typedef unsigned int uint;

extern const double pi;
extern const double e;

//RNG engine
extern std::mt19937 randomEngine;

template<class T>
int vectorMaxIndex(const std::vector<T> &elems){
  int m=0;
  T mv=elems[0];
  for(uint i=0;i<elems.size();++i){
    if(elems[i]>mv){
      mv=elems[i];
      m=i;
    }
  }
  return m;
}

template<class T>
T vectorMax(std::vector<T> &elems){
  T mv=elems[0];
  for(uint i=0;i<elems.size();++i){
    if(elems[i]>mv){
      mv=elems[i];
    }
  }
  return mv;
}

template<class T>
T vectorSum(const std::vector<T> &elems){
  T s=0;
  for(uint i=0;i<elems.size();++i){
    s+=elems[i];
  }
  return s;
}

std::string itos(int number);
std::string dtos(double number);

} //namespace
<|endoftext|>


<commit_before>/*****************************************************************************
 * Licensed to Qualys, Inc. (QUALYS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * QUALYS 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
 *
 * 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.
 ****************************************************************************/

/**
 * @file
 * @brief IronBee++ Internals — Configuration Directives Tests
 * @internal
 *
 * @author Christopher Alfeld <calfeld@qualys.com>
 **/

#include <ironbeepp/configuration_directives.hpp>
#include <ironbeepp/context.hpp>
#include <ironbeepp/site.hpp>
#include "fixture.hpp"

#include "gtest/gtest.h"

#include <ironbee/engine.h>
#include <ironbee/config.h>

using namespace std;
using namespace IronBee;

class TestConfigurationDirectives :
    public ::testing::Test, public IBPPTestFixture
{
};

TEST_F(TestConfigurationDirectives, ConfigurationParser)
{
    ib_cfgparser_t parser;

    ConfigurationParser P(&parser);

    ASSERT_TRUE(P);
    ASSERT_EQ(&parser, P.ib());

    parser.ib = m_ib_engine;
    parser.mp = ib_engine_pool_main_get(m_ib_engine);

    ib_context_t ctx;
    parser.cur_ctx = &ctx;
    ib_site_t site;
    parser.cur_site = &site;
    ib_loc_t loc;
    parser.cur_loc = &loc;
    parser.cur_blkname = "foobar";

    EXPECT_EQ(parser.ib, P.engine().ib());
    EXPECT_EQ(parser.mp, P.memory_pool().ib());
    EXPECT_EQ(parser.cur_ctx, P.current_context().ib());
    EXPECT_EQ(parser.cur_site, P.current_site().ib());
    EXPECT_EQ(parser.cur_loc, P.current_location().ib());
    EXPECT_EQ(parser.cur_blkname, P.current_block_name());
}
<commit_msg>IrIronBee++/ConfigurationDirectives: Complete tests.<commit_after>/*****************************************************************************
 * Licensed to Qualys, Inc. (QUALYS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * QUALYS 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
 *
 * 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.
 ****************************************************************************/

/**
 * @file
 * @brief IronBee++ Internals — Configuration Directives Tests
 * @internal
 *
 * @author Christopher Alfeld <calfeld@qualys.com>
 **/

#include <ironbeepp/configuration_directives.hpp>
#include <ironbeepp/context.hpp>
#include <ironbeepp/site.hpp>
#include "fixture.hpp"

#include "gtest/gtest.h"

#include <ironbee/engine.h>
#include <ironbee/config.h>

#include <boost/foreach.hpp>

using namespace std;
using namespace IronBee;

class TestConfigurationDirectives :
    public ::testing::Test, public IBPPTestFixture
{
};

TEST_F(TestConfigurationDirectives, ConfigurationParser)
{
    ib_cfgparser_t parser;

    ConfigurationParser P(&parser);

    ASSERT_TRUE(P);
    ASSERT_EQ(&parser, P.ib());

    parser.ib = m_ib_engine;
    parser.mp = ib_engine_pool_main_get(m_ib_engine);

    ib_context_t ctx;
    parser.cur_ctx = &ctx;
    ib_site_t site;
    parser.cur_site = &site;
    ib_loc_t loc;
    parser.cur_loc = &loc;
    parser.cur_blkname = "foobar";

    EXPECT_EQ(parser.ib, P.engine().ib());
    EXPECT_EQ(parser.mp, P.memory_pool().ib());
    EXPECT_EQ(parser.cur_ctx, P.current_context().ib());
    EXPECT_EQ(parser.cur_site, P.current_site().ib());
    EXPECT_EQ(parser.cur_loc, P.current_location().ib());
    EXPECT_EQ(parser.cur_blkname, P.current_block_name());

    // Parse routines tested below.
}

struct Info
{
    Info() : which(0) {}

    int                 which;
    ConfigurationParser parser;
    string              name;
    string              param1;
    string              param2;
    bool                on;
    vector<string>      nparam;
    uint32_t            mask;
    uint32_t            value;
};

class Handler
{
public:
    explicit Handler(
        Info& out_info
    ) :
        m_out_info(out_info)
    {
        // nop
    }

    void operator()(
        ConfigurationParser parser,
        const char*         name,
        const char*         param1
    )
    {
        m_out_info.which  = 1;
        m_out_info.parser = parser;
        m_out_info.name   = name;
        m_out_info.param1 = param1;
    }

    void operator()(
        ConfigurationParser parser,
        const char*         name,
        const char*         param1,
        const char*         param2
    )
    {
        m_out_info.which  = 2;
        m_out_info.parser = parser;
        m_out_info.name   = name;
        m_out_info.param1 = param1;
        m_out_info.param2 = param2;
    }

    void operator()(
        ConfigurationParser parser,
        const char*         name
    )
    {
        m_out_info.which  = 3;
        m_out_info.parser = parser;
        m_out_info.name   = name;
    }

    void operator()(
        ConfigurationParser parser,
        const char*         name,
        bool                on
    )
    {
        m_out_info.which  = 4;
        m_out_info.parser = parser;
        m_out_info.name   = name;
        m_out_info.on     = on;
    }

    void operator()(
        ConfigurationParser parser,
        const char*         name,
        List<const char*>   args
    )
    {
        m_out_info.which  = 5;
        m_out_info.parser = parser;
        m_out_info.name   = name;

        BOOST_FOREACH(const char* a, args) {
            m_out_info.nparam.push_back(a);
        }
    }
    void operator()(
        ConfigurationParser parser,
        const char*         name,
        uint32_t            value,
        uint32_t            mask
    )
    {
        m_out_info.which  = 6;
        m_out_info.parser = parser;
        m_out_info.name   = name;
        m_out_info.value  = value;
        m_out_info.mask   = mask;
    }

private:
    Info& m_out_info;
};

TEST_F(TestConfigurationDirectives, Registrar)
{
    ib_cfgparser_t* parser = NULL;
    ib_status_t rc;

    rc = ib_cfgparser_create(&parser, m_ib_engine);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(parser);
    ConfigurationParser P(parser);

    Engine engine(m_ib_engine);
    Info info;
    Handler handler(info);
    Info info2;
    Handler handler2(info2);

    ConfigurationDirectivesRegistrar R(engine);
    R.param1("Param1", handler);
    R.param2("Param2", handler);
    R.block("Block", handler, handler2);
    R.on_off("OnOff", handler);
    R.list("List", handler);
    map<string,int64_t> value_map;
    value_map["a"] = (1 << 1) & (1 << 3);
    value_map["b"] = (1 << 7);
    R.op_flags("OpFlags", handler, value_map);

    info = Info();
    P.parse_buffer("Param1 HelloWorld\n", true);
    EXPECT_EQ(1,            info.which);
    EXPECT_EQ(P,            info.parser);
    EXPECT_EQ("Param1",     info.name);
    EXPECT_EQ("HelloWorld", info.param1);

    info = Info();
    P.parse_buffer("Param2 Foo Bar\n", true);
    EXPECT_EQ(2,        info.which);
    EXPECT_EQ(P,        info.parser);
    EXPECT_EQ("Param2", info.name);
    EXPECT_EQ("Foo",    info.param1);
    EXPECT_EQ("Bar",    info.param2);

    info = Info();
    info2 = Info();
    P.parse_buffer("<Block Foo>\n</Block>\n", true);
    EXPECT_EQ(1,       info.which);
    EXPECT_EQ(P,       info.parser);
    EXPECT_EQ("Block", info.name);
    EXPECT_EQ("Foo",   info.param1);
    EXPECT_EQ(3,       info2.which);
    EXPECT_EQ(P,       info2.parser);
    EXPECT_EQ("Block", info2.name);

    info = Info();
    P.parse_buffer("OnOff true\n", true);
    EXPECT_EQ(4,       info.which);
    EXPECT_EQ(P,       info.parser);
    EXPECT_EQ("OnOff", info.name);
    EXPECT_TRUE(info.on);

    info = Info();
    P.parse_buffer("OnOff false\n", true);
    EXPECT_EQ(4,       info.which);
    EXPECT_EQ(P,       info.parser);
    EXPECT_EQ("OnOff", info.name);
    EXPECT_FALSE(info.on);    info = Info();

    info = Info();
    P.parse_buffer("List a b c d\n", true);
    EXPECT_EQ(5,      info.which);
    EXPECT_EQ(P,      info.parser);
    EXPECT_EQ("List", info.name);
    EXPECT_EQ(4,      info.nparam.size());
    EXPECT_EQ("a",    info.nparam[0]);
    EXPECT_EQ("b",    info.nparam[1]);
    EXPECT_EQ("c",    info.nparam[2]);
    EXPECT_EQ("d",    info.nparam[3]);

    info = Info();
    P.parse_buffer("OpFlags +a -b\n", true);
    EXPECT_EQ(6,         info.which);
    EXPECT_EQ(P,         info.parser);
    EXPECT_EQ("OpFlags", info.name);
    EXPECT_EQ(value_map["a"] | value_map["b"], info.mask);
    EXPECT_EQ(value_map["a"], info.value & info.mask);
    EXPECT_EQ(value_map["b"], ~info.value & info.mask);
}
<|endoftext|>


<commit_before>// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/autofill/atom_autofill_client.h"

#include "atom/browser/autofill/personal_data_manager_factory.h"
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/browser/brave_browser_context.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/popup_item_ids.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/common/origin_util.h"
#include "google_apis/gaia/identity_provider.h"
#include "native_mate/dictionary.h"

DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::AtomAutofillClient);

// stubs TODO - move to separate files
#include "net/http/http_request_headers.h"
namespace variations {
void AppendVariationHeaders(const GURL& url,
                            bool incognito,
                            bool uma_enabled,
                            net::HttpRequestHeaders* headers) {
}
}

namespace rappor {
void SampleDomainAndRegistryFromGURL(RapporService* rappor_service,
                                     const std::string& metric,
                                     const GURL& gurl) {}
}  // namespace rappor
// end stubs

namespace mate {

template<>
struct Converter<autofill::Suggestion> {
  static v8::Local<v8::Value> ToV8(
    v8::Isolate* isolate, autofill::Suggestion val) {
    mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
    dict.Set("value", val.value);
    dict.Set("frontend_id", val.frontend_id);
    return dict.GetHandle();
  }
};
}  // namespace mate

class StubIdentityProvider : public IdentityProvider{
 public:
  StubIdentityProvider() {}
  ~StubIdentityProvider() override {}

  std::string GetActiveUsername() override {
    return std::string();
  };

  std::string GetActiveAccountId() override {
    return std::string();
  ;}

  OAuth2TokenService* GetTokenService() override {
    return nullptr;
  };

  bool RequestLogin() override {
    return false;
  };
};

namespace autofill {

AtomAutofillClient::AtomAutofillClient(content::WebContents* web_contents)
    : content::WebContentsObserver(web_contents),
      api_web_contents_(nullptr) {
  DCHECK(web_contents);
}

AtomAutofillClient::~AtomAutofillClient() {
}

void AtomAutofillClient::Initialize(atom::api::WebContents* api_web_contents) {
  api_web_contents_ = api_web_contents;
}

void AtomAutofillClient::DidAcceptSuggestion(const std::string& value,
                                             int frontend_id,
                                             int index) {
  if (delegate_) {
    delegate_->DidAcceptSuggestion(base::UTF8ToUTF16(value), frontend_id,
                                   index);
  }
}

PersonalDataManager* AtomAutofillClient::GetPersonalDataManager() {
  content::BrowserContext* context = web_contents()->GetBrowserContext();
  return PersonalDataManagerFactory::GetForBrowserContext(context);
}

scoped_refptr<AutofillWebDataService> AtomAutofillClient::GetDatabase() {
  content::BrowserContext* context = web_contents()->GetBrowserContext();
  return static_cast<brave::BraveBrowserContext*>(context)
                ->GetAutofillWebdataService();
}

PrefService* AtomAutofillClient::GetPrefs() {
  return user_prefs::UserPrefs::Get(web_contents()->GetBrowserContext());
}

sync_driver::SyncService* AtomAutofillClient::GetSyncService() {
  return nullptr;
}

IdentityProvider* AtomAutofillClient::GetIdentityProvider() {
  if (!identity_provider_) {
     identity_provider_.reset(new StubIdentityProvider());
  }
  return identity_provider_.get();
}

rappor::RapporService* AtomAutofillClient::GetRapporService() {
  return nullptr;
}

void AtomAutofillClient::ShowAutofillSettings() {
  if (api_web_contents_) {
    api_web_contents_->Emit("show-autofill-settings");
  }
}

void AtomAutofillClient::ShowUnmaskPrompt(
    const CreditCard& card,
    UnmaskCardReason reason,
    base::WeakPtr<CardUnmaskDelegate> delegate) {
}

void AtomAutofillClient::OnUnmaskVerificationResult(
    PaymentsRpcResult result) {
}

void AtomAutofillClient::ConfirmSaveCreditCardLocally(
    const CreditCard& card,
    const base::Closure& callback) {
}

void AtomAutofillClient::ConfirmSaveCreditCardToCloud(
    const CreditCard& card,
    std::unique_ptr<base::DictionaryValue> legal_message,
    const base::Closure& callback) {
}

void AtomAutofillClient::LoadRiskData(
    const base::Callback<void(const std::string&)>& callback) {
}

bool AtomAutofillClient::HasCreditCardScanFeature() {
  return false;
}

void AtomAutofillClient::ScanCreditCard(
    const CreditCardScanCallback& callback) {
}

void AtomAutofillClient::ShowAutofillPopup(
    const gfx::RectF& element_bounds,
    base::i18n::TextDirection text_direction,
    const std::vector<autofill::Suggestion>& suggestions,
    base::WeakPtr<AutofillPopupDelegate> delegate) {
  delegate_ = delegate;
  if (api_web_contents_) {
    v8::Locker locker(api_web_contents_->isolate());
    v8::HandleScope handle_scope(api_web_contents_->isolate());
    mate::Dictionary rect =
      mate::Dictionary::CreateEmpty(api_web_contents_->isolate());
    gfx::Rect client_area = web_contents()->GetContainerBounds();
    rect.Set("x", element_bounds.x());
    rect.Set("y", element_bounds.y());
    rect.Set("width", element_bounds.width());
    rect.Set("height", element_bounds.height());
    rect.Set("clientWidth", client_area.width());
    rect.Set("clientHeight", client_area.height());

    api_web_contents_->Emit("show-autofill-popup",
                            suggestions,
                            rect);
  }
  delegate->OnPopupShown();
}

void AtomAutofillClient::UpdateAutofillPopupDataListValues(
    const std::vector<base::string16>& values,
    const std::vector<base::string16>& labels) {
  if (api_web_contents_) {
    api_web_contents_->Emit("update-autofill-popup-data-list-values",
                            values, labels);
  }
}

void AtomAutofillClient::HideAutofillPopup() {
  // TODO(anthony): conflict with context menu
  // delegate_.reset();
  if (api_web_contents_) {
    api_web_contents_->Emit("hide-autofill-popup");
  }
}

bool AtomAutofillClient::IsAutocompleteEnabled() {
  // For browser, Autocomplete is always enabled as part of Autofill.
  return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
}

void AtomAutofillClient::PropagateAutofillPredictions(
    content::RenderFrameHost* rfh,
    const std::vector<autofill::FormStructure*>& forms) {
}

void AtomAutofillClient::DidFillOrPreviewField(
    const base::string16& autofilled_value,
    const base::string16& profile_full_name) {
}

void AtomAutofillClient::OnFirstUserGestureObserved() {
  ContentAutofillDriverFactory* factory =
    ContentAutofillDriverFactory::FromWebContents(web_contents());
  DCHECK(factory);

  for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) {
    // No need to notify non-live frames.
    // And actually they have no corresponding drivers in the factory's map.
    if (!frame->IsRenderFrameLive())
      continue;
    ContentAutofillDriver* driver = factory->DriverForFrame(frame);
    DCHECK(driver);
    driver->NotifyFirstUserGestureObservedInTab();
  }
}

bool AtomAutofillClient::IsContextSecure(const GURL& form_origin) {
  return content::IsOriginSecure(form_origin);
}

}  // namespace autofill
<commit_msg>Avoid duplicate definition on linux<commit_after>// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/autofill/atom_autofill_client.h"

#include "atom/browser/autofill/personal_data_manager_factory.h"
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/browser/brave_browser_context.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/popup_item_ids.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/common/origin_util.h"
#include "google_apis/gaia/identity_provider.h"
#include "native_mate/dictionary.h"

DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::AtomAutofillClient);

// stubs TODO - move to separate files
#include "net/http/http_request_headers.h"
namespace variations {
void AppendVariationHeaders(const GURL& url,
                            bool incognito,
                            bool uma_enabled,
                            net::HttpRequestHeaders* headers) {
}
}
#if !defined(OS_LINUX)
namespace rappor {
void SampleDomainAndRegistryFromGURL(RapporService* rappor_service,
                                     const std::string& metric,
                                     const GURL& gurl) {}
}  // namespace rappor
#endif
// end stubs

namespace mate {

template<>
struct Converter<autofill::Suggestion> {
  static v8::Local<v8::Value> ToV8(
    v8::Isolate* isolate, autofill::Suggestion val) {
    mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
    dict.Set("value", val.value);
    dict.Set("frontend_id", val.frontend_id);
    return dict.GetHandle();
  }
};
}  // namespace mate

class StubIdentityProvider : public IdentityProvider{
 public:
  StubIdentityProvider() {}
  ~StubIdentityProvider() override {}

  std::string GetActiveUsername() override {
    return std::string();
  };

  std::string GetActiveAccountId() override {
    return std::string();
  ;}

  OAuth2TokenService* GetTokenService() override {
    return nullptr;
  };

  bool RequestLogin() override {
    return false;
  };
};

namespace autofill {

AtomAutofillClient::AtomAutofillClient(content::WebContents* web_contents)
    : content::WebContentsObserver(web_contents),
      api_web_contents_(nullptr) {
  DCHECK(web_contents);
}

AtomAutofillClient::~AtomAutofillClient() {
}

void AtomAutofillClient::Initialize(atom::api::WebContents* api_web_contents) {
  api_web_contents_ = api_web_contents;
}

void AtomAutofillClient::DidAcceptSuggestion(const std::string& value,
                                             int frontend_id,
                                             int index) {
  if (delegate_) {
    delegate_->DidAcceptSuggestion(base::UTF8ToUTF16(value), frontend_id,
                                   index);
  }
}

PersonalDataManager* AtomAutofillClient::GetPersonalDataManager() {
  content::BrowserContext* context = web_contents()->GetBrowserContext();
  return PersonalDataManagerFactory::GetForBrowserContext(context);
}

scoped_refptr<AutofillWebDataService> AtomAutofillClient::GetDatabase() {
  content::BrowserContext* context = web_contents()->GetBrowserContext();
  return static_cast<brave::BraveBrowserContext*>(context)
                ->GetAutofillWebdataService();
}

PrefService* AtomAutofillClient::GetPrefs() {
  return user_prefs::UserPrefs::Get(web_contents()->GetBrowserContext());
}

sync_driver::SyncService* AtomAutofillClient::GetSyncService() {
  return nullptr;
}

IdentityProvider* AtomAutofillClient::GetIdentityProvider() {
  if (!identity_provider_) {
     identity_provider_.reset(new StubIdentityProvider());
  }
  return identity_provider_.get();
}

rappor::RapporService* AtomAutofillClient::GetRapporService() {
  return nullptr;
}

void AtomAutofillClient::ShowAutofillSettings() {
  if (api_web_contents_) {
    api_web_contents_->Emit("show-autofill-settings");
  }
}

void AtomAutofillClient::ShowUnmaskPrompt(
    const CreditCard& card,
    UnmaskCardReason reason,
    base::WeakPtr<CardUnmaskDelegate> delegate) {
}

void AtomAutofillClient::OnUnmaskVerificationResult(
    PaymentsRpcResult result) {
}

void AtomAutofillClient::ConfirmSaveCreditCardLocally(
    const CreditCard& card,
    const base::Closure& callback) {
}

void AtomAutofillClient::ConfirmSaveCreditCardToCloud(
    const CreditCard& card,
    std::unique_ptr<base::DictionaryValue> legal_message,
    const base::Closure& callback) {
}

void AtomAutofillClient::LoadRiskData(
    const base::Callback<void(const std::string&)>& callback) {
}

bool AtomAutofillClient::HasCreditCardScanFeature() {
  return false;
}

void AtomAutofillClient::ScanCreditCard(
    const CreditCardScanCallback& callback) {
}

void AtomAutofillClient::ShowAutofillPopup(
    const gfx::RectF& element_bounds,
    base::i18n::TextDirection text_direction,
    const std::vector<autofill::Suggestion>& suggestions,
    base::WeakPtr<AutofillPopupDelegate> delegate) {
  delegate_ = delegate;
  if (api_web_contents_) {
    v8::Locker locker(api_web_contents_->isolate());
    v8::HandleScope handle_scope(api_web_contents_->isolate());
    mate::Dictionary rect =
      mate::Dictionary::CreateEmpty(api_web_contents_->isolate());
    gfx::Rect client_area = web_contents()->GetContainerBounds();
    rect.Set("x", element_bounds.x());
    rect.Set("y", element_bounds.y());
    rect.Set("width", element_bounds.width());
    rect.Set("height", element_bounds.height());
    rect.Set("clientWidth", client_area.width());
    rect.Set("clientHeight", client_area.height());

    api_web_contents_->Emit("show-autofill-popup",
                            suggestions,
                            rect);
  }
  delegate->OnPopupShown();
}

void AtomAutofillClient::UpdateAutofillPopupDataListValues(
    const std::vector<base::string16>& values,
    const std::vector<base::string16>& labels) {
  if (api_web_contents_) {
    api_web_contents_->Emit("update-autofill-popup-data-list-values",
                            values, labels);
  }
}

void AtomAutofillClient::HideAutofillPopup() {
  // TODO(anthony): conflict with context menu
  // delegate_.reset();
  if (api_web_contents_) {
    api_web_contents_->Emit("hide-autofill-popup");
  }
}

bool AtomAutofillClient::IsAutocompleteEnabled() {
  // For browser, Autocomplete is always enabled as part of Autofill.
  return GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
}

void AtomAutofillClient::PropagateAutofillPredictions(
    content::RenderFrameHost* rfh,
    const std::vector<autofill::FormStructure*>& forms) {
}

void AtomAutofillClient::DidFillOrPreviewField(
    const base::string16& autofilled_value,
    const base::string16& profile_full_name) {
}

void AtomAutofillClient::OnFirstUserGestureObserved() {
  ContentAutofillDriverFactory* factory =
    ContentAutofillDriverFactory::FromWebContents(web_contents());
  DCHECK(factory);

  for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) {
    // No need to notify non-live frames.
    // And actually they have no corresponding drivers in the factory's map.
    if (!frame->IsRenderFrameLive())
      continue;
    ContentAutofillDriver* driver = factory->DriverForFrame(frame);
    DCHECK(driver);
    driver->NotifyFirstUserGestureObservedInTab();
  }
}

bool AtomAutofillClient::IsContextSecure(const GURL& form_origin) {
  return content::IsOriginSecure(form_origin);
}

}  // namespace autofill
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: hfi_constgroup.cxx,v $
 *
 *  $Revision: 1.9 $
 *
 *  last change: $Author: vg $ $Date: 2007-09-18 13:55:49 $
 *
 *  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
 *
 ************************************************************************/

#include <precomp.h>
#include "hfi_constgroup.hxx"

// NOT FULLY DEFINED SERVICES
#include <ary/idl/i_ce.hxx>
#include <ary/idl/ik_constgroup.hxx>
#include <toolkit/hf_linachain.hxx>
#include <toolkit/hf_navi_sub.hxx>
#include <toolkit/hf_title.hxx>
#include "hfi_navibar.hxx"
#include "hfi_property.hxx"
#include "hi_linkhelper.hxx"

extern const String
    C_sCePrefix_Constants("constants group");

namespace
{

const String
    C_sList_Constants("Constants");
const String
    C_sList_Constants_Label("Constants");
const String
    C_sList_ConstantDetails("Constants' Details");
const String
    C_sList_ConstantDetails_Label("ConstantDetails");

enum E_SubListIndices
{
    sli_ConstantsSummary = 0,
    sli_ConstantDetails = 1
};

}   // anonymous namespace

HF_IdlConstGroup::HF_IdlConstGroup( Environment &   io_rEnv,
                                    Xml::Element &  o_rOut )
    :   HtmlFactory_Idl(io_rEnv, &o_rOut)
{
}

HF_IdlConstGroup::~HF_IdlConstGroup()
{
}

void
HF_IdlConstGroup::Produce_byData( const client & i_ce ) const
{
    Dyn<HF_NaviSubRow>
        pNaviSubRow( &make_Navibar(i_ce) );

    HF_TitleTable
        aTitle(CurOut());
    HF_LinkedNameChain
        aNameChain(aTitle.Add_Row());

    aNameChain.Produce_CompleteChain(Env().CurPosition(), nameChainLinker);
    produce_Title(aTitle, C_sCePrefix_Constants, i_ce);

    write_Docu(aTitle.Add_Row(), i_ce);
    CurOut() << new Html::HorizontalLine();

    dyn_ce_list
        dpConstants;
    ary::idl::ifc_constgroup::attr::Get_Constants(dpConstants, i_ce);

    if ( (*dpConstants).operator bool() )
    {
        produce_Members( *dpConstants,
                         C_sList_Constants,
                         C_sList_Constants_Label,
                         C_sList_ConstantDetails,
                         C_sList_ConstantDetails_Label );
        pNaviSubRow->SwitchOn(sli_ConstantsSummary);
        pNaviSubRow->SwitchOn(sli_ConstantDetails);
    }
    pNaviSubRow->Produce_Row();
}

HF_NaviSubRow &
HF_IdlConstGroup::make_Navibar( const client & i_ce ) const
{
    HF_IdlNavigationBar
        aNaviBar(Env(), CurOut());
    aNaviBar.Produce_CeMainRow(i_ce,true);  // true := avoid link to Use-page.

    DYN HF_NaviSubRow &
        ret = aNaviBar.Add_SubRow();
    ret.AddItem(C_sList_Constants, C_sList_Constants_Label, false);
    ret.AddItem(C_sList_ConstantDetails, C_sList_ConstantDetails_Label, false);

    CurOut() << new Html::HorizontalLine();
    return ret;
}

void
HF_IdlConstGroup::produce_MemberDetails( HF_SubTitleTable &  o_table,
                                         const client &      i_ce ) const
{
    HF_IdlConstant
        aElement( Env(), o_table );
    aElement.Produce_byData(i_ce);
}

<commit_msg>INTEGRATION: CWS changefileheader (1.9.26); FILE MERGED 2008/03/28 16:02:02 rt 1.9.26.1: #i87441# Change license header to LPGL v3.<commit_after>/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: hfi_constgroup.cxx,v $
 * $Revision: 1.10 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org 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 version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include <precomp.h>
#include "hfi_constgroup.hxx"

// NOT FULLY DEFINED SERVICES
#include <ary/idl/i_ce.hxx>
#include <ary/idl/ik_constgroup.hxx>
#include <toolkit/hf_linachain.hxx>
#include <toolkit/hf_navi_sub.hxx>
#include <toolkit/hf_title.hxx>
#include "hfi_navibar.hxx"
#include "hfi_property.hxx"
#include "hi_linkhelper.hxx"

extern const String
    C_sCePrefix_Constants("constants group");

namespace
{

const String
    C_sList_Constants("Constants");
const String
    C_sList_Constants_Label("Constants");
const String
    C_sList_ConstantDetails("Constants' Details");
const String
    C_sList_ConstantDetails_Label("ConstantDetails");

enum E_SubListIndices
{
    sli_ConstantsSummary = 0,
    sli_ConstantDetails = 1
};

}   // anonymous namespace

HF_IdlConstGroup::HF_IdlConstGroup( Environment &   io_rEnv,
                                    Xml::Element &  o_rOut )
    :   HtmlFactory_Idl(io_rEnv, &o_rOut)
{
}

HF_IdlConstGroup::~HF_IdlConstGroup()
{
}

void
HF_IdlConstGroup::Produce_byData( const client & i_ce ) const
{
    Dyn<HF_NaviSubRow>
        pNaviSubRow( &make_Navibar(i_ce) );

    HF_TitleTable
        aTitle(CurOut());
    HF_LinkedNameChain
        aNameChain(aTitle.Add_Row());

    aNameChain.Produce_CompleteChain(Env().CurPosition(), nameChainLinker);
    produce_Title(aTitle, C_sCePrefix_Constants, i_ce);

    write_Docu(aTitle.Add_Row(), i_ce);
    CurOut() << new Html::HorizontalLine();

    dyn_ce_list
        dpConstants;
    ary::idl::ifc_constgroup::attr::Get_Constants(dpConstants, i_ce);

    if ( (*dpConstants).operator bool() )
    {
        produce_Members( *dpConstants,
                         C_sList_Constants,
                         C_sList_Constants_Label,
                         C_sList_ConstantDetails,
                         C_sList_ConstantDetails_Label );
        pNaviSubRow->SwitchOn(sli_ConstantsSummary);
        pNaviSubRow->SwitchOn(sli_ConstantDetails);
    }
    pNaviSubRow->Produce_Row();
}

HF_NaviSubRow &
HF_IdlConstGroup::make_Navibar( const client & i_ce ) const
{
    HF_IdlNavigationBar
        aNaviBar(Env(), CurOut());
    aNaviBar.Produce_CeMainRow(i_ce,true);  // true := avoid link to Use-page.

    DYN HF_NaviSubRow &
        ret = aNaviBar.Add_SubRow();
    ret.AddItem(C_sList_Constants, C_sList_Constants_Label, false);
    ret.AddItem(C_sList_ConstantDetails, C_sList_ConstantDetails_Label, false);

    CurOut() << new Html::HorizontalLine();
    return ret;
}

void
HF_IdlConstGroup::produce_MemberDetails( HF_SubTitleTable &  o_table,
                                         const client &      i_ce ) const
{
    HF_IdlConstant
        aElement( Env(), o_table );
    aElement.Produce_byData(i_ce);
}

<|endoftext|>


<commit_before>// Used for testing, do not use it as an example
#include <qpp.h>
#include <experimental/experimental.h>
// #include <MATLAB/matlab.h>
using namespace qpp;

// Future work
// TODO: Implement a circuit description language, load/save from/to file
// TODO: Write a MatrixView class, for multi-index lazy evaluation view - wip

cmat gettmp()
{
    cmat tmp(2, 2);
    tmp << 10, 20, 30, 40;
    return tmp;
}

int main()
{
    // testing qpp::experimental::MatrixView
    idx ROWS = 4, COLS = 4;
    cmat A = gt.CNOT;
    //A << 1, 2., 3., 4.;

    std::cout << "Initial:\n";
    std::cout << disp(A) << std::endl << std::endl;

    auto viewA = experimental::make_MatrixView(A, {1, 0});
    experimental::MatrixView<cmat> viewB = viewA;

    // this should not compile, and it doesn't :)
    // experimental::MatrixView<cmat> tmpview{gettmp()};

    std::cout << "MatrixView: \n";
    std::cout << disp(viewA) << std::endl << std::endl;

    std::cout << "Copy via static_cast of the MatrixView:\n";
    cmat result = static_cast<cmat>(viewA); // convert
    std::cout << disp(result) << std::endl;
}<commit_msg>commit<commit_after>// Used for testing, do not use it as an example
#include <qpp.h>
#include <experimental/experimental.h>
// #include <MATLAB/matlab.h>
using namespace qpp;

// Future work
// TODO: Implement a circuit description language, load/save from/to file
// TODO: Write a MatrixView class, for multi-index lazy evaluation view - wip

cmat gettmp()
{
    cmat tmp(2, 2);
    tmp << 10, 20, 30, 40;
    return tmp;
}

int main()
{
    // testing qpp::experimental::MatrixView
    idx ROWS = 4, COLS = 4;
    cmat A = gt.CNOT;
    //A << 1, 2., 3., 4.;

    std::cout << "Initial:\n";
    std::cout << disp(A) << std::endl << std::endl;

    auto viewA = experimental::make_MatrixView(A, {1, 0});
    experimental::MatrixView<cmat> viewB = viewA;

    // this should not compile, and it doesn't :)
    // experimental::MatrixView<cmat> tmpview{gettmp()};

    std::cout << "MatrixView: \n";
    std::cout << disp(viewA) << std::endl << std::endl;

    std::cout << "Copy via static_cast of the MatrixView:\n";
    cmat result = static_cast<cmat>(viewA); // convert
    std::cout << disp(result) << std::endl << std::endl;

    std::cout << "Copy MatrixView:\n";
    auto viewAcopy = viewA; // copy
    std::cout << disp(viewAcopy) << std::endl;
}<|endoftext|>


<commit_before>#include "converter.hpp"

#include <iostream>

#include "../Server/DataStructures/InternalDataFacade.h"
#include "../DataStructures/QueryEdge.h"

#include "../../../../coding/matrix_traversal.hpp"
#include "../../../../routing/osrm_data_facade.hpp"

#include "../../../succinct/elias_fano.hpp"
#include "../../../succinct/gamma_vector.hpp"
#include "../../../succinct/mapper.hpp"

namespace  mapsme
{

#define PRINT_STATUS(v) { if (v) PRINT_OK else PRINT_FAIL std::cout << std::endl; }
#define PRINT_OK    std::cout << "[Ok]";
#define PRINT_FAIL  std::cout << "[Fail]";

Converter::Converter()
{

}

void Converter::run(const std::string & name)
{
  ServerPaths server_paths;

  server_paths["hsgrdata"] = boost::filesystem::path(name + ".hsgr");
  server_paths["ramindex"] = boost::filesystem::path(name + ".ramIndex");
  server_paths["fileindex"] = boost::filesystem::path(name + ".fileIndex");
  server_paths["geometries"] = boost::filesystem::path(name + ".geometry");
  server_paths["nodesdata"] = boost::filesystem::path(name + ".nodes");
  server_paths["edgesdata"] = boost::filesystem::path(name + ".edges");
  server_paths["namesdata"] = boost::filesystem::path(name + ".names");
  server_paths["timestamp"] = boost::filesystem::path(name + ".timestamp");

  std::cout << "Create internal data facade for file: " << name << "...";
  InternalDataFacade<QueryEdge::EdgeData> facade(server_paths);
  PRINT_STATUS(true);

  uint64_t nodeCount = facade.GetNumberOfNodes();

  std::vector<uint64_t> edges;
  std::vector<int32_t> edgesData;
  std::vector<bool> shortcuts;
  std::vector<int32_t> edgeId;

  std::cout << "Repack graph...";

  for (uint64_t node = 0; node < nodeCount; ++node)
  {
    for (auto edge : facade.GetAdjacentEdgeRange(node))
    {
      uint64_t target = facade.GetTarget(edge);
      auto const & data = facade.GetEdgeData(edge);

      assert(data.forward || data.backward);

      uint32_t d = data.distance;
      d = d << 1;
      d += data.forward ? 1 : 0;
      d = d << 1;
      d += data.backward ? 1 : 0;

      edges.push_back(TraverseMatrixInRowOrder<uint64_t>(nodeCount, node, target, data.backward));
      edgesData.push_back(d);
      shortcuts.push_back(data.shortcut);
      edgeId.push_back(data.id);
    }
  }
  PRINT_STATUS(true);

  std::cout << "Sort edges...";
  std::sort(edges.begin(), edges.end());
  PRINT_STATUS(true);

  std::cout << "Edges count: " << edges.size() << std::endl;

  std::cout << "--- Save matrix" << std::endl;
  succinct::elias_fano::elias_fano_builder builder(edges.back(), edges.size());
  for (auto e : edges)
    builder.push_back(e);
  succinct::elias_fano matrix(&builder);

  std::string fileName = name + ".matrix";
  succinct::mapper::freeze(matrix, fileName.c_str());

  std::cout << "--- Save edge data" << std::endl;
  succinct::gamma_vector edgeVector(edgesData);
  fileName = name + ".edgedata";
  succinct::mapper::freeze(edgeVector, fileName.c_str());

  std::cout << "--- Save edge shortcut id's" << std::endl;
  succinct::gamma_vector edgeIdVector(edgeId);
  fileName = name + ".edgeid";
  succinct::mapper::freeze(edgeIdVector, fileName.c_str());

  std::cout << "--- Save edge shortcuts" << std::endl;
  succinct::bit_vector shortcutsVector(shortcuts);
  fileName = name + ".shortcuts";
  succinct::mapper::freeze(shortcutsVector, fileName.c_str());

  std::cout << "--- Test packed data" << std::endl;
  routing::OsrmDataFacade<QueryEdge::EdgeData> facadeNew(name);

  std::cout << "Check node count " << facade.GetNumberOfNodes() << " == " << facadeNew.GetNumberOfNodes() <<  "...";
  PRINT_STATUS(facade.GetNumberOfNodes() == facadeNew.GetNumberOfNodes());
  std::cout << "Check edges count " << facade.GetNumberOfEdges() << " == " << facadeNew.GetNumberOfEdges() << "...";
  PRINT_STATUS(facade.GetNumberOfEdges() == facadeNew.GetNumberOfEdges());

  std::cout << "Check edges data ...";
  bool error = false;
  assert(facade.GetNumberOfEdges() == facadeNew.GetNumberOfEdges());
  for (uint32_t e = 0; e < facade.GetNumberOfEdges(); ++e)
  {
    QueryEdge::EdgeData d1 = facade.GetEdgeData(e);
    QueryEdge::EdgeData d2 = facadeNew.GetEdgeData(e);

    if (d1.backward != d2.backward ||
        d1.forward != d2.forward ||
        d1.distance != d2.distance ||
        d1.id != d2.id ||
        d1.shortcut != d2.shortcut)
    {
      std::cout << "Edge num: " << e << std::endl;
      std::cout << "d1 (backward: " << (uint32_t)d1.backward << ", forward: " << (uint32_t)d1.forward << ", distance: "
                << (uint32_t)d1.distance << ", id: " << (uint32_t)d1.id << ", shortcut: " << (uint32_t)d1.shortcut << std::endl;
      std::cout << "d2 (backward: " << (uint32_t)d2.backward << ", forward: " << (uint32_t)d2.forward << ", distance: "
                << (uint32_t)d2.distance << ", id: " << (uint32_t)d2.id << ", shortcut: " << (uint32_t)d2.shortcut << std::endl;
      error = true;
      break;
    }
  }
  PRINT_STATUS(!error);

  std::cout << "Check graph structure...";
  error = false;
  for (uint32_t node = 0; node < facade.GetNumberOfNodes(); ++node)
  {
    EdgeRange r1 = facade.GetAdjacentEdgeRange(node);
    EdgeRange r2 = facadeNew.GetAdjacentEdgeRange(node);

    if ((r1.front() != r2.front()) || (r1.back() != r2.back()))
    {
      std::cout << "Node num: " << node << std::endl;
      std::cout << "r1 (" << r1.front() << ", " << r1.back() << ")" << std::endl;
      std::cout << "r2 (" << r2.front() << ", " << r2.back() << ")" << std::endl;

      error = true;
      break;
    }
  }
  PRINT_STATUS(!error);
}

}
<commit_msg>[osrm] Removed builded data checking. Need to link with mom libraries.<commit_after>#include "converter.hpp"

#include <iostream>

#include "../Server/DataStructures/InternalDataFacade.h"
#include "../DataStructures/QueryEdge.h"

#include "../../../../coding/matrix_traversal.hpp"
//#include "../../../../routing/osrm_data_facade.hpp"

#include "../../../succinct/elias_fano.hpp"
#include "../../../succinct/gamma_vector.hpp"
#include "../../../succinct/mapper.hpp"

namespace  mapsme
{

void PrintStatus(bool b)
{
  std::cout << (b ? "[Ok]" : "[Fail]") << std::endl;
}

Converter::Converter()
{

}

void Converter::run(const std::string & name)
{
  ServerPaths server_paths;

  server_paths["hsgrdata"] = boost::filesystem::path(name + ".hsgr");
  server_paths["ramindex"] = boost::filesystem::path(name + ".ramIndex");
  server_paths["fileindex"] = boost::filesystem::path(name + ".fileIndex");
  server_paths["geometries"] = boost::filesystem::path(name + ".geometry");
  server_paths["nodesdata"] = boost::filesystem::path(name + ".nodes");
  server_paths["edgesdata"] = boost::filesystem::path(name + ".edges");
  server_paths["namesdata"] = boost::filesystem::path(name + ".names");
  server_paths["timestamp"] = boost::filesystem::path(name + ".timestamp");

  std::cout << "Create internal data facade for file: " << name << "...";
  InternalDataFacade<QueryEdge::EdgeData> facade(server_paths);
  PrintStatus(true);

  uint64_t nodeCount = facade.GetNumberOfNodes();

  std::vector<uint64_t> edges;
  std::vector<int32_t> edgesData;
  std::vector<bool> shortcuts;
  std::vector<int32_t> edgeId;

  std::cout << "Repack graph...";

  for (uint64_t node = 0; node < nodeCount; ++node)
  {
    for (auto edge : facade.GetAdjacentEdgeRange(node))
    {
      uint64_t target = facade.GetTarget(edge);
      auto const & data = facade.GetEdgeData(edge);

      assert(data.forward || data.backward);

      uint32_t d = data.distance;
      d = d << 1;
      d += data.forward ? 1 : 0;
      d = d << 1;
      d += data.backward ? 1 : 0;

      edges.push_back(TraverseMatrixInRowOrder<uint64_t>(nodeCount, node, target, data.backward));
      edgesData.push_back(d);
      shortcuts.push_back(data.shortcut);
      edgeId.push_back(data.id);
    }
  }
  std::cout << "Edges count: " << edgeId.size() << std::endl;
  PrintStatus(true);

  std::cout << "Sort edges...";
  std::sort(edges.begin(), edges.end());
  PrintStatus(true);

  std::cout << "Edges count: " << edges.size() << std::endl;

  std::cout << "--- Save matrix" << std::endl;
  succinct::elias_fano::elias_fano_builder builder(edges.back(), edges.size());
  for (auto e : edges)
    builder.push_back(e);
  succinct::elias_fano matrix(&builder);

  std::string fileName = name + ".matrix";
  succinct::mapper::freeze(matrix, fileName.c_str());

  std::cout << "--- Save edge data" << std::endl;
  succinct::gamma_vector edgeVector(edgesData);
  fileName = name + ".edgedata";
  succinct::mapper::freeze(edgeVector, fileName.c_str());

  std::cout << "--- Save edge shortcut id's" << std::endl;
  succinct::gamma_vector edgeIdVector(edgeId);
  fileName = name + ".edgeid";
  succinct::mapper::freeze(edgeIdVector, fileName.c_str());

  std::cout << "--- Save edge shortcuts" << std::endl;
  succinct::bit_vector shortcutsVector(shortcuts);
  fileName = name + ".shortcuts";
  succinct::mapper::freeze(shortcutsVector, fileName.c_str());

  /// @todo Restore this checking. Now data facade depends on mwm libraries.
  /*
  std::cout << "--- Test packed data" << std::endl;
  routing::OsrmDataFacade<QueryEdge::EdgeData> facadeNew(name);

  std::cout << "Check node count " << facade.GetNumberOfNodes() << " == " << facadeNew.GetNumberOfNodes() <<  "...";
  PrintStatus(facade.GetNumberOfNodes() == facadeNew.GetNumberOfNodes());
  std::cout << "Check edges count " << facade.GetNumberOfEdges() << " == " << facadeNew.GetNumberOfEdges() << "...";
  PrintStatus(facade.GetNumberOfEdges() == facadeNew.GetNumberOfEdges());

  std::cout << "Check edges data ...";
  bool error = false;
  assert(facade.GetNumberOfEdges() == facadeNew.GetNumberOfEdges());
  for (uint32_t e = 0; e < facade.GetNumberOfEdges(); ++e)
  {
    QueryEdge::EdgeData d1 = facade.GetEdgeData(e);
    QueryEdge::EdgeData d2 = facadeNew.GetEdgeData(e);

    if (d1.backward != d2.backward ||
        d1.forward != d2.forward ||
        d1.distance != d2.distance ||
        d1.id != d2.id ||
        d1.shortcut != d2.shortcut)
    {
      std::cout << "Edge num: " << e << std::endl;
      std::cout << "d1 (backward: " << (uint32_t)d1.backward << ", forward: " << (uint32_t)d1.forward << ", distance: "
                << (uint32_t)d1.distance << ", id: " << (uint32_t)d1.id << ", shortcut: " << (uint32_t)d1.shortcut << std::endl;
      std::cout << "d2 (backward: " << (uint32_t)d2.backward << ", forward: " << (uint32_t)d2.forward << ", distance: "
                << (uint32_t)d2.distance << ", id: " << (uint32_t)d2.id << ", shortcut: " << (uint32_t)d2.shortcut << std::endl;
      error = true;
      break;
    }
  }
  PrintStatus(!error);

  std::cout << "Check graph structure...";
  error = false;
  for (uint32_t node = 0; node < facade.GetNumberOfNodes(); ++node)
  {
    EdgeRange r1 = facade.GetAdjacentEdgeRange(node);
    EdgeRange r2 = facadeNew.GetAdjacentEdgeRange(node);

    if ((r1.front() != r2.front()) || (r1.back() != r2.back()))
    {
      std::cout << "Node num: " << node << std::endl;
      std::cout << "r1 (" << r1.front() << ", " << r1.back() << ")" << std::endl;
      std::cout << "r2 (" << r2.front() << ", " << r2.back() << ")" << std::endl;

      error = true;
      break;
    }
  }
  PrintStatus(!error);
  */
}

}
<|endoftext|>


<commit_before>/*
 *
 * Copyright 2015, Google Inc.
 * 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 Google Inc. 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 <node.h>

#include "grpc/grpc.h"
#include "grpc/grpc_security.h"
#include "grpc/support/log.h"
#include "credentials.h"

namespace grpc {
namespace node {

using v8::Exception;
using v8::External;
using v8::Function;
using v8::FunctionTemplate;
using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Object;
using v8::ObjectTemplate;
using v8::Persistent;
using v8::Value;

NanCallback *Credentials::constructor;
Persistent<FunctionTemplate> Credentials::fun_tpl;

Credentials::Credentials(grpc_credentials *credentials)
    : wrapped_credentials(credentials) {}

Credentials::~Credentials() {
  grpc_credentials_release(wrapped_credentials);
}

void Credentials::Init(Handle<Object> exports) {
  NanScope();
  Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
  tpl->SetClassName(NanNew("Credentials"));
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  NanAssignPersistent(fun_tpl, tpl);
  Handle<Function> ctr = tpl->GetFunction();
  ctr->Set(NanNew("createDefault"),
           NanNew<FunctionTemplate>(CreateDefault)->GetFunction());
  ctr->Set(NanNew("createSsl"),
           NanNew<FunctionTemplate>(CreateSsl)->GetFunction());
  ctr->Set(NanNew("createComposite"),
           NanNew<FunctionTemplate>(CreateComposite)->GetFunction());
  ctr->Set(NanNew("createGce"),
           NanNew<FunctionTemplate>(CreateGce)->GetFunction());
  ctr->Set(NanNew("createFake"),
           NanNew<FunctionTemplate>(CreateFake)->GetFunction());
  ctr->Set(NanNew("createIam"),
           NanNew<FunctionTemplate>(CreateIam)->GetFunction());
  constructor = new NanCallback(ctr);
  exports->Set(NanNew("Credentials"), ctr);
}

bool Credentials::HasInstance(Handle<Value> val) {
  NanScope();
  return NanHasInstance(fun_tpl, val);
}

Handle<Value> Credentials::WrapStruct(grpc_credentials *credentials) {
  NanEscapableScope();
  if (credentials == NULL) {
    return NanEscapeScope(NanNull());
  }
  const int argc = 1;
  Handle<Value> argv[argc] = {
    NanNew<External>(reinterpret_cast<void *>(credentials))};
  return NanEscapeScope(constructor->GetFunction()->NewInstance(argc, argv));
}

grpc_credentials *Credentials::GetWrappedCredentials() {
  return wrapped_credentials;
}

NAN_METHOD(Credentials::New) {
  NanScope();

  if (args.IsConstructCall()) {
    if (!args[0]->IsExternal()) {
      return NanThrowTypeError(
          "Credentials can only be created with the provided functions");
    }
    Handle<External> ext = args[0].As<External>();
    grpc_credentials *creds_value =
        reinterpret_cast<grpc_credentials *>(ext->Value());
    Credentials *credentials = new Credentials(creds_value);
    credentials->Wrap(args.This());
    NanReturnValue(args.This());
  } else {
    const int argc = 1;
    Local<Value> argv[argc] = {args[0]};
    NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv));
  }
}

NAN_METHOD(Credentials::CreateDefault) {
  NanScope();
  NanReturnValue(WrapStruct(grpc_google_default_credentials_create()));
}

NAN_METHOD(Credentials::CreateSsl) {
  NanScope();
  char *root_certs = NULL;
  grpc_ssl_pem_key_cert_pair key_cert_pair = {NULL, NULL};
  if (::node::Buffer::HasInstance(args[0])) {
    root_certs = ::node::Buffer::Data(args[0]);
  } else if (!(args[0]->IsNull() || args[0]->IsUndefined())) {
    return NanThrowTypeError("createSsl's first argument must be a Buffer");
  }
  if (::node::Buffer::HasInstance(args[1])) {
    key_cert_pair.private_key = ::node::Buffer::Data(args[1]);
  } else if (!(args[1]->IsNull() || args[1]->IsUndefined())) {
    return NanThrowTypeError(
        "createSSl's second argument must be a Buffer if provided");
  }
  if (::node::Buffer::HasInstance(args[2])) {
    key_cert_pair.cert_chain = ::node::Buffer::Data(args[2]);
  } else if (!(args[2]->IsNull() || args[2]->IsUndefined())) {
    return NanThrowTypeError(
        "createSSl's third argument must be a Buffer if provided");
  }

  NanReturnValue(WrapStruct(grpc_ssl_credentials_create(
      root_certs, key_cert_pair.private_key == NULL ? NULL : &key_cert_pair)));
}

NAN_METHOD(Credentials::CreateComposite) {
  NanScope();
  if (!HasInstance(args[0])) {
    return NanThrowTypeError(
        "createComposite's first argument must be a Credentials object");
  }
  if (!HasInstance(args[1])) {
    return NanThrowTypeError(
        "createComposite's second argument must be a Credentials object");
  }
  Credentials *creds1 = ObjectWrap::Unwrap<Credentials>(args[0]->ToObject());
  Credentials *creds2 = ObjectWrap::Unwrap<Credentials>(args[1]->ToObject());
  NanReturnValue(WrapStruct(grpc_composite_credentials_create(
      creds1->wrapped_credentials, creds2->wrapped_credentials)));
}

NAN_METHOD(Credentials::CreateGce) {
  NanScope();
  NanReturnValue(WrapStruct(grpc_compute_engine_credentials_create()));
}

NAN_METHOD(Credentials::CreateFake) {
  NanScope();
  NanReturnValue(WrapStruct(grpc_fake_transport_security_credentials_create()));
}

NAN_METHOD(Credentials::CreateIam) {
  NanScope();
  if (!args[0]->IsString()) {
    return NanThrowTypeError("createIam's first argument must be a string");
  }
  if (!args[1]->IsString()) {
    return NanThrowTypeError("createIam's second argument must be a string");
  }
  NanUtf8String auth_token(args[0]);
  NanUtf8String auth_selector(args[1]);
  NanReturnValue(
      WrapStruct(grpc_iam_credentials_create(*auth_token, *auth_selector)));
}

}  // namespace node
}  // namespace grpc
<commit_msg>move fake_transport_security_credentials to private API<commit_after>/*
 *
 * Copyright 2015, Google Inc.
 * 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 Google Inc. 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 <node.h>

#include "grpc/grpc.h"
#include "grpc/grpc_security.h"
#include "grpc/support/log.h"
#include "credentials.h"

namespace grpc {
namespace node {

using v8::Exception;
using v8::External;
using v8::Function;
using v8::FunctionTemplate;
using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::Object;
using v8::ObjectTemplate;
using v8::Persistent;
using v8::Value;

NanCallback *Credentials::constructor;
Persistent<FunctionTemplate> Credentials::fun_tpl;

Credentials::Credentials(grpc_credentials *credentials)
    : wrapped_credentials(credentials) {}

Credentials::~Credentials() {
  grpc_credentials_release(wrapped_credentials);
}

void Credentials::Init(Handle<Object> exports) {
  NanScope();
  Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
  tpl->SetClassName(NanNew("Credentials"));
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  NanAssignPersistent(fun_tpl, tpl);
  Handle<Function> ctr = tpl->GetFunction();
  ctr->Set(NanNew("createDefault"),
           NanNew<FunctionTemplate>(CreateDefault)->GetFunction());
  ctr->Set(NanNew("createSsl"),
           NanNew<FunctionTemplate>(CreateSsl)->GetFunction());
  ctr->Set(NanNew("createComposite"),
           NanNew<FunctionTemplate>(CreateComposite)->GetFunction());
  ctr->Set(NanNew("createGce"),
           NanNew<FunctionTemplate>(CreateGce)->GetFunction());
  ctr->Set(NanNew("createIam"),
           NanNew<FunctionTemplate>(CreateIam)->GetFunction());
  constructor = new NanCallback(ctr);
  exports->Set(NanNew("Credentials"), ctr);
}

bool Credentials::HasInstance(Handle<Value> val) {
  NanScope();
  return NanHasInstance(fun_tpl, val);
}

Handle<Value> Credentials::WrapStruct(grpc_credentials *credentials) {
  NanEscapableScope();
  if (credentials == NULL) {
    return NanEscapeScope(NanNull());
  }
  const int argc = 1;
  Handle<Value> argv[argc] = {
    NanNew<External>(reinterpret_cast<void *>(credentials))};
  return NanEscapeScope(constructor->GetFunction()->NewInstance(argc, argv));
}

grpc_credentials *Credentials::GetWrappedCredentials() {
  return wrapped_credentials;
}

NAN_METHOD(Credentials::New) {
  NanScope();

  if (args.IsConstructCall()) {
    if (!args[0]->IsExternal()) {
      return NanThrowTypeError(
          "Credentials can only be created with the provided functions");
    }
    Handle<External> ext = args[0].As<External>();
    grpc_credentials *creds_value =
        reinterpret_cast<grpc_credentials *>(ext->Value());
    Credentials *credentials = new Credentials(creds_value);
    credentials->Wrap(args.This());
    NanReturnValue(args.This());
  } else {
    const int argc = 1;
    Local<Value> argv[argc] = {args[0]};
    NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv));
  }
}

NAN_METHOD(Credentials::CreateDefault) {
  NanScope();
  NanReturnValue(WrapStruct(grpc_google_default_credentials_create()));
}

NAN_METHOD(Credentials::CreateSsl) {
  NanScope();
  char *root_certs = NULL;
  grpc_ssl_pem_key_cert_pair key_cert_pair = {NULL, NULL};
  if (::node::Buffer::HasInstance(args[0])) {
    root_certs = ::node::Buffer::Data(args[0]);
  } else if (!(args[0]->IsNull() || args[0]->IsUndefined())) {
    return NanThrowTypeError("createSsl's first argument must be a Buffer");
  }
  if (::node::Buffer::HasInstance(args[1])) {
    key_cert_pair.private_key = ::node::Buffer::Data(args[1]);
  } else if (!(args[1]->IsNull() || args[1]->IsUndefined())) {
    return NanThrowTypeError(
        "createSSl's second argument must be a Buffer if provided");
  }
  if (::node::Buffer::HasInstance(args[2])) {
    key_cert_pair.cert_chain = ::node::Buffer::Data(args[2]);
  } else if (!(args[2]->IsNull() || args[2]->IsUndefined())) {
    return NanThrowTypeError(
        "createSSl's third argument must be a Buffer if provided");
  }

  NanReturnValue(WrapStruct(grpc_ssl_credentials_create(
      root_certs, key_cert_pair.private_key == NULL ? NULL : &key_cert_pair)));
}

NAN_METHOD(Credentials::CreateComposite) {
  NanScope();
  if (!HasInstance(args[0])) {
    return NanThrowTypeError(
        "createComposite's first argument must be a Credentials object");
  }
  if (!HasInstance(args[1])) {
    return NanThrowTypeError(
        "createComposite's second argument must be a Credentials object");
  }
  Credentials *creds1 = ObjectWrap::Unwrap<Credentials>(args[0]->ToObject());
  Credentials *creds2 = ObjectWrap::Unwrap<Credentials>(args[1]->ToObject());
  NanReturnValue(WrapStruct(grpc_composite_credentials_create(
      creds1->wrapped_credentials, creds2->wrapped_credentials)));
}

NAN_METHOD(Credentials::CreateGce) {
  NanScope();
  NanReturnValue(WrapStruct(grpc_compute_engine_credentials_create()));
}

NAN_METHOD(Credentials::CreateIam) {
  NanScope();
  if (!args[0]->IsString()) {
    return NanThrowTypeError("createIam's first argument must be a string");
  }
  if (!args[1]->IsString()) {
    return NanThrowTypeError("createIam's second argument must be a string");
  }
  NanUtf8String auth_token(args[0]);
  NanUtf8String auth_selector(args[1]);
  NanReturnValue(
      WrapStruct(grpc_iam_credentials_create(*auth_token, *auth_selector)));
}

}  // namespace node
}  // namespace grpc
<|endoftext|>