text:
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
    This is an example illustrating the use of the deep learning tools from the
    dlib C++ Library.  I'm assuming you have already read the dnn_mnist_ex.cpp
    example.  So in this example program I'm going to go over a number of more
    advanced parts of the API, including:
        - Using multiple GPUs
        - Training on large datasets that don't fit in memory
        - Defining large networks
        - Accessing and configuring layers in a network
*/

#include <dlib/dnn.h>
#include <iostream>
#include <dlib/data_io.h>

using namespace std;
using namespace dlib;

// ----------------------------------------------------------------------------------------

// Let's start by showing how you can conveniently define large networks.  The
// most important tool for doing this are C++'s alias templates.  These let us
// define new layer types that are combinations of a bunch of other layers.
// These will form the building blocks for more complex networks.

// So let's begin by defining the building block of a residual network (see
// Figure 2 in Deep Residual Learning for Image Recognition by He, Zhang, Ren,
// and Sun).  You can see a few things in this statement.  The most obvious is
// that we have combined a bunch of layers into the name "base_res".  You can
// also see the use of the tag1 layer.  This layer doesn't do any computation.
// It exists solely so other layers can refer to it.  In this case, the
// add_prev1 layer looks for the tag1 layer and will take the tag1 output and
// add it to the input of the add_prev1 layer.  This combination allows us to
// implement skip and residual style networks.
template <int stride, typename SUBNET>
using base_res  = relu<add_prev1<bn_con<con<8,3,3,1,1,relu<bn_con<con<8,3,3,stride,stride,tag1<SUBNET>>>>>>>>;

// Let's also define the same block but with all the batch normalization layers
// replaced with affine transform layers.  We will use this type of construction
// when testing our networks.
template <int stride, typename SUBNET>
using base_ares = relu<add_prev1<affine<con<8,3,3,1,1,relu<affine<con<8,3,3,stride,stride,tag1<SUBNET>>>>>>>>;

// And of course we can define more alias templates based on previously defined
// alias templates.  The _down versions downsample the inputs by a factor of 2
// while the res and ares layer types don't.
template <typename SUBNET> using res       = base_res<1,SUBNET>;
template <typename SUBNET> using res_down  = base_res<2,SUBNET>;
template <typename SUBNET> using ares      = base_ares<1,SUBNET>;
template <typename SUBNET> using ares_down = base_ares<2,SUBNET>;

// Now that we have these convenient aliases, we can define a residual network
// without a lot of typing.  Note the use of a repeat layer.  This special layer
// type allows us to type repeat<9,res<SUBNET>> instead of
// res<res<res<res<res<res<res<res<res<SUBNET>>>>>>>>>.  It will also prevent
// the compiler from complaining about super deep template nesting when creating
// large networks.
const unsigned long number_of_classes = 10;
using net_type = loss_multiclass_log<fc<number_of_classes,
                            avg_pool<11,11,11,11,
                            res<res<res<res_down<
                            repeat<9,res, // repeat this layer 9 times
                            res_down<
                            res<
                            input<matrix<unsigned char>>
                            >>>>>>>>>>;

// And finally, let's define a residual network building block that uses
// parametric ReLU units instead of regular ReLU.
template <typename SUBNET>
using pres  = prelu<add_prev1<bn_con<con<8,3,3,1,1,prelu<bn_con<con<8,3,3,1,1,tag1<SUBNET>>>>>>>>;

// ----------------------------------------------------------------------------------------

int main(int argc, char** argv) try
{
    if (argc != 2)
    {
        cout << "This example needs the MNIST dataset to run!" << endl;
        cout << "You can get MNIST from http://yann.lecun.com/exdb/mnist/" << endl;
        cout << "Download the 4 files that comprise the dataset, decompress them, and" << endl;
        cout << "put them in a folder.  Then give that folder as input to this program." << endl;
        return 1;
    }

    std::vector<matrix<unsigned char>> training_images;
    std::vector<unsigned long> training_labels;
    std::vector<matrix<unsigned char>> testing_images;
    std::vector<unsigned long> testing_labels;
    load_mnist_dataset(argv[1], training_images, training_labels, testing_images, testing_labels);

    // dlib uses cuDNN under the covers.  One of the features of cuDNN is the
    // option to use slower methods that use less RAM or faster methods that use
    // a lot of RAM.  If you find that you run out of RAM on your graphics card
    // then you can call this function and we will request the slower but more
    // RAM frugal cuDNN algorithms.
    set_dnn_prefer_smallest_algorithms();

    // Create a network as defined above.  This network will produce 10 outputs
    // because that's how we defined net_type.  However, fc layers can have the
    // number of outputs they produce changed at runtime.
    net_type net;
    // So if you wanted to use the same network but override the number of
    // outputs at runtime you can do so like this:
    net_type net2(num_fc_outputs(15));

    // Now, let's imagine we wanted to replace some of the relu layers with
    // prelu layers.  We might do it like this:
    using net_type2 = loss_multiclass_log<fc<number_of_classes,
                                avg_pool<11,11,11,11,
                                pres<res<res<res_down< // 2 prelu layers here
                                tag4<repeat<9,pres,    // 9 groups, each containing 2 prelu layers
                                res_down<
                                res<
                                input<matrix<unsigned char>>
                                >>>>>>>>>>>;

    // prelu layers have a floating point parameter.  If you want to set it to
    // something other than its default value you can do so like this:
    net_type2 pnet(prelu_(0.2),
                   prelu_(0.25),
                   repeat_group(prelu_(0.3),prelu_(0.4)) // Initialize all the prelu instances in the repeat
                                                         // layer.  repeat_group() is needed to group the
                                                         // things that are part of repeat's block.
                   );
    // As you can see, a network will greedily assign things given to its
    // constructor to the layers inside itself.  The assignment is done in the
    // order the layers are defined, but it will skip layers where the
    // assignment doesn't make sense.

    // Now let's print the details of the pnet to the screen and inspect it.
    cout << "The pnet has " << pnet.num_layers << " layers in it." << endl;
    cout << pnet << endl;
    // These print statements will output this (I've truncated it since it's
    // long, but you get the idea):
    /*
        The pnet has 125 layers in it.
        layer<0>      loss_multiclass_log
        layer<1>      fc       (num_outputs=10)
        layer<2>      avg_pool (nr=11, nc=11, stride_y=11, _stride_x=11)
        layer<3>      prelu    (initial_param_value=0.2)
        layer<4>      add_prev
        layer<5>      bn_con
        layer<6>      con      (num_filters=8, nr=3, nc=3, stride_y=1, stride_x=1)
        layer<7>      prelu    (initial_param_value=0.25)
        layer<8>      bn_con
        layer<9>      con      (num_filters=8, nr=3, nc=3, stride_y=1, stride_x=1)
        layer<10>     tag1
        ...
        layer<33>     con      (num_filters=8, nr=3, nc=3, stride_y=2, stride_x=2)
        layer<34>     tag1
        layer<35>     tag4
        layer<36>     prelu    (initial_param_value=0.3)
        layer<37>     add_prev
        layer<38>     bn_con
        ...
        layer<114>    con      (num_filters=8, nr=3, nc=3, stride_y=2, stride_x=2)
        layer<115>    tag1
        layer<116>    relu
        layer<117>    add_prev
        layer<118>    bn_con
        layer<119>    con      (num_filters=8, nr=3, nc=3, stride_y=1, stride_x=1)
        layer<120>    relu
        layer<121>    bn_con
        layer<122>    con      (num_filters=8, nr=3, nc=3, stride_y=1, stride_x=1)
        layer<123>    tag1
        layer<124>    input<matrix>
    */

    // Now that we know the index numbers for each layer, we can access them
    // individually using layer<index>(pnet).  For example, to access the output
    // tensor for the first prelu layer we can say:
    layer<3>(pnet).get_output();
    // Or to print the prelu parameter for layer 7 we can say:
    cout << "prelu param: "<< layer<7>(pnet).layer_details().get_initial_param_value() << endl;

    // We can also access layers by their type.  This next statement finds the
    // first tag1 layer in pnet, and is therefore equivalent to calling
    // layer<10>(pnet):
    layer<tag1>(pnet);
    // The tag layers don't do anything at all and exist simply so you can tag
    // parts of your network and access them by layer<tag>().  You can also
    // index relative to a tag.  So for example, to access the layer immediately
    // after tag4 you can say:
    layer<tag4,1>(pnet); // Equivalent to layer<35+1>(pnet).

    // Or to access the layer 2 layers after tag4:
    layer<tag4,2>(pnet);
    // Tagging is a very useful tool for making complex network structures.  For
    // example, the add_prev1 layer is implemented internally by using a call to
    // layer<tag1>().

    // Ok, that's enough talk about defining and inspecting networks.  Let's
    // talk about training networks!

    // The dnn_trainer will use SGD by default, but you can tell it to use
    // different solvers like adam.
    dnn_trainer<net_type,adam> trainer(net,adam(0.001));
    // Also, if you have multiple graphics cards you can tell the trainer to use
    // them together to make the training faster.  For example, replacing the
    // above constructor call with this one would cause it to use GPU cards 0
    // and 1.
    //dnn_trainer<net_type,adam> trainer(net,adam(0.001), {0,1});

    trainer.be_verbose();
    trainer.set_synchronization_file("mnist_resnet_sync", std::chrono::seconds(100));
    // While the trainer is running it keeps an eye on the training error.  If
    // it looks like the error hasn't decreased for the last 2000 iterations it
    // will automatically reduce the step size by 0.1.  You can change these
    // default parameters to some other values by calling these functions.  Or
    // disable them entirely by setting the shrink amount to 1.
    trainer.set_iterations_without_progress_threshold(2000);
    trainer.set_step_size_shrink_amount(0.1);

    // Now, what if your training dataset is so big it doesn't fit in RAM?  You
    // make mini-batches yourself, any way you like, and you send them to the
    // trainer by repeatedly calling trainer.train_one_step().
    //
    // For example, the loop below stream MNIST data to out trainer.
    std::vector<matrix<unsigned char>> mini_batch_samples;
    std::vector<unsigned long> mini_batch_labels;
    dlib::rand rnd(time(0));
    // Loop until the trainer's automatic shrinking has shrunk the step size by
    // 1e-3.  For the default shrinks amount of 0.1 this means stop after it
    // shrinks it 3 times.
    while(trainer.get_step_size() >= 1e-3)
    {
        mini_batch_samples.clear();
        mini_batch_labels.clear();

        // make a 128 image mini-batch
        while(mini_batch_samples.size() < 128)
        {
            auto idx = rnd.get_random_32bit_number()%training_images.size();
            mini_batch_samples.push_back(training_images[idx]);
            mini_batch_labels.push_back(training_labels[idx]);
        }

        trainer.train_one_step(mini_batch_samples, mini_batch_labels);
    }

    // When you call train_one_step(), the trainer will do its processing in a
    // separate thread.  This allows the main thread to work on loading data
    // while the trainer is busy executing the mini-batches in parallel.
    // However, this also means we need to wait for any mini-batches that are
    // still executing to stop before we mess with the net object.  Calling
    // get_net() performs the necessary synchronization.
    trainer.get_net();

    net.clean();
    serialize("mnist_res_network.dat") << net;

    // Now we have a trained network.  However, it has batch normalization
    // layers in it.  As is customary, we should replace these with simple
    // affine layers before we use the network.  This can be accomplished by
    // making a network type which is identical to net_type but with the batch
    // normalization layers replaced with affine.  For example:
    using test_net_type = loss_multiclass_log<fc<number_of_classes,
                                avg_pool<11,11,11,11,
                                ares<ares<ares<ares_down<
                                repeat<9,res,
                                ares_down<
                                ares<
                                input<matrix<unsigned char>>
                                >>>>>>>>>>;
    // Then we can simply assign our trained net to our testing net.
    test_net_type tnet = net;
    // Or if you only had a file with your trained network you could deserialize
    // it directly into your testing network.
    deserialize("mnist_res_network.dat") >> tnet;

    // And finally, we can run the testing network over our data.

    std::vector<unsigned long> predicted_labels = tnet(training_images);
    int num_right = 0;
    int num_wrong = 0;
    for (size_t i = 0; i < training_images.size(); ++i)
    {
        if (predicted_labels[i] == training_labels[i])
            ++num_right;
        else
            ++num_wrong;

    }
    cout << "training num_right: " << num_right << endl;
    cout << "training num_wrong: " << num_wrong << endl;
    cout << "training accuracy:  " << num_right/(double)(num_right+num_wrong) << endl;

    predicted_labels = tnet(testing_images);
    num_right = 0;
    num_wrong = 0;
    for (size_t i = 0; i < testing_images.size(); ++i)
    {
        if (predicted_labels[i] == testing_labels[i])
            ++num_right;
        else
            ++num_wrong;

    }
    cout << "testing num_right: " << num_right << endl;
    cout << "testing num_wrong: " << num_wrong << endl;
    cout << "testing accuracy:  " << num_right/(double)(num_right+num_wrong) << endl;

}
catch(std::exception& e)
{
    cout << e.what() << endl;
}

max_stars_repo_name: clcarwin/dlib-smalldb

max_issues_repo_name: clcarwin/dlib-smalldb

max_forks_repo_name: clcarwin/dlib-smalldb


text:
#define BOOST_TEST_MODULE pcraster com clone
#include <boost/test/unit_test.hpp>
#include "com_functions.h"

BOOST_AUTO_TEST_CASE(minimum_)
{
  using namespace com;

  double min;
  double mv;
  pcr::setMV(mv);

  {
    double from[] = {  2.0,  5.0,  3.0 };
    min = minimum(from, from + 3);
    BOOST_CHECK(min ==  2.0);
  }

  {
    double from[] = { mv, mv, mv };
    min = minimum(from, from + 3);
    BOOST_CHECK(pcr::isMV(min));
  }
}

BOOST_AUTO_TEST_CASE(maximum_)
{
  using namespace com;

  double max;
  double mv;
  pcr::setMV(mv);

  {
    double from[] = {  2.0,  5.0,  3.0 };
    max = maximum(from, from + 3);
    BOOST_CHECK(max == 5.0);
  }

  {
    double from[] = { mv, mv, mv };
    max = maximum(from, from + 3);
    BOOST_CHECK(pcr::isMV(max));
  }
}

ext: cc

max_stars_repo_name: quanpands/wflow

max_issues_repo_name: quanpands/wflow

max_forks_repo_name: quanpands/wflow


text:
#pragma once
#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/range/iterator_range.hpp>

#include <qtor/types.hpp>
#include <qtor/model_meta.hpp>

namespace qtor
{
	template <class type>
	struct field_iterator_proxy
	{
		using reference  = type &;
		using pointer    = type *;

		using index_type = model_meta::index_type;
		using any_type   = model_meta::any_type;

	private:
		const model_accessor<type> * meta;
		pointer valptr;
		index_type index;

	public:
		operator any_type() const { return meta->get_item(valptr, index); }
		field_iterator_proxy & operator =(const any_type & val) { meta->set_item(*valptr, index, val); return *this; }

	public:
		field_iterator_proxy(const model_accessor<type> * meta, pointer valptr, index_type index)
		    : meta(meta), valptr(valptr), index(index) {}
	};

	template <class index_iterator>
	struct field_iterator_category_helper
	{
		using type = typename std::iterator_traits<index_iterator>::iterator_category;
		static auto index(const index_iterator & it) { return *it; }
	};

	template <>
	struct field_iterator_category_helper<model_meta::index_type>
	{
		using type = std::random_access_iterator_tag;
		static auto index(model_meta::index_type idx) { return idx; }
	};

	template <class type, class index_iterator, bool is_const>
	struct field_iterator_helper
	{
		using index_type = model_meta::index_type;
		using any_type = model_meta::any_type;
		using any_type_ref = std::conditional_t<is_const, const any_type &, any_type &>;

		using value_type = any_type;
		using reference = std::conditional_t<is_const, any_type, field_iterator_proxy<type>>;
		using type_pointer = std::conditional_t<is_const, const type *, type *>;
		using type_reference = std::conditional_t<is_const, const type &, type &>;
		using iterator_category = typename field_iterator_category_helper<index_iterator>::type;

		static index_type index(const index_iterator & it) noexcept { return field_iterator_category_helper<index_iterator>::index(it); }
		static auto dereference(const model_accessor<type> * meta, const type * valptr, index_type idx) { return meta->get_item(*valptr, idx); }
		static auto dereference(const model_accessor<type> * meta,       type * valptr, index_type idx) { return reference(meta, valptr, idx); }
	};

	template <class type, class index_iterator, bool is_const>
	class field_iterator_base :
		public boost::iterator_facade<
	        field_iterator_base<type, index_iterator, is_const>,
	        typename field_iterator_helper<type, index_iterator, is_const>::value_type,
	        typename field_iterator_helper<type, index_iterator, is_const>::iterator_category,
	        typename field_iterator_helper<type, index_iterator, is_const>::reference
		>
	{
		using self_type = field_iterator_base;
		using helper = field_iterator_helper<type, index_iterator, is_const>;
		using base_type = boost::iterator_facade<
			self_type,
			typename helper::value_type,
			typename helper::iterator_category,
			typename helper::reference
		>;

		friend boost::iterator_core_access;
		friend field_iterator_base<type, index_iterator, true>;

	public:
		using type_pointer = typename helper::type_pointer;
		using type_reference = typename helper::type_reference;
		using index_type = model_meta::index_type;

		using typename base_type::difference_type;
		using typename base_type::reference;

	private:
		const model_accessor<type> * m_meta;
		type_pointer m_valptr;
		index_iterator m_index;

	public:
		auto index() const noexcept { return helper::index(m_index); }
		auto meta()  const noexcept { return m_meta; }

	private:
		void increment() noexcept { ++m_index; }
		void decrement() noexcept { --m_index; }

		void advance(difference_type n) noexcept { m_index += n; }

		auto dereference() const -> reference { return helper::dereference(m_meta, m_valptr, index()); }
		auto distance_to(const self_type & it) const noexcept -> difference_type { return it.m_index - m_index; }
		bool equal(const self_type & it) const noexcept { return m_valptr == it.m_valptr and m_index == it.m_index; }

	public:
		field_iterator_base(const model_accessor<type> * meta, type_reference valref, index_iterator idx)
		    : m_meta(meta), m_valptr(&valref), m_index(idx) {}
	};

	template <class type, class index_iterator> using const_field_iterator = field_iterator_base<type, index_iterator, true>;
	template <class type, class index_iterator> using       field_iterator = field_iterator_base<type, index_iterator, false>;

	template <class type, class index_iterator> using const_field_range = boost::iterator_range<const_field_iterator<type, index_iterator>>;
	template <class type, class index_iterator> using       field_range = boost::iterator_range<      field_iterator<type, index_iterator>>;

	template <class type>
	auto make_field_range(const model_accessor<type> * meta, const type & valref)
	{
		using iterator = const_field_iterator<type, model_meta::index_type>;
		iterator first(meta, valref, 0);
		iterator last(meta, valref, meta->item_count());

		return boost::make_iterator_range(first, last);
	}

	template <class type>
	auto make_editable_field_range(const model_accessor<type> * meta, type & valref)
	{
		const model_meta::index_type * first_writable, * last_writable;
		std::tie(first_writable, last_writable) = meta->true_fields();

		using iterator = field_iterator<type, const model_meta::index_type *>;
		iterator first(meta, valref, first_writable);
		iterator last(meta, valref, last_writable);

		return boost::make_iterator_range(first, last);
	}
}

ext: hpp

max_stars_repo_name: dmlys/qtor

max_issues_repo_name: dmlys/qtor

max_forks_repo_name: dmlys/qtor


text:
#include <beluga/http/http_method.hpp>
#include <boost/algorithm/string.hpp>

beluga::http_method beluga::to_http_method(const std::string& string)
{
    std::string temp = boost::to_upper_copy<std::string>(string);

    if(temp == "GET")
	return http_method::GET;
    else if(temp == "POST")
	return http_method::POST;
    else if(temp == "HEAD")
	return http_method::HEAD;
    else if(temp == "PATCH")
	return http_method::PATCH;
    else if(temp == "PUT")
	return http_method::PUT;
    else if(temp == "DELETE")
	return http_method::DELETE;
    else if(temp == "CONNECT")
	return http_method::CONNECT;
    else if(temp == "OPTIONS")
	return http_method::OPTIONS;
    else if(temp == "TRACE")
	return http_method::TRACE;

    return http_method::UNKNOWN;
}
std::string beluga::to_string(http_method method)
{
    switch(method)
    {
    case http_method::GET:
	return "GET";

    case http_method::POST:
	return "POST";

    case http_method::HEAD:
	return "HEAD";

    case http_method::PATCH:
	return "PATCH";

    case http_method::PUT:
	return "PUT";

    case http_method::DELETE:
	return "DELETE";

    case http_method::CONNECT:
	return "CONNECT";

    case http_method::OPTIONS:
	return "OPTIONS";

    case http_method::TRACE:
	return "TRACE";
    }

    return "";
}

ext: cpp

max_stars_repo_name: Stazer/beluga

max_issues_repo_name: Stazer/beluga

max_forks_repo_name: Stazer/beluga


text: #include <boost/range/algorithm_ext/insert.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include <Tanker/AsyncCore.hpp>

#include <Tanker/Encryptor.hpp>
#include <Tanker/Errors/DeviceUnusable.hpp>
#include <Tanker/Log/LogHandler.hpp>
#include <Tanker/Status.hpp>
#include <Tanker/Trustchain/DeviceId.hpp>
#include <Tanker/Types/VerificationKey.hpp>
#include <Tanker/Utils.hpp>
#include <Tanker/Version.hpp>

#include <mgs/base64.hpp>
#include <tconcurrent/async.hpp>
#include <tconcurrent/coroutine.hpp>
#include <tconcurrent/lazy/async.hpp>
#include <tconcurrent/lazy/sink_receiver.hpp>
#include <tconcurrent/lazy/then.hpp>

#include <boost/scope_exit.hpp>

#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

TLOG_CATEGORY(AsyncCore);

namespace Tanker
{
namespace
{
auto makeEventHandler(tc::lazy::task_canceler& taskCanceler,
                      std::function<void()> cb)
{
  auto cancelableSender = taskCanceler.wrap(
      tc::lazy::then(tc::lazy::async(tc::get_default_executor()), cb));
  return [cancelableSender = std::move(cancelableSender),
          cb = std::move(cb)]() mutable {
    cancelableSender.submit(tc::lazy::sink_receiver{});
  };
}
}

template <typename F>
auto AsyncCore::runResumable(F&& f, bool stopCheck)
{
  using Func = std::decay_t<F>;
  using ReturnValue =
      typename tc::detail::task_return_type<std::invoke_result_t<F>>::type;

  if (_stopping && !stopCheck)
    tc::make_exceptional_future<ReturnValue>(std::make_exception_ptr(
        Errors::formatEx(Errors::Errc::PreconditionFailed,
                         "the Tanker session is closing")));

  return tc::submit_to_future<ReturnValue>(_taskCanceler.wrap(tc::lazy::connect(
      tc::lazy::async(tc::get_default_executor()),
      tc::lazy::run_resumable(tc::get_default_executor(),
                              {},
                              &AsyncCore::runResumableImpl<Func>,
                              this,
                              std::forward<F>(f)))));
}

template <typename F>
std::invoke_result_t<F> AsyncCore::runResumableImpl(F f)
{
  bool isRevoked = false;
  bool isUnusable = false;
  std::exception_ptr eptr;
  try
  {
    if constexpr (std::is_same_v<std::invoke_result_t<F>, tc::cotask<void>>)
    {
      TC_AWAIT(f());
      TC_RETURN();
    }
    else
    {
      TC_RETURN(TC_AWAIT(f()));
    }
  }
  catch (Errors::DeviceUnusable const& ex)
  {
    eptr = std::current_exception();
    TERROR("Device is unusable: {}", ex.what());
    isUnusable = true;
  }
  catch (Errors::Exception const& ex)
  {
    eptr = std::current_exception();
    if (ex.errorCode() == Errors::AppdErrc::DeviceRevoked)
    {
      TINFO("Device is revoked: {}", ex.what());
      isRevoked = true;
    }
    else
      throw;
  }
  if (isRevoked)
    TC_AWAIT(handleDeviceRevocation()); // this is noreturn
  else if (isUnusable)
  {
    TC_AWAIT(handleDeviceUnrecoverable());
    std::rethrow_exception(eptr);
  }
  throw Errors::AssertionError("unreachable code in runResumable");
}

AsyncCore::AsyncCore(std::string url,
                     SdkInfo info,
                     std::string writablePath,
                     std::unique_ptr<Network::Backend> backend)
  : _core(std::move(url),
          std::move(info),
          std::move(writablePath),
          std::move(backend))
{
}

AsyncCore::~AsyncCore()
{
  assert(tc::get_default_executor().is_in_this_context());
  // stop() calls aren't put in the task canceler, so cancel it explicitly
  if (_cancelStop)
    _cancelStop();
}

tc::future<void> AsyncCore::destroy()
{
  if (tc::get_default_executor().is_in_this_context())
    return tc::sync([this] { delete this; });
  else
    return tc::async([this] { delete this; });
}

tc::future<Status> AsyncCore::start(std::string const& identity)
{
  return runResumable([=]() -> tc::cotask<Status> {
    TC_RETURN(TC_AWAIT(this->_core.start(identity)));
  });
}

tc::future<std::optional<std::string>> AsyncCore::registerIdentity(
    Verification::Verification const& verification,
    Core::VerifyWithToken withToken)
{
  return runResumable([=]() -> tc::cotask<std::optional<std::string>> {
    TC_RETURN(TC_AWAIT(this->_core.registerIdentity(verification, withToken)));
  });
}

tc::future<std::optional<std::string>> AsyncCore::verifyIdentity(
    Verification::Verification const& verification,
    Core::VerifyWithToken withToken)
{
  return runResumable([=]() -> tc::cotask<std::optional<std::string>> {
    TC_RETURN(TC_AWAIT(this->_core.verifyIdentity(verification, withToken)));
  });
}

tc::future<void> AsyncCore::stop()
{
  // Set _stopping so that we reject all new calls
  if (_stopping.exchange(true))
    tc::make_exceptional_future<void>(std::make_exception_ptr(Errors::formatEx(
        Errors::Errc::PreconditionFailed, "the Tanker session is closing")));

  auto fut = tc::async_resumable([&]() -> tc::cotask<void> {
    BOOST_SCOPE_EXIT_ALL(&)
    {
      _stopping = false;
    };

    // Terminate all calls so that we can stop and delete stuff safely. Note
    // that the current call will not be canceled because we didn't add its
    // future to the task canceler.
    _taskCanceler.terminate();

    TC_AWAIT(this->_core.stop());
  });
  _cancelStop = fut.make_canceler();

  return fut;
}

Tanker::Status AsyncCore::status() const
{
  return this->_core.status();
}

tc::future<void> AsyncCore::encrypt(
    uint8_t* encryptedData,
    gsl::span<uint8_t const> clearData,
    std::vector<SPublicIdentity> const& publicIdentities,
    std::vector<SGroupId> const& groupIds,
    Core::ShareWithSelf shareWithSelf)
{
  return runResumable([=]() -> tc::cotask<void> {
    TC_AWAIT(this->_core.encrypt(
        encryptedData, clearData, publicIdentities, groupIds, shareWithSelf));
  });
}

tc::future<void> AsyncCore::decrypt(uint8_t* decryptedData,
                                    gsl::span<uint8_t const> encryptedData)
{
  return runResumable([=]() -> tc::cotask<void> {
    TC_AWAIT(this->_core.decrypt(decryptedData, encryptedData));
  });
}

tc::future<std::vector<uint8_t>> AsyncCore::encrypt(
    gsl::span<uint8_t const> clearData,
    std::vector<SPublicIdentity> const& publicIdentities,
    std::vector<SGroupId> const& groupIds,
    Core::ShareWithSelf shareWithSelf)
{
  return runResumable([=]() -> tc::cotask<std::vector<uint8_t>> {
    TC_RETURN(TC_AWAIT(
        _core.encrypt(clearData, publicIdentities, groupIds, shareWithSelf)));
  });
}

tc::future<std::vector<uint8_t>> AsyncCore::decrypt(
    gsl::span<uint8_t const> encryptedData)
{
  return runResumable([=]() -> tc::cotask<std::vector<uint8_t>> {
    std::vector<uint8_t> decryptedData(Encryptor::decryptedSize(encryptedData));
    TC_AWAIT(_core.decrypt(decryptedData.data(), encryptedData));
    TC_RETURN(std::move(decryptedData));
  });
}

tc::future<void> AsyncCore::share(
    std::vector<SResourceId> const& resourceId,
    std::vector<SPublicIdentity> const& publicIdentities,
    std::vector<SGroupId> const& groupIds)
{
  return runResumable([=]() -> tc::cotask<void> {
    TC_AWAIT(this->_core.share(resourceId, publicIdentities, groupIds));
  });
}

tc::future<SGroupId> AsyncCore::createGroup(
    std::vector<SPublicIdentity> const& members)
{
  return runResumable([=]() -> tc::cotask<SGroupId> {
    TC_RETURN(TC_AWAIT(this->_core.createGroup(members)));
  });
}

tc::future<void> AsyncCore::updateGroupMembers(
    SGroupId const& groupId,
    std::vector<SPublicIdentity> const& usersToAdd,
    std::vector<SPublicIdentity> const& usersToRemove)
{
  return runResumable([=]() -> tc::cotask<void> {
    TC_AWAIT(
        this->_core.updateGroupMembers(groupId, usersToAdd, usersToRemove));
  });
}

tc::future<VerificationKey> AsyncCore::generateVerificationKey()
{
  return runResumable([this]() -> tc::cotask<VerificationKey> {
    TC_RETURN(TC_AWAIT(this->_core.generateVerificationKey()));
  });
}

tc::future<std::optional<std::string>> AsyncCore::setVerificationMethod(
    Verification::Verification const& method, Core::VerifyWithToken withToken)
{
  return runResumable([=]() -> tc::cotask<std::optional<std::string>> {
    TC_RETURN(TC_AWAIT(this->_core.setVerificationMethod(method, withToken)));
  });
}

tc::future<std::vector<Verification::VerificationMethod>>
AsyncCore::getVerificationMethods()
{
  return runResumable(
      [=]() -> tc::cotask<std::vector<Verification::VerificationMethod>> {
        TC_RETURN(TC_AWAIT(this->_core.getVerificationMethods()));
      });
}

tc::future<AttachResult> AsyncCore::attachProvisionalIdentity(
    SSecretProvisionalIdentity const& sidentity)
{
  return runResumable([=]() -> tc::cotask<AttachResult> {
    TC_RETURN(TC_AWAIT(this->_core.attachProvisionalIdentity(sidentity)));
  });
}

tc::future<void> AsyncCore::verifyProvisionalIdentity(
    Verification::Verification const& verification)
{
  return runResumable([=]() -> tc::cotask<void> {
    TC_AWAIT(this->_core.verifyProvisionalIdentity(verification));
  });
}

tc::future<SDeviceId> AsyncCore::deviceId() const
{
  return tc::submit_to_future<SDeviceId>(_taskCanceler.wrap(tc::lazy::then(
      tc::lazy::async(tc::get_default_executor()),
      [this] { return SDeviceId(mgs::base64::encode(_core.deviceId())); })));
}

tc::future<std::vector<Users::Device>> AsyncCore::getDeviceList()
{
  return runResumable([this]() -> tc::cotask<std::vector<Users::Device>> {
    auto devices = TC_AWAIT(this->_core.getDeviceList());
    devices.erase(
        std::remove_if(devices.begin(), devices.end(), [](auto const& device) {
          return device.isGhostDevice();
        }));
    TC_RETURN(devices);
  });
}

tc::future<void> AsyncCore::revokeDevice(SDeviceId const& deviceId)
{
  return runResumable([this, deviceId]() -> tc::cotask<void> {
    TC_AWAIT(
        this->_core.revokeDevice(base64DecodeArgument<Trustchain::DeviceId>(
            deviceId.string(), "device id")));
  });
}

void AsyncCore::connectSessionClosed(std::function<void()> cb)
{
  this->_core.setSessionClosedHandler(
      makeEventHandler(this->_taskCanceler, std::move(cb)));
}

void AsyncCore::disconnectSessionClosed()
{
  this->_core.setSessionClosedHandler(nullptr);
}

void AsyncCore::connectDeviceRevoked(std::function<void()> cb)
{
  this->_asyncDeviceRevoked =
      makeEventHandler(this->_taskCanceler, std::move(cb));
}

void AsyncCore::disconnectDeviceRevoked()
{
  this->_asyncDeviceRevoked = nullptr;
}

void AsyncCore::setLogHandler(Log::LogHandler handler)
{
  Log::setLogHandler([handler](Log::Record const& record) {
    tc::dispatch_on_thread_context([&] { handler(record); });
  });
}

uint64_t AsyncCore::encryptedSize(uint64_t clearSize)
{
  return Encryptor::encryptedSize(clearSize);
}

expected<uint64_t> AsyncCore::decryptedSize(
    gsl::span<uint8_t const> encryptedData)
{
  return tc::sync([&] { return Encryptor::decryptedSize(encryptedData); });
}

expected<SResourceId> AsyncCore::getResourceId(
    gsl::span<uint8_t const> encryptedData)
{
  return tc::sync([&] {
    return mgs::base64::encode<SResourceId>(Core::getResourceId(encryptedData));
  });
}

tc::future<Streams::EncryptionStream> AsyncCore::makeEncryptionStream(
    Streams::InputSource cb,
    std::vector<SPublicIdentity> const& suserIds,
    std::vector<SGroupId> const& sgroupIds,
    Core::ShareWithSelf shareWithSelf)
{
  return runResumable(
      [=, cb = std::move(cb)]() -> tc::cotask<Streams::EncryptionStream> {
        TC_RETURN(TC_AWAIT(this->_core.makeEncryptionStream(
            std::move(cb), suserIds, sgroupIds, shareWithSelf)));
      });
}

tc::future<Streams::DecryptionStreamAdapter> AsyncCore::makeDecryptionStream(
    Streams::InputSource cb)
{
  return runResumable(
      [this,
       cb = std::move(cb)]() -> tc::cotask<Streams::DecryptionStreamAdapter> {
        TC_RETURN(TC_AWAIT(this->_core.makeDecryptionStream(std::move(cb))));
      });
}

tc::future<EncryptionSession> AsyncCore::makeEncryptionSession(
    std::vector<SPublicIdentity> const& publicIdentities,
    std::vector<SGroupId> const& groupIds,
    Core::ShareWithSelf shareWithSelf)
{
  return runResumable([=]() -> tc::cotask<EncryptionSession> {
    TC_RETURN(TC_AWAIT(this->_core.makeEncryptionSession(
        publicIdentities, groupIds, shareWithSelf)));
  });
}

tc::cotask<void> AsyncCore::handleDeviceRevocation()
{
  // If multiple coroutines get here waiting for this lock, one will get the
  // lock and eventually call _taskCanceler.terminate() which will abort all the
  // other ones.
  auto const lock = TC_AWAIT(_quickStopSemaphore.get_scope_lock());

  std::exception_ptr deviceRevokedException;
  try
  {
    TDEBUG("Refreshing user to verify verification");
    TC_AWAIT(_core.confirmRevocation());
    TERROR("While trying to confirm revocation, didn't get any error");
    throw Errors::formatEx(
        Errors::Errc::InternalError,
        "server declared this device revoked, but it is not");
  }
  catch (Errors::Exception const& ex)
  {
    if (ex.errorCode() != Errors::Errc::DeviceRevoked)
    {
      TERROR("While trying to confirm revocation, got a different error: {}",
             ex.what());
      throw;
    }
    else
    {
      deviceRevokedException = std::current_exception();
    }
  }
  nukeAndStop();
  if (_asyncDeviceRevoked)
    _asyncDeviceRevoked();
  TINFO("Revocation handled, self-destruct complete");
  std::rethrow_exception(deviceRevokedException);
}

tc::cotask<void> AsyncCore::handleDeviceUnrecoverable()
{
  // See handleDeviceRevocation
  auto const lock = TC_AWAIT(_quickStopSemaphore.get_scope_lock());

  nukeAndStop();
}

void AsyncCore::nukeAndStop()
{
  // - This device was revoked or has been deemed unusable, we need to stop so
  // that Session gets destroyed.
  // - There might be calls in progress on this session, so we
  // must terminate() them before going on.
  // - We can't call this->stop() because the terminate() would
  // cancel this coroutine too.
  // - We must not wait on terminate() because that means waiting
  // on ourselves and deadlocking.
  _taskCanceler.terminate();
  // We have asked for termination of all running tasks, including this one.
  // From now on, we must not TC_AWAIT or we will be canceled.
  _core.nukeDatabase();
  _core.quickStop();
}

std::string const& AsyncCore::version()
{
  return TANKER_VERSION;
}

tc::future<void> AsyncCore::setHttpSessionToken(std::string token)
{
  return tc::async([this, tk = std::move(token)] {
    this->_core.setHttpSessionToken(std::move(tk));
  });
}
}

max_stars_repo_name: TankerHQ/sdk-native

max_issues_repo_name: TankerHQ/sdk-native

max_forks_repo_name: TankerHQ/sdk-native