Skip to content

Commit

Permalink
Add basic support for std::deque (JuliaInterop#121)
Browse files Browse the repository at this point in the history
* Add basic support for std::deque

* removed the constructor that takes an std::allocator and some includes that I have added randomly before

* removed the settings of vscode and added it to .gitignore
  • Loading branch information
abdoei authored Mar 23, 2023
1 parent 9fa283d commit f29c54f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/.vs
/out/build/x64-Debug
/CMakeSettings.json
/build
/build
/.vscode
24 changes: 24 additions & 0 deletions include/jlcxx/stl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <valarray>
#include <vector>
#include <deque>

#include "module.hpp"
#include "smart_pointers.hpp"
Expand Down Expand Up @@ -45,6 +46,7 @@ class JLCXX_API StlWrappers
public:
TypeWrapper1 vector;
TypeWrapper1 valarray;
TypeWrapper1 deque;

static void instantiate(Module& mod);
static StlWrappers& instance();
Expand Down Expand Up @@ -156,11 +158,33 @@ struct WrapValArray
}
};

struct WrapDeque
{
template<typename TypeWrapperT>
void operator()(TypeWrapperT&& wrapped)
{
using WrappedT = typename TypeWrapperT::type;
using T = typename WrappedT::value_type;
wrapped.template constructor<std::size_t>();
wrapped.module().set_override_module(StlWrappers::instance().module());
wrapped.method("cppsize", &WrappedT::size);
wrapped.method("resize", [](WrappedT &v, const cxxint_t s) { v.resize(s); });
wrapped.method("cxxgetindex", [](const WrappedT& v, cxxint_t i) -> const T& { return v[i - 1]; });
wrapped.method("cxxsetindex!", [](WrappedT& v, const T& val, cxxint_t i) { v[i - 1] = val; });
wrapped.method("push_back!", [] (WrappedT& v, const T& val) { v.push_back(val); });
wrapped.method("push_front!", [] (WrappedT& v, const T& val) { v.push_front(val); });
wrapped.method("pop_back!", [] (WrappedT& v) { v.pop_back(); });
wrapped.method("pop_front!", [] (WrappedT& v) { v.pop_front(); });
wrapped.module().unset_override_module();
}
};

template<typename T>
inline void apply_stl(jlcxx::Module& mod)
{
TypeWrapper1(mod, StlWrappers::instance().vector).apply<std::vector<T>>(WrapVector());
TypeWrapper1(mod, StlWrappers::instance().valarray).apply<std::valarray<T>>(WrapValArray());
TypeWrapper1(mod, StlWrappers::instance().deque).apply<std::deque<T>>(WrapDeque());
}

}
Expand Down
4 changes: 3 additions & 1 deletion src/stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ JLCXX_API void StlWrappers::instantiate(Module& mod)
m_instance.reset(new StlWrappers(mod));
m_instance->vector.apply_combination<std::vector, stltypes>(stl::WrapVector());
m_instance->valarray.apply_combination<std::valarray, stltypes>(stl::WrapValArray());
m_instance->deque.apply_combination<std::deque, stltypes>(stl::WrapDeque());
smartptr::apply_smart_combination<std::shared_ptr, stltypes>(mod);
smartptr::apply_smart_combination<std::weak_ptr, stltypes>(mod);
smartptr::apply_smart_combination<std::unique_ptr, stltypes>(mod);
Expand All @@ -41,7 +42,8 @@ JLCXX_API StlWrappers& wrappers()
JLCXX_API StlWrappers::StlWrappers(Module& stl) :
m_stl_mod(stl),
vector(stl.add_type<Parametric<TypeVar<1>>>("StdVector", julia_type("AbstractVector"))),
valarray(stl.add_type<Parametric<TypeVar<1>>>("StdValArray", julia_type("AbstractVector")))
valarray(stl.add_type<Parametric<TypeVar<1>>>("StdValArray", julia_type("AbstractVector"))),
deque(stl.add_type<Parametric<TypeVar<1>>>("StdDeque", julia_type("AbstractVector")))
{
}

Expand Down
1 change: 1 addition & 0 deletions test/test_type_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ int main()
test_type<char16_t>("CxxChar16");
test_type<char32_t>("CxxChar32");
test_type<bool>("CxxBool");
test_type<wchar_t>("CxxWchar");

jl_atexit_hook(0);
return 0;
Expand Down

0 comments on commit f29c54f

Please sign in to comment.