#ifndef __RW_EPCNTNR_H__
#define __RW_EPCNTNR_H__

/***************************************************************************
 *
 * ep_cntnr.h - RW_PBase<StdColl,RWColl>
 *   
 *     : pointer-based interface components for STL Containers
 * 
 * $Id: ep_cntnr.h,v 7.24 1996/03/16 13:13:17 jims 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.
 *
 ****************************************************************************
 * This class is at the base of the implementation hierarchy
 * for the pointer-based collections.  The methods here are
 * those which only need to know that StdColl is some sort of
 * an STL-compliant container, whether it holds singleton values
 * as does a vector or set, or pairs as does a map.  RWColl is the
 * Tools.h++ wrapper class that uses StdColl as its implementation.
 * RWColl must ultimately derive from RW_PBase<StdColl, RWColl>
 * and supply a member function std() which returns an instance
 * of StdColl.
 ****************************************************************************
 * 
 * $Log: ep_cntnr.h,v $
 * Revision 7.24  1996/03/16 13:13:17  jims
 * Remove commented specialization of destroy
 *
 * Revision 7.23  1996/03/13 00:50:23  pearson
 * #2812 - Removed RWTExport modifier.  This never has to be in the public
 * interface of Tools.  The initialization of static data in ep_cntnr.cc
 * confused Borland 4.52 which thought it had to perform the initialization
 * in a DLL.  However, this class never gets put into a DLL!
 *
 * Revision 7.22  1996/03/08 13:41:23  jims
 * Make RWDefHArgs and RWDefCArgs available to all
 *
 * Revision 7.21  1996/02/19 12:34:12  jims
 * Port to Stdlib 1.2
 *
 * Revision 7.20  1996/02/18 01:40:32  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.19  1996/02/13 12:14:17  jims
 * Small mods to the interface
 *
 * Revision 7.18  1996/01/30 08:05:08  jims
 * Move functions with 'while' statements out-of-line
 *
 * Revision 7.17  1996/01/24 21:07:41  jims
 * Simplify implementation of otis-stl ptr-based collections
 * by using container<T*> impls instead of container<void*>
 *
 * Revision 7.16  1996/01/19 14:07:54  jims
 * at() now does bounds checking to match Tools 6 semantics
 *
 * Revision 7.15  1996/01/08 17:35:22  griswolf
 * Scopus #2160: DEVNOTE to move loops out of line. Remove note.
 *
 * Revision 7.14  1995/12/15 06:08:08  jims
 * Move minElement and maxElement to e[pv]_scntn.h
 *
 * Revision 7.13  1995/12/12 00:03:06  jims
 * Add RWTExport
 *
 * Revision 7.12  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.11  1995/11/13 12:41:05  jims
 * Add overloaded mfs that take function pointers with value_type args
 *
 * Revision 7.10  1995/10/27 16:04:39  jims
 * port to cset: use templ args in copy ctor parameter
 *
 * Revision 7.9  1995/10/20  06:49:29  jims
 * port to msvc 4.0: add casts to op() const, op[] const
 *
 * Revision 7.8  1995/09/25  11:14:09  jims
 * Fix preprocessor directives (#if RWDEBUG --> #ifdef RWDEBUG)
 *
 * Revision 7.7  1995/09/15  09:59:43  jims
 * More work-arounds for bcc45 re calling stdlib advance
 *
 * Revision 7.6  1995/09/09  01:16:31  jims
 * Add preconditions to min and maxElement mfs
 *
 * Revision 7.5  1995/09/05  21:40:17  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.4  1995/08/21  03:33:45  jims
 * get around problems with operator(), operator[] on Borland
 *
 * Revision 7.3  1995/08/12  00:25:10  kevinj
 * Template persistence design 2.3
 *
 * Revision 7.2  1995/07/27  22:02:33  jims
 * Add header with file name, short description, $Id and $Log
 *
 * Revision 7.2  1995/07/27  22:02:33  jims
 * Add header with file name, short description, $Id and $Log
 * 
 ****************************************************************************/

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

/*
 * StdColl:     the STL collection used for implementation
 * RWColl:      the RW collection which wraps the above
 */

template <class StdColl, class RWColl>
class RW_PBase {
          
public:
  typedef StdColl::value_type        value_type;
  typedef StdColl::size_type         size_type;
  typedef StdColl::iterator          iterator;
  typedef StdColl::const_iterator    const_iterator;
  typedef StdColl::reference         reference;
  typedef StdColl::const_reference   const_reference;

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

  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(); }


/*
 * Unsolved problem with msvc forces cast away of const below:
 */

  const_reference operator() (size_type index) const
  {
#ifdef RWDEBUG
    boundsCheck(index);
#endif
#ifndef _MSC_VER
    const_iterator i = begin();
#else
    iterator i = ((StdColl&)std()).begin();
#endif
    advance(i,index);
    return *i;
  }

  const_reference operator[](size_type index) const
  {
    boundsCheck(index);
#ifndef _MSC_VER
    const_iterator i = begin();
#else
    iterator i = ((StdColl&)std()).begin();
#endif
    advance(i,index);
    return *i;
  }

  reference operator()(size_type index) 
  {
#ifdef RWDEBUG
    boundsCheck(index);
#endif
    iterator i = begin();
    advance(i,index);
    return *i;
  }

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

///// public methods
  void  apply(void (*fn)(value_type,void*),void *x);
  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(bool(*fn)(value_type,void*),void* x) const;

  const_reference first() const  { return *begin(); }
  reference       first()        { return *begin(); }

  StdColl::
  size_type  index(bool (*fn)(value_type, void*), void* x) const;

  StdColl::
  size_type  occurrencesOf(bool (*fn)(value_type,void*),void* x) const;

  static size_type npos;
  

protected:
  void          boundsCheck(size_type i) const
    {
      if (!(i < std().size()) )
        RWTHROW( RWBoundsErr(RWMessage(RWTOOL_INDEXERR, i, entries() )));
    }

// Can only be used as a base class:
  RW_PBase() { }
  RW_PBase(const RW_PBase<StdColl,RWColl>&) { }
};


#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/ep_cntnr.cc"
#endif

#endif /* __RW_EPCNTNR_H__ */

