/******************************************************************************* Copyright (c) 2010, Perforce Software, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ /******************************************************************************* * Name : p4base.h * * Author : dbb * * Description : p4base is the base class for all classes which handles * (pointers)for instances of those classes are passed in and out if the DLL. * It provides a mechanism to register the handles to those objects when they * are created so they can be validated when passed in as parameters. * ******************************************************************************/ #include "StdAfx.h" #include "p4base.h" /******************************************************************************* * Keep a doubly linked list of handles for each type to be tracked. *******************************************************************************/ p4base** p4base::pFirstObject = NULL; p4base** p4base::pLastObject = NULL; /******************************************************************************* * Constructor * * Add this object to the correct list, based on its type. * * nType: The type of this object. It is passed from the derived objects * constructer so it can be determined at run time. A call to the virtual * function GetType() does not work here in the base class constructor. * *******************************************************************************/ p4base::p4base(int ntype) { // Check to see if we have allocated are array of lists yet. These will only // be created when the first object is registered. if (!pFirstObject) { pFirstObject = new p4base*[p4typesCount]; for (int i = 0; i < p4typesCount; i++) { pFirstObject[i] = NULL; } } if (!pLastObject) { pLastObject = new p4base*[p4typesCount]; for (int i = 0; i < p4typesCount; i++) { pLastObject[i] = NULL; } } // save the type for use in the destructor when we can no longer use the // virtual function GetType(). type = ntype; // Initialize the list pointers pNextObject = NULL; pPrevObject = NULL; // Add to the list of objects registered to be exported if(!pFirstObject[type]) { // first object, initialize the list with this as the only element pFirstObject[type] = this; pLastObject[type] = this; } else { // add to the end of the list pLastObject[type]->pNextObject = this; pPrevObject = pLastObject[type]; pLastObject[type] = this; } } /******************************************************************************* * Destructor * * Remove this object from the correct list, based on its type. * *******************************************************************************/ p4base::~p4base(void) { // Remove from the list if (!pPrevObject && !pNextObject) { // last object in the list, so NULL out the list head and tail pointers pFirstObject[type] = NULL; pLastObject[type] = NULL; } else if (!pPrevObject && pNextObject) { // first object in list, set the head to the next object in the list pFirstObject[type] = pNextObject; pNextObject->pPrevObject = NULL; } else if (pPrevObject && !pNextObject) { // last object, set the tail to the pervious object in the list pLastObject[type] = pPrevObject; pPrevObject->pNextObject = NULL; } else { // in the middle of the list, so link the pointers for the previous // and next objects. pPrevObject->pNextObject = pNextObject; pNextObject->pPrevObject = pPrevObject; } } /******************************************************************************* * ValidateHandle( p4base* pObject ) * * Static function to validate a handle * * nType: The type of this object. It is passed from the derived objects * constructer so it can be determined at run time. A call to the virtual * function GetType() does not work here in the base class constructor. * *******************************************************************************/ int p4base::ValidateHandle( p4base* pObject, int type ) { if (!pObject) return 0; p4base* pCur = NULL; // Use Windows Structured Exception Handling to detect memory violations __try { if ((type < 0) || (type >=p4typesCount)) return 0; // invalid type pCur = pFirstObject[type]; } __except (1) //EXCEPTION_EXECUTE_HANDLER #if _WIN32 { // access violation, so definitely not valid. return 0; } #endif while ( pCur != NULL) { if (pObject == pCur) // pointers are the same return 1; pCur = pCur->pNextObject; } return 0; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 16210 | Norman Morse | Remove files from old locations | ||
#2 | 12135 | Norman Morse |
Integrate dev branch changes into main. This code is the basiis of the 2.7 BETA release which provides Unity 5 compatibility |
||
#1 | 10940 | Norman Morse |
Inital Workshop release of P4Connect. Released under BSD-2 license |