|
|
HP C++
|
Previous | Contents | Index |
The vector package provides ways to define vectors or stacks of objects of any type by using the macro expansion capability of the C++ preprocessor.
declare(vector, TYPE) |
implement(vector, TYPE) |
class MyType {/*...*/}; declare(vector,MyType) implement(vector,MyType) vector(MyType) vec1(100), vec2(5); MyType x,y; //... if(vec2[4] == y) vec1[98] = x; |
The TYPE parameter must be an identifier. If it is not a class name, a fundamental type, or a type name, create a name for the type using a typedef declaration. For example:
typedef char *PCHAR; declare(vector, PCHAR) implement(vector, PCHAR) implement(vector, PCHAR) void f() { vector(PCHAR) ptrvec(10); char *p = "Text"; ptrvec[0] = p; // ... } |
The generation of error messages within the vector package is not thread safe; the package relies on static members to handle the current error message and there is no synchonization between threads. If this creates a problem for your application, HP recommends that you define a single Mutex object to synchronize all use of the vector package. For more information on synchronizing access to user-defined objects, see Chapter 6.
Provides a generic (parameterized) data abstraction for a fixed-sized stack of objects of some given type.
#include <vector.hxx>#include <vector.h>
TYPE---The type of the objects in the stack. It must be an identifier.
class stack(TYPE): private vector(TYPE) { public: stack(TYPE)(int); // objection size_error stack(TYPE)(stack(TYPE) &); void push(TYPE); // objection overflow_error TYPE pop(); // objection underflow_error TYPE &top(); // objection no_top_error int full(); int empty(); int size(); int size_used(); static Objection overflow_error; static Objection underflow_error; static Objection no_top_error; };
This class provides a generic (parameterized) data abstraction for a fixed-sized stack of objects of some given type.Before a stack object can be declared or implemented, the base class, a vector object with the same type parameter, must also be declared and implemented. To declare a stack object you need to both declare and implement the base vector class and the stack class.
Exceptions are implemented with the Objection package. The initial action function for all objections prints an error message on cerr and calls abort() .
stack(TYPE)(int size)
Constructs a stack object with room for size elements in the stack. If size is less than or equal to 0, the objection vector(TYPE)::size_error is raised.stack(TYPE)(stack(TYPE) &src)
Constructs a stack object that takes the initial values of the elements from another stack object of the same type and size.
The following objections are raised for the stack errors described.static Objection no_top_error
Attempted to reference the top of an empty stack.static Objection overflow_error
Attempted to push too many elements onto the stack.static Objection underflow_error
Attempted to pop an empty stack.
int empty()
Returns TRUE if the stack is empty; otherwise, it returns FALSE .int full()
Returns TRUE if the stack is full; otherwise, it returns FALSE .TYPE pop()
Pops an element off the top of the stack. If the stack underflows, the objection stack(TYPE)::underflow_error is raised.void push(TYPE new_elem)
Pushes an element onto the stack. If the stack overflows, the objection stack(TYPE)::overflow_error is raised.int size()
Returns the maximum number of elements in the stack.int size_used()
Returns the number of elements currently used in a generic stack.TYPE &top()
Returns a reference to the element on the top of the stack. If the stack is empty, the objection stack(TYPE)::no_top_error is raised.
declare(vector, int) implement(vector, int) declare(stack, int) implement(stack, int) void f() { stack(int) st(20); st.push(17); // ... } |
This example shows the four steps required to declare and implement the base vector class and to declare and implement the stack class.
Objection Package
generic Package
vector(TYPE) class
Provides the (parameterized) data abstraction for a fixed-sized vector of objects of some given type.
#include <vector.hxx>#include <vector.h>
TYPE---The type of the objects in the vector. It must be an identifier.
class vector(TYPE) { public: // objection size_error vector(TYPE)(int); vector(TYPE)(vector(TYPE) &); ~vector(TYPE)(); // objection copy_size_error vector(TYPE) &operator=(vector(TYPE) &); TYPE &elem(int); // objection index_error TYPE &operator[](int); int size(); void set_size(int); static Objection size_error; static Objection copy_size_error; static Objection index_error; };
This class provides the (parameterized) data abstraction for a fixed-sized vector of objects of some given type.
Exceptions are implemented with the Objection package. The initial action function for all objections prints an error message on cerr and calls abort() .
vector(TYPE)(int new_size)
Constructs a vector object with the integer argument representing the number of elements in the vector. If the number of elements is less than or equal to 0, the objection vector(TYPE)::size_error is raised.vector(TYPE)(vector(TYPE) &src)
Constructs a vector object that takes initial values of the elements from another vector object of the same type and size.~vector(TYPE)()
Deletes a vector object.
The following objections are raised for the vector errors described.static Objection copy_size_error
Attempted to assign a vector to another vector that has a different number of elements.static Objection index_error
Attempted to reference a vector element with a subscript out of range.static Objection size_error
Attempted to create a vector with less than one element in it.
vector(TYPE) &operator = (vector(TYPE) &src)
Assigns a vector to another vector. If the sizes of the vectors are different, the objection vector(TYPE)::copy_size_error is raised.TYPE &operator [] (int i)
Returns a reference to the ith element in the vector. The value of i has a range from 0 to size() --1. If the subscript is out of bounds, the objection vector(TYPE)::index_error is raised.
TYPE &elem(int i)
Behaves like operator [] but without bounds checking.void set_size(int new_size)
Changes the size of the vector.int size()
Returns the number of elements in the vector.
Objection Package
generic Package
Index | Contents |
|