You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2012/01/25 19:43:24 UTC
svn commit: r1235865 - in /trafficserver/traffic/trunk: lib/ts/TsBuffer.h
lib/tsconfig/TsBuffer.h lib/tsconfig/TsValue.h lib/wccp/Wccp.h
lib/wccp/WccpLocal.h proxy/ControlBase.cc
Author: amc
Date: Wed Jan 25 18:43:24 2012
New Revision: 1235865
URL: http://svn.apache.org/viewvc?rev=1235865&view=rev
Log:
Moving TsBuffer from lib/tsconfig to lib/ts.
Added:
trafficserver/traffic/trunk/lib/ts/TsBuffer.h
- copied, changed from r1235862, trafficserver/traffic/trunk/lib/tsconfig/TsBuffer.h
Removed:
trafficserver/traffic/trunk/lib/tsconfig/TsBuffer.h
Modified:
trafficserver/traffic/trunk/lib/tsconfig/TsValue.h
trafficserver/traffic/trunk/lib/wccp/Wccp.h
trafficserver/traffic/trunk/lib/wccp/WccpLocal.h
trafficserver/traffic/trunk/proxy/ControlBase.cc
Copied: trafficserver/traffic/trunk/lib/ts/TsBuffer.h (from r1235862, trafficserver/traffic/trunk/lib/tsconfig/TsBuffer.h)
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/lib/ts/TsBuffer.h?p2=trafficserver/traffic/trunk/lib/ts/TsBuffer.h&p1=trafficserver/traffic/trunk/lib/tsconfig/TsBuffer.h&r1=1235862&r2=1235865&rev=1235865&view=diff
==============================================================================
--- trafficserver/traffic/trunk/lib/tsconfig/TsBuffer.h (original)
+++ trafficserver/traffic/trunk/lib/ts/TsBuffer.h Wed Jan 25 18:43:24 2012
@@ -4,6 +4,9 @@
/** @file
Definitions for a buffer type, to carry a reference to a chunk of memory.
+ @internal This is a copy of TsBuffer.h in lib/tsconfig. That should
+ eventually be replaced with this promoted file.
+
@section license License
Licensed to the Apache Software Foundation (ASF) under one
@@ -40,6 +43,7 @@ namespace ts {
*/
struct Buffer {
typedef Buffer self; ///< Self reference type.
+ typedef bool (self::*pseudo_bool)() const;
char * _ptr; ///< Pointer to base of memory chunk.
size_t _size; ///< Size of memory chunk.
@@ -55,26 +59,40 @@ namespace ts {
);
/** Equality.
- @return @c true if the @c Buffer instances are identical,
+ @return @c true if @a that refers to the same memory as @a this,
@c false otherwise.
*/
bool operator == (self const& that) const;
/** Inequality.
- @return @c true if the @c Buffer instances are different,
+ @return @c true if @a that does not refer to the same memory as @a this,
@c false otherwise.
*/
bool operator != (self const& that) const;
/** Equality for a constant buffer.
- @return @c true if @a that contains identical contents.
+ @return @c true if @a that refers to the same memory as @a this.
@c false otherwise.
*/
bool operator == (ConstBuffer const& that) const;
/** Inequality.
- @return @c true if the instances have different content,
- @c false if they are identical.
+ @return @c true if @a that does not refer to the same memory as @a this,
+ @c false otherwise.
*/
bool operator != (ConstBuffer const& that) const;
+ /// @return The first character in the buffer.
+ char operator* () const;
+ /** Discard the first character in the buffer.
+ @return @a this object.
+ */
+ self& operator++();
+
+ /// Check for empty buffer.
+ /// @return @c true if the buffer has a zero pointer @b or size.
+ bool operator ! () const;
+ /// Check for non-empty buffer.
+ /// @return @c true if the buffer has a non-zero pointer @b and size.
+ operator pseudo_bool() const;
+
/// @name Accessors.
//@{
/// Get the data in the buffer.
@@ -99,6 +117,7 @@ namespace ts {
*/
struct ConstBuffer {
typedef ConstBuffer self; ///< Self reference type.
+ typedef bool (self::*pseudo_bool)() const;
char const * _ptr; ///< Pointer to base of memory chunk.
size_t _size; ///< Size of memory chunk.
@@ -118,22 +137,22 @@ namespace ts {
);
/** Equality.
- @return @c true if the @c Buffer instances are identical,
+ @return @c true if @a that refers to the same memory as @a this,
@c false otherwise.
*/
bool operator == (self const& that) const;
/** Equality.
- @return @c true if the @c Buffer instances are identical,
+ @return @c true if @a that refers to the same memory as @a this,
@c false otherwise.
*/
bool operator == (Buffer const& that) const;
/** Inequality.
- @return @c true if the @c Buffer instances are different,
+ @return @c true if @a that does not refer to the same memory as @a this,
@c false otherwise.
*/
bool operator != (self const& that) const;
/** Inequality.
- @return @c true if the @c Buffer instances are different,
+ @return @c true if @a that does not refer to the same memory as @a this,
@c false otherwise.
*/
bool operator != (Buffer const& that) const;
@@ -142,13 +161,31 @@ namespace ts {
Buffer const& that ///< Source buffer.
);
+ /// @return The first character in the buffer.
+ char operator* () const;
+ /** Discard the first character in the buffer.
+ @return @a this object.
+ */
+ self& operator++();
+
+ /// Check for empty buffer.
+ /// @return @c true if the buffer has a zero pointer @b or size.
+ bool operator ! () const;
+ /// Check for non-empty buffer.
+ /// @return @c true if the buffer has a non-zero pointer @b and size.
+ operator pseudo_bool() const;
+
/// @name Accessors.
//@{
/// Get the data in the buffer.
char const * data() const;
/// Get the size of the buffer.
size_t size() const;
+ /// Access a character (no bounds check).
+ char operator[] (int n) const;
//@}
+ /// @return @c true if @a p points at a character in @a this.
+ bool contains(char const* p) const;
/// Set the chunk.
/// Any previous values are discarded.
@@ -159,6 +196,67 @@ namespace ts {
);
/// Reset to empty.
self& reset();
+
+ /** Find a character.
+ @return A pointer to the first occurrence of @a c in @a this
+ or @c NULL if @a c is not found.
+ */
+ char const* find(char c) const;
+
+ /** Split the buffer on the character at @a p.
+
+ The buffer is split in to two parts and the character at @a p
+ is discarded. @a this retains all data @b after @a p. The
+ initial part of the buffer is returned. Neither buffer will
+ contain the character at @a p.
+
+ This is convenient when tokenizing and @a p points at the token
+ separator.
+
+ @note If @a *p is not a character in the buffer then @a this
+ is not changed and an empty buffer is returned. This means the
+ caller can simply pass the result of @c find and check for an
+ empty buffer returned to detect no more separators.
+
+ @return A buffer containing data up to but not including @a p.
+ */
+ self splitOn(char const* p);
+
+ /** Split the buffer on the character @a c.
+
+ The buffer is split in to two parts and the occurrence of @a c
+ is discarded. @a this retains all data @b after @a c. The
+ initial part of the buffer is returned. Neither buffer will
+ contain the first occurrence of @a c.
+
+ This is convenient when tokenizing and @a c is the token
+ separator.
+
+ @note If @a c is not found then @a this is not changed and an
+ empty buffer is returned.
+
+ @return A buffer containing data up to but not including @a p.
+ */
+ self splitOn(char c);
+ /** Get a trailing segment of the buffer.
+
+ @return A buffer that contains all data after @a p.
+ */
+ self after(char const* p) const;
+ /** Get a trailing segment of the buffer.
+
+ @return A buffer that contains all data after the first
+ occurrence of @a c.
+ */
+ self after(char c) const;
+ /** Remove trailing segment.
+
+ Data at @a p and beyond is removed from the buffer.
+ If @a p is not in the buffer, no change is made.
+
+ @return @a this.
+ */
+ self& clip(char const* p);
};
// ----------------------------------------------------------
@@ -171,10 +269,17 @@ namespace ts {
inline bool Buffer::operator != (self const& that) const { return ! (*this == that); }
inline bool Buffer::operator != (ConstBuffer const& that) const { return ! (*this == that); }
inline bool Buffer::operator == (self const& that) const {
- return _size == that._size && 0 == memcmp(_ptr, that._ptr, _size);
+ return _size == that._size && _ptr == that._ptr;
}
inline bool Buffer::operator == (ConstBuffer const& that) const {
- return _size == that._size && 0 == memcmp(_ptr, that._ptr, _size);
+ return _size == that._size && _ptr == that._ptr;
+ }
+ inline bool Buffer::operator ! () const { return !(_ptr && _size); }
+ inline Buffer::operator pseudo_bool() const { return _ptr && _size ? &self::operator! : 0; }
+ inline char Buffer::operator * () const { return *_ptr; }
+ inline Buffer& Buffer::operator++ () {
+ if (_ptr && _size) { ++_ptr; --_size; }
+ return *this;
}
inline char * Buffer::data() const { return _ptr; }
inline size_t Buffer::size() const { return _size; }
@@ -193,8 +298,55 @@ namespace ts {
inline bool ConstBuffer::operator == (Buffer const& that) const {
return _size == that._size && 0 == memcmp(_ptr, that._ptr, _size);
}
+ inline bool ConstBuffer::operator ! () const { return !(_ptr && _size); }
+ inline ConstBuffer::operator pseudo_bool() const { return _ptr && _size ? &self::operator! : 0; }
+ inline char ConstBuffer::operator * () const { return *_ptr; }
+ inline ConstBuffer& ConstBuffer::operator++ () {
+ if (_ptr && _size) { ++_ptr; --_size; }
+ return *this;
+ }
inline char const * ConstBuffer::data() const { return _ptr; }
+ inline char ConstBuffer::operator[] (int n) const { return _ptr[n]; }
inline size_t ConstBuffer::size() const { return _size; }
-}
+ inline bool ConstBuffer::contains(char const* p) const {
+ return _ptr <= p && p < _ptr + _size;
+ }
+
+ inline ConstBuffer ConstBuffer::splitOn(char const* p) {
+ self zret(0,0); // default to empty return.
+ if (this->contains(p)) {
+ size_t n = p - _ptr;
+ zret.set(_ptr, n);
+ _ptr = p + 1;
+ _size -= n + 1;
+ }
+ return zret;
+ }
+
+ inline char const* ConstBuffer::find(char c) const {
+ return static_cast<char const*>(memchr(_ptr, c, _size));
+ }
+
+ inline ConstBuffer ConstBuffer::splitOn(char c) {
+ return this->splitOn(this->find(c));
+ }
+
+ inline ConstBuffer ConstBuffer::after(char const* p) const {
+ return this->contains(p) ? self(p + 1, (_size-(p-_ptr))-1) : self(0,0);
+ }
+ inline ConstBuffer ConstBuffer::after(char c) const {
+ return this->after(this->find(c));
+ }
+ inline ConstBuffer& ConstBuffer::clip(char const* p) {
+ if (this->contains(p)) {
+ _size = p - _ptr;
+ }
+ return *this;
+ }
+
+} // end namespace
+
+typedef ts::Buffer TsBuffer;
+typedef ts::ConstBuffer TsConstBuffer;
# endif // TS_BUFFER_HEADER
Modified: trafficserver/traffic/trunk/lib/tsconfig/TsValue.h
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/lib/tsconfig/TsValue.h?rev=1235865&r1=1235864&r2=1235865&view=diff
==============================================================================
--- trafficserver/traffic/trunk/lib/tsconfig/TsValue.h (original)
+++ trafficserver/traffic/trunk/lib/tsconfig/TsValue.h Wed Jan 25 18:43:24 2012
@@ -24,7 +24,7 @@
limitations under the License.
*/
-# include <tsconfig/TsBuffer.h>
+# include <ts/TsBuffer.h>
# include <tsconfig/NumericType.h>
# include <tsconfig/IntrusivePtr.h>
# include <tsconfig/Errata.h>
Modified: trafficserver/traffic/trunk/lib/wccp/Wccp.h
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/lib/wccp/Wccp.h?rev=1235865&r1=1235864&r2=1235865&view=diff
==============================================================================
--- trafficserver/traffic/trunk/lib/wccp/Wccp.h (original)
+++ trafficserver/traffic/trunk/lib/wccp/Wccp.h Wed Jan 25 18:43:24 2012
@@ -23,7 +23,7 @@
limitations under the License.
*/
-# include <tsconfig/TsBuffer.h>
+# include <ts/TsBuffer.h>
# include <tsconfig/Errata.h>
# include <memory.h>
# include <ink_port.h>
Modified: trafficserver/traffic/trunk/lib/wccp/WccpLocal.h
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/lib/wccp/WccpLocal.h?rev=1235865&r1=1235864&r2=1235865&view=diff
==============================================================================
--- trafficserver/traffic/trunk/lib/wccp/WccpLocal.h (original)
+++ trafficserver/traffic/trunk/lib/wccp/WccpLocal.h Wed Jan 25 18:43:24 2012
@@ -25,7 +25,7 @@
# include "Wccp.h"
# include "WccpUtil.h"
-# include <tsconfig/TsBuffer.h>
+# include <ts/TsBuffer.h>
// Needed for template use of byte ordering functions.
# include <netinet/in.h>
# include <memory.h>
Modified: trafficserver/traffic/trunk/proxy/ControlBase.cc
URL: http://svn.apache.org/viewvc/trafficserver/traffic/trunk/proxy/ControlBase.cc?rev=1235865&r1=1235864&r2=1235865&view=diff
==============================================================================
--- trafficserver/traffic/trunk/proxy/ControlBase.cc (original)
+++ trafficserver/traffic/trunk/proxy/ControlBase.cc Wed Jan 25 18:43:24 2012
@@ -45,7 +45,7 @@
#include "HdrUtils.h"
#include "Vec.h"
-#include "tsconfig/TsBuffer.h"
+#include <ts/TsBuffer.h>
/** Used for printing IP address.
@code