W
William Payne
Hello, have you seen a recent files menu in a GUI application? In many GUI
applications there's a menu the displays the most recent files that has been
opened by the program. Say such a menu has four entries and the most recent
file is displayed on top and the oldest at the bottom. New entries are added
at the top, pushing the oldest of the list. I wrote a class which I've named
CircularContainer (please help me think of a better name!) that implements a
data structure suited to handling the entries in a recent files menu
(basically it stores std::strings). Given the code below, is it ready to be
used or could it use improving/bug fixing? The code (written in standard
C++):
circular_container.hpp:
#ifndef CIRCULAR_CONTAINER_HPP
#define CIRCULAR_CONTAINER_HPP
#include <cstddef> /* size_t */
#include <cstdio> /* sprintf */
#include <stdexcept>
#include <string>
#include <vector>
/* CircularContainer
* When constructed, the user specifies the max number of *
* elements the container can hold. New elements are added *
* at the beginning of the array. When the container is full *
* and a new element is added, the last one is removed. This *
* makes the CircularContainer a FIFO-type container? Circu- *
* larContainer is ideal for use in implementing Recent Files *
* menus. */
class CircularContainer
{
public:
CircularContainer(std::size_t max_size)
:
m_max_size(max_size)
{
; /* Do nothing. */
}
virtual ~CircularContainer()
{
; /* Do nothing. */
}
void insert(const std::string& s);
std::string get(std::size_t index) const;
std::size_t size() const
{
return m_elements.size();
}
std::size_t max_size() const
{
return m_max_size;
}
void clear();
private:
std::vector<std::string> m_elements;
std::size_t m_max_size;
};
#endif /* #ifndef CIRCULAR_CONTAINER_HPP */
circular_container.cpp:
#include "circular_container.hpp"
using std::sprintf;
using std::string;
using std::vector;
using std::size_t;
using std::runtime_error;
void CircularContainer::insert(const string& s)
{
m_elements.insert(m_elements.begin(), s);
if(m_elements.size() > m_max_size)
{
m_elements.pop_back();
}
}
string CircularContainer::get(size_t index) const
{
char error_message[64];
sprintf(error_message, "Invalid index %i", index);
if(index >= m_elements.size())
{
throw runtime_error(error_message);
}
return m_elements[index];
}
void CircularContainer::clear()
{
m_elements.erase(m_elements.begin(), m_elements.end());
}
Thanks for any replies!
/ WP
applications there's a menu the displays the most recent files that has been
opened by the program. Say such a menu has four entries and the most recent
file is displayed on top and the oldest at the bottom. New entries are added
at the top, pushing the oldest of the list. I wrote a class which I've named
CircularContainer (please help me think of a better name!) that implements a
data structure suited to handling the entries in a recent files menu
(basically it stores std::strings). Given the code below, is it ready to be
used or could it use improving/bug fixing? The code (written in standard
C++):
circular_container.hpp:
#ifndef CIRCULAR_CONTAINER_HPP
#define CIRCULAR_CONTAINER_HPP
#include <cstddef> /* size_t */
#include <cstdio> /* sprintf */
#include <stdexcept>
#include <string>
#include <vector>
/* CircularContainer
* When constructed, the user specifies the max number of *
* elements the container can hold. New elements are added *
* at the beginning of the array. When the container is full *
* and a new element is added, the last one is removed. This *
* makes the CircularContainer a FIFO-type container? Circu- *
* larContainer is ideal for use in implementing Recent Files *
* menus. */
class CircularContainer
{
public:
CircularContainer(std::size_t max_size)
:
m_max_size(max_size)
{
; /* Do nothing. */
}
virtual ~CircularContainer()
{
; /* Do nothing. */
}
void insert(const std::string& s);
std::string get(std::size_t index) const;
std::size_t size() const
{
return m_elements.size();
}
std::size_t max_size() const
{
return m_max_size;
}
void clear();
private:
std::vector<std::string> m_elements;
std::size_t m_max_size;
};
#endif /* #ifndef CIRCULAR_CONTAINER_HPP */
circular_container.cpp:
#include "circular_container.hpp"
using std::sprintf;
using std::string;
using std::vector;
using std::size_t;
using std::runtime_error;
void CircularContainer::insert(const string& s)
{
m_elements.insert(m_elements.begin(), s);
if(m_elements.size() > m_max_size)
{
m_elements.pop_back();
}
}
string CircularContainer::get(size_t index) const
{
char error_message[64];
sprintf(error_message, "Invalid index %i", index);
if(index >= m_elements.size())
{
throw runtime_error(error_message);
}
return m_elements[index];
}
void CircularContainer::clear()
{
m_elements.erase(m_elements.begin(), m_elements.end());
}
Thanks for any replies!
/ WP