#ifndef __RWEV_CNTNR_H__
#define __RWEV_CNTNR_H__

/***************************************************************************
 *
 * ev_cntnr.h - RW_VBase<StdColl,RWColl>
 *   
 *     : value-based interface components for STL Containers
 * 
 * $Id: ev_cntnr.h,v 7.28 1996/03/15 00:49:21 pearson Exp $
 *
 * (c) Copyright 1989 - 1996 Rogue Wave Software, Inc.
 * ALL RIGHTS RESERVED
 *
 * The software and information contained herein are proprietary to, and
 * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
 * intends to preserve as trade secrets such software and information.
 * This software is furnished pursuant to a written license agreement and
 * may be used, copied, transmitted, and stored only in accordance with
 * the terms of such license and with the inclusion of the above copyright
 * notice.  This software and information or any other copies thereof may
 * not be provided or otherwise made available to any other person.
 *
 * Notwithstanding any other lease or license that may pertain to, or
 * accompany the delivery of, this computer software and information, the
 * rights of the Government regarding its use, reproduction and disclosure
 * are as set forth in Section 52.227-19 of the FARS Computer
 * Software-Restricted Rights clause.
 * 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7013.
 * Contractor/Manufacturer is Rogue Wave Software, Inc.,
 * P.O. Box 2328, Corvallis, Oregon 97339.
 *
 * This computer software and information is distributed with "restricted
 * rights."  Use, duplication or disclosure is subject to restrictions as
 * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
 * Computer Software-Restricted Rights (April 1985)."  If the Clause at
 * 18-52.227-74 "Rights in Data General" is specified in the contract,
 * then the "Alternate III" clause applies.
 *
 ****************************************************************************
 * 
 * $Log: ev_cntnr.h,v $
 * Revision 7.28  1996/03/15 00:49:21  pearson
 * Remove RWTExport modifier on RWV_Base, it is not necessary and causes
 * problems for Borland 4.52 because of the initialization of the static
 * member npos.
 *
 * Revision 7.27  1996/03/14 23:50:00  kyle
 * Millenium corrected support for RW_HEADER_WITH_EXTENSION
 *
 * Revision 7.26  1996/03/08 13:41:23  jims
 * Make RWDefHArgs and RWDefCArgs available to all
 *
 * Revision 7.25  1996/02/18 01:41:15  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.24  1996/01/29 23:55:24  pearson
 * Reverse sense of RW_NO_NEW_HEADERS
 *
 * Revision 7.23  1996/01/29 18:23:42  kyle
 * Millenium updates include files to work with pre-C++ Standard
 * headers using RWSTD_NO_NEW_HEADER config define
 *
 * Revision 7.22  1996/01/23 07:38:53  jims
 * at() now does bounds checking to match Tools 6 semantics
 *
 * Revision 7.21  1995/12/18 21:55:44  pearson
 * Use existing typedef instead of scoping to StdColl::size_type, fixes error
 * in SGI compiler.
 *
 * Revision 7.20  1995/12/15 06:08:08  jims
 * Move minElement and maxElement to e[pv]_scntn.h
 *
 * Revision 7.19  1995/12/12 00:03:06  jims
 * Add RWTExport
 *
 * Revision 7.18  1995/12/11 22:30:41  jims
 * Remove 'Sca' from interior class names
 * Add 'rw_' to global helper classes
 * Change 'muterator' to 'filterator'
 *
 * Revision 7.17  1995/10/20 07:07:01  jims
 * port to msvc 4.0
 *
 * Revision 7.16  1995/10/02  17:57:59  pearson
 * Removed RW_BC5_MULT_DECL_BUG workaround, no longer needed.
 *
 * Revision 7.15  1995/09/15  09:57:38  jims
 * Use (reference) casts in mf first et al to work around problem
 * with SL1016 re reference, const_reference typedefs
 *
 * Revision 7.14  1995/09/07  22:40:26  griswolf
 * remove DOS-ish ^M characters.
 *
 * Revision 7.13  1995/09/07  22:07:28  pearson
 * Added RW_BC5_MULT_DECL_BUG to work around template instantiation problem
 * with Borland 5.0 compiler(Beta)
 *
 * Revision 7.12  1995/09/05  21:53:05  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.11  1995/08/11  17:03:34  kevinj
 * Template persistence 2.2.
 *
 * Revision 7.10  1995/07/27  22:02:33  jims
 * Add header with file name, short description, $Id and $Log
 * 
 ****************************************************************************/

#include "rw/defs.h"
#include "rw/rwerr.h"
#include "rw/toolerr.h"

#ifndef RW_HEADER_WITH_EXTENSION
# include <algorithm>
# include <iterator>
#else
#  include <algorithm.hpp>
#  include <iterator.hpp>
#endif  /* RW_HEADER_WITH_EXTENSION */

template <class StdColl, class RWColl>
class RW_VBase {
public:
  typedef StdColl::iterator                 iterator;
  typedef StdColl::const_iterator           const_iterator;
  typedef StdColl::size_type                size_type;
  typedef StdColl::value_type               value_type;
  typedef StdColl::reference                reference;
  typedef StdColl::const_reference          const_reference;
/* The following is not supported by set -- shouldn't it be? */
//  typedef StdColl::pointer pointer

  StdColl&        std()       { return ((RWColl*) this)->impl_; }
  const StdColl&  std() const { return ((const RWColl*) this)->impl_; }

// Allow easy access to iterators:
  iterator        begin()         { return std().begin(); }
  iterator        end()           { return std().end();   }

  const_iterator  begin() const   { return std().begin(); }
  const_iterator  end()   const   { return std().end();   }

// RW interface: public member functions
  size_type       entries() const { return std().size(); }
  bool            isEmpty() const { return std().empty(); }

///// public methods
  void  apply(void (*fn)(const_reference,void*),void *x) const;

  void  apply(void (*fn)(reference,void*),void *x);

  const_reference at(size_type index) const { return (*this)[index]; }
  reference       at(size_type index)       { return (*this)[index]; }
    
  bool          contains(const_reference) const;

  bool          contains(
                        bool(*fn)(const_reference,void*),
                        void* x
                        ) const;

///// operators
  const_reference operator[](size_type index) const
  {
    boundsCheck(index);
    const_iterator i = begin();
    advance(i,index);
    return *i;
  }

  reference operator[](size_type index) 
  {
    boundsCheck(index);
    iterator i = begin();
    advance(i,index);
    return (reference) *i;
  }

  const_reference operator()(size_type index) const
  {
    RWPRECONDITION(index < entries()); 
    const_iterator i = begin();
    advance(i,index);
    return *i;
  }

  reference operator()(size_type index) 
  {
    RWPRECONDITION(index < entries()); 
    iterator i = begin();
    advance(i,index);
    return (reference) *i;
  }

  bool    operator==(const RWColl& rwc)   const { return std() == rwc.std(); }
  bool    operator< (const RWColl& rwc)   const { return std() < rwc.std(); }
  bool    operator==(const StdColl& stdc) const { return std() == stdc; }
  bool    operator< (const StdColl& stdc) const { return std() < stdc;  }

  const_reference first() const
    { RWPRECONDITION(!isEmpty()); return *(begin()); }

  reference       first()
    { RWPRECONDITION(!isEmpty()); return (reference) *(begin()); }

  size_type index(bool (*fn)(const_reference, void*),
                           void* x) const;

  size_type     index(const_reference key) const;

  size_type     occurrencesOf(const_reference) const;

  size_type     occurrencesOf(bool (*fn)(const_reference,void*),void* x) const;

  static size_type npos;

protected:
// This class can only be used as a base class
// Note also that no member functions may be called during construction
//   since they all depend on the pseudo-virtual function "std()"
  RW_VBase() { }
  RW_VBase(const RW_VBase<StdColl,RWColl>&) { }

  void          boundsCheck(size_type i) const
    {
      if (i >= entries())
        RWTHROW( RWBoundsErr(RWMessage(RWTOOL_INDEXERR,
                                       (unsigned)i, (unsigned) entries()) ));
    }
};

#ifndef RWDefCArgs
#  define RWDefCArgs(T) ,less<T>
#endif

#ifndef RWDefHArgs
#  define RWDefHArgs(T) ,RWTHasher<T>,equal_to<T>
#endif


#ifdef RW_COMPILE_INSTANTIATE
#include "rw/ev_cntnr.cc"
#endif

#endif /* __RWEV_CNTNR_H__ */
