2016-05-17 03:04:11 +12:00
|
|
|
#pragma once
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifndef TCG_CONTAINERS_READER_H
|
|
|
|
#define TCG_CONTAINERS_READER_H
|
|
|
|
|
|
|
|
// STD includes
|
|
|
|
#include <stack>
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace tcg {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
// Generic Reader declaration
|
|
|
|
//******************************************************************************
|
|
|
|
|
|
|
|
template <typename Val>
|
2016-06-15 18:43:10 +12:00
|
|
|
class generic_containers_reader {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
typedef Val value_type;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
virtual void openContainer() = 0;
|
|
|
|
virtual void addElement(const value_type &value) = 0;
|
|
|
|
virtual void closeContainer() = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// NOTE: Algorithms may require the openContainer() function to accept the
|
|
|
|
// container's header data. Check the algorithms description if necessary.
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
// Some Actual Container Readers
|
|
|
|
//******************************************************************************
|
|
|
|
|
|
|
|
template <typename Container, typename Val = typename Container::value_type>
|
2016-06-15 18:43:10 +12:00
|
|
|
class sequential_reader {
|
|
|
|
Container *m_values;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
typedef Container container_type;
|
|
|
|
typedef Val value_type;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
sequential_reader(Container *container) : m_values(container) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void openContainer() { m_values->clear(); }
|
|
|
|
void addElement(const value_type &val) { m_values->push_back(val); }
|
|
|
|
void closeContainer() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
const container_type *values() const { return m_values; }
|
|
|
|
container_type *values() { return m_values; }
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename Container, typename Val = typename Container::value_type>
|
2016-06-15 18:43:10 +12:00
|
|
|
class ordered_reader {
|
|
|
|
Container *m_values;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
typedef Container container_type;
|
|
|
|
typedef Val value_type;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
ordered_reader(Container &container) : m_values(container) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void openContainer() { m_values->clear(); }
|
|
|
|
void addElement(const value_type &val) { m_values->insert(val); }
|
|
|
|
void closeContainer() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
const container_type *values() const { return m_values; }
|
|
|
|
container_type *values() { return m_values; }
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
template <typename Container, typename ReaderType,
|
|
|
|
typename Val = typename ReaderType::value_type>
|
|
|
|
class multiple_reader {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
typedef Container container_type;
|
|
|
|
typedef ReaderType reader_type;
|
|
|
|
typedef Val value_type;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
Container *m_storage;
|
|
|
|
std::stack<reader_type> m_stack;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
multiple_reader(Container *storage) : m_storage(storage) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
reader_type &reader() { return m_stack.top(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void openContainer() {
|
|
|
|
m_stack.push(reader_type(new typename reader_type::container_type));
|
|
|
|
}
|
|
|
|
void addElement(const value_type &val) { reader().addElement(val); }
|
|
|
|
void closeContainer() {
|
|
|
|
if (reader().values()->empty())
|
|
|
|
delete reader().values();
|
|
|
|
else
|
|
|
|
m_storage->push_back(reader().values());
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
m_stack.pop();
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
const container_type &storage() const { return m_storage; }
|
|
|
|
container_type &storage() { return m_storage; }
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace tcg
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif // TCG_CONTAINERS_READER_H
|