// ---------------------------------------------------------------------------
// - Instance.hpp                                                            -
// - afnix engine - afnix instance class definition                          -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  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.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2008 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_INSTANCE_HPP
#define  AFNIX_INSTANCE_HPP

#ifndef  AFNIX_CLASS_HPP
#include "Class.hpp"
#endif

namespace afnix {

  /// The Instance class is used as an instance of the Class class. When an
  /// instance is created, all data members are bounded in the instance local
  /// nameset. By default, all data members are set to nil. If a constructor
  /// is invoked with arguments, the data members are set with them.
  /// @author amaury darsch

  class Instance : public virtual Object {
  private:
    /// the meta class
    Class*  p_meta;
    /// the meta flag
    bool    d_ctmta;
    /// the super instance
    Object* p_super;
    /// the super flag
    bool    d_ctsup;
    /// the instance localset
    Localset* p_iset;

  public:
    /// create a default instance
    Instance (void);

    /// create an instance with a meta class
    /// @param meta the meta class to bind
    Instance (Class* meta);

    /// destroy this instance
    ~Instance (void);

    /// @return the class name
    String repr (void) const;

    /// reset this instance
    virtual void reset (void);

    /// set the instance meta class object
    /// @param object the meta object to set
    /// @param flag   the meta const flag
    virtual Object* setmeta (Object* object, const bool flag);

    /// set the super instance value
    /// @param object the super object to set
    /// @param flag   the super const flag
    virtual Object* setsuper (Object* object, const bool flag);

  private:
    // make the copy constructor private
    Instance (const Instance&);
    // make the assignment operator private
    Instance& operator = (const Instance&);

  public:
    /// create a new object in a generic way
    /// @param argv the argument vector
    static Object* mknew (Vector* argv);

    /// @return true if the given quark is defined
    bool isquark (const long quark, const bool hflg) const;

    /// preset the instance by arguments
    /// @param robj the current runnable
    /// @param nset the current nameset
    /// @param args the arguments to apply
    Object* pdef (Runnable* robj, Nameset* nset, Cons* args);
    
    /// mute the instance by arguments
    /// @param robj the current runnable
    /// @param nset the current nameset
    /// @param args the arguments to apply
    Object* mute (Runnable* robj, Nameset* nset, Cons* args);

    /// set a constant object by quark
    /// @param robj   the current runnable
    /// @param nset   the current nameset
    /// @param quark  the quark to define as const
    /// @param object the object to set
    Object* cdef (Runnable* robj, Nameset* nset, const long quark, 
		  Object* object);
    
    /// set an object by quark
    /// @param robj   the current runnable
    /// @param nset   the current nameset
    /// @param quark  the quark to define as trans
    /// @param object the object to set
    Object* vdef (Runnable* robj, Nameset* nset, const long quark, 
		  Object* object);

    /// evaluate an object member by quark
    /// @param robj  the current runnable
    /// @param nset  the current nameset
    /// @param quark the quark to evaluate
    Object* eval (Runnable* robj, Nameset* nset, const long quark);

    /// apply an object method with a set of arguments by quark
    /// @param robj  the current runnable
    /// @param nset  the current nameset    
    /// @param quark the name to apply these arguments
    /// @param args  the arguments to apply
    Object* apply (Runnable* robj, Nameset* nset, const long quark, 
		   Cons* args);

    /// apply an object method by object with a set of arguments
    /// @param robj the current runnable
    /// @param nset the current nameset    
    /// @param name the name to apply this arguments
    /// @param args the arguments to apply
    Object* apply (Runnable* robj, Nameset* nset, Object* object, Cons* args);
  };
}

#endif
