Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_CONTAINERS_READER_H
Toshihiro Shimizu 890ddd
#define TCG_CONTAINERS_READER_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STD includes
Toshihiro Shimizu 890ddd
#include <stack></stack>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace tcg {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//******************************************************************************
Toshihiro Shimizu 890ddd
//    Generic Reader declaration
Toshihiro Shimizu 890ddd
//******************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename val=""></typename>
Shinya Kitaoka 120a6e
class generic_containers_reader {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  typedef Val value_type;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  virtual void openContainer()                     = 0;
Shinya Kitaoka 120a6e
  virtual void addElement(const value_type &value) = 0;
Shinya Kitaoka 120a6e
  virtual void closeContainer()                    = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // NOTE: Algorithms may require the openContainer() function to accept the
Shinya Kitaoka 120a6e
  // container's header data. Check the algorithms description if necessary.
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//******************************************************************************
Toshihiro Shimizu 890ddd
//    Some Actual Container Readers
Toshihiro Shimizu 890ddd
//******************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename container,="" container::value_type="" typename="" val="typename"></typename>
Shinya Kitaoka 120a6e
class sequential_reader {
Shinya Kitaoka 120a6e
  Container *m_values;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  typedef Container container_type;
Shinya Kitaoka 120a6e
  typedef Val value_type;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  sequential_reader(Container *container) : m_values(container) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void openContainer() { m_values->clear(); }
Shinya Kitaoka 120a6e
  void addElement(const value_type &val) { m_values->push_back(val); }
Shinya Kitaoka 120a6e
  void closeContainer() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const container_type *values() const { return m_values; }
Shinya Kitaoka 120a6e
  container_type *values() { return m_values; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename container,="" container::value_type="" typename="" val="typename"></typename>
Shinya Kitaoka 120a6e
class ordered_reader {
Shinya Kitaoka 120a6e
  Container *m_values;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  typedef Container container_type;
Shinya Kitaoka 120a6e
  typedef Val value_type;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ordered_reader(Container &container) : m_values(container) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void openContainer() { m_values->clear(); }
Shinya Kitaoka 120a6e
  void addElement(const value_type &val) { m_values->insert(val); }
Shinya Kitaoka 120a6e
  void closeContainer() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const container_type *values() const { return m_values; }
Shinya Kitaoka 120a6e
  container_type *values() { return m_values; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
template 
Shinya Kitaoka 120a6e
          typename Val = typename ReaderType::value_type>
Shinya Kitaoka 120a6e
class multiple_reader {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  typedef Container container_type;
Shinya Kitaoka 120a6e
  typedef ReaderType reader_type;
Shinya Kitaoka 120a6e
  typedef Val value_type;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  Container *m_storage;
Shinya Kitaoka 120a6e
  std::stack<reader_type> m_stack;</reader_type>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  multiple_reader(Container *storage) : m_storage(storage) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  reader_type &reader() { return m_stack.top(); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void openContainer() {
Shinya Kitaoka 120a6e
    m_stack.push(reader_type(new typename reader_type::container_type));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void addElement(const value_type &val) { reader().addElement(val); }
Shinya Kitaoka 120a6e
  void closeContainer() {
Shinya Kitaoka 120a6e
    if (reader().values()->empty())
Shinya Kitaoka 120a6e
      delete reader().values();
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      m_storage->push_back(reader().values());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    m_stack.pop();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const container_type &storage() const { return m_storage; }
Shinya Kitaoka 120a6e
  container_type &storage() { return m_storage; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCG_CONTAINERS_READER_H