(file) Return to ArrayRep.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

File: [Pegasus] / pegasus / src / Pegasus / Common / ArrayRep.h (download)
Revision: 1.4, Wed Aug 6 13:31:21 2003 UTC (20 years, 11 months ago) by keith.petley
Branch: MAIN
CVS Tags: RELEASE_2_3_0-msg-freeze
Changes since 1.3: +5 -5 lines
  - Array<T>::create changed local variable capacity to initialCapacity as
    the Solaris compiler keeps warning that the local variable hides a more
    global one. (Not really a problem, just noisy and annoying).

//%/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
// The Open Group, Tivoli Systems
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//==============================================================================
//
// Author: Mike Brasher (mbrasher@bmc.com)
//
// Modified By: Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
//
//%/////////////////////////////////////////////////////////////////////////////

#ifndef Pegasus_ArrayRep_h
#define Pegasus_ArrayRep_h

#include <new>
#include <Pegasus/Common/Config.h>
#include <Pegasus/Common/Memory.h>

PEGASUS_NAMESPACE_BEGIN

/*  ArrayRep<T>
    The ArrayRep object represents the array size, capacity,
    and elements in one contiguous chunk of memory. The elements
    follow immediately after the end of the ArrayRep structure in memory.
    The union is used to force 64 bit alignment of these elements. This is
    a private class and should not be accessed directly by the user.
*/
template<class T>
#ifdef PEGASUS_OS_OS400
/* For OS/400, this forces the alignment mentioned above. */
__align(16) 
#endif
struct ArrayRep
{
    Uint32 size;

    /* This union forces the first element (which follows this structure
        in memory) to be aligned on a 64 bit boundary. It is a requirement
        that even an array of characters be aligned for any purpose (as malloc()
        does). That way, arrays of characters can be used for alignement
        sensitive data.
    */
    union
    {
        Uint32 capacity;
        Uint64 alignment;
    };

    // Obtains a pointer to the first element in the array.
    T* data() { return (T*)(void*)(this + 1); }

    // Same as method above but returns a constant pointer.
    const T* data() const { return (const T*)(void*)(this + 1); }

    /* Creates a clone of the current */
    ArrayRep<T>* clone() const;

    /* Create and initialize a ArrayRep instance. Note that the
        memory for the elements is unitialized and must be initialized by
        the caller.
    */
    static ArrayRep<T>* PEGASUS_STATIC_CDECL create(Uint32 size);

    /* Disposes of the object.
    */
    static void PEGASUS_STATIC_CDECL destroy(ArrayRep<T>* rep);
};

template<class T>
ArrayRep<T>* ArrayRep<T>::clone() const
{
    ArrayRep<T>* rep = ArrayRep<T>::create(capacity);
    rep->size = size;
    CopyToRaw(rep->data(), data(), size);
    return rep;
}

template<class T>
ArrayRep<T>* PEGASUS_STATIC_CDECL ArrayRep<T>::create(Uint32 size)
{
    // Calculate capacity (size rounded to the next power of two).

    Uint32 initialCapacity = 8;

    while (initialCapacity < size)
        initialCapacity <<= 1;

    // Create object:

    ArrayRep<T>* rep =
        (ArrayRep<T>*)operator new(sizeof(ArrayRep<T>) + sizeof(T) * initialCapacity);

    rep->size = size;
    rep->capacity = initialCapacity;

    return rep;
}

template<class T>
void PEGASUS_STATIC_CDECL ArrayRep<T>::destroy(ArrayRep<T>* rep)
{
    if (rep)
    {
        Destroy(rep->data(), rep->size);
        operator delete(rep);
        return;
    }
}

PEGASUS_NAMESPACE_END

#endif /* Pegasus_ArrayRep_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2