text:
// Boost.TypeErasure library
//
// Copyright 2011 Steven Watanabe
//
// 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)
//
// $Id: custom.cpp 82978 2013-02-18 18:31:36Z steven_watanabe $

#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/concept_interface.hpp>
#include <boost/type_erasure/rebind_any.hpp>
#include <vector>

namespace mpl = boost::mpl;
using namespace boost::type_erasure;

//[custom1
/*`
    Earlier, we used __BOOST_TYPE_ERASURE_MEMBER to define
    a concept for containers that support `push_back`.  Sometimes
    this interface isn't flexible enough, however.  The library
    also provides a lower level interface that gives full
    control of the behavior.  Let's take a look at what we
    would need in order to define `has_push_back.`  First,
    we need to define the `has_push_back` template itself.  We'll
    give it two template parameters, one for the container
    and one for the element type.  This template must have
    a static member function called apply which is used
    to execute the operation.
*/

template<class C, class T>
struct has_push_back
{
    static void apply(C& cont, const T& arg) { cont.push_back(arg); }
};
//]

//[custom3
/*`
    Our second task is to customize __any so that we can call `c.push_back(10)`.
    We do this by specializing __concept_interface.
    The first argument is `has_push_back`, since we want to inject a member
    into every __any that uses the `has_push_back` concept.  The second argument,
    `Base`, is used by the library to chain multiple uses of __concept_interface
    together.  We have to inherit from it publicly.  `Base` is also used
    to get access to the full __any type.  The third argument is the placeholder
    that represents this any.  If someone used `push_back<_c, _b>`,
    we only want to insert a `push_back` member in the container,
    not the value type.  Thus, the third argument is the container
    placeholder.

    When we define `push_back` the argument type uses the metafunction
    __as_param.  This is just to handle the case where `T` is a
    placeholder.  If `T` is not a placeholder, then the metafunction
    just returns its argument, `const T&`, unchanged.
*/
namespace boost {
namespace type_erasure {
template<class C, class T, class Base>
struct concept_interface<has_push_back<C, T>, Base, C> : Base
{
    void push_back(typename as_param<Base, const T&>::type arg)
    { call(has_push_back<C, T>(), *this, arg); }
};
}
}
//]

void custom2() {
    //[custom2
    /*`
        Now, we can use this in an __any using
        __call to dispatch the operation.
    */
    std::vector<int> vec;
    any<has_push_back<_self, int>, _self&> c(vec);
    int i = 10;
    call(has_push_back<_self, int>(), c, i);
    // vec is [10].
    //]
}

void custom4() {
    //[custom4
    /*`
        Our example now becomes
    */
    std::vector<int> vec;
    any<has_push_back<_self, int>, _self&> c(vec);
    c.push_back(10);
    /*`
        which is what we want.
    */
    //]
}

//[custom
//` (For the source of the examples in this section see
//` [@boost:/libs/type_erasure/example/custom.cpp custom.cpp])
//` [custom1]
//` [custom2]
//` [custom3]
//` [custom4]
//]

ext: cpp

max_stars_repo_name: NixaSoftware/CVis

max_issues_repo_name: NixaSoftware/CVis

max_forks_repo_name: NixaSoftware/CVis


text:
// Boost.Function library

//  Copyright 2016 Peter Dimov

//  Use, modification and distribution is subject to
//  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)

#include <boost/function.hpp>
#include <boost/core/is_same.hpp>
#include <boost/core/lightweight_test_trait.hpp>

struct X
{
};

struct Y
{
};

struct Z
{
};

int main()
{
    typedef boost::function<X(Y)> F1;

    BOOST_TEST_TRAIT_TRUE(( boost::core::is_same<F1::result_type, X> ));
    BOOST_TEST_TRAIT_TRUE(( boost::core::is_same<F1::argument_type, Y> ));

    typedef boost::function<X(Y, Z)> F2;

    BOOST_TEST_TRAIT_TRUE(( boost::core::is_same<F2::result_type, X> ));
    BOOST_TEST_TRAIT_TRUE(( boost::core::is_same<F2::first_argument_type, Y> ));
    BOOST_TEST_TRAIT_TRUE(( boost::core::is_same<F2::second_argument_type, Z> ));

    return boost::report_errors();
}


text:
#include "cacos/test/generate/generator.h"

#include "cacos/executable/executable.h"

#include "cacos/lang/lang.h"

#include "cacos/test/suite/test.h"

#include "cacos/util/logger.h"
#include "cacos/util/mt/fixed_queue.h"
#include "cacos/util/progress_bar.h"
#include "cacos/util/string.h"

#include <boost/asio.hpp>

#include <boost/container/stable_vector.hpp>

#include <functional>

namespace cacos::test {

Generator::Generator(const config::Config& cfg, const GeneratorOptions& opts)
    : config_(cfg)
    , opts_(opts) {
}

void Generator::run() {
    executable::Executable exe = config_.langs().runnable(
        {opts_.generatorSources, {opts::hostArch(), opts::BuildType::release}});

    InlineVariables vars;

    executable::ExecPool pool(
        process::Limits{process::Limits::unlimited<bytes>, seconds(1.0), seconds(2.0)},
        opts_.threads);

    boost::container::stable_vector<std::string> stdIn;
    boost::container::stable_vector<fs::path> stdOut;
    boost::container::stable_vector<fs::path> stdErr;
    std::vector<std::pair<Test, fs::path>> generatedTests;

    size_t totalTasks = 0;
    size_t crashedTasks = 0;

    util::ProgressBar<size_t> bar;
    traverse(opts_.vars.begin(), vars, [&](const InlineVariables& vars) {
        ++totalTasks;

        std::string name = vars.parse(opts_.name);
        fs::path dir = config_.dir(config::DirType::cache) / name;

        if (!fs::exists(dir)) {
            fs::create_directories(dir);
        }

        stdIn.emplace_back(vars.parse(opts_.genIO.input));
        stdOut.emplace_back(dir / "gen.stdout");
        stdErr.emplace_back(dir / "gen.stderr");
        log::log().print("{}", stdIn.back());

        std::vector<std::string> testArgs;
        testArgs.reserve(opts_.testArgs.size());
        for (auto&& arg : opts_.testArgs) {
            testArgs.push_back(vars.parse(arg));
        }

        auto callback = [&, dir, name, testArgs, i = totalTasks - 1](
                            process::Result res, std::optional<process::Info>&& info) {
            if (res.status != process::status::OK || res.returnCode != 0) {
                ++crashedTasks;
            } else {
                test::Test test;
                test.name(name)
                    .type(opts_.type)
                    .args(testArgs)
                    .input(dir / opts_.testIO.input)
                    .workingDirectory(dir);
                if (opts_.type == Type::canonical) {
                    test.output(dir / opts_.testIO.output);
                }

                generatedTests.emplace_back(std::move(test), std::move(dir));
            }

            log::log().print(
                "Run {} / {}: Exit status: {}, return code = {}, cpu time = {:.3f} s, max rss = "
                "{:.3f} mb",
                i,
                totalTasks,
                process::status::serialize(res.status),
                res.returnCode,
                info->cpuTime.count(),
                info->maxRss / (1024. * 1024.));

            bar.process(1, fmt::format("Crashed tasks: {}", crashedTasks));
        };

        std::vector<std::string> args;
        args.reserve(opts_.args.size());
        for (auto&& s : opts_.args) {
            args.push_back(vars.parse(s));
        }

        executable::ExecTaskContext ctx{args, boost::this_process::environment(), dir, callback};
        auto task = executable::makeTask(
            exe,
            std::move(ctx),
            bp::buffer(std::as_const(stdIn.back())),
            stdOut.back().string(),
            stdErr.back().string());

        pool.push(std::move(task));
    });

    bar.reset(totalTasks);

    pool.run();

    util::mt::FixedQueue serializationPool;
    for (auto& p : generatedTests) {
        serializationPool.add([&p, this] {
            auto& [test, dir] = p;
            test.serialize(config_.dir(config::DirType::test), opts_.force);
            fs::remove_all(dir);
        });
    }

    serializationPool.run();

    fmt::print(std::cout, "Generated {} / {} tests\n", totalTasks - crashedTasks, totalTasks);
    if (crashedTasks > 0) {
        fmt::print(std::cout, "Generator crashed in {} / {} tests\n", crashedTasks, totalTasks);
        fmt::print(
            std::cout,
            "Working directories of crashed tests: {}.\nUse --keep-working-dirs to save them\n",
            config_.dir(config::DirType::cache));
    }
}

void Generator::traverse(
    VarsIterator it,
    InlineVariables& vars,
    const std::function<void(const InlineVariables& vars)>& callback) {
    if (it == opts_.vars.end()) {
        callback(vars);
    } else {
        auto next = std::next(it);
        for (i64 i = it->second.from; i < it->second.to; i += it->second.step) {
            vars.set(it->first, util::string::to(i));
            traverse(next, vars, callback);
        }
    }
}

} // namespace cacos::test

max_stars_repo_name: BigRedEye/cacos

max_issues_repo_name: BigRedEye/cacos

max_forks_repo_name: BigRedEye/cacos