// src/backend/concrete_history.hh - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#ifndef CONCRETE_HISTORY_HH
#define CONCRETE_HISTORY_HH 1

#include "config.h"

#include "concrete_environment.hh"
#include "dynamic_process.hh"
#include "dynamic_request.hh"
#include "dynamic_resource.hh"
#include "dynamic_sub_request.hh"
#include "dynamic_thread.hh"
#include <sgpemv2/history.hh>

#include <glibmm/ustring.h>

#include <map>
#include <stdexcept>
#include <utility>
#include <vector>

namespace sgpem
{
  class ConcreteHistory;

  class SG_DLLLOCAL ConcreteHistory : public History
  {
  public:
    ConcreteHistory();
    ConcreteHistory(const ConcreteHistory&);
    virtual ~ConcreteHistory();

    virtual       void                 append_new_environment(ConcreteEnvironment* environment);
    virtual       size_t               get_size() const;
    virtual const ConcreteEnvironment& get_last_environment() const;
    virtual const ConcreteEnvironment& get_environment_at(position index) const throw(std::out_of_range);

    virtual void  remove(resource_key_t resource_key);
    virtual void  remove(Process&    process);
    virtual void  remove(Thread&     thread);
    virtual void  remove(Request&    request);
    virtual void  remove(SubRequest& subrequest);

    virtual void clear();

    virtual ResourcePair        add_resource(const Glib::ustring& name,
        bool   preemptable  = false,
        size_t places       = 1,
        size_t availability = 0);

    virtual void        edit_resource(Resource& resource,
                                      const Glib::ustring& name,
                                      bool   preemptable  = false,
                                      size_t places       = 1,
                                      size_t availability = 0);

    virtual DynamicProcess&     add_process(const Glib::ustring& name,
                                            time_t arrival_time,
                                            prio_t base_priority = 0);

    virtual void                edit_process(Process& process,
                                             const Glib::ustring& name,
                                             time_t arrival_time,
                                             prio_t base_priority = 0);

    virtual DynamicThread&      add_thread(const Glib::ustring& name,
                                           Process& parent,
                                           time_t cpu_time,
                                           time_t arrival_time = 0,
                                           prio_t base_priority = 0);

    virtual void                edit_thread(Thread& thread,
                                            const Glib::ustring& name,
                                            time_t cpu_time,
                                            time_t arrival_time = 0,
                                            prio_t base_priority = 0);


    virtual DynamicRequest&     add_request(Thread& owner,
                                            time_t instant);

    virtual void                edit_request(Request& request,
                                             time_t instant);

    virtual DynamicSubRequest&  add_subrequest(Request& request,
                                               resource_key_t resource_key,
                                               time_t  duration);

    virtual void                edit_subrequest(SubRequest& subrequest,
                                               resource_key_t resource_key,
                                               time_t  duration);

    // sets the front to position p
    virtual void set_front(position p);

		bool is_sealed() const;

		// (Returns if the History was sealed before this call)
		bool seal();

    void reset(); // Implements a virtual method
    void reset(bool notify); // Only available in this class

  protected:
    typedef std::vector<ConcreteEnvironment*> Snapshots;
    Snapshots _snapshots;

    virtual void notify_change();

  private:
    // Disable assignment, implement it only if needed
    ConcreteHistory& operator=(const ConcreteHistory& op2);
	
		bool _sealed;
  }
  ; //~ class ConcreteHistory

}//~ namespace sgpem

#endif //~ CONCRETE_HISTORY_HH


