view oldclasslib/include/array.h @ 24:a4899cdfc2d6 default tip

Obfuscate the regexps to strip off the IBM compiler's copyright banners. I don't want bots scanning github to think they're real copyright notices because that could cause real problems.
author David A. Holland
date Mon, 13 Jun 2022 00:40:23 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * AnaGram, a System for Syntax Directed Programming
 *
 * Array Storage Class Definition
 *
 * Please note that the entire class is defined in this module. There
 * is no supplementary code module.
 *
 * Copyright 1993 Parsifal Software. All Rights Reserved.
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

#ifndef ARRAY_H
#define ARRAY_H

#include <assert.h>
#include <stdio.h>
#include <string.h>

// some forward decls required by current C++
template <class T> class array;
template <class T> unsigned size(const array <T> &c);

template <class T>
class array {
private:
  T *base;                                   // pointer to storage
  unsigned length;                           // size of array
  int *use_count;                            // reference counter

public:

// Constructors

  array (const unsigned n) {
    base = new T[length = n];                // allocate storage
    memset(base,0,n*sizeof(T));              // clear to zero
    use_count = new int;
    *use_count = 1;                          // init use count
  }
  array (const T *src, const unsigned n) {
    base = new T[length = n];                // allocate storage
    memmove(base,src, n*sizeof(T));          // copy initial data
    use_count = new int;
    *use_count = 1;                          // init use count
  }
  array(const array<T> &a) {
    *this = a;
    (*use_count)++;                          // increment use count
  }
  array<T> &operator =(const array<T> &a) {
    if (--(*use_count) == 0) {
      delete [] base;
      delete use_count;
    }
    *this = a;
    (*use_count)++;                          // increment use count
    return *this;
  }

// Destructor
  ~array() {
    if (--(*use_count) == 0) {
      delete [] base;
      delete use_count;
    }
  }

// Access to data

  operator T* () const {              // Return pointer to data
    return base;
  }

  T &operator [](unsigned n) const {        // Direct access to data
    assert(n < length);
    return base[n];
  }

  T &operator [](int n) const {             // Direct access to data
    assert(n >= 0 && (unsigned) n < length);
    return base[n];
  }


// Size of array

#ifdef __IBMCPP__
  friend unsigned size(const array<T> &c);
#else
  friend unsigned size<>(const array<T> &c);
#endif

};

template <class T>
inline unsigned size(const array <T> &c) {
  return c.length;
}

#endif