Skip to content

Commit 47a760f

Browse files
committed
Add basic support for std::valarray
1 parent d4f38a3 commit 47a760f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

include/jlcxx/stl.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef JLCXX_STL_HPP
22
#define JLCXX_STL_HPP
33

4+
#include <valarray>
45
#include <vector>
56

67
#include "module.hpp"
@@ -43,6 +44,7 @@ class JLCXX_API StlWrappers
4344
Module& m_stl_mod;
4445
public:
4546
TypeWrapper1 vector;
47+
TypeWrapper1 valarray;
4648

4749
static void instantiate(Module& mod);
4850
static StlWrappers& instance();
@@ -134,10 +136,31 @@ struct WrapVector
134136
}
135137
};
136138

139+
struct WrapValArray
140+
{
141+
template<typename TypeWrapperT>
142+
void operator()(TypeWrapperT&& wrapped)
143+
{
144+
using WrappedT = typename TypeWrapperT::type;
145+
using T = typename WrappedT::value_type;
146+
wrapped.template constructor<std::size_t>();
147+
wrapped.template constructor<const T&, std::size_t>();
148+
wrapped.template constructor<const T*, std::size_t>();
149+
wrapped.module().set_override_module(StlWrappers::instance().module());
150+
wrapped.method("cppsize", &WrappedT::size);
151+
wrapped.method("resize", [] (WrappedT& v, const cxxint_t s) { v.resize(s); });
152+
wrapped.method("cxxgetindex", [] (const WrappedT& v, cxxint_t i) -> const T& { return v[i-1]; });
153+
wrapped.method("cxxgetindex", [] (WrappedT& v, cxxint_t i) -> T& { return v[i-1]; });
154+
wrapped.method("cxxsetindex!", [] (WrappedT& v, const T& val, cxxint_t i) { v[i-1] = val; });
155+
wrapped.module().unset_override_module();
156+
}
157+
};
158+
137159
template<typename T>
138160
inline void apply_stl(jlcxx::Module& mod)
139161
{
140162
TypeWrapper1(mod, StlWrappers::instance().vector).apply<std::vector<T>>(WrapVector());
163+
TypeWrapper1(mod, StlWrappers::instance().valarray).apply<std::valarray<T>>(WrapValArray());
141164
}
142165

143166
}

src/stl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ JLCXX_API void StlWrappers::instantiate(Module& mod)
1616
{
1717
m_instance.reset(new StlWrappers(mod));
1818
m_instance->vector.apply_combination<std::vector, stltypes>(stl::WrapVector());
19+
m_instance->valarray.apply_combination<std::valarray, stltypes>(stl::WrapValArray());
1920
smartptr::apply_smart_combination<std::shared_ptr, stltypes>(mod);
2021
smartptr::apply_smart_combination<std::weak_ptr, stltypes>(mod);
2122
smartptr::apply_smart_combination<std::unique_ptr, stltypes>(mod);
@@ -37,7 +38,8 @@ JLCXX_API StlWrappers& wrappers()
3738

3839
JLCXX_API StlWrappers::StlWrappers(Module& stl) :
3940
m_stl_mod(stl),
40-
vector(stl.add_type<Parametric<TypeVar<1>>>("StdVector", julia_type("AbstractVector")))
41+
vector(stl.add_type<Parametric<TypeVar<1>>>("StdVector", julia_type("AbstractVector"))),
42+
valarray(stl.add_type<Parametric<TypeVar<1>>>("StdValArray", julia_type("AbstractVector")))
4143
{
4244
}
4345

0 commit comments

Comments
 (0)