This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-07-24

smplque

Container class with FIFO queue structure
    This is a container class with FIFO queue structure.
    template <typename T, int N, class Intr> smplbuf_local
    template <typename T, class Intr> smplbuf_attach
    template <typename T, class Intr> smplbuf_heap

    smplque is a container class that provides FIFO queue operations on a memory region specified by the element type T and the memory allocation method alloc. Since specifying alloc can be complicated, alias definitions using using are provided.

    You can register a class Intr that sets interrupt disabling settings at declaration. If not specified, the default behavior will not perform any interrupt control.

    Here are examples of object declarations. Initialization methods are called right after declaration. In all cases, the maximum size immediately after initialization is 128 bytes, and the initial size is 0, meaning nothing is stored. The maximum size cannot be changed.

    void some_func() {
      // Uses internal fixed-size array
      smplque_local<uint8_t, 128> q1;
    
      // Uses an existing array
      uint8_t buf[128];
      smplque_attach<uint8_t> q2;
    
      // Allocates on the heap
      smplque_heap<uint8_t> q3;
    }
    
    void setup() {
      // Initialize global objects in setup()
      q1.init_local();
      q2.attach(buf, 128);
      q3.init_heap(128);
    }
    
    void some_func() {
      // Local smplque_local can omit init_local()
      smplque_local<uint8_t, 128> q_local;
      ..
    }

    As a FIFO queue, it is operated using methods such as push(), pop(), and front().

    void begin() { // begin() runs only once at startup
    	smplque_local<int, 32> q1;
    
    	q1.push(1);
    	q1.push(4);
    	q1.push(9);
    	q1.push(16);
    	q1.push(25);
    
    	while(!q1.empty()) {
    		Serial << int(q1.front()) << ',';
    		q1.pop();
    	}
    	// output -> 1,4,9,16,25,
    }

    Access using iterators is also possible.

    void begin() { // begin() runs only once at startup
    	smplque_local<int, 32> q1;
    	q1.init_local();
    
    	q1.push(1);
    	q1.push(4);
    	q1.push(9);
    	q1.push(16);
    	q1.push(25);
    
    	// Using iterator
    	for(int x : q1) {
    		Serial << int(x) << ',';
    	}
    
    	// Using STL algorithms
    	auto&& minmax = std::minmax_element(q1.begin(), q1.end());
    	Serial <<  "min=" << int(*minmax.first)
    		     << ",max=" << int(*minmax.second);
    	// output -> 1,4,9,16,25,min=1,max=25[]
    }

    Declaration and Initialization

    smplbuf_local<T,N>
    smplbuf_local<T,N>::init_local()
    
    smplbuf_attach<T>
    smplbuf_attach<T>::attach(T* buf, uint16_t N)
    
    smplbuf_heap<T>
    smplbuf_heap<T>::init_heap(uint16_t N);
    
    // Example
    // Uses internal fixed-size array
    smplque_local<uint8_t, 128> q1;
    q1.init_local();
    
    // Uses an existing array
    uint8_t buf[128];
    smplque_attach<uint8_t> q2;
    q2.attach(buf, 128);
    
    // Allocates on the heap
    smplque_heap<uint8_t> q3;
    q3.init_heap(128);

    Declare a container of type T and size N. After declaration, call the initialization method.

    smplque_local allocates space with an internal fixed-size array. It can also be initialized using a constructor.

    smplque_attach requires the pointer to the buffer to use (T* buf), the initial size of the array, and the maximum size N. It can also be initialized using a constructor.

    smplque_heap allocates memory in the HEAP area (memory that cannot be released but can be allocated at any time). Since it cannot be released once allocated, it is usually defined in the global scope. The memory allocation is performed by calling init_heap(). Memory allocation via constructor is not possible. Always call init_heap() to use it.

    Methods

    push(), pop(), front(), back()

    inline void push(T&& c)
    inline void push(T& c)
    inline void pop()
    inline T& front()
    inline T& back()
    
    inline T& pop_front()

    push() adds an entry to the queue.

    pop() removes an entry from the queue.

    front() accesses the first entry (the one added first).

    back() accesses the last entry (the one added last).

    pop_front() returns the first entry and simultaneously removes it from the queue.

    empty(), size(), is_full()

    inline bool empty()
    inline bool is_full()
    inline uint16_t size()
    inline uint16_t capacity()

    empty() returns true if the queue has no elements. is_full() returns true when the queue is full.

    size() returns the number of elements currently in the queue.

    capacity() returns the maximum number of elements the queue can hold.

    clear()

    inline void clear()

    Removes all elements from the queue.

    operator []

    inline T& operator[] (int i)

    Accesses the element at index i. 0 is the first added element.

    Iterator

    inline smplque::iterator begin()
    inline smplque::iterator end()

    Returns iterators via begin() and end(). The beginning iterator points to the first element added to the queue. By using iterators, you can use range-based for loops and algorithms.

    As an advanced usage, see iterator access focusing on specific members of the axis_xyzt structure.