Skip to content

Commit

Permalink
feat: add std::list and std::forward_list
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneethJain authored and barche committed Jun 21, 2024
1 parent ca848c7 commit e157d61
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ set(JLCXX_STL_SOURCES
${JLCXX_SOURCE_DIR}/stl_multiset.cpp
${JLCXX_SOURCE_DIR}/stl_unordered_set.cpp
${JLCXX_SOURCE_DIR}/stl_unordered_multiset.cpp
${JLCXX_SOURCE_DIR}/stl_list.cpp
${JLCXX_SOURCE_DIR}/stl_forward_list.cpp
${JLCXX_SOURCE_DIR}/stl_shared_ptr.cpp
${JLCXX_SOURCE_DIR}/stl_unique_ptr.cpp
${JLCXX_SOURCE_DIR}/stl_weak_ptr.cpp
Expand Down
50 changes: 50 additions & 0 deletions include/jlcxx/stl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <stack>
#include <set>
#include <unordered_set>
#include <list>
#include <forward_list>

#include "module.hpp"
#include "smart_pointers.hpp"
Expand Down Expand Up @@ -59,6 +61,8 @@ class JLCXX_API StlWrappers
TypeWrapper1 multiset;
TypeWrapper1 unordered_set;
TypeWrapper1 unordered_multiset;
TypeWrapper1 list;
TypeWrapper1 forward_list;

static void instantiate(Module& mod);
static StlWrappers& instance();
Expand All @@ -80,6 +84,8 @@ void apply_set(TypeWrapper1& set);
void apply_multiset(TypeWrapper1& multiset);
void apply_unordered_set(TypeWrapper1& unordered_set);
void apply_unordered_multiset(TypeWrapper1& unordered_multiset);
void apply_list(TypeWrapper1& list);
void apply_forward_list(TypeWrapper1& forward_list);
void apply_shared_ptr();
void apply_weak_ptr();
void apply_unique_ptr();
Expand Down Expand Up @@ -347,6 +353,48 @@ struct WrapMultisetType
}
};

struct WrapList
{
template<typename TypeWrapperT>
void operator()(TypeWrapperT&& wrapped)
{
using WrappedT = typename TypeWrapperT::type;
using T = typename WrappedT::value_type;

wrapped.template constructor<>();
wrapped.module().set_override_module(StlWrappers::instance().module());
wrapped.method("cppsize", &WrappedT::size);
wrapped.method("list_empty!", [] (WrappedT& v) { v.clear(); });
wrapped.method("list_isempty", [] (WrappedT& v) { return v.empty(); });
wrapped.method("list_front", [] (WrappedT& v) { return v.front(); });
wrapped.method("list_back", [] (WrappedT& v) { return v.back(); });
wrapped.method("list_push_back!", [] (WrappedT& v, const T& val) { v.push_back(val); });
wrapped.method("list_push_front!", [] (WrappedT& v, const T& val) { v.push_front(val); });
wrapped.method("list_pop_back!", [] (WrappedT& v) { v.pop_back(); });
wrapped.method("list_pop_front!", [] (WrappedT& v) { v.pop_front(); });
wrapped.module().unset_override_module();
}
};

struct WrapForwardList
{
template<typename TypeWrapperT>
void operator()(TypeWrapperT&& wrapped)
{
using WrappedT = typename TypeWrapperT::type;
using T = typename WrappedT::value_type;

wrapped.template constructor<>();
wrapped.module().set_override_module(StlWrappers::instance().module());
wrapped.method("flist_empty!", [] (WrappedT& v) { v.clear(); });
wrapped.method("flist_isempty", [] (WrappedT& v) { return v.empty(); });
wrapped.method("flist_front", [] (WrappedT& v) { return v.front(); });
wrapped.method("flist_push_front!", [] (WrappedT& v, const T& val) { v.push_front(val); });
wrapped.method("flist_pop_front!", [] (WrappedT& v) { v.pop_front(); });
wrapped.module().unset_override_module();
}
};

template <typename T, typename = void>
struct has_less_than_operator : std::false_type {};

Expand Down Expand Up @@ -416,6 +464,8 @@ inline void apply_stl(jlcxx::Module& mod)
TypeWrapper1(mod, StlWrappers::instance().unordered_set).apply<std::unordered_set<T>>(WrapSetType());
TypeWrapper1(mod, StlWrappers::instance().unordered_multiset).apply<std::unordered_multiset<T>>(WrapMultisetType());
}
TypeWrapper1(mod, StlWrappers::instance().list).apply<std::list<T>>(WrapList());
TypeWrapper1(mod, StlWrappers::instance().forward_list).apply<std::forward_list<T>>(WrapForwardList());
}

}
Expand Down
6 changes: 5 additions & 1 deletion src/stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ JLCXX_API void StlWrappers::instantiate(Module& mod)
apply_multiset(m_instance->multiset);
apply_unordered_set(m_instance->unordered_set);
apply_unordered_multiset(m_instance->unordered_multiset);
apply_list(m_instance->list);
apply_forward_list(m_instance->forward_list);
apply_shared_ptr();
apply_weak_ptr();
apply_unique_ptr();
Expand Down Expand Up @@ -58,7 +60,9 @@ JLCXX_API StlWrappers::StlWrappers(Module& stl) :
set(stl.add_type<Parametric<TypeVar<1>>>("StdSet")),
multiset(stl.add_type<Parametric<TypeVar<1>>>("StdMultiset")),
unordered_set(stl.add_type<Parametric<TypeVar<1>>>("StdUnorderedSet")),
unordered_multiset(stl.add_type<Parametric<TypeVar<1>>>("StdUnorderedMultiset"))
unordered_multiset(stl.add_type<Parametric<TypeVar<1>>>("StdUnorderedMultiset")),
list(stl.add_type<Parametric<TypeVar<1>>>("StdList")),
forward_list(stl.add_type<Parametric<TypeVar<1>>>("StdForwardList"))
{
}

Expand Down
16 changes: 16 additions & 0 deletions src/stl_forward_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "jlcxx/stl.hpp"

namespace jlcxx
{

namespace stl
{

void apply_forward_list(TypeWrapper1& forward_list)
{
forward_list.apply_combination<std::forward_list, stltypes>(stl::WrapForwardList());
}

}

}
16 changes: 16 additions & 0 deletions src/stl_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "jlcxx/stl.hpp"

namespace jlcxx
{

namespace stl
{

void apply_list(TypeWrapper1& list)
{
list.apply_combination<std::list, stltypes>(stl::WrapList());
}

}

}

0 comments on commit e157d61

Please sign in to comment.