CopyOnWriteData.hxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:13k
源码类别:
流媒体/Mpeg4/MP4
开发平台:
C/C++
- #ifndef COPY_ON_WRITE_DATA_HXX_
- #define COPY_ON_WRITE_DATA_HXX_
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, 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.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF $1,000, NOR FOR ANY 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.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- static const char* const CopyOnWriteData_hxx_Version =
- "$Id: CopyOnWriteData.hxx,v 1.15 2001/05/28 21:28:34 bko Exp $";
- // this code is for defining an assert-like method for enforcing
- // singlethreadedness.
- #include <cstring>
- #include <string>
- #include "mstring.hxx"
- #include "VException.hxx"
- #include "CWBuffer.hxx"
- #include "ThreadSafeBuffer.hxx"
- #include "DataException.hxx"
- #include "limits.h"
- // set this to 0 to turn off
- #define VOCAL_DEBUG_BAD_USAGE_OF_DATA 0
- #if VOCAL_DEBUG_BAD_USAGE_OF_DATA
- #include "AssertSingleThreaded.hxx"
- #define ASSERT_UNLOCK(x___) x___.unlock()
- #define ASSERT_WRITELOCK(x___) x___.writelock()
- #define ASSERT_READLOCK(x___) x___.readlock()
- #else
- #define ASSERT_WRITE(x, SingleThreadAssertable_) ((void) 0)
- #define ASSERT_READ(x, SingleThreadAssertable_) ((void) 0)
- #define ASSERT_UNLOCK(x___) ((void) 0)
- #define ASSERT_WRITELOCK(x___) ((void) 0)
- #define ASSERT_READLOCK(x___) ((void) 0)
- #endif
- /** Class for representing binary data and strings, in a thread-safe
- ** manner. This version is implemented as copy on write buffers.
- */
- class CopyOnWriteData
- {
- public:
- /// "no position" -- used to indicate non-position when returning a value
- static const int npos = INT_MAX;
- /// Default constructor
- CopyOnWriteData();
- /** constructor for C style strings
- ** @param str null-terminated (C style) character array
- */
- CopyOnWriteData( const char* str );
- /** constructor for character arrays with length
- ** @param buffer character array
- ** @param len size of buffer
- */
- CopyOnWriteData( const char* buffer, int length );
- /// copy constructor
- CopyOnWriteData( const CopyOnWriteData& data );
- /** constructor for C++ strings
- */
- CopyOnWriteData( const string& str);
- /** constructor for mstring (a specialization of C++ strings)
- */
- CopyOnWriteData( const mstring& mstr);
- /// constructor that converts an int to a Data
- CopyOnWriteData( const int value);
- /// destructor
- ~CopyOnWriteData();
- /** compares two Data objects, returning the value of a
- * dictionary comparison of the two strings
- */
- bool operator>(const CopyOnWriteData& ) const ;
- /** compares two Data objects, returning the value of a
- * dictionary comparison of the two strings
- */
- bool operator<(const CopyOnWriteData& ) const;
- /** compares two Data objects, returning the value of a
- * dictionary comparison of the two strings
- */
- bool operator>(const char* ) const ;
- /** compares two Data objects, returning the value of a
- * dictionary comparison of the two strings
- */
- bool operator<(const char* ) const;
- /** assignment operator
- ** @param str C string character array
- */
- CopyOnWriteData& operator=(const char* str);
- /** assignment operator
- ** @param data CopyOnWriteData object
- */
- CopyOnWriteData& operator=(const CopyOnWriteData& data);
- /// returns a NUL terminated (a C string) buffer
- const char* getData() const;
- /** returns a pointer to the buffer. Note that this buffer is
- ** NOT NUL terminated (not a C string).
- */
- const char* getDataBuf() const;
- /** return one character from the string
- ** @param i index into the Data object
- */
- char getChar( int i ) const;
- /** return one character from the string
- ** @param i index into the object
- ** @param c character to set
- */
- void setchar( int i, char c );
- /** return one character from the string
- ** @param i index into the object
- */
- char operator[]( int i ) const;
- /// length of the Data object
- int length() const;
- /** suggest a size for the underlying buffer (optimizes
- ** performance in some implementations)
- */
- void setBufferSize(int size);
- /** equality operator
- ** @param str C string to compare to
- */
- bool operator==( const char* str ) const;
- /** equality operator
- ** @param data Data to compare to
- */
- bool operator==( const CopyOnWriteData& data ) const;
- /// inequality operator
- bool operator!=( const char* str ) const;
- /// inequality operator
- bool operator!=( const CopyOnWriteData& data ) const;
- /// friend to compare a c-style string to a data (avoids conversion)
- friend bool operator==( const char* str, const CopyOnWriteData& d );
- /// friend to compare a c-style string to a data (avoids conversion)
- friend bool operator!=( const char* str, const CopyOnWriteData& d );
- ///
- int compare(const char* str, int length) const;
- ///
- int compare(const CopyOnWriteData& data) const;
- ///
- int compareNoCase(const char* str, int length) const;
- ///
- int compareNoCase(const CopyOnWriteData& data) const;
- /** concatenate two Data objects together. Warning -- this
- * creates an extra copy of the Data object, so it is not
- * terribly efficient. If possible, it is better to use +=
- * instead.
- */
- CopyOnWriteData operator+( const CopyOnWriteData& data) const;
- /** concatenate a Data object and a C-style string together.
- * Warning -- this creates an extra copy of the Data object,
- * so it is not terribly efficient. If possible, it is better
- * to use += instead.
- */
- CopyOnWriteData operator+( const char* str) const;
- /** append a Data object d to this Data.
- ** this is potentially much more efficient than operator+().
- */
- void operator+=(const CopyOnWriteData& d);
- /** append a string s to this Data.
- ** this is potentially much more efficient than operator+().
- */
- void operator+=(const char*);
- /// erase this object
- void erase();
- /// convert Data to uppercase
- void lowercase();
- /// convert Data to lowercase
- void uppercase();
- /// convert to a string
- operator string() const;
- /// convert to a C style character array
- operator const char*() const;
- /// convert to an mstring
- operator mstring() const;
- /// convert to an int (depreciated)
- operator int() const;
- /** match the string and return the text prior to the match.
- * If a match is found, this Data is set to the remainder
- * after the matched string.
- *
- * @param match the string to be matched
- * @param beforeMatch the data before the matched string
- * @param replace whether to replace the matched data
- * @param replaceWith the data to replace the matched data
- */
- int match(const char* match,
- CopyOnWriteData* data,
- bool replace = false,
- CopyOnWriteData replaceWith = "");
- /**
- match (and eat) the first contiguous block composed of the
- characters in match, which is outside of double quotes <">
- and angle brackets "<" and ">". Returned is the data
- before the matched characters. If no characters match,
- return the empty Data. If matchFail is set to a bool ptr,
- the bool *matchFail will be set to true if the match
- fails, and false otherwise.
- This is designed for use in separating a list of
- parameters at the commas (e.g. Contact:)
- */
- CopyOnWriteData parseOutsideQuotes(const char* match,
- bool useQuote,
- bool useAngle,
- bool* matchFail = 0 );
- /**
- match (and eat) the first contiguous block composed of the
- characters in match. Returned is the data before the
- matched characters. If no characters match, return the
- empty Data. If matchFail is set to a bool ptr, the bool
- *matchFail will be set to true if the match fails, and
- false otherwise.
- */
- CopyOnWriteData parse(const char* match, bool* matchFail = 0 );
- /** match (and eat) any one of the characters in match. If
- matchedChar points to a char, it will be set to the
- matching character, or