/*
 * Copyright 2017 Jan Dorazil <deu439@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef READDATA_HPP
#define READDATA_HPP

#include <stddef.h>
#include <vector>
#include <map>
#include <string>

#include "dataunit.hpp"

class DataIterator;

class ReadData
{
    typedef std::map<std::string, std::string> RecordMap;
    std::vector< RecordMap > mRecords;
    std::string prefix;
    
public:
    typedef DataIterator iterator;
    typedef ptrdiff_t difference_type;
    typedef size_t size_type;
    typedef DataUnit value_type;
    typedef DataUnit * pointer;
    typedef DataUnit & reference;
    
    friend class DataIterator;
    
    ReadData( const std::string &xmlfile );
    DataUnit getUnit( int i );
    
    iterator begin();
    iterator end();
    
};

class DataIterator {
  ReadData &mReader;
  int mIdx = 0;
  
public:
    DataIterator(ReadData &reader, int idx);
    
    bool operator!=(const DataIterator &other) const
      { return mIdx != other.mIdx; }
      
    DataIterator &operator++() { ++mIdx; return *this; }
    DataUnit operator*() { return mReader.getUnit( mIdx ); };
};

#endif // READDATA_HPP
