mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-26 20:38:45 +08:00
Moved common code to base.
This commit is contained in:
parent
1803dca3eb
commit
61a9b2019b
72
src/queue.h
72
src/queue.h
@ -7,7 +7,7 @@ Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2014 jwellbelove
|
||||
Copyright(c) 2014 jwellbelove, Mark Kitson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
@ -157,12 +157,39 @@ namespace etl
|
||||
//*************************************************************************
|
||||
queue_base(size_type max_size)
|
||||
: in(0),
|
||||
out(0),
|
||||
current_size(0),
|
||||
CAPACITY(max_size)
|
||||
out(0),
|
||||
current_size(0),
|
||||
CAPACITY(max_size)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Increments (and wraps) the 'in' index value to record a queue addition.
|
||||
//*************************************************************************
|
||||
void add_in()
|
||||
{
|
||||
if (++in == CAPACITY)
|
||||
{
|
||||
in = 0;
|
||||
}
|
||||
|
||||
++current_size;
|
||||
++construct_count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Decrements (and wraps) the 'out' index value to record a queue deletion.
|
||||
//*************************************************************************
|
||||
void del_out()
|
||||
{
|
||||
if (++out == CAPACITY)
|
||||
{
|
||||
out = 0;
|
||||
}
|
||||
--current_size;
|
||||
--construct_count;
|
||||
}
|
||||
|
||||
size_type in; ///< Where to input new data.
|
||||
size_type out; ///< Where to get the oldest data.
|
||||
size_type current_size; ///< The number of items in the queue.
|
||||
@ -196,6 +223,7 @@ namespace etl
|
||||
private:
|
||||
|
||||
typedef typename etl::parameter_type<T>::type parameter_t;
|
||||
typedef typename etl::queue_base base_t;
|
||||
|
||||
public:
|
||||
|
||||
@ -246,9 +274,7 @@ namespace etl
|
||||
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
|
||||
#endif
|
||||
::new (&p_buffer[in]) T(value);
|
||||
in = (in == (CAPACITY - 1)) ? 0 : in + 1;
|
||||
++current_size;
|
||||
++construct_count;
|
||||
base_t::add_in();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -265,10 +291,7 @@ namespace etl
|
||||
#if defined(ETL_CHECK_PUSH_POP)
|
||||
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
|
||||
#endif
|
||||
::new (&p_buffer[in]) T();
|
||||
in = (in == (CAPACITY - 1)) ? 0 : in + 1;
|
||||
++current_size;
|
||||
++construct_count;
|
||||
base_t::add_in();
|
||||
|
||||
return p_buffer[next];
|
||||
}
|
||||
@ -285,9 +308,7 @@ namespace etl
|
||||
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
|
||||
#endif
|
||||
::new (&p_buffer[in]) T(value1);
|
||||
in = (in == (CAPACITY - 1)) ? 0 : in + 1;
|
||||
++current_size;
|
||||
++construct_count;
|
||||
base_t::add_in();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -302,9 +323,7 @@ namespace etl
|
||||
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
|
||||
#endif
|
||||
::new (&p_buffer[in]) T(value1, value2);
|
||||
in = (in == (CAPACITY - 1)) ? 0 : in + 1;
|
||||
++current_size;
|
||||
++construct_count;
|
||||
base_t::add_in();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -319,9 +338,7 @@ namespace etl
|
||||
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
|
||||
#endif
|
||||
::new (&p_buffer[in]) T(value1, value2, value3);
|
||||
in = (in == (CAPACITY - 1)) ? 0 : in + 1;
|
||||
++current_size;
|
||||
++construct_count;
|
||||
base_t::add_in();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -336,9 +353,7 @@ namespace etl
|
||||
ETL_ASSERT(!full(), ETL_ERROR(queue_full));
|
||||
#endif
|
||||
::new (&p_buffer[in]) T(value1, value2, value3, value4);
|
||||
in = (in == (CAPACITY - 1)) ? 0 : in + 1;
|
||||
++current_size;
|
||||
++construct_count;
|
||||
base_t::add_in();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -349,9 +364,7 @@ namespace etl
|
||||
while (current_size > 0)
|
||||
{
|
||||
p_buffer[out].~T();
|
||||
out = (out == (CAPACITY - 1)) ? 0 : out + 1;
|
||||
--current_size;
|
||||
--construct_count;
|
||||
base_t::del_out();
|
||||
}
|
||||
|
||||
in = 0;
|
||||
@ -369,9 +382,7 @@ namespace etl
|
||||
ETL_ASSERT(!empty(), ETL_ERROR(queue_empty));
|
||||
#endif
|
||||
p_buffer[out].~T();
|
||||
out = (out == (CAPACITY - 1)) ? 0 : out + 1;
|
||||
--current_size;
|
||||
--construct_count;
|
||||
base_t::del_out();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -391,6 +402,7 @@ namespace etl
|
||||
{
|
||||
if (&rhs != this)
|
||||
{
|
||||
clear();
|
||||
clone(rhs);
|
||||
}
|
||||
|
||||
@ -404,6 +416,8 @@ namespace etl
|
||||
//*************************************************************************
|
||||
void clone(const iqueue& other)
|
||||
{
|
||||
clear();
|
||||
|
||||
size_t index = other.out;
|
||||
|
||||
for (size_t i = 0; i < other.size(); ++i)
|
||||
|
||||
54
src/stack.h
54
src/stack.h
@ -7,7 +7,7 @@ Embedded Template Library.
|
||||
https://github.com/ETLCPP/etl
|
||||
http://www.etlcpp.com
|
||||
|
||||
Copyright(c) 2014 jwellbelove
|
||||
Copyright(c) 2014 jwellbelove, Mark Kitson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
@ -163,6 +163,25 @@ namespace etl
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Increments the indexes value to record a stack addition.
|
||||
//*************************************************************************
|
||||
void add_in()
|
||||
{
|
||||
top_index = current_size++;
|
||||
++construct_count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Decrements the indexes value to record a queue deletion.
|
||||
//*************************************************************************
|
||||
void del_out()
|
||||
{
|
||||
--top_index;
|
||||
--current_size;
|
||||
--construct_count;
|
||||
}
|
||||
|
||||
size_type top_index; ///< The index of the top of the stack.
|
||||
size_type current_size; ///< The number of items in the stack.
|
||||
const size_type CAPACITY; ///< The maximum number of items in the stack.
|
||||
@ -174,8 +193,8 @@ namespace etl
|
||||
///\brief This is the base for all stacks that contain a particular type.
|
||||
///\details Normally a reference to this type will be taken from a derived stack.
|
||||
///\code
|
||||
/// etl::stack<int, 10> myQueue;
|
||||
/// etl::istack<int>& iQueue = myQueue;
|
||||
/// etl::stack<int, 10> myStack;
|
||||
/// etl::istack<int>& iStack = myStack;
|
||||
///\endcode
|
||||
/// \warning This stack cannot be used for concurrent access from multiple threads.
|
||||
/// \tparam T The type of value that the stack holds.
|
||||
@ -195,6 +214,7 @@ namespace etl
|
||||
private:
|
||||
|
||||
typedef typename etl::parameter_type<T>::type parameter_t;
|
||||
typedef typename etl::stack_base base_t;
|
||||
|
||||
public:
|
||||
|
||||
@ -217,9 +237,8 @@ namespace etl
|
||||
#if defined(ETL_CHECK_PUSH_POP)
|
||||
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
|
||||
#endif
|
||||
top_index = current_size++;
|
||||
base_t::add_in();
|
||||
::new (&p_buffer[top_index]) T(value);
|
||||
++construct_count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -233,9 +252,8 @@ namespace etl
|
||||
#if defined(ETL_CHECK_PUSH_POP)
|
||||
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
|
||||
#endif
|
||||
top_index = current_size++;
|
||||
base_t::add_in();
|
||||
::new (&p_buffer[top_index]) T(value1);
|
||||
++construct_count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -249,9 +267,8 @@ namespace etl
|
||||
#if defined(ETL_CHECK_PUSH_POP)
|
||||
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
|
||||
#endif
|
||||
top_index = current_size++;
|
||||
base_t::add_in();
|
||||
::new (&p_buffer[top_index]) T(value1, value2);
|
||||
++construct_count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -265,9 +282,8 @@ namespace etl
|
||||
#if defined(ETL_CHECK_PUSH_POP)
|
||||
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
|
||||
#endif
|
||||
top_index = current_size++;
|
||||
base_t::add_in();
|
||||
::new (&p_buffer[top_index]) T(value1, value2, value3);
|
||||
++construct_count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -281,9 +297,8 @@ namespace etl
|
||||
#if defined(ETL_CHECK_PUSH_POP)
|
||||
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
|
||||
#endif
|
||||
top_index = current_size++;
|
||||
base_t::add_in();
|
||||
::new (&p_buffer[top_index]) T(value1, value2, value3, value4);
|
||||
++construct_count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -298,9 +313,7 @@ namespace etl
|
||||
#if defined(ETL_CHECK_PUSH_POP)
|
||||
ETL_ASSERT(!full(), ETL_ERROR(stack_full));
|
||||
#endif
|
||||
top_index = current_size++;
|
||||
::new (&p_buffer[top_index]) T();
|
||||
++construct_count;
|
||||
base_t::add_in();
|
||||
|
||||
return p_buffer[top_index];
|
||||
}
|
||||
@ -322,9 +335,7 @@ namespace etl
|
||||
while (current_size > 0)
|
||||
{
|
||||
p_buffer[top_index].~T();
|
||||
--top_index;
|
||||
--current_size;
|
||||
--construct_count;
|
||||
base_t::del_out();
|
||||
}
|
||||
}
|
||||
|
||||
@ -337,9 +348,7 @@ namespace etl
|
||||
ETL_ASSERT(!empty(), ETL_ERROR(stack_empty));
|
||||
#endif
|
||||
p_buffer[top_index].~T();
|
||||
--top_index;
|
||||
--current_size;
|
||||
--construct_count;
|
||||
base_t::del_out();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
@ -358,6 +367,7 @@ namespace etl
|
||||
{
|
||||
if (&rhs != this)
|
||||
{
|
||||
clear();
|
||||
clone(rhs);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user