text:
/*
 * Copyright 2015 David A. Boyuka II
 *
 * 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.
 */

/*
 * quantize.cpp
 *
 *  Created on: May 22, 2014
 *      Author: David A. Boyuka II
 */

extern "C" {
#include <getopt.h>
#ifndef _Bool
#define _Bool bool
#endif
#include <pique/util/myopts.h>
}

#include <iostream>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <set>
#include <map>

#include <boost/algorithm/string.hpp>

using namespace std;

enum struct QuantizeMode { TRUNCATE, CONV_1C_TO_2C, ZERO_INSIGBITS};

struct cmd_args_t {
	char *in_filename_str;
	char *out_filename_str;
	int datatype_size;
	int outdatatype_size;
	int sigbits;
	char *quantize_mode;
	bool as_bids;
};

struct cmd_config_t {
	string in_filename;
	string out_filename;
	int datatype_size;
	int outdatatype_size;
	int sigbits;
	QuantizeMode mode;
	bool as_bids;
};

template<typename WordT, typename OutWordT>
static void quantize_binids(const cmd_config_t &conf) {
	const int wordbits = (sizeof(WordT) << 3);
	const int insigbits = wordbits - conf.sigbits;

	FILE *fin = std::fopen(conf.in_filename.c_str(), "r");
	FILE *fout = std::fopen(conf.out_filename.c_str(), "w");
	assert(fin && fout);

	std::set< WordT > keys;

	const uint64_t BUFFER_SIZE = 1048576;
	std::vector<WordT> buf(BUFFER_SIZE);

	while (!feof(fin)) {
		const size_t nwords = fread(&buf.front(), sizeof(WordT), buf.size(), fin);
		switch (conf.mode) {
		case QuantizeMode::TRUNCATE:
			for (size_t i = 0; i < nwords; i++)
				keys.insert(buf[i] >> insigbits);
			break;
		case QuantizeMode::CONV_1C_TO_2C:
		{
			const WordT signbitmask = (WordT)1 << (wordbits - 1);
			for (size_t i = 0; i < nwords; i++) {
				// 0XXXXXXXX -> 1XXXXXXXX
				// 1XXXXXXXX -> 0~(XXXXXXXX)
				if (buf[i] & signbitmask) {
					keys.insert((WordT)(~buf[i]) >> insigbits);
				} else {
					keys.insert((buf[i] | signbitmask) >> insigbits);
				}
			}
			break;
		}
		case QuantizeMode::ZERO_INSIGBITS:
		{
			const WordT keepmask = (~(WordT)0) << insigbits;
			for (size_t i = 0; i < nwords; i++)
				keys.insert(buf[i] & keepmask);
		}
		}
	}

	fseek(fin, 0, SEEK_SET);

	OutWordT nextbid = 0;
	std::map< WordT, OutWordT > bin_id_map;
	for (auto it = keys.begin(); it != keys.end(); it++)
		bin_id_map.insert(std::make_pair(*it, nextbid++));

	std::vector<OutWordT> outbuf(BUFFER_SIZE);

	while (!feof(fin)) {
		const size_t nwords = fread(&buf.front(), sizeof(WordT), buf.size(), fin);
		switch (conf.mode) {
		case QuantizeMode::TRUNCATE:
			for (size_t i = 0; i < nwords; i++)
				outbuf[i] = bin_id_map[buf[i] >> insigbits];
			break;
		case QuantizeMode::CONV_1C_TO_2C:
		{
			const WordT signbitmask = (WordT)1 << (wordbits - 1);
			for (size_t i = 0; i < nwords; i++) {
				// 0XXXXXXXX -> 1XXXXXXXX
				// 1XXXXXXXX -> 0~(XXXXXXXX)
				if (buf[i] & signbitmask) {
					outbuf[i] = bin_id_map[(WordT)(~buf[i]) >> insigbits];
				} else {
					outbuf[i] = bin_id_map[(buf[i] | signbitmask) >> insigbits];
				}
			}
			break;
		}
		case QuantizeMode::ZERO_INSIGBITS:
		{
			const WordT keepmask = (~(WordT)0) << insigbits;
			for (size_t i = 0; i < nwords; i++)
				outbuf[i] = bin_id_map[buf[i] & keepmask];
		}
		}
		fwrite(&outbuf.front(), sizeof(OutWordT), nwords, fout);
	}

	fclose(fin);
	fclose(fout);
}

template<typename WordT, typename OutWordT>
static void quantize(const cmd_config_t &conf) {
	if (conf.as_bids) {
		quantize_binids<WordT, OutWordT>(conf);
		return;
	}

	const int wordbits = (sizeof(WordT) << 3);
	const int insigbits = wordbits - conf.sigbits;

	FILE *fin = std::fopen(conf.in_filename.c_str(), "r");
	FILE *fout = std::fopen(conf.out_filename.c_str(), "w");
	assert(fin && fout);

	const uint64_t BUFFER_SIZE = 1048576;
	std::vector<WordT> buf(BUFFER_SIZE);
	std::vector<OutWordT> outbuf(BUFFER_SIZE);

	while (!feof(fin)) {
		const size_t nwords = fread(&buf.front(), sizeof(WordT), buf.size(), fin);
		switch (conf.mode) {
		case QuantizeMode::TRUNCATE:
			std::cerr << "TRUNCATE MODE" << std::endl;
			for (size_t i = 0; i < nwords; i++)
				outbuf[i] = (OutWordT)(buf[i] >> insigbits);
			break;
		case QuantizeMode::CONV_1C_TO_2C:
		{
			std::cerr << "1's TO 2's COMPLEMENT CONVERT MODE" << std::endl;
			const WordT signbitmask = (WordT)1 << (wordbits - 1);
			for (size_t i = 0; i < nwords; i++) {
				if (buf[i] & signbitmask) {
					const WordT rmsignbit = buf[i] ^ signbitmask;
					outbuf[i] = ~(OutWordT)(rmsignbit >> insigbits) + 1;
				} else {
					outbuf[i] = (OutWordT)(buf[i] >> insigbits);
				}
			}
			break;
		}
		case QuantizeMode::ZERO_INSIGBITS:
		{
			std::cerr << "ZERO INSIGBITS MODE" << std::endl;
			assert(typeid(WordT) == typeid(OutWordT));
			const WordT keepmask = (~(WordT)0) << insigbits;
			for (size_t i = 0; i < nwords; i++)
				outbuf[i] = (OutWordT)(buf[i] & keepmask);
		}
		}
		fwrite(&outbuf.front(), sizeof(OutWordT), nwords, fout);
	}

	fclose(fin);
	fclose(fout);
}

static void validate_and_fully_parse_args(cmd_config_t &conf, cmd_args_t &args) {
	conf.in_filename = string(args.in_filename_str);
	conf.out_filename = string(args.out_filename_str);
	conf.datatype_size = args.datatype_size;
	conf.outdatatype_size = args.outdatatype_size;
	conf.sigbits = args.sigbits;
	conf.as_bids = args.as_bids;

	if (strcasecmp(args.quantize_mode, "truncate") == 0)
		conf.mode = QuantizeMode::TRUNCATE;
	else if (strcasecmp(args.quantize_mode, "conv2c") == 0)
		conf.mode = QuantizeMode::CONV_1C_TO_2C;
	else if (strcasecmp(args.quantize_mode, "zero") == 0)
		conf.mode = QuantizeMode::ZERO_INSIGBITS;
	else
		abort();
}

static myoption addopt(const char *flagname, int hasarg, OPTION_VALUE_TYPE type, void *output, void *fixedval = NULL) {
	myoption opt;
	opt.flagname = flagname;
	opt.hasarg = hasarg;
	opt.type = type;
	opt.output = output;
	opt.fixedval = fixedval;
	return opt;
}

template<typename WordT>
static void quantize_outword_switch(const cmd_config_t &conf) {
    switch (conf.outdatatype_size) {
    case 1: quantize<WordT, uint8_t>(conf); break;
    case 2: quantize<WordT, uint16_t>(conf); break;
    case 4: quantize<WordT, uint32_t>(conf); break;
    case 8: quantize<WordT, uint64_t>(conf); break;
    default:
    	std::cout << "Invalid out datatype size " << conf.outdatatype_size << std::endl;
    	abort();
    }
}

static void quantize_inword_switch(const cmd_config_t &conf) {
    switch (conf.datatype_size) {
    case 1: quantize_outword_switch<uint8_t>(conf); break;
    case 2: quantize_outword_switch<uint16_t>(conf); break;
    case 4: quantize_outword_switch<uint32_t>(conf); break;
    case 8: quantize_outword_switch<uint64_t>(conf); break;
    default:
    	std::cout << "Invalid datatype size " << conf.datatype_size << std::endl;
    	abort();
    }
}

int main(int argc, char **argv) {
    cmd_args_t args;
    cmd_config_t conf;

    static bool FALSEVAL = false; // , TRUEVAL = true;
    static char DEF_QUANTIZE_MODE[] = "truncate";

    myoption opts[] = {
        addopt("fin", required_argument, OPTION_TYPE_STRING, &args.in_filename_str),
        addopt("fout", required_argument, OPTION_TYPE_STRING, &args.out_filename_str),
        addopt("dtsize", required_argument, OPTION_TYPE_INT, &args.datatype_size),
        addopt("odtsize", required_argument, OPTION_TYPE_INT, &args.outdatatype_size),
        addopt("sigbits", required_argument, OPTION_TYPE_INT, &args.sigbits),
        addopt("quantmode", required_argument, OPTION_TYPE_STRING, &args.quantize_mode, DEF_QUANTIZE_MODE),
        addopt("as_bids", required_argument, OPTION_TYPE_BOOLEAN, &args.as_bids, &FALSEVAL),
        addopt(NULL, required_argument, OPTION_TYPE_BOOLEAN, NULL),
    };
    parse_args(&argc, &argv, opts);
    validate_and_fully_parse_args(conf, args);

    assert(conf.sigbits >= 1 && conf.sigbits <= (conf.datatype_size << 3));

    quantize_inword_switch(conf);
}

max_stars_repo_name: daboyuka/PIQUE

max_issues_repo_name: daboyuka/PIQUE

max_forks_repo_name: daboyuka/PIQUE


text:
#include "utils/command.h"
#include <mtl/string.hpp>

namespace tofi
{
    namespace commands
    {
        Command parse(std::string_view commandline)
        {
            std::vector<std::string> argv;
            mtl::string::split<char, std::string>(commandline, " ", std::back_inserter(argv));

            // Remove any empty args, may have happened from removing app info placeholders
            argv.erase(std::remove_if(std::begin(argv), std::end(argv), [](const std::string &str) {
                           return str.empty();
                       }),
                       std::end(argv));

            Command retval;
            retval.path = argv[0];
            retval.argv = std::move(argv);

            return retval;
        }
    } // namespace commands
} // namespace tofi

ext: cpp

max_stars_repo_name: scaryrawr/tofi

max_issues_repo_name: scaryrawr/tofi

max_forks_repo_name: scaryrawr/tofi


text:
#include "dump_archive.hpp"
#include "table_extractor.hpp"
#include "types.hpp"

#include <string>
#include <stdexcept>
#include <iostream>
#include <fstream>

#include <boost/format.hpp>
#include <boost/noncopyable.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/exception/all.hpp>
#include <boost/thread.hpp>
#include <boost/make_shared.hpp>
#include <boost/foreach.hpp>

namespace bt = boost::posix_time;
namespace fs = boost::filesystem;

namespace {

struct tag_table_name;
typedef boost::error_info<tag_table_name, std::string> errinfo_table_name;

template <typename R>
bt::ptime extract_table_with_timestamp(const std::string &table_name,
                                       const std::string &dump_file,
                                       bool resume,
                                       unsigned int max_concurrency) {
  typedef R row_type;
  fs::path base_dir(table_name);
  boost::optional<bt::ptime> timestamp;

  if (fs::exists(base_dir)) {
    if (fs::is_directory(base_dir) && fs::exists(base_dir / ".complete") && resume) {
      std::string timestamp_str;
      fs::ifstream in(base_dir / ".complete");
      std::getline(in, timestamp_str);
      if (timestamp_str == "-infinity") {
        timestamp = bt::ptime(bt::neg_infin);
      } else {
        timestamp = bt::time_from_string(timestamp_str);
      }

    } else {
      fs::remove_all(base_dir);
    }
  }

  if (timestamp) {
    return timestamp.get();

  } else {
    table_extractor_with_timestamp<row_type> extractor(table_name, dump_file, max_concurrency);
    timestamp = extractor.read();
    fs::ofstream out(base_dir / ".complete");
    out << bt::to_simple_string(timestamp.get()) << "\n";
    return timestamp.get();
  }
}

template <typename R>
void thread_extract_with_timestamp(bt::ptime &timestamp,
                                   boost::exception_ptr &error,
                                   std::string table_name,
                                   std::string dump_file,
                                   bool resume,
                                   unsigned int max_concurrency) {
  try {
    bt::ptime ts = extract_table_with_timestamp<R>(table_name, dump_file, resume, max_concurrency);
    timestamp = ts;

  } catch (const boost::exception &e) {
    error = boost::current_exception();

  } catch (const std::exception &e) {
    error = boost::current_exception();

  } catch (...) {
    std::cerr << "Unexpected exception of unknown type in "
              << "thread_extract_with_timestamp(" << table_name
              << ", " << dump_file << ")!" << std::endl;
    abort();
  }
}

} // anonymous namespace

base_thread::~base_thread() {}

template <typename R>
run_thread<R>::run_thread(std::string table_name_, std::string dump_file, bool resume, unsigned int max_concurrency)
  : timestamp(), error(),
    thr(&thread_extract_with_timestamp<R>,
        boost::ref(timestamp), boost::ref(error),
        table_name_, dump_file, resume, max_concurrency), table_name(table_name_) {
}

template <typename R>
run_thread<R>::~run_thread() {
  try {
    thr.join();
  } catch (...) {
  }
}

template <typename R>
bt::ptime run_thread<R>::join() {
  thr.join();
  if (error) {
    boost::throw_exception(boost::enable_error_info(std::runtime_error("Error during archive dump to disk database."))
                           << boost::errinfo_nested_exception(error)
                           << errinfo_table_name(table_name));
  }
  return timestamp;
}

template struct run_thread<user>;
template struct run_thread<changeset>;
template struct run_thread<current_tag>;
template struct run_thread<old_tag>;
template struct run_thread<node>;
template struct run_thread<way>;
template struct run_thread<way_node>;
template struct run_thread<relation>;
template struct run_thread<relation_member>;
template struct run_thread<changeset_comment>;

max_stars_repo_name: ZKoshak/planet-dump-ng

max_issues_repo_name: ZKoshak/planet-dump-ng

max_forks_repo_name: ZKoshak/planet-dump-ng


text:
#include "asset_utils.h"
#include <filesystem>
#include <iostream>
#include <boost/dll.hpp>

namespace fs = std::filesystem;

namespace asset_utils {
fs::path findAssetDirectory() {
  fs::path currentPath(boost::dll::program_location().string());
  auto sharePath = currentPath.parent_path().parent_path() / "share" / "stray" / "assets/";
  if (std::filesystem::exists("../assets") && std::filesystem::exists("../assets/x_axis.ply")) {
    // For development, load assets from parent directory.
    return "../assets/";
  } else if (std::filesystem::exists(sharePath.string())) {
    return sharePath;
  } else {
    std::cout << "Can't find asset directory." << std::endl;
    exit(EXIT_FAILURE);
  }
}
}

ext: cc


text:
//  Copyright (c) 2001-2011 Hartmut Kaiser
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

// compilation test only

#include <boost/config/warning_disable.hpp>
#include <boost/detail/lightweight_test.hpp>

#include <string>
#include <vector>

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

#include <boost/variant.hpp>

#include "test.hpp"

using namespace spirit_test;

//////////////////////////////////////////////////////////////////////////////
struct ast; // Forward declaration

typedef boost::variant<
    double, char, int, std::string, boost::recursive_wrapper<ast>
> ast_element;

struct ast
{
    int op;
    std::vector<ast_element> children;
    ast() {}
};

BOOST_FUSION_ADAPT_STRUCT(
    ast,
    (int, op)
    (std::vector<ast_element>, children)
)

///////////////////////////////////////////////////////////////////////////////
int main()
{
    namespace qi = boost::spirit::qi;

    {
        qi::rule<char const*, ast()> num_expr;
        num_expr = (*(qi::char_ >> num_expr))[ qi::_1 ];
    }

// doesn't currently work
//     {
//         qi::rule<char const*, std::string()> str = "abc";
//         qi::rule<char const*, std::string()> r =
//             '"' >> *('\\' >> qi::char_ | str) >> "'";
//
//         std::string s;
//         BOOST_TEST(test_attr("\"abc\\a\"", r, s) && s == "abca");
//     }

    return boost::report_errors();
}


text:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Calculator
#include <boost/test/unit_test.hpp>

#include <iostream>
#include <calc.h>

BOOST_AUTO_TEST_SUITE(CalculatorTests)

BOOST_AUTO_TEST_CASE(PlusTest)
{
    Calculator calc;
    BOOST_CHECK_EQUAL(calc.plus( 1, 2 ), 3);
}

BOOST_AUTO_TEST_CASE(MinusTest)
{
    Calculator calc;
    BOOST_CHECK_EQUAL(calc.minus( 2, 1 ), 2);
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: wcrbrm/cpp-boost-cmake-examples

max_issues_repo_name: wcrbrm/cpp-boost-cmake-examples

max_forks_repo_name: wcrbrm/cpp-boost-cmake-examples


text:
#include <iostream>
#include <Eigen/Dense>
#include <opencv2/core/eigen.hpp>

int main()
{
	cv::Mat_<float> a = cv::Mat_<float>::ones(2, 2);
	Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> b;
	cv::cv2eigen(a, b);
}

max_stars_repo_name: raphaelmenges/CPPFramework

max_issues_repo_name: raphaelmenges/CPPFramework

max_forks_repo_name: raphaelmenges/CPPFramework


text:
/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */
#include <base/os.h>
#include <iostream>
#include <fstream>
#include <pugixml/pugixml.hpp>
#include <boost/uuid/uuid.hpp>

#include <test/test_cmn_util.h>
#include <pkt/test/test_pkt_util.h>
#include <oper/physical_device.h>
#include <oper/physical_device_vn.h>

#include <test-xml/test_xml.h>
#include <test-xml/test_xml_oper.h>
#include <test-xml/test_xml_validate.h>
#include "test_xml_physical_device.h"

using namespace std;
using namespace pugi;
using namespace boost::uuids;
using namespace AgentUtXmlUtils;

AgentUtXmlValidationNode *
CreatePhysicalDeviceValidateNode(const string &type, const string &name,
                                 const uuid &id, const xml_node &node) {
    if (type == "physical-router" || type == "device")
        return new AgentUtXmlPhysicalDeviceValidate(name, id, node);
    if (type == "physical-interface" || type == "physical-port")
        return new AgentUtXmlPhysicalInterfaceValidate(name, id, node);
    if (type == "remote-physical-interface" || type == "remote-physical-port")
        return new AgentUtXmlRemotePhysicalInterfaceValidate(name, id, node);
    if (type == "logical-interface" || type == "logical-port")
        return new AgentUtXmlLogicalInterfaceValidate(name, id, node);
    if (type == "physical-router-vn" || type == "device-vn")
        return new AgentUtXmlPhysicalDeviceVnValidate(name, id, node);
}

AgentUtXmlNode *
CreatePhysicalDeviceNode(const string &type, const string &name,const uuid &id,
                         const xml_node &node, AgentUtXmlTestCase *test_case) {
    if (type == "physical-router" || type == "device")
        return new AgentUtXmlPhysicalDevice(name, id, node, test_case);
    if (type == "physical-interface" || type == "physical-port")
        return new AgentUtXmlPhysicalInterface(name, id, node, test_case);
    if (type == "remote-physical-interface" || type == "remote-physical-port")
        return new AgentUtXmlRemotePhysicalInterface(name, id, node, test_case);
    if (type == "logical-interface" || type == "logical-port")
        return new AgentUtXmlLogicalInterface(name, id, node, test_case);
}

void AgentUtXmlPhysicalDeviceInit(AgentUtXmlTest *test) {
    test->AddConfigEntry("physical-router", CreatePhysicalDeviceNode);
    test->AddConfigEntry("device", CreatePhysicalDeviceNode);
    test->AddConfigEntry("physical-interface", CreatePhysicalDeviceNode);
    test->AddConfigEntry("physical-port", CreatePhysicalDeviceNode);
    test->AddConfigEntry("remote-physical-interface", CreatePhysicalDeviceNode);
    test->AddConfigEntry("logical-interface", CreatePhysicalDeviceNode);
    test->AddConfigEntry("logical-port", CreatePhysicalDeviceNode);

    test->AddValidateEntry("physical-router", CreatePhysicalDeviceValidateNode);
    test->AddValidateEntry("device", CreatePhysicalDeviceValidateNode);
    test->AddValidateEntry("physical-interface", CreatePhysicalDeviceValidateNode);
    test->AddValidateEntry("physical-port", CreatePhysicalDeviceValidateNode);
    test->AddValidateEntry("remote-physical-interface",
                           CreatePhysicalDeviceValidateNode);
    test->AddValidateEntry("logical-interface", CreatePhysicalDeviceValidateNode);
    test->AddValidateEntry("logical-port", CreatePhysicalDeviceValidateNode);
    test->AddValidateEntry("physical-router-vn", CreatePhysicalDeviceValidateNode);
    test->AddValidateEntry("device-vn", CreatePhysicalDeviceValidateNode);
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlPhysicalDevice routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlPhysicalDevice::AgentUtXmlPhysicalDevice(const string &name,
                                                   const uuid &id,
                                                   const xml_node &node,
                                                   AgentUtXmlTestCase *test_case) :
    AgentUtXmlConfig(name, id, node, test_case) {
}

AgentUtXmlPhysicalDevice::~AgentUtXmlPhysicalDevice() {
}

bool AgentUtXmlPhysicalDevice::ReadXml() {
    return AgentUtXmlConfig::ReadXml();
}

bool AgentUtXmlPhysicalDevice::ToXml(xml_node *parent) {
    xml_node n = AddXmlNodeWithAttr(parent, NodeType().c_str());
    AddXmlNodeWithValue(&n, "name", name());
    AddIdPerms(&n);
    return true;
}

void AgentUtXmlPhysicalDevice::ToString(string *str) {
    AgentUtXmlConfig::ToString(str);
    *str += "\n";
    return;
}

string AgentUtXmlPhysicalDevice::NodeType() {
    return "physical-router";
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlPhysicalInterface routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlPhysicalInterface::AgentUtXmlPhysicalInterface
    (const string &name, const uuid &id, const xml_node &node,
     AgentUtXmlTestCase *test_case) :
    AgentUtXmlConfig(name, id, node, test_case) {
}

AgentUtXmlPhysicalInterface::~AgentUtXmlPhysicalInterface() {
}

bool AgentUtXmlPhysicalInterface::ReadXml() {
    if (AgentUtXmlConfig::ReadXml() == false)
        return false;

    GetStringAttribute(node(), "device", &device_name_);
    return true;
}

bool AgentUtXmlPhysicalInterface::ToXml(xml_node *parent) {
    xml_node n = AddXmlNodeWithAttr(parent, NodeType().c_str());
    AddXmlNodeWithValue(&n, "name", name());
    AddXmlNodeWithValue(&n, "display-name", name());
    AddIdPerms(&n);

    if (device_name_ != "") {
        LinkXmlNode(parent, NodeType(), name(), "physical-router",
                    device_name_);
    }

    return true;
}

void AgentUtXmlPhysicalInterface::ToString(string *str) {
    AgentUtXmlConfig::ToString(str);
    *str += "\n";
    return;
}

string AgentUtXmlPhysicalInterface::NodeType() {
    return "physical-interface";
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlRemotePhysicalInterface routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlRemotePhysicalInterface::AgentUtXmlRemotePhysicalInterface
    (const string &name, const uuid &id, const xml_node &node,
     AgentUtXmlTestCase *test_case) :
    AgentUtXmlConfig(name, id, node, test_case) {
}

AgentUtXmlRemotePhysicalInterface::~AgentUtXmlRemotePhysicalInterface() {
}

bool AgentUtXmlRemotePhysicalInterface::ReadXml() {
    if (AgentUtXmlConfig::ReadXml() == false)
        return false;

    GetStringAttribute(node(), "device", &device_name_);
    return true;
}

bool AgentUtXmlRemotePhysicalInterface::ToXml(xml_node *parent) {
    Agent *agent = Agent::GetInstance();
    xml_node n = AddXmlNodeWithAttr(parent, NodeType().c_str());
    string fqdn;
    if (device_name_ != "") {
        fqdn = "dummy:" + device_name_ + ":" + name();
    } else {
        fqdn = "dummy:" + agent->agent_name() + ":" + name();
    }
    AddXmlNodeWithValue(&n, "name", fqdn);
    AddXmlNodeWithValue(&n, "display-name", name());
    AddIdPerms(&n);

    if (device_name_ != "") {
        LinkXmlNode(parent, NodeType(), name(), "physical-router",
                    device_name_);
    }

    return true;
}

void AgentUtXmlRemotePhysicalInterface::ToString(string *str) {
    AgentUtXmlConfig::ToString(str);
    *str += "\n";
    return;
}

string AgentUtXmlRemotePhysicalInterface::NodeType() {
    return "physical-interface";
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlLogicalInterface routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlLogicalInterface::AgentUtXmlLogicalInterface
    (const string &name, const uuid &id, const xml_node &node,
     AgentUtXmlTestCase *test_case) :
    AgentUtXmlConfig(name, id, node, test_case) {
}

AgentUtXmlLogicalInterface::~AgentUtXmlLogicalInterface() {
}

bool AgentUtXmlLogicalInterface::ReadXml() {
    if (AgentUtXmlConfig::ReadXml() == false)
        return false;

    GetStringAttribute(node(), "port", &port_name_);
    GetStringAttribute(node(), "vmi", &vmi_name_);
    return true;
}

bool AgentUtXmlLogicalInterface::ToXml(xml_node *parent) {
    xml_node n = AddXmlNodeWithAttr(parent, NodeType().c_str());
    AddXmlNodeWithValue(&n, "name", name());
    AddXmlNodeWithValue(&n, "display-name", name());
    AddIdPerms(&n);

    if (port_name_ != "") {
        LinkXmlNode(parent, NodeType(), name(), "physical-interface",
                    port_name_);
    }

    if (vmi_name_ != "") {
        LinkXmlNode(parent, NodeType(), name(), "virtual-machine-interface",
                    vmi_name_);
    }
    return true;
}

void AgentUtXmlLogicalInterface::ToString(string *str) {
    AgentUtXmlConfig::ToString(str);
    *str += "\n";
    return;
}

string AgentUtXmlLogicalInterface::NodeType() {
    return "logical-interface";
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlPhysicalDeviceValidate routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlPhysicalDeviceValidate::AgentUtXmlPhysicalDeviceValidate
(const string &name, const uuid &id, const xml_node &node) :
    AgentUtXmlValidationNode(name, node), id_(id) {
}

AgentUtXmlPhysicalDeviceValidate::~AgentUtXmlPhysicalDeviceValidate() {
}

bool AgentUtXmlPhysicalDeviceValidate::ReadXml() {
    return true;
}

bool AgentUtXmlPhysicalDeviceValidate::Validate() {
    PhysicalDevice *dev;
    PhysicalDeviceKey key(id_);
    dev = static_cast<PhysicalDevice *>
        (Agent::GetInstance()->physical_device_table()->FindActiveEntry(&key));
    if (present()) {
        return dev != NULL;
    } else {
        return dev == NULL;
    }
}

const string AgentUtXmlPhysicalDeviceValidate::ToString() {
    return "physical-router";
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlPhysicalInterfaceValidate routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlPhysicalInterfaceValidate::AgentUtXmlPhysicalInterfaceValidate
    (const string &name, const uuid &id, const xml_node &node) :
    AgentUtXmlValidationNode(name, node), id_(id) {
}

AgentUtXmlPhysicalInterfaceValidate::~AgentUtXmlPhysicalInterfaceValidate() {
}

bool AgentUtXmlPhysicalInterfaceValidate::ReadXml() {

    if (AgentUtXmlValidationNode::ReadXml() == false)
        return false;
    return true;
}

bool AgentUtXmlPhysicalInterfaceValidate::Validate() {
    Agent *agent = Agent::GetInstance();

    PhysicalInterface *port;
    PhysicalInterfaceKey key(name());
    port = static_cast<PhysicalInterface *>
        (agent->interface_table()->FindActiveEntry(&key));

    if (present() == false) {
        if (port != NULL)
            return false;
        return true;
    }

    if (port == NULL)
        return false;

    return true;
}

const string AgentUtXmlPhysicalInterfaceValidate::ToString() {
    return "physical-interface";
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlRemotePhysicalInterfaceValidate routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlRemotePhysicalInterfaceValidate::AgentUtXmlRemotePhysicalInterfaceValidate
    (const string &name, const uuid &id, const xml_node &node) :
    AgentUtXmlValidationNode(name, node), id_(id), device_uuid_() {
}

AgentUtXmlRemotePhysicalInterfaceValidate::~AgentUtXmlRemotePhysicalInterfaceValidate() {
}

bool AgentUtXmlRemotePhysicalInterfaceValidate::ReadXml() {

    if (AgentUtXmlValidationNode::ReadXml() == false)
        return false;

    uint16_t id = 0;
    GetUintAttribute(node(), "device", &id);
    if (id) {
        device_uuid_ = MakeUuid(id);
    }

    return true;
}

bool AgentUtXmlRemotePhysicalInterfaceValidate::Validate() {
    Agent *agent = Agent::GetInstance();

    RemotePhysicalInterface *port;
    RemotePhysicalInterfaceKey key(name());
    port = static_cast<RemotePhysicalInterface *>
        (agent->interface_table()->FindActiveEntry(&key));

    if (present() == false) {
        if (port != NULL)
            return false;
        return true;
    }

    if (port == NULL)
        return false;

    if (device_uuid_ != nil_uuid()) {
        PhysicalDevice *dev = port->physical_device();
        if (dev->uuid() != device_uuid_)
            return false;
    }

    return true;
}

const string AgentUtXmlRemotePhysicalInterfaceValidate::ToString() {
    return "physical-interface";
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlLogicalInterfaceValidate routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlLogicalInterfaceValidate::AgentUtXmlLogicalInterfaceValidate
    (const string &name, const uuid &id, const xml_node &node) :
    AgentUtXmlValidationNode(name, node), id_(id), physical_port_(),
    device_uuid_(), vmi_uuid_(), vlan_() {
}

AgentUtXmlLogicalInterfaceValidate::~AgentUtXmlLogicalInterfaceValidate() {
}

bool AgentUtXmlLogicalInterfaceValidate::ReadXml() {
    if (AgentUtXmlValidationNode::ReadXml() == false)
        return false;

    GetStringAttribute(node(), "port", &physical_port_);

    uint16_t id = 0;
    GetUintAttribute(node(), "vmi", &id);
    if (id) {
        vmi_uuid_ = MakeUuid(id);
    }

    GetUintAttribute(node(), "vlan", &vlan_);
    return true;
}

bool AgentUtXmlLogicalInterfaceValidate::Validate() {
    Agent *agent = Agent::GetInstance();

    LogicalInterface *port;

    VlanLogicalInterfaceKey key(id_, name());
    port = static_cast<LogicalInterface *>
        (agent->interface_table()->FindActiveEntry(&key));

    if (present() == false) {
        if (port != NULL)
            return false;
        return true;
    }

    if (port == NULL)
        return false;

    if (physical_port_ != "") {
        PhysicalInterface *physical_port = static_cast<PhysicalInterface *>
            (port->physical_interface());
        if (physical_port == NULL)
            return false;
        if (physical_port->name() != physical_port_)
            return false;
    }

    if (vmi_uuid_ != nil_uuid()) {
        Interface *vmi = port->vm_interface();
        if (vmi == NULL)
            return false;
        if (vmi->GetUuid() != vmi_uuid_)
            return false;
    }

    return true;
}

const string AgentUtXmlLogicalInterfaceValidate::ToString() {
    return "logical-interface";
}

/////////////////////////////////////////////////////////////////////////////
//  AgentUtXmlPhysicalDeviceVnValidate routines
/////////////////////////////////////////////////////////////////////////////
AgentUtXmlPhysicalDeviceVnValidate::AgentUtXmlPhysicalDeviceVnValidate
(const string &name, const uuid &id, const xml_node &node) :
    AgentUtXmlValidationNode(name, node), id_(id), vxlan_id_(0xFFFF) {
}

AgentUtXmlPhysicalDeviceVnValidate::~AgentUtXmlPhysicalDeviceVnValidate() {
}

bool AgentUtXmlPhysicalDeviceVnValidate::ReadXml() {
    if (AgentUtXmlValidationNode::ReadXml() == false)
        return false;

    uint16_t id = 0;
    GetUintAttribute(node(), "device", &id);
    if (id) {
        device_uuid_ = MakeUuid(id);
    }

    GetUintAttribute(node(), "vn", &id);
    if (id) {
        vn_uuid_ = MakeUuid(id);
    }

    GetUintAttribute(node(), "vxlan-id", &vxlan_id_);
    return true;
}

bool AgentUtXmlPhysicalDeviceVnValidate::Validate() {
    Agent *agent = Agent::GetInstance();

    PhysicalDeviceVn *entry;
    PhysicalDeviceVnKey key(device_uuid_, vn_uuid_);
    entry = static_cast<PhysicalDeviceVn *>
        (agent->physical_device_vn_table()->FindActiveEntry(&key));

    if (present() == false) {
        if (entry != NULL)
            return false;
        return true;
    }

    if (entry == NULL)
        return false;

    if (vxlan_id_ != 0xFFFF) {
        if (entry->vxlan_id() != vxlan_id_)
            return false;
        VnEntry *vn = entry->vn();
        if (vn == NULL)
            return false;
        if (vn->GetVxLanId() != vxlan_id_)
            return false;
    }

    return true;
}

const string AgentUtXmlPhysicalDeviceVnValidate::ToString() {
    return "physical-device-vn";
}


text:
// @@@LICENSE
//
//      Copyright (c) 2010-2013 LG Electronics, 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.
//
// LICENSE@@@

#include <boost/lexical_cast.hpp>

#include "SmtpBusDispatcher.h"
#include "client/SmtpSession.h"
#include "client/CommandManager.h"
#include "data/SmtpAccount.h"
#include "commands/AccountFinderCommand.h"
#include "commands/ConnectCommand.h"
#include "commands/StartTlsCommand.h"
#include "commands/ExtendedHelloCommand.h"
#include "commands/OldHelloCommand.h"
#include "commands/AuthPlainCommand.h"
#include "commands/AuthYahooCommand.h"
#include "commands/AuthLoginCommand.h"
#include "commands/QuitCommand.h"
#include "commands/SmtpSendMailCommand.h"
#include "SmtpCommon.h"
#include "SmtpClient.h"

#include <iostream>

const char * const	SmtpSession::OUR_DOMAIN = "www.palm.com";
const int	SmtpSession::TIMEOUT_CONNECTION			= 2 * 60;
const int	SmtpSession::TIMEOUT_GREETING			= 5 * 60;
const int	SmtpSession::TIMEOUT_MAIL_FROM			= 5 * 60;
const int	SmtpSession::TIMEOUT_RCPT_TO			= 5 * 60;
const int	SmtpSession::TIMEOUT_DATA_INITIATION	= 2 * 60;
const int	SmtpSession::TIMEOUT_DATA_BLOCK			= 3 * 60;
const int	SmtpSession::TIMEOUT_DATA_TERMINATION	= 10 * 60;
const int	SmtpSession::TIMEOUT_GENERAL			= 5 * 60;

SmtpSession::SmtpSession(MojService * service)
: BusClient(service),
  m_log(SmtpBusDispatcher::s_log),
  m_canShutdown(true),
  m_client(NULL),
  m_commandManager(new CommandManager(1, true)),
  m_currentRequestId(0),
  m_state(State_NeedsAccount),
  m_startedTLS(false),
  m_hasSizeExtension(false),
  m_serverMaxSize(0),
  m_hasTLSExtension(false),
  m_hasChunkingExtension(false),
  m_hasPipeliningExtension(false),
  m_hasAuthExtension(false),
  m_hasPlainAuth(false),
  m_hasLoginAuth(false),
  m_hasYahooAuth(false),
  m_resetAccount(false)
{
}

SmtpSession::SmtpSession(boost::shared_ptr<SmtpAccount> account, MojService * service)
: BusClient(service),
  m_log(SmtpBusDispatcher::s_log),
  m_canShutdown(true),
  m_client(NULL),
  m_commandManager(new CommandManager(1, true)),
  m_account(account),
  m_currentRequestId(0),
  m_state(State_Unconnected),
  m_startedTLS(false),
  m_hasSizeExtension(false),
  m_serverMaxSize(0),
  m_hasTLSExtension(false),
  m_hasChunkingExtension(false),
  m_hasPipeliningExtension(false),
  m_hasAuthExtension(false),
  m_hasPlainAuth(false),
  m_hasLoginAuth(false),
  m_hasYahooAuth(false),
  m_resetAccount(false)
{
}

SmtpSession::~SmtpSession()
{
}

const char* SmtpSession::GetStateName(State state)
{
	switch(state) {
	case State_NeedsAccount:				return "State_NeedsAccount";
	case State_LoadingAccount:				return "State_LoadingAccount";
	case State_Unconnected:					return "State_Unconnected";
	case State_NeedsConnection:				return "State_NeedsConnection";
	case State_Connecting:					return "State_Connecting";
	case State_SendExtendedHelloCommand:	return "State_SendExtendedHelloCommand";
	case State_SendingExtendedHelloCommand:	return "State_SendingExtendedHelloCommand";
	case State_SendOldHelloCommand:			return "State_SendOldHelloCommand";
	case State_SendingOldHelloCommand:		return "State_SendingOldHelloCommand";
	case State_SendStartTlsCommand:			return "State_SendStartTlsCommand";
	case State_SendingStartTlsCommand:		return "State_SendingStartTlsCommand";
	case State_SendAuthPlainCommand:		return "State_SendAuthPlainCommand";
	case State_SendingAuthPlainCommand:		return "State_SendingAuthPlainCommand";
	case State_SendAuthYahooCommand:		return "State_SendAuthYahooCommand";
	case State_SendingAuthYahooCommand:		return "State_SendingAuthYahooCommand";
	case State_SendAuthLoginCommand:		return "State_SendAuthLoginCommand";
	case State_SendingAuthLoginCommand:		return "State_SendingAuthLoginCommand";
	case State_LoginSuccess:				return "State_LoginSuccess";
	case State_NeedsCancelling:				return "State_NeedsCancelling";
	case State_LoggedIn:					return "State_LoggedIn";
	case State_InvalidCredentials:			return "State_InvalidCredentials";
	case State_AccountDeleted:				return "State_AccountDeleted";
	case State_SendQuitCommand:				return "State_SendQuitCommand";
	case State_SendingQuitCommand:			return "State_SendingQuitCommand";
	}

	return "unknown";
}

MojRefCountedPtr<MojServiceRequest> SmtpSession::CreateRequest()
{
	MojLogTrace(m_log);

	assert(m_service);
	MojRefCountedPtr<MojServiceRequest> req;
	MojErr err = m_service->createRequest(req);
	ErrorToException(err);
	return req;
}

void SmtpSession::AccountFinderSuccess(boost::shared_ptr<SmtpAccount> account)
{
	if (m_state == State_LoadingAccount) {
		MojLogInfo(m_log, "AccountFinderSuccess received new account");
		m_account = account;
		RunState(State_Unconnected);
	} else {
		MojLogInfo(m_log, "AccountFinderSuccess triggered in wrong state %d.", m_state);
		InternalFailure("AccountFinderSuccess triggered in wrong state.");
	}
}

void SmtpSession::AccountFinderFailure(SmtpError error)
{
	// FIXME: is this right? Not sure how likely we are to get here.
	Failure(error);
}

void SmtpSession::ConnectSuccess()
{
	if (m_state == State_Connecting) {
		m_startedTLS = false;
		RunState(State_SendExtendedHelloCommand);
	} else {
		MojLogInfo(m_log, "ConnectSuccess triggered in wrong state %d.", m_state);
		InternalFailure("ConnectSuccess triggered in wrong state.");
	}
}

void SmtpSession::ConnectFailure(SmtpError error)
{
	MojLogError(m_log, "ConnectFailure.");
	Disconnect();
	Failure(error);
}

void SmtpSession::HelloSuccess()
{
	if (m_state == State_SendingExtendedHelloCommand) {
		if (m_hasTLSExtension && m_account->WantsTLS()) {
			m_startedTLS = true;
			RunState(State_SendStartTlsCommand);
		} else {
			if (!m_startedTLS && m_account->NeedsTLS()) {
				SmtpError error;
				error.errorCode = MailError::CONFIG_NO_SSL;
				error.errorText = "";
				error.errorOnAccount = true;
				error.internalError = "TLS required by user, but not advertised by EHLO.";
				Failure(error);
				return;
			}
			if (m_hasYahooAuth && m_account->GetYahooToken().length() > 0 ) {
				// Yahoo server, and we loaded a yahoo security token during account loading
				RunState(State_SendAuthYahooCommand);
			} else if (m_hasLoginAuth && m_account->UseSmtpAuth()) {
				RunState(State_SendAuthLoginCommand);
			} else if (m_hasPlainAuth && m_account->UseSmtpAuth()) {
				RunState(State_SendAuthPlainCommand);
			} else {
				// NOTE: no auth needed?
				RunState(State_LoginSuccess);
				// N.B. Watch for 5xx errors on subsequent commands indicating need for credentials
			}
		}
	} else if (m_state == State_SendingOldHelloCommand) {
		if (!m_startedTLS && m_account->NeedsTLS()) {
			SmtpError error;
			error.errorCode = MailError::ACCOUNT_UNKNOWN_AUTH_ERROR;
			error.errorText = "";
			error.errorOnAccount = true;
			error.internalError = "TLS required, but not advertised by HELO.";
			Failure(error);
			return;
		}
		RunState(State_LoginSuccess);
	} else {
		MojLogInfo(m_log, "HelloSuccess triggered in wrong state.");
		InternalFailure("HelloSuccess triggered in wrong state.");
	}
}

void SmtpSession::HelloFailure(SmtpError code)
{
	if (m_state == State_SendingExtendedHelloCommand) {
		// ignore code, just try old hello
		MojLogInfo(m_log, "Extended hello not supported, falling back to old hello.");
		RunState(State_SendOldHelloCommand);
	} else {
		MojLogError(m_log, "HelloFailure in wrong state.");
		InternalFailure("HelloFailure triggered in wrong state.");
	}
}

void SmtpSession::Failure(SmtpError error) {
	MojLogError(m_log, "Failure in SmtpSession %p: code=%d, text=%s, internalText=%s, account=%d, email=%d",
		this,
		error.errorCode,
		error.errorText.c_str(),
		error.internalError.c_str(),
		error.errorOnAccount,
		error.errorOnEmail);
	m_error = error;
	RunState(State_NeedsCancelling);
}

void SmtpSession::InternalFailure(std::string internalText)
{
	SmtpError error;
	error.errorCode = MailError::INTERNAL_ERROR;
	error.errorText = "";
	error.errorOnAccount = true;
	error.internalError = internalText;
	Failure(error);
}

bool SmtpSession::HasError()
{
	return m_error.errorCode != 0;
}

SmtpSession::SmtpError SmtpSession::GetError()
{
	return m_error;
}

void SmtpSession::ClearError()
{
	MojLogInfo(m_log, "Resetting error.");
	m_error.clear();
}

void SmtpSession::TlsSuccess()
{
	if (m_state == State_SendingStartTlsCommand) {
		// Restart the extension negotiation
		RunState(State_SendExtendedHelloCommand);
	} else {
		MojLogInfo(m_log, "TlsSuccess triggered in wrong state.");
		InternalFailure("TlsSuccess triggered in wrong state.");
	}

}

void SmtpSession::TlsFailure(SmtpError error)
{
	MojLogError(m_log, "TlsFailure.");
	Failure(error);
}

void SmtpSession::AuthYahooSuccess()
{
	if (m_state == State_SendingAuthYahooCommand) {
		RunState(State_LoginSuccess);
	} else {
		MojLogInfo(m_log, "AuthYahooSuccess triggered in wrong state.");
		InternalFailure("AuthYahooSuccess triggered in wrong state.");
	}

}

void SmtpSession::AuthYahooFailure(SmtpError error)
{
	MojLogError(m_log, "AuthYahooFailure.");
	Failure(error);
}

void SmtpSession::AuthPlainSuccess()
{
	if (m_state == State_SendingAuthPlainCommand) {
		RunState(State_LoginSuccess);
	} else {
		MojLogInfo(m_log, "AuthPlainSuccess triggered in wrong state.");
		InternalFailure("AuthPlainSuccess triggered in wrong state.");
	}

}

void SmtpSession::AuthPlainFailure(SmtpError error)
{
	MojLogError(m_log, "AuthPlainFailure.");
	Failure(error);
}

void SmtpSession::AuthLoginSuccess()
{
	if (m_state == State_SendingAuthLoginCommand) {
		RunState(State_LoginSuccess);
	} else {
		MojLogInfo(m_log, "AuthLoginSuccess triggered in wrong state.");
		InternalFailure("AuthLoginSuccess triggered in wrong state.");
	}

}

void SmtpSession::AuthLoginFailure(SmtpError error)
{
	MojLogError(m_log, "AuthLoginFailure.");
	Failure(error);
}

void SmtpSession::QuitSuccess()
{
	if (m_state == State_SendingQuitCommand) {
		Disconnect();
		ClearError();
		if (m_resetAccount)
			RunState(State_NeedsAccount);
		else
			RunState(State_Unconnected);
	} else {
		MojLogInfo(m_log, "QuitSuccess triggered in wrong state.");
		InternalFailure("QuitSuccess triggered in wrong state.");
	}
}

void SmtpSession::QuitFailure(SmtpError error)
{
	//ignore error code

	MojLogError(m_log, "QuitFailure.");

	Disconnect();

	ClearError();

	RunState(State_Unconnected);
}

void SmtpSession::SetAccountId(MojObject& accountId)
{
	MojLogTrace(m_log);

	m_accountId = accountId;
}

void SmtpSession::SetConnection(MojRefCountedPtr<SocketConnection> connection)
{
	m_connection = connection;

	if(connection.get()) {
		m_inputStream = connection->GetInputStream();
		m_outputStream = connection->GetOutputStream();
	}
}

void SmtpSession::SetDatabaseInterface(boost::shared_ptr<DatabaseInterface> dbInterface)
{
	m_dbInterface = dbInterface;
}

void SmtpSession::SetFileCacheClient(boost::shared_ptr<FileCacheClient> fileCacheClient)
{
	m_fileCacheClient = fileCacheClient;
}

void SmtpSession::SetState(State toSet)
{
	MojLogDebug(m_log, "changing session %p state from %d to %d", this, m_state, toSet);
	m_state = toSet;

	if (toSet != State_LoggedIn) {
		// Disable running commands until we are logged in
		m_commandManager->Pause();
	}
}

void SmtpSession::RunState(State newState)
{
	SetState(newState);
	CheckQueue();
}

DatabaseInterface& SmtpSession::GetDatabaseInterface()
{
	return *m_dbInterface.get();
}

FileCacheClient& SmtpSession::GetFileCacheClient()
{
	return *m_fileCacheClient.get();
}

const boost::shared_ptr<SmtpAccount>& SmtpSession::GetAccount()
{
	return m_account;
}

LineReaderPtr& SmtpSession::GetLineReader()
{
	if(m_lineReader.get() == NULL)
		m_lineReader.reset(new LineReader(GetInputStream()));

	return m_lineReader;
}

InputStreamPtr& SmtpSession::GetInputStream()
{
	assert( m_inputStream.get() != NULL );
	return m_inputStream;
}

OutputStreamPtr& SmtpSession::GetOutputStream()
{
	assert( m_outputStream.get() != NULL );
	return m_outputStream;
}

MojRefCountedPtr<SocketConnection> SmtpSession::GetConnection()
{
	return m_connection;
}

void SmtpSession::Disconnect()
{
	MojLogInfo(m_log, "session %p disconnecting from server", this);

	if(m_connection.get()) {
		m_connection->Shutdown();
	}

	m_connection.reset();
	m_inputStream.reset();
	m_outputStream.reset();
	m_lineReader.reset();

	Disconnected();
}

void SmtpSession::Disconnected()
{
	MojLogInfo(m_log, "Session %p disconnected", this);

	m_canShutdown = true;

	if(m_client) {
		m_client->SessionShutdown();
	}
}

void SmtpSession::CommandComplete(Command* command)
{
	m_commandManager->CommandComplete(command);

	if (m_resetAccount) {
		RunState(State_SendQuitCommand);
	}

	// Disconnect from the server after running all commands.
	if(m_state == State_NeedsCancelling && m_commandManager->GetPendingCommandCount() == 0
			&& m_commandManager->GetActiveCommandCount() == 0) {
		MojLogInfo(m_log, "no commands active or pending after cancelling, quitting and resetting");

		// Go to the state for a clean disconnect
		RunState(State_SendQuitCommand);
		return;
	}

	// Disconnect from the server after running all commands.
	if(m_state == State_LoggedIn && m_commandManager->GetPendingCommandCount() == 0
			&& m_commandManager->GetActiveCommandCount() == 0) {
		MojLogInfo(m_log, "no commands active or pending, quitting");

		// Go to the state for a clean disconnect
		RunState(State_SendQuitCommand);
		return;
	}
}

void SmtpSession::CheckQueue()
{
	MojLogTrace(m_log);
	MojLogInfo(m_log, "checking the session queue. current state: %d. commands active: %d pending: %d\n",
					m_state, m_commandManager->GetActiveCommandCount(), m_commandManager->GetPendingCommandCount());

	assert( m_state == State_NeedsAccount || m_account.get() || m_state == State_NeedsCancelling);

	// If we need to reset the account, put the queue on hold -- it'll wake itself back up after
	// the account is reloaded.
	if (m_resetAccount && m_state == State_LoggedIn) {
		m_state = State_SendQuitCommand;
		m_commandManager->Pause();
	}

	// If there is anything on the queue and we are not connected, bring up the connection.
	if (m_commandManager->GetPendingCommandCount() > 0 && m_state == State_Unconnected) {
		if (HasError()) {
			MojLogInfo(m_log, "actions in queue, but account has error, so setting state to NeedsCancelling");
			m_state = State_NeedsCancelling;
		} else {
			MojLogInfo(m_log, "actions in queue, setting state to NeedsConnection");
			m_state = State_NeedsConnection;
		}
		// The queue will remain paused until we are logged in.
	}

	switch(m_state)
	{
		case State_NeedsAccount:
		{
			m_canShutdown = false;

			MojLogInfo(m_log, "State of SmtpSession %p: NeedsAccount", this);
			MojLogInfo(m_log, "Loading smtp account");
			m_resetAccount = false; // Only clear this before we start a new AccountFinder,
									// in case an accountUpdate happens in the middle of finding the current account.
			MojRefCountedPtr<AccountFinderCommand> accountFinder(new AccountFinderCommand(*this, m_accountId));
			m_commandManager->RunCommand(accountFinder);

			SetState(State_LoadingAccount);
			break;
		}
		case State_LoadingAccount:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: LoadingAccount", this);
			break;
		}
		case State_Unconnected:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: Unconnected", this);
			break;
		}
		case State_NeedsCancelling:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: NeedsCancelling", this);
			if (m_commandManager->GetPendingCommandCount() == 0
			&& m_commandManager->GetActiveCommandCount() == 0) {
				MojLogInfo(m_log, "no commands active or pending, quitting");
				// Go to the state for a clean disconnect
				RunState(State_SendQuitCommand);
			} else {
				m_commandManager->Resume(); // expect commands will die off with errors due to m_session.HasError()
			}
			break;
		}
		case State_NeedsConnection:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: NeedsConnection", this);

			SetState(State_Connecting);

			MojRefCountedPtr<ConnectCommand> command(new ConnectCommand(*this));
			m_commandManager->RunCommand(command);
			break;
		}
		case State_Connecting:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: Connecting", this);
			break;
		}
		case State_SendExtendedHelloCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendExtendedHelloCommand", this);
			SetState(State_SendingExtendedHelloCommand);

			MojRefCountedPtr<ExtendedHelloCommand> command(new ExtendedHelloCommand(*this, OUR_DOMAIN));
			m_commandManager->RunCommand(command);
			break;
		}
		case State_SendingExtendedHelloCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendingExtendedHelloCommand", this);

			break;
		}
		case State_SendOldHelloCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendOldHelloCommand", this);
			SetState(State_SendingOldHelloCommand);

			MojRefCountedPtr<OldHelloCommand> command(new OldHelloCommand(*this, OUR_DOMAIN));
			m_commandManager->RunCommand(command);
			break;
		}
		case State_SendingOldHelloCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendingOldHelloCommand", this);

			break;
		}
		case State_SendStartTlsCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendStartTlsCommand", this);
			SetState(State_SendingStartTlsCommand);

			MojRefCountedPtr<StartTlsCommand> command(new StartTlsCommand(*this));
			m_commandManager->RunCommand(command);
			break;
		}
		case State_SendingStartTlsCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendingStartTlsCommand", this);
			break;
		}
		case State_SendAuthYahooCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendAuthYahooCommand", this);
			SetState(State_SendingAuthYahooCommand);

			MojRefCountedPtr<AuthYahooCommand> command(new AuthYahooCommand(*this));
			m_commandManager->RunCommand(command);
			break;
		}
		case State_SendingAuthYahooCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendingAuthYahooCommand", this);
			break;
		}
		case State_SendAuthPlainCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendAuthPlainCommand", this);
			SetState(State_SendingAuthPlainCommand);

			MojRefCountedPtr<AuthPlainCommand> command(new AuthPlainCommand(*this));
			m_commandManager->RunCommand(command);
			break;
		}
		case State_SendingAuthPlainCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendingAuthPlainCommand", this);
			break;
		}
		case State_SendAuthLoginCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendAuthLoginCommand", this);
			SetState(State_SendingAuthLoginCommand);

			MojRefCountedPtr<AuthLoginCommand> command(new AuthLoginCommand(*this));
			m_commandManager->RunCommand(command);
			break;
		}
		case State_SendingAuthLoginCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendingAuthLoginCommand", this);
			break;
		}
		case State_LoginSuccess:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: LoginSuccess", this);
			LoginSuccess();
			break;
		}
		case State_LoggedIn:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: LoggedIn", this);
			m_commandManager->Resume();
			break;
		}
		case State_SendQuitCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendQuitCommand", this);

			if (m_connection.get() && !m_connection->IsClosed()) {
				SetState(State_SendingQuitCommand);

				MojRefCountedPtr<QuitCommand> command(new QuitCommand(*this));
				m_commandManager->RunCommand(command);
			} else {
				ClearError();
				if (m_resetAccount)  {
					MojLogInfo(m_log, "Updating account state to NeedsAccount");
					RunState(State_NeedsAccount);
				}
				else {
					MojLogInfo(m_log, "Updating account state to Unconnected");
					RunState(State_Unconnected);
				}
			}
			break;
		}
		case State_SendingQuitCommand:
		{
			MojLogInfo(m_log, "State of SmtpSession %p: SendingQuitCommand", this);
			break;
		}
		default:
		{

		}
	}
}

void SmtpSession::SendMail(const MojObject emailId, MojSignal<SmtpSession::SmtpError>::SlotRef doneSignal)
{
	MojLogTrace(m_log);

	MojRefCountedPtr<SmtpSendMailCommand> send = new SmtpSendMailCommand(*this, emailId);
	send->SetSlots(doneSignal);
	m_commandManager->QueueCommand(send, true);

	CheckQueue();
}

void SmtpSession::ClearAccount()
{
	MojLogTrace(m_log);

	MojLogInfo(m_log, "Clearing account state on session %p, in state %d", this, m_state);

	m_resetAccount = true;

	// If we're in Unconnected, we will be quiescent, so we need to kick to a different state.
	// Other states will either progress to LoggedIn or Unconnected, and CommandComplete will
	// perform this transition for us.
	if (m_state == State_Unconnected) {
		RunState(State_NeedsAccount);
	}
}

void SmtpSession::RunCommandsInQueue()
{
	m_commandManager->RunCommands();
}

void SmtpSession::Validate()
{
	SetState(State_NeedsConnection);
	CheckQueue();
}

void SmtpSession::Send()
{
	CheckQueue();
}

void SmtpSession::LoginSuccess()
{
	MojLogInfo(m_log, "Login success.");
	RunState(State_LoggedIn);
}

void SmtpSession::SendSuccess()
{
	MojLogError(m_log, "Send success.\n");
	RunState(State_SendQuitCommand);
}

void SmtpSession::SendFailure(const std::string& errorText)
{
	MojLogError(m_log, "Failed to send (%s).\n", errorText.c_str());
	RunState(State_SendQuitCommand);
}

void SmtpSession::HasSizeExtension(bool value)
{
	m_hasSizeExtension = value;
}

void SmtpSession::HasSizeMax(int value)
{
	m_serverMaxSize = value;
}

bool SmtpSession::GetSizeMax()
{
	return m_hasSizeExtension;
}

size_t SmtpSession::GetSizeMaxValue()
{
	if (!m_hasSizeExtension)
		return 0;
	else
		return m_serverMaxSize;
}

void SmtpSession::HasTLSExtension(bool value)
{
	m_hasTLSExtension = value;
}

void SmtpSession::HasChunkingExtension(bool value)
{
	m_hasChunkingExtension = value;
}

void SmtpSession::HasPipeliningExtension(bool value)
{
	m_hasPipeliningExtension = value;
}

void SmtpSession::HasAuthExtension(bool value)
{
	m_hasAuthExtension = value;
}

void SmtpSession::HasPlainAuth(bool value)
{
	m_hasPlainAuth = value;
}

void SmtpSession::HasLoginAuth(bool value)
{
	m_hasLoginAuth = value;
}

void SmtpSession::HasYahooAuth(bool value)
{
	m_hasYahooAuth = value;
}

void SmtpSession::Status(MojObject& status) const
{
	MojErr err;

	err = status.putString("state", GetStateName(m_state));
	ErrorToException(err);

	if(m_commandManager->GetActiveCommandCount() > 0 || m_commandManager->GetPendingCommandCount() > 0) {
		MojObject cmStatus;
		m_commandManager->Status(cmStatus);

		err = status.put("commandManager", cmStatus);
		ErrorToException(err);
	}

	if(m_connection.get()) {
		MojObject connectionStatus;
		m_connection->Status(connectionStatus);

		err = status.put("connection", connectionStatus);
		ErrorToException(err);
	}

	if(m_lineReader.get()) {
		MojObject lineReaderStatus;
		m_lineReader->Status(lineReaderStatus);

		err = status.put("lineReader", lineReaderStatus);
		ErrorToException(err);
	}

	if(m_account.get()) {
		MojObject accountInfo;

		err = accountInfo.putString("templateId", m_account->GetTemplateId().c_str());
		ErrorToException(err);

		err = status.put("accountInfo", accountInfo);
		ErrorToException(err);
	}
}

max_stars_repo_name: webOS-ports/mojomail

max_issues_repo_name: openwebos/app-services

max_forks_repo_name: openwebos/app-services


text:
#ifndef ASIO_CORO_EXTENSIONS_ASYNC_READ_HPP
#define ASIO_CORO_EXTENSIONS_ASYNC_READ_HPP

#include "detail/coroutine_holder.hpp"

#include <boost/asio.hpp>
#include <boost/scope_exit.hpp>
#include <boost/system/error_code.hpp>

#include <utility>

namespace asio_coro {
using async_read_result = std::pair<boost::system::error_code, std::size_t>;

/// Returns an awaitable that suspends the awaiting coroutine, performs one asynchronous read from the specified socket
/// into the specified buffer, and resumes the awaiting coroutine.
///
/// The awaitable returns the pair of values async_read_result, where the first item contains the operation result, and
/// the second - the amount of bytes has been read.
///
/// \param stream   A socket to read data from.
/// \param buffer   A buffer to read data into.
template <class Stream, class MutableBuffer> auto async_read(Stream &stream, const MutableBuffer &buffer) {
  class awaitable {
  public:
    explicit awaitable(Stream &stream, const MutableBuffer &buffer) : _stream(stream), _buffer(buffer) {}

    constexpr bool await_ready() const noexcept { return false; }

    async_read_result await_resume() const noexcept { return _result; }

    void await_suspend(detail::coroutine_handle<> continuation) {
      detail::coroutine_holder<> holder(continuation);
      BOOST_SCOPE_EXIT_ALL(&) { holder.release(); };

      _stream.async_read_some(_buffer, [this, holder = std::move(holder)](const boost::system::error_code &error,
                                                                          std::size_t size) mutable {
        _result = std::make_pair(error, size);
        holder.release().resume();
      });
    }

  private:
    Stream &_stream;
    MutableBuffer _buffer;
    async_read_result _result;
  };

  return awaitable(stream, buffer);
}

/// Returns an awaitable that suspends the awaiting coroutine, performs one or more reads from the specified socket to
/// the specified destination buffer upon either the completion condition is met or an error is occurred during reading
/// the data.
///
/// The awaitable returns a value of type async_read_result, see async_read for details on return value.
///
/// \param stream                   Socket to read data from.
/// \param buffer                   The destination data buffer.
/// \param completion_condition     The read completion condition. See documentation on boost::asio::async_read for
///                                 details.
template <class Stream, class MutableBuffer, class CompletionCondition>
auto async_read(Stream &stream, const MutableBuffer &buffer, const CompletionCondition &completion_condition) {
  class awaitable {
  public:
    explicit awaitable(Stream &stream, const MutableBuffer &buffer, const CompletionCondition &completion_condition)
        : _stream(stream), _buffer(buffer), _completion_condition(completion_condition) {}

    constexpr bool await_ready() const noexcept { return false; }

    async_read_result await_resume() const noexcept { return _result; }

    void await_suspend(detail::coroutine_handle<> continuation) {
      detail::coroutine_holder<> holder(continuation);
      BOOST_SCOPE_EXIT_ALL(&) { holder.release(); };

      boost::asio::async_read(
          _stream, _buffer, _completion_condition,
          [holder = std::move(holder), this](const boost::system::error_code &error, std::size_t size) mutable {
            _result = std::make_pair(error, size);
            holder.release().resume();
          });
    }

  private:
    Stream &_stream;
    MutableBuffer _buffer;
    CompletionCondition _completion_condition;
    async_read_result _result;
  };

  return awaitable(stream, buffer, completion_condition);
}
} // namespace asio_coro

#endif // ASIO_CORO_EXTENSIONS_ASYNC_READ_HPP

ext: hpp


text:
// (C) Copyright Jeremy Siek 2000. Permission to copy, use, modify,
// sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
//
// Change Log:
// 20 Jan 2001 - Added warning suppression (David Abrahams)

#include <boost/concept_check.hpp>

/*

  This file verifies that class_requires of the Boost Concept Checking
  Library catches errors when it is suppose to.

*/

struct foo { };

class class_requires_test
{
  BOOST_CLASS_REQUIRE(foo, boost, EqualityComparableConcept);
};

int
main()
{
  class_requires_test x;
  (void)x; // suppress unused variable warning
  return 0;
}


text:
// Copyright (c) 2014, Sailing Lab
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. 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.
//
// 3. Neither the name of the <ORGANIZATION> 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 HOLDER 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.
#pragma once

#include <petuum_ps/consistency/ssp_consistency_controller.hpp>
#include <petuum_ps/oplog/oplog.hpp>
#include <boost/thread/tss.hpp>
#include <utility>
#include <vector>
#include <cstdint>
#include <atomic>

namespace petuum {

class SSPPushConsistencyController : public SSPConsistencyController {
public:
  SSPPushConsistencyController(const TableInfo& info,
    int32_t table_id,
    ProcessStorage& process_storage,
    TableOpLog& oplog,
    const AbstractRow* sample_row,
    boost::thread_specific_ptr<ThreadTable> &thread_cache,
    TableOpLogIndex &oplog_index);

  void GetAsync(int32_t row_id);
  void WaitPendingAsnycGet();

  // Check freshness; make request and block if too stale or row_id not found
  // in storage.
  void Get(int32_t row_id, RowAccessor* row_accessor);

  void ThreadGet(int32_t row_id, ThreadRowAccessor* row_accessor);

private:
  static const size_t kMaxPendingAsyncGetCnt = 256;
  boost::thread_specific_ptr<size_t> pending_async_get_cnt_;
};

}  // namespace petuum

max_stars_repo_name: ForrestGan/public

max_issues_repo_name: ForrestGan/public

max_forks_repo_name: ForrestGan/public


text:
#include "Framebuffer.h"
#include "../shared/glforward.h"
#include <map>
#include <algorithm>
#include <iostream>
#include <boost/bind.hpp>
#include "GLUtility.h"

namespace lgl
{
  ViewFramebuffer::ViewFramebuffer(uint32_t width, uint32_t height)
  {
    m_width = width;
    m_height = height;
  }

  void ViewFramebuffer::useWriting()
  {
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    GLenum viewBuffer = GL_BACK_LEFT;
    glDrawBuffers(1, &viewBuffer);
    glViewport(0, 0, m_width, m_height);
  }

  Renderbuffer::Renderbuffer()
  {}

  Renderbuffer::~Renderbuffer()
  {
    destroy();
  }

  void Renderbuffer::init(uint32_t components,
      uint32_t width, uint32_t height, uint32_t attachment)
  {
    // glGetIntegerv GL_MAX_RENDERBUFFER_SIZE
    // glRenderbufferStorageMultisample
    glGenRenderbuffers(1, &m_name);
    glBindRenderbuffer(GL_RENDERBUFFER, m_name);
    glRenderbufferStorage(GL_RENDERBUFFER, components, width, height);
    m_attachment = attachment;
  }

  void Renderbuffer::destroy()
  {
    glDeleteRenderbuffers(1, &m_name);
  }

  std::vector<Renderbuffer> makeRenderbufferD1C3(uint32_t width, uint32_t height)
  {
    std::vector<Renderbuffer> buffers(4);
    buffers[0].init(GL_DEPTH_COMPONENT32, width, height, GL_DEPTH_ATTACHMENT);
    buffers[1].init(GL_RGBA8, width, height, GL_COLOR_ATTACHMENT0);
    buffers[2].init(GL_RGBA8, width, height, GL_COLOR_ATTACHMENT1);
    buffers[3].init(GL_RGBA8, width, height, GL_COLOR_ATTACHMENT2);
    return buffers;
  }

  Framebuffer::Framebuffer()
  {
    m_glname = m_width = m_height = 0;
  }

  Framebuffer::~Framebuffer()
  {
    destroy();
  }

  bool Framebuffer::init(const std::string& name,
      uint32_t width, uint32_t height, FramebufferBuilder builder)
  {
    m_name = name;
    m_width = width;
    m_height = height;
    glGenFramebuffers(1, &m_glname);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_glname);
    m_rbo = builder(width, height);
    for (std::vector<Renderbuffer>::const_iterator iter = m_rbo.begin();
        iter != m_rbo.end(); ++iter)
    {
      glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, iter->attachment(),
          GL_RENDERBUFFER, iter->glname());
    }

    checkGLErrors();
    // TODO Awkard, we resolve the colour attachments from the user-given RB layout.
    // The user needs to know how they're mapped though...
    m_colorBuffers.resize(0);
    for (std::vector<Renderbuffer>::const_iterator iter = m_rbo.begin();
        iter != m_rbo.end(); ++iter)
    {
      if (iter->attachment() >= GL_COLOR_ATTACHMENT0 &&
          iter->attachment() <= GL_COLOR_ATTACHMENT15)
      {
        m_colorBuffers.push_back(iter->attachment());
      }
    }

    checkGLErrors();
    return isReady();
  }

  bool Framebuffer::isReady()
  {
    static std::map<GLenum, std::string> codes;
    if (codes.empty())
    {
      codes[GL_FRAMEBUFFER_UNDEFINED] = "undefined";
      codes[GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT] = "incomplete-attachment";
      codes[GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT] = "incomplete-missing-attachment";
      codes[GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER] = "incomplete-draw-buffer";
      codes[GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER] = "incomplete-read-buffer";
      codes[GL_FRAMEBUFFER_UNSUPPORTED] = "unsupported";
      codes[GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE] = "incomplete-multisample";
      codes[GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS] = "incomplete-layer-targets";
    }
    GLenum status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
    if (status == GL_FRAMEBUFFER_COMPLETE)
    {
      return true;
    }
    std::cerr << "Framebuffer " << name() << " error: " <<
      codes[status] << "\n";
    return false;
  }

  void Framebuffer::destroy()
  {
    if (0 == m_glname) return;

    std::for_each(m_rbo.begin(), m_rbo.end(),
        boost::bind(&Renderbuffer::destroy, _1));
    glDeleteFramebuffers(1, &m_glname);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  }

  void Framebuffer::useWriting()
  {
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_glname);
    glDrawBuffers(m_colorBuffers.size(), &m_colorBuffers[0]);
  }

  //todo - ease association of read-buffer -> render-buffer
  void Framebuffer::useReading(uint32_t renderBufferIndex)
  {
    glBindFramebuffer(GL_READ_FRAMEBUFFER, m_glname);
    glReadBuffer(m_rbo[renderBufferIndex].attachment());
  }
}

max_stars_repo_name: danielgrigg/sandbox

max_issues_repo_name: danielgrigg/sandbox

max_forks_repo_name: danielgrigg/sandbox


text:
#include <frovedis.hpp>
#include <frovedis/matrix/pblas_wrapper.hpp>

#define BOOST_TEST_MODULE FrovedisTest
#include <boost/test/unit_test.hpp>
#include "../../rmse.hpp"

using namespace frovedis;
using namespace std;

BOOST_AUTO_TEST_CASE( frovedis_test )
{
    int argc = 1;
    char** argv = NULL;
    use_frovedis use(argc, argv);

    // creating blockcyclic matrix from file
    auto bm1 = make_blockcyclic_matrix_load<float> ("./sample_4x4");
    auto bm2 = bm1;

    // slicing rows
    auto row1 = make_row_vector<float> (bm1,0);
    auto row2 = make_row_vector<float> (bm1,1);
    auto row3 = make_row_vector<float> (bm1,2);
    auto row4 = make_row_vector<float> (bm1,3);

    // row3-of-bm1 = 5*bm2*row1-of-bm1
    gemv<float>(bm2,row1,row3,'N',5.0);

    // row4-of-bm1 = 2*trans(bm2)*row2-of-bm1 + 3*row4-of-bm1
    gemv<float>(bm2,row2,row4,'T',2.0,3.0);
    bm1.save("./out");

    double tol = 0.01;
    auto out = make_rowmajor_matrix_local_load<float> ("./out");
    auto ref = make_rowmajor_matrix_local_load<float> ("./ref_4x4");
    BOOST_CHECK (calc_rms_err<float> (out.val, ref.val) < tol);
    system("rm -f ./out");
}

max_stars_repo_name: XpressAI/frovedis

max_issues_repo_name: XpressAI/frovedis

max_forks_repo_name: XpressAI/frovedis


text:
/*
 * Copyright (c) 2019 Opticks Team. All Rights Reserved.
 *
 * This file is part of Opticks
 * (see https://bitbucket.org/simoncblyth/opticks).
 *
 * 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>

#include "BMap.hh"

#include "OPTICKS_LOG.hh"
#include <boost/property_tree/ptree.hpp>

#include "BJSONParser.hh"

const char* pathSU = "$TMP/BMapTestSU.json" ;
const char* pathUS = "$TMP/BMapTestUS.json" ;
const char* pathSUi = "$TMP/BMapTestSU.ini" ;
const char* pathSSi = "$TMP/BMapTestSS.ini" ;

void test_saveMapSU()
{
   std::map<std::string, unsigned int> index ;
   index["/prefix/red"] = 1 ;
   index["/prefix/green"] = 2 ;
   index["/prefix/blue"] = 3 ;

   BMap<std::string,unsigned int>::save(&index, pathSU );
   BMap<std::string,unsigned int>::save(&index, pathSUi );
}
void test_loadMapSU()
{
   std::map<std::string, unsigned int> index ;
   BMap<std::string, unsigned int>::load(&index, pathSU );
   BMap<std::string, unsigned int>::dump(&index,"loadMapSU");
}

void test_saveMapUS()
{
   std::map<unsigned int, std::string> index ;
   index[0] = "hello0" ;
   index[1] = "hello1" ;
   index[10] = "hello10" ;
   BMap<unsigned int, std::string>::save(&index, pathUS );
}
void test_loadMapUS()
{
   std::map<unsigned int, std::string> index ;
   BMap<unsigned int, std::string>::load(&index, pathUS );
   BMap<unsigned int, std::string>::dump(&index,"loadMapUS");
}

void test_saveIni()
{
   std::map<std::string, std::string> md ;
   md["red"] = "a" ;
   md["green"] = "b" ;
   md["blue"] = "c" ;

   BMap<std::string, std::string>::dump(&md, "saveIni");
   BMap<std::string, std::string>::save(&md, pathSSi );
}

void test_loadIni()
{
   std::map<std::string, std::string> md ;
   BMap<std::string, std::string>::load(&md, pathSSi );
   BMap<std::string, std::string>::dump(&md, "loadIni");
}

void test_LoadJSONString_() {

    namespace pt = boost::property_tree;

    pt::ptree t;
    std::stringstream pippo("{\"size\":1000,\"reserved\":100,\"list\": {\"122\":1,\"123\":3}}");
    // std::stringstream pippo;
    // pippo << "{\"size\":1000,\"reserved\":100,\"list\": {\"122\":1,\"123\":3}}";
    pt::read_json(pippo,t);
    pt::write_json(pippo,t,false);
    pippo.seekg(0,pippo.beg);
    pt::read_json(pippo,t);

    std::cout << "done. " << std::endl;
}

void test_LoadJSONString()
{
   std::map<std::string, unsigned> md ;

   //const char* json = "{\"hello\":\"world\"}" ;
   //const char* json = "{}" ;
   std::map<std::string, int> mattbl;
   mattbl["LS"] = 48;
   mattbl["Acrylic"] = 24;
   std::stringstream ss;

   ss << "{" << std::endl;
   for (std::map<std::string, int>::iterator it = mattbl.begin();
        it != mattbl.end(); ++it) {
       ss << "\"" << it->first << "\"" << ":" << it->second << "," << std::endl;
   }
   ss << "\"" << "ENDMAT" << "\"" << ":999999" << std::endl;
   ss << "}" << std::endl;
   std::string json_string = ss.str();
   const char* json = json_string.c_str() ;
   std::cout << json << std::endl;

   // namespace pt = boost::property_tree;

   // std::cout << "READ JSON start." << std::endl;

   // pt::ptree t;
   // std::stringstream pippo(json);
   // pt::read_json(pippo,t);
   // std::cout << "READ JSON done." << std::endl;

   BMap<std::string, unsigned>::LoadJSONString(&md, json, 0 );
   BMap<std::string, unsigned>::dump(&md, "LoadJSONString");

}

int main(int argc, char** argv)
{
    OPTICKS_LOG(argc, argv);

    LOG(info) << argv[0] ;

/*
    test_saveMapSU();
    test_loadMapSU();

    test_saveMapUS();
    test_loadMapUS();

    test_saveIni();
    test_loadIni();

*/

    test_LoadJSONString_();
    test_LoadJSONString();

    return 0 ;
}

max_stars_repo_name: hanswenzel/opticks

max_issues_repo_name: hanswenzel/opticks

max_forks_repo_name: hanswenzel/opticks


text:
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. 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. See accompanying
 * LICENSE file.
 */

/**
 *  Created on: 31 May 2014
 *      Author: swale
 */

#include "ControlConnection.h"

#include <boost/assign/list_of.hpp>
#include <boost/thread/lock_guard.hpp>

using namespace apache::thrift;
using namespace com::pivotal::gemfirexd::client;

/*
ControlConnection::ControlConnection()
{
  // TODO Auto-generated constructor stub

}

  public static final class SearchRandomServer implements TObjectProcedure {

    private final ServerType searchServerType;
    private final Set<HostAddress> failedServers;
    private HostAddress foundServer;
    private int searchIndex;

    public static final Random rand = new Random();

    SearchRandomServer(ServerType searchServerType, int setSize,
        Set<HostAddress> failedServers) {
      this.searchServerType = searchServerType;
      this.failedServers = failedServers != null ? failedServers : Collections
          .<HostAddress> emptySet();
      this.searchIndex = rand.nextInt(setSize);
    }

    public HostAddress getRandomServer() {
      return this.foundServer;
    }

    @Override
    public boolean execute(Object o) {
      if (this.searchIndex > 0) {
        this.searchIndex--;
      }
      else if (this.foundServer != null) {
        return false;
      }

      HostAddress hostAddr = (HostAddress)o;
      SanityManager.DEBUG_PRINT("SW:", "SW: random host " + hostAddr
          + " serverType=" + hostAddr.getServerType() + ", searchType="
          + this.searchServerType + ", failed servers = " + failedServers
          + ", isFailed=" + failedServers.contains(hostAddr));
      if (hostAddr.getServerType() == this.searchServerType
          && !this.failedServers.contains(hostAddr)) {
        this.foundServer = hostAddr;
      }
      return true;
    }
  }

const std::vector<AutoPtr<ControlConnection> > ControlConnection::
    s_allConnections(2);
const boost::mutex ControlConnection::s_allConnsLock;

ControlConnection::ControlConnection(const ClientService& service) :
    m_gfxdServerType(service.m_reqdServerType), m_gfxdServerTypeSet(
        boost::assign::list_of(service.m_reqdServerType)), m_locators(
            service.m_locators), m_controlHost(), m_controlLocator(),
        m_controlHosts(service.m_connHosts), m_controlHostSet(
            service.m_connHosts), m_serverGroups(service.m_serverGroups)
{
}

const AutoPtr<ControlConnection>& ControlConnection::getOrCreateControlConnection(
    const thrift::HostAddress& hostAddr, const ClientService& service)
{
  // loop through all ControlConnections since size of this global list is
  // expected to be in single digit (total number of distributed systems)
  boost::lock_guard<boost::mutex> globalGuard(s_allConnsLock);

  size_t index = s_allConnections.size();
  while (--index >= 0) {
    AutoPtr<ControlConnection>& controlService = s_allConnections[index];

    boost::lock_guard<boost::mutex> serviceGuard(
        controlService->m_lock);
    if (controlService->m_controlHostSet.find(hostAddr)
        != controlService->m_controlHostSet.end()) {
      return controlService;
    }
  }
  // if we reached here, then need to create a new ControlConnection
  AutoPtr<ControlConnection> controlService = new ControlConnection(
      service);
  std::set<thrift::HostAddress> emptyServers;
  controlService->getPreferredServer(emptyServers, true);
  s_allConnections.push_back(controlService);
  return controlService;
}

const thrift::HostAddress& ControlConnection::getPreferredServer(
    std::set<thrift::HostAddress>& failedServers,
    bool forFailover)
{
    if (m_controlLocator.get() == NULL) {
      failoverToAvailableHost(failedServers, NULL);
      forFailover = true;
    }

    while (true) {
      try {
        HostAddress preferredServer;
        if (forFailover) {
          if (SanityManager.TraceClientHA) {
            SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
                "getAllServersWithPreferredServer() trying using host "
                    + this.controlHost);
          }
          if (SanityManager.TraceClientStatement) {
            final long ns = System.nanoTime();
            SanityManager.DEBUG_PRINT_COMPACT(
                "getAllServersWithPreferredServer_S", null, 0, ns, true, null);
          }

          // refresh the full host list
          List<HostAddress> prefServerAndAllHosts = controlLocator
              .getAllServersWithPreferredServer(this.gfxdServerTypeSet,
                  this.serverGroups, failedServers);
          // refresh the new server list
          List<HostAddress> allHosts = prefServerAndAllHosts.subList(1,
              prefServerAndAllHosts.size());
          refreshAllHosts(allHosts);
          preferredServer = prefServerAndAllHosts.get(0);

          if (SanityManager.TraceClientHA) {
            SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
                "Returning all hosts " + allHosts
                    + " using control connection to " + this.controlHost);
          }
          if (SanityManager.TraceClientStatement) {
            final long ns = System.nanoTime();
            SanityManager.DEBUG_PRINT_COMPACT(
                "getAllServersWithPreferredServer_E", null, 0, ns, false,
                null);
          }
        }
        else {
          if (SanityManager.TraceClientHA) {
            SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
                "getPreferredServer() trying using host " + this.controlHost);
          }
          if (SanityManager.TraceClientStatement) {
            final long ns = System.nanoTime();
            SanityManager.DEBUG_PRINT_COMPACT("getPreferredServer_S", null,
                0, ns, true, null);
          }

          preferredServer = controlLocator.getPreferredServer(
              this.gfxdServerTypeSet, this.serverGroups, failedServers);

          if (SanityManager.TraceClientStatement) {
            final long ns = System.nanoTime();
            SanityManager.DEBUG_PRINT_COMPACT("getPreferredServer_E", null,
                0, ns, false, null);
          }
        }

        if (SanityManager.TraceClientHA) {
          SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
              "Got preferred server " + preferredServer
                  + " using control connection to " + this.controlHost
                  + (preferredServer.getPort() <= 0 ? "" : " (trying random "
                      + "server since no preferred server received)"));
        }
        if (preferredServer.getPort() <= 0) {
          // for this case we don't have a locator so choose some server
          // randomly as the "preferredServer"
          SearchRandomServer search = new SearchRandomServer(this.gfxdServerType,
              this.controlHostSet.size(), failedServers);
          this.controlHostSet.forEach(search);
          if ((preferredServer = search.getRandomServer()) == null) {
            throw failoverExhausted(null);
          }
        }
        if (SanityManager.TraceClientHA) {
          SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
              "Returning preferred server " + preferredServer
                  + " with current control connection to " + this.controlHost);
        }
        return preferredServer;
      } catch (TException te) {
        if (SanityManager.TraceClientHA) {
          SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
              "getPreferredServer() received exception from control host "
                  + this.controlHost, te);
        }
        if (te instanceof GFXDException) {
          GFXDException gfxde = (GFXDException)te;
          NetConnection.FailoverStatus status;
          if ((status = NetConnection.getFailoverStatus(gfxde
              .getExceptionData().getSqlState(), gfxde.getExceptionData()
              .getSeverity(), gfxde)).isNone()) {
            throw gfxde;
          }
          else if (status == NetConnection.FailoverStatus.RETRY) {
            forFailover = true;
            continue;
          }
        }
        // search for a new host for locator query
        if (failedServers == null) {
          Set<HostAddress> servers = new THashSet(2);
          failedServers = servers;
        }
        failedServers.add(controlHost);
        controlLocator.getOutputProtocol().getTransport().close();
        failedServers = failoverToAvailableHost(failedServers, te);
      } catch (Throwable t) {
        throw unexpectedError(t, controlHost);
      }
      forFailover = true;
    }
  }

void ControlConnection::failoverToAvailableHost(
    std::set<thrift::HostAddress>& failedServers,
    const std::exception* failure)
{
  NEXT_SERVER: for (std::vector<thrift::HostAddress>::const_iterator iter =
      m_controlHosts.begin(); iter != m_controlHosts.end(); ++iter) {
    const thrift::HostAddress& controlAddr = *iter;
    if (!failedServers.empty() && failedServers.find(controlAddr) != failedServers.end()) {
        continue;
      }
    m_controlHost.hostName.clear();
    m_controlLocator.reset(NULL);

    boost::shared_ptr<transport::TSocket> transport;
    boost::shared_ptr<protocol::TProtocol> protocol;
      while (true) {
        try {
          if (transport.get() != NULL) {
            transport->close();
          }
          if (SanityManager.TraceClientHA) {
            SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
                "Trying control connection to host " + controlAddr);
          }
          if (SanityManager.TraceClientStatement
              | SanityManager.TraceClientConn) {
            final long ns = System.nanoTime();
            SanityManager.DEBUG_PRINT_COMPACT(
                "failoverToAvailableHost_S", null, 0, ns, true,
                SanityManager.TraceClientConn ? new Throwable() : null);
          }

          // using socket with TCompactProtocol to match the server
          // settings; this could become configurable in future
          if (m_gfxdServerType.isThriftSSL()) {
            transport = new GfxdTSSLSocket(controlAddr, this.sslParams);
          }
          else {
            transport = new GfxdTSocket(controlAddr);
          }
          if (this.gfxdServerType.isThriftBinaryProtocol()) {
            protocol = new TBinaryProtocol(transport);
          }
          else {
            protocol = new TCompactProtocol(transport);
          }
          break;
        } catch (TException te) {
          if (SanityManager.TraceClientHA) {
            SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
                "Received exception in control connection to host "
                    + controlAddr, te);
          }
          failure = te;
          // search for a new host for locator query
          if (failedServers == null) {
            @SuppressWarnings("unchecked")
            Set<HostAddress> servers = new THashSet(2);
            failedServers = servers;
          }
          failedServers.add(controlAddr);
          if (transport != null) {
            transport.close();
          }
          continue NEXT_SERVER;
        } catch (Throwable t) {
          throw unexpectedError(t, controlAddr);
        }
      }
      this.controlHost = controlAddr;
      this.controlLocator = new LocatorService.Client(protocol);

      if (SanityManager.TraceClientHA) {
        SanityManager.DEBUG_PRINT(SanityManager.TRACE_CLIENT_HA,
            "Established control connection to host " + controlAddr);
      }
      if (SanityManager.TraceClientStatement
          | SanityManager.TraceClientConn) {
        final long ns = System.nanoTime();
        SanityManager.DEBUG_PRINT_COMPACT(
            "failoverToAvailableHost_E", null, 0, ns, false,
            SanityManager.TraceClientConn ? new Throwable() : null);
      }
      return failedServers;
    }
    throw failoverExhausted(failure);
  }

  private void refreshAllHosts(List<HostAddress> allHosts) {
    // refresh the new server list

    // we remove all from the set and re-populate since we would like
    // to prefer the ones coming as "allServers" with "isServer" flag
    // correctly set rather than the ones in "secondary-locators"
    this.controlHostSet.clear();
    this.controlHosts.subList(this.locators.size(), this.controlHosts.size())
        .clear();

    this.controlHostSet.addAll(allHosts);
    this.controlHostSet.addAll(this.controlHosts);

    this.controlHosts.addAll(allHosts);
  }

  private GFXDException unexpectedError(Throwable t, HostAddress host) {
    this.controlHost = null;
    if (this.controlLocator != null) {
      this.controlLocator.getOutputProtocol().getTransport().close();
      this.controlLocator = null;
    }
    return ClientExceptionUtil.newGFXDException(SQLState.JAVA_EXCEPTION, t,
        t.getClass(), t.getMessage() + " (Server=" + host + ')');
  }

  private GFXDException failoverExhausted(Throwable cause) {
    return ClientExceptionUtil.newGFXDException(
        SQLState.DATA_CONTAINER_CLOSED, cause, this.locators.get(0),
            " {failed after trying all available servers: " + controlHostSet
            + (this.locators.size() > 1 ? ", secondary-locators="
                + this.locators.subList(1, this.locators.size()) : "")
                + (cause instanceof TException ? " with: " + ThriftExceptionUtil
                    .getExceptionString(cause) + '}' : "}"));
  }
}
*/

max_stars_repo_name: gemxd/gemfirexd-oss

max_issues_repo_name: vmware-archive/gemfirexd-oss

max_forks_repo_name: gemxd/gemfirexd-oss


text:
/**
 *  @file
 *  @copyright Block Republic
 */
 #include <string>

 #include <eosio/eosio.hpp>
 #include <eosio/asset.hpp>

 #define EOS_SYMBOL symbol("EOS", 4)

#include <boost/container/flat_map.hpp>
#include <cmath>

namespace eosio {
  class [[eosio::contract("eossubaccount")]] SubAccounts : public eosio::contract {

  using contract::contract;

  const symbol token_symbol; // The symbol for the currency we play for

  public:
    SubAccounts(name receiver, name code, datastream<const char *> ds) : contract(receiver, code, ds),token_symbol("SYS", 4){}

    // From https://eosio.stackexchange.com/a/4610
    [[eosio::on_notify("*::transfer")]] void ontransfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo);
    [[eosio::on_notify("eosio.token::transfer")]] void dummytansfer(eosio::name from, eosio::name to, eosio::asset quantity, std::string memo){ontransfer(from,to,quantity,memo);} // This is a hack, otherwise the ontransfer function won't work

    // https://eosio.stackexchange.com/questions/4381/why-is-eosioon-notifyeosio-tokentransfer-not-working
    using transfer_action = eosio::action_wrapper<eosio::name("transfer"), &SubAccounts::ontransfer>;

    [[eosio::action]]
    void withdraw( name  from, asset amount, std::string memo);
    // Withdraw from the subscription service

    [[eosio::action]]
    void openaccount(name  user, name payer);
    // User opens account on this contract.
    // user = the user who owns the account
    // payer = whomever is paying for the user's RAM

    [[eosio::action]]
    void closeacc(name  user);
    // CLose user account if balance is zero and free any outstanding RAM
    // user = the user who owns the account

  public:
    struct [[eosio::table]] balance {
      asset         funds;

      auto primary_key() const { return funds.symbol.raw(); };
    };

    using balance_table = eosio::multi_index<"balance"_n, balance>;;

  };
}

max_stars_repo_name: blockrepublictech/eos-token-subaccounts

max_issues_repo_name: blockrepublictech/eos-token-subaccounts

max_forks_repo_name: blockrepublictech/eos-token-subaccounts


text:
/*
 * Copyright 2017 MapD Technologies, 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.
 */

#include "InValuesBitmap.h"
#include "CodeGenerator.h"
#include "Execute.h"
#ifdef HAVE_CUDA
#include "GpuMemUtils.h"
#endif  // HAVE_CUDA
#include "../Parser/ParserNode.h"
#include "../Shared/checked_alloc.h"
#include "GroupByAndAggregate.h"
#include "Logger/Logger.h"
#include "RuntimeFunctions.h"

#include <boost/multiprecision/cpp_int.hpp>
#include <limits>

using checked_int64_t = boost::multiprecision::number<
    boost::multiprecision::cpp_int_backend<64,
                                           64,
                                           boost::multiprecision::signed_magnitude,
                                           boost::multiprecision::checked,
                                           void>>;

InValuesBitmap::InValuesBitmap(const std::vector<int64_t>& values,
                               const int64_t null_val,
                               const Data_Namespace::MemoryLevel memory_level,
                               const int device_count,
                               Data_Namespace::DataMgr* data_mgr)
    : rhs_has_null_(false)
    , null_val_(null_val)
    , memory_level_(memory_level)
    , device_count_(device_count)
    , data_mgr_(data_mgr) {
#ifdef HAVE_CUDA
  CHECK(memory_level_ == Data_Namespace::CPU_LEVEL ||
        memory_level == Data_Namespace::GPU_LEVEL);
#else
  CHECK_EQ(Data_Namespace::CPU_LEVEL, memory_level_);
#endif  // HAVE_CUDA
  if (values.empty()) {
    return;
  }
  min_val_ = std::numeric_limits<int64_t>::max();
  max_val_ = std::numeric_limits<int64_t>::min();
  for (const auto value : values) {
    if (value == null_val) {
      rhs_has_null_ = true;
      continue;
    }
    if (value < min_val_) {
      min_val_ = value;
    }
    if (value > max_val_) {
      max_val_ = value;
    }
  }
  if (max_val_ < min_val_) {
    CHECK_EQ(std::numeric_limits<int64_t>::max(), min_val_);
    CHECK_EQ(std::numeric_limits<int64_t>::min(), max_val_);
    CHECK(rhs_has_null_);
    return;
  }
  const int64_t MAX_BITMAP_BITS{8 * 1000 * 1000 * 1000LL};
  const auto bitmap_sz_bits =
      static_cast<int64_t>(checked_int64_t(max_val_) - min_val_ + 1);
  if (bitmap_sz_bits > MAX_BITMAP_BITS) {
    throw FailedToCreateBitmap();
  }
  const auto bitmap_sz_bytes = bitmap_bits_to_bytes(bitmap_sz_bits);
  auto cpu_bitset = static_cast<int8_t*>(checked_calloc(bitmap_sz_bytes, 1));
  for (const auto value : values) {
    if (value == null_val) {
      continue;
    }
    agg_count_distinct_bitmap(reinterpret_cast<int64_t*>(&cpu_bitset), value, min_val_);
  }
#ifdef HAVE_CUDA
  if (memory_level_ == Data_Namespace::GPU_LEVEL) {
    for (int device_id = 0; device_id < device_count_; ++device_id) {
      auto device_allocator = data_mgr_->createGpuAllocator(device_id);
      gpu_buffers_.emplace_back(
          data_mgr->alloc(Data_Namespace::GPU_LEVEL, device_id, bitmap_sz_bytes));
      auto gpu_bitset = gpu_buffers_.back()->getMemoryPtr();
      device_allocator->copyToDevice(gpu_bitset, cpu_bitset, bitmap_sz_bytes);
      bitsets_.push_back(gpu_bitset);
    }
    free(cpu_bitset);
  } else {
    bitsets_.push_back(cpu_bitset);
  }
#else
  CHECK_EQ(1, device_count_);
  bitsets_.push_back(cpu_bitset);
#endif  // HAVE_CUDA
}

InValuesBitmap::~InValuesBitmap() {
  if (bitsets_.empty()) {
    return;
  }
  if (memory_level_ == Data_Namespace::CPU_LEVEL) {
    CHECK_EQ(size_t(1), bitsets_.size());
    free(bitsets_.front());
  } else {
    CHECK(data_mgr_);
    for (auto& gpu_buffer : gpu_buffers_) {
      data_mgr_->free(gpu_buffer);
    }
  }
}

llvm::Value* InValuesBitmap::codegen(llvm::Value* needle, Executor* executor) const {
  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
  std::vector<std::shared_ptr<const Analyzer::Constant>> constants_owned;
  std::vector<const Analyzer::Constant*> constants;
  for (const auto bitset : bitsets_) {
    const int64_t bitset_handle = reinterpret_cast<int64_t>(bitset);
    const auto bitset_handle_literal = std::dynamic_pointer_cast<Analyzer::Constant>(
        Parser::IntLiteral::analyzeValue(bitset_handle));
    CHECK(bitset_handle_literal);
    CHECK_EQ(kENCODING_NONE, bitset_handle_literal->get_type_info().get_compression());
    constants_owned.push_back(bitset_handle_literal);
    constants.push_back(bitset_handle_literal.get());
  }
  const auto needle_i64 = executor->cgen_state_->castToTypeIn(needle, 64);
  const auto null_bool_val =
      static_cast<int8_t>(inline_int_null_val(SQLTypeInfo(kBOOLEAN, false)));
  if (bitsets_.empty()) {
    return executor->cgen_state_->emitCall("bit_is_set",
                                           {executor->cgen_state_->llInt(int64_t(0)),
                                            needle_i64,
                                            executor->cgen_state_->llInt(int64_t(0)),
                                            executor->cgen_state_->llInt(int64_t(0)),
                                            executor->cgen_state_->llInt(null_val_),
                                            executor->cgen_state_->llInt(null_bool_val)});
  }
  CodeGenerator code_generator(executor);
  const auto bitset_handle_lvs =
      code_generator.codegenHoistedConstants(constants, kENCODING_NONE, 0);
  CHECK_EQ(size_t(1), bitset_handle_lvs.size());
  return executor->cgen_state_->emitCall(
      "bit_is_set",
      {executor->cgen_state_->castToTypeIn(bitset_handle_lvs.front(), 64),
       needle_i64,
       executor->cgen_state_->llInt(min_val_),
       executor->cgen_state_->llInt(max_val_),
       executor->cgen_state_->llInt(null_val_),
       executor->cgen_state_->llInt(null_bool_val)});
}

bool InValuesBitmap::isEmpty() const {
  return bitsets_.empty();
}

bool InValuesBitmap::hasNull() const {
  return rhs_has_null_;
}

max_stars_repo_name: Pahandrovich/omniscidb

max_issues_repo_name: Pahandrovich/omniscidb

max_forks_repo_name: Pahandrovich/omniscidb