Skip to content

Commit

Permalink
feat: add std::stack
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneethJain committed Jun 16, 2024
1 parent 96d8d1f commit b1cab7b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ set(JLCXX_STL_SOURCES
${JLCXX_SOURCE_DIR}/stl_deque.cpp
${JLCXX_SOURCE_DIR}/stl_queue.cpp
${JLCXX_SOURCE_DIR}/stl_priority_queue.cpp
${JLCXX_SOURCE_DIR}/stl_stack.cpp
${JLCXX_SOURCE_DIR}/stl_set.cpp
${JLCXX_SOURCE_DIR}/stl_multiset.cpp
${JLCXX_SOURCE_DIR}/stl_unordered_set.cpp
Expand Down
23 changes: 23 additions & 0 deletions include/jlcxx/stl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <unordered_set>

Expand Down Expand Up @@ -53,6 +54,7 @@ class JLCXX_API StlWrappers
TypeWrapper1 deque;
TypeWrapper1 queue;
TypeWrapper1 priority_queue;
TypeWrapper1 stack;
TypeWrapper1 set;
TypeWrapper1 multiset;
TypeWrapper1 unordered_set;
Expand All @@ -73,6 +75,7 @@ void apply_valarray(TypeWrapper1& valarray);
void apply_deque(TypeWrapper1& deque);
void apply_queue(TypeWrapper1& queue);
void apply_priority_queue(TypeWrapper1& priority_queue);
void apply_stack(TypeWrapper1& stack);
void apply_set(TypeWrapper1& set);
void apply_multiset(TypeWrapper1& multiset);
void apply_unordered_set(TypeWrapper1& unordered_set);
Expand Down Expand Up @@ -284,6 +287,25 @@ struct WrapPriorityQueue
}
};

struct WrapStack
{
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("stack_isempty", [] (WrappedT& v) { return v.empty(); });
wrapped.method("stack_push!", [] (WrappedT& v, const T& val) { v.push(val); });
wrapped.method("stack_top", [] (WrappedT& v) { return v.top(); });
wrapped.method("stack_pop!", [] (WrappedT& v) { v.pop(); });
wrapped.module().unset_override_module();
}
};

struct WrapSetType
{
template<typename TypeWrapperT>
Expand Down Expand Up @@ -382,6 +404,7 @@ inline void apply_stl(jlcxx::Module& mod)
TypeWrapper1(mod, StlWrappers::instance().valarray).apply<std::valarray<T>>(WrapValArray());
TypeWrapper1(mod, StlWrappers::instance().deque).apply<std::deque<T>>(WrapDeque());
TypeWrapper1(mod, StlWrappers::instance().queue).apply<std::queue<T>>(WrapQueue());
TypeWrapper1(mod, StlWrappers::instance().stack).apply<std::stack<T>>(WrapStack());
if constexpr (container_has_less_than_operator<T>::value)
{
TypeWrapper1(mod, StlWrappers::instance().set).apply<std::set<T>>(WrapSetType());
Expand Down
2 changes: 2 additions & 0 deletions src/stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ JLCXX_API void StlWrappers::instantiate(Module& mod)
apply_deque(m_instance->deque);
apply_queue(m_instance->queue);
apply_priority_queue(m_instance->priority_queue);
apply_stack(m_instance->stack);
apply_set(m_instance->set);
apply_multiset(m_instance->multiset);
apply_unordered_set(m_instance->unordered_set);
Expand Down Expand Up @@ -53,6 +54,7 @@ JLCXX_API StlWrappers::StlWrappers(Module& stl) :
// Assign appropriate parent types after iterators are implemented
queue(stl.add_type<Parametric<TypeVar<1>>>("StdQueue")),
priority_queue(stl.add_type<Parametric<TypeVar<1>>>("StdPriorityQueue")),
stack(stl.add_type<Parametric<TypeVar<1>>>("StdStack")),
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")),
Expand Down
16 changes: 16 additions & 0 deletions src/stl_stack.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_stack(TypeWrapper1& stack)
{
stack.apply_combination<std::stack, stltypes>(stl::WrapStack());
}

}

}

0 comments on commit b1cab7b

Please sign in to comment.