-
Notifications
You must be signed in to change notification settings - Fork 11
Wip/cpp #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vendethiel
wants to merge
11
commits into
master
Choose a base branch
from
wip/cpp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Wip/cpp #4
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bc6a1fa
WIP C++ impl
vendethiel 8385002
Start working on the Bridge class
vendethiel a418514
Implement bridges
vendethiel 5f7ff6c
add wrapper class
vendethiel 09014be
Makefile rule to fetch nlohmann/json.hpp
ingydotnet 27979da
Hackery hackety hacks for PMFs
vendethiel 7bc87c1
small adjustements
vendethiel 7cae02f
add boost::callable_traits
vendethiel d2e5956
Add json.hpp directly to the repository (for now)
ingydotnet 6201219
Add C++ bin symlinks
ingydotnet 08f2b6f
Replace local ext/ with symlink to ext/cpp branch
ingydotnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../src/cpp/bin/testml-c++ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../src/cpp/bin/testml-cpp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../src/cpp/bin/testml-cpp-tap |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../ext/cpp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "bridge.hpp" | ||
|
||
namespace testml { | ||
|
||
using json = nlohmann::json; | ||
|
||
json Bridge::call(std::string const& name, std::vector<json> const& args) { | ||
auto it = _fns.find(name); | ||
if (it == _fns.end()) { | ||
throw std::runtime_error("Bridge method not found: " + name + "."); | ||
} | ||
return it->second->call(args); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#pragma once | ||
|
||
#include <tuple> | ||
#include <utility> | ||
#include <unordered_map> | ||
#include <memory> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include "boost/callable_traits/args.hpp" | ||
#include "boost/callable_traits/class_of.hpp" | ||
#include "boost/callable_traits/return_type.hpp" | ||
#include "nlohmann/json.hpp" | ||
|
||
#include "utils.hpp" | ||
#include "wrapper.hpp" | ||
|
||
namespace testml { | ||
|
||
namespace details { | ||
|
||
using json = nlohmann::json; | ||
using wrapper::cook; | ||
using wrapper::uncook; | ||
|
||
namespace ct = boost::callable_traits; | ||
|
||
// we need this details class so that we can have a non-templated value | ||
// stored in the Bridge _fns map. | ||
struct FnHolder { | ||
virtual json call(std::vector<json> const&) = 0; | ||
}; | ||
|
||
// the implementation of a FnHolder, which keeps the types around | ||
template<typename BridgeT, typename Fn> | ||
class FnHolderImpl : public FnHolder { | ||
Fn _fn; | ||
BridgeT* _bridge; | ||
static constexpr bool _is_pmf = std::is_member_function_pointer<Fn>::value; | ||
using RawArg = ct::args_t<Fn>; | ||
// in case of a PMF, remove the class type from the argument list | ||
using Arg = std::conditional_t<_is_pmf, typename utils::remove_first_type<RawArg>::type, RawArg>; | ||
using Ret = ct::return_type_t<Fn>; | ||
static constexpr std::size_t _num_args = std::tuple_size<Arg>::value; | ||
|
||
// type of the N-th argument that the stored function takes | ||
template<std::size_t I> | ||
using ArgType = typename std::tuple_element<I, Arg>::type; | ||
|
||
// uncook each argument to its expected type, and call the function | ||
// we do SFINAE in the return type, using comma+sizeof() to get a dependance on I. | ||
|
||
// PMF case | ||
template<std::size_t... I> | ||
auto call_impl(std::vector<json> const& args, std::index_sequence<I...>) | ||
-> typename std::enable_if<(sizeof...(I), _is_pmf), Ret>::type { | ||
return (_bridge->*_fn)(uncook<ArgType<I>>(args[I])...); | ||
} | ||
|
||
// non-PMF case (BridgeT = nullptr_t) | ||
template<std::size_t... I> | ||
auto call_impl(std::vector<json> const& args, std::index_sequence<I...>) | ||
-> typename std::enable_if<(sizeof...(I), !_is_pmf), Ret>::type { | ||
return _fn(uncook<ArgType<I>>(args[I])...); | ||
} | ||
|
||
public: | ||
FnHolderImpl(BridgeT* bridge, Fn fn) | ||
: _fn{std::move(fn)}, | ||
_bridge{bridge} { | ||
} | ||
|
||
// check arity and call the function using our little helper, before wrapping it back to json | ||
json call(std::vector<json> const& args) override { | ||
if (args.size() != _num_args) { | ||
throw std::runtime_error("Bridge method call with wrong arity, expected " + std::to_string(_num_args) + ", got " + std::to_string(args.size()) + "."); | ||
} | ||
|
||
return cook(call_impl(args, std::make_index_sequence<_num_args>{})); | ||
} | ||
|
||
}; | ||
|
||
} | ||
|
||
class Bridge { | ||
// store a wrapper FnHolder in the map, with FnHolderImpl to keep the correct types around and do FFI correctly | ||
std::unordered_map<std::string, std::unique_ptr<details::FnHolder>> _fns; | ||
|
||
public: | ||
// PMF version, takes the object to call the function on | ||
template<typename BridgeT, typename Fn> | ||
auto bind(std::string const& name, BridgeT* obj, Fn fn) | ||
-> typename std::enable_if<std::is_member_function_pointer<Fn>::value, void>::type { | ||
static_assert(std::is_same<details::ct::class_of_t<Fn>, BridgeT>::value, "Bridge subclass must pass itself"); | ||
|
||
using HolderType = details::FnHolderImpl<BridgeT, Fn>; | ||
_fns[name] = std::make_unique<HolderType>(obj, std::move(fn)); | ||
} | ||
|
||
// any other candidate | ||
template<typename Fn> | ||
auto bind(std::string const& name, Fn fn) | ||
-> typename std::enable_if<!std::is_member_function_pointer<Fn>::value, void>::type { | ||
using HolderType = details::FnHolderImpl<std::nullptr_t, Fn>; | ||
_fns[name] = std::make_unique<HolderType>(nullptr, std::move(fn)); | ||
} | ||
|
||
nlohmann::json call(std::string const& name, std::vector<nlohmann::json> const& args); | ||
}; | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,37 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include "tap.h" | ||
#include <iostream> | ||
#include "tap.hpp" | ||
|
||
namespace testml { | ||
namespace run { | ||
|
||
void TestML_Run_TAP::run(std::string file) { | ||
TestML_Run_TAP tap; | ||
void TAP::testml_eq(json want, json got, std::string const& label) { | ||
if (want == got) { | ||
tap_pass(label); | ||
} else { | ||
tap_fail(label); | ||
} | ||
} | ||
|
||
tap.from_file(file); | ||
void TAP::tap_pass(std::string const& label) { | ||
std::cout << "ok " << ++count; | ||
if (!label.empty()) { | ||
std::cout << " - " << label; | ||
} | ||
std::cout << "\n"; | ||
} | ||
|
||
std::cout << "1..1" << std::endl; | ||
std::cout << "ok 1 - It worked" << std::endl; | ||
void TAP::tap_fail(std::string const& label) { | ||
std::cout << "not ok " << ++count; | ||
if (!label.empty()) { | ||
std::cout << " - " << label; | ||
} | ||
std::cout << "\n"; | ||
} | ||
|
||
void TAP::testml_end() { | ||
std::cout << "1.." << count; | ||
} | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
#include "../runtime.hpp" | ||
|
||
#include <string> | ||
|
||
namespace testml { | ||
namespace run { | ||
|
||
class TAP : public Runtime { | ||
using json = nlohmann::json; | ||
|
||
using Runtime::Runtime; | ||
|
||
protected: | ||
void testml_eq(json want, json got, std::string const& label) override; | ||
void testml_end() override; | ||
|
||
private: | ||
void tap_pass(std::string const& label); | ||
void tap_fail(std::string const& label); | ||
|
||
private: | ||
int count = 0; | ||
}; | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the
call
method belongs in the bridge base class. This implies that a user defined bridge method would want to call this method directly. Maybe this method should be calledcan
and it returns the bridge method pointer of the requested method, if found.