From 55518e7f5a7780093a929a755c7366314fd23160 Mon Sep 17 00:00:00 2001 From: skidd13 Date: Fri, 19 Mar 2010 22:14:12 +0100 Subject: [PATCH 5/9] -Codechange: Rename CBlobT to SmallBlob -Codechange: apply coding style to SmallBlob -Doc: SmallBlob --- src/misc/blob.hpp | 200 ++++++++++++++++++++------------- src/misc/str.hpp | 4 +- src/pathfinder/yapf/yapf_costrail.hpp | 6 +- 3 files changed, 129 insertions(+), 81 deletions(-) diff --git a/src/misc/blob.hpp b/src/misc/blob.hpp index d5c88c4..a31666c 100644 --- a/src/misc/blob.hpp +++ b/src/misc/blob.hpp @@ -264,112 +264,160 @@ public: } }; -/** Blob - simple dynamic T array. T (template argument) is a placeholder for any type. - * T can be any integral type, pointer, or structure. Using Blob instead of just plain C array - * simplifies the resource management in several ways: - * 1. When adding new item(s) it automatically grows capacity if needed. - * 2. When variable of type Blob comes out of scope it automatically frees the data buffer. - * 3. Takes care about the actual data size (number of used items). - * 4. Dynamically constructs only used items (as opposite of static array which constructs all items) */ +/** + * SmallBlob class - a fast dynamic T array based on ByteBlob + * SmallBlob is mainly a wrapper for ByteBlob which cares about + * type_size. + * + * @see ByteBlob + * + * @tparam T the type of items stored in the array + */ template -class CBlobT : public ByteBlob { - /* make template arguments public: */ -public: +class SmallBlob : public ByteBlob { +protected: typedef ByteBlob base; - static const uint type_size = sizeof(T); +public: + /** Wrapper to simplify the take ownership process */ struct OnTransfer { typename base::BlobHeader *header; - OnTransfer(const OnTransfer& src) : header(src.header) {assert(src.header != NULL); *const_cast(&src.header) = NULL;} - OnTransfer(CBlobT& src) : header(src.header) {src.Init();} - ~OnTransfer() {assert(header == NULL);} + + OnTransfer(const OnTransfer& src) + : header(src.header) + { + assert(src.header != NULL); + *const_cast(&src.header) = NULL; + } + + OnTransfer(SmallBlob& src) + : header(src.header) + { + src.Init(); + } + + ~OnTransfer() + { + assert(this->header == NULL); + } }; /** Default constructor - makes new Blob ready to accept any data */ - FORCEINLINE CBlobT() - : base() - {} + FORCEINLINE SmallBlob() : base() {} - /** Take ownership constructor */ - FORCEINLINE CBlobT(const OnTransfer& ot) - : base(ot.header) - {} + /** + * Take ownership constructor + * + * @param ot the transfer object which content we want to overtake + */ + FORCEINLINE SmallBlob(const OnTransfer &ot) : base(ot.header) {} - /** Return pointer to the first data item - non-const version */ - FORCEINLINE T *Begin() - { - return (T*)base::Begin(); - } + /** + * Get the number of items in the Blob. + * + * @return number of items in the Blob + */ + FORCEINLINE uint Length() const { return base::Length() / type_size; } - /** Return pointer to the first data item - const version */ - FORCEINLINE const T *Begin() const - { - return (const T*)base::Begin(); - } + /** + * Get the maximum number of items in the Blob. + * + * @return capacity of the Blob + */ + FORCEINLINE uint Capacity() const { return base::Capacity() / type_size; } - /** Return pointer to the index-th data item - non-const version */ + /** + * Get the number of additional items that can fit in the Blob + * without buffer reallocation. + * + * @return number of additional items that fit into current capacity + */ + FORCEINLINE uint Reserve() const { return base::Reserve() / type_size; } + + /** + * Get the pointer to the first item + * + * @return the pointer to the first item + */ + FORCEINLINE T *Begin() { return (T *)base::Begin(); } + + /** + * Get the pointer to the first item (const) + * + * @return the pointer to the first item + */ + FORCEINLINE const T *Begin() const { return (T *)base::Begin(); } + + /** + * Get the pointer to item "number" + * + * @param index the position of the item + * @return the pointer to the item + */ FORCEINLINE T *Get(uint index) { assert(index < Length()); return (Begin() + index); } - /** Return pointer to the index-th data item - const version */ + /** + * Get the pointer to item "number" (const) + * + * @param index the position of the item + * @return the pointer to the item + */ FORCEINLINE const T *Get(uint index) const { assert(index < Length()); return (Begin() + index); } - /** Return reference to the index-th data item - non-const version */ - FORCEINLINE T &operator[](uint index) - { - return *Get(index); - } - - /** Return reference to the index-th data item - const version */ - FORCEINLINE const T &operator[](uint index) const - { - return *Get(index); - } - - /** Return number of items in the Blob */ - FORCEINLINE uint Length() const - { - return (base::Length() / type_size); - } + /** + * Get item "number" + * + * @param index the positon of the item + * @return the item + */ + FORCEINLINE T &operator[](uint index) { return *Get(index); } - /** Return total number of items that can fit in the Blob without buffer reallocation */ - FORCEINLINE uint Capacity() const - { - return (base::Capacity() / type_size); - } + /** + * Get item "number" (const) + * + * @param index the positon of the item + * @return the item + */ + FORCEINLINE const T &operator[](uint index) const { return *Get(index); } - /** Return number of additional items that can fit in the Blob without buffer reallocation */ - FORCEINLINE uint Reserve() const - { - return base::Reserve() / type_size; - } + /** Prepare addition of num_items + * + * @param num_items the number of items to append + * @return the pointer to the new data to append + */ + FORCEINLINE T *Prepare(uint num_items) { return (T*)base::Prepare(num_items * type_size); } - /** Grow number of data items in Blob by given number - doesn't construct items */ - FORCEINLINE T *Append(uint num_items) - { - return (T*)base::Append(num_items * type_size); - } + /** + * Append a number of items and return the first. + * + * @param num_items the number of items to append + * @return pointer to first newly allocated item + */ + FORCEINLINE T *Append(uint num_items) { return (T*)base::Append(num_items * type_size); } - /** Ensures that given number of items can be added to the end of Blob. Returns pointer to the - * first free (unused) item */ - FORCEINLINE T *Prepare(uint num_items) - { - return (T*)base::Prepare(num_items * type_size); - } + /** + * Append (copy) a number of items to the end of the blob. + * + * @param src the pointer to the first item to append + * @param num_items the number of items to be append + */ + FORCEINLINE void Append(const T *src, uint num_items) { base::Append(src, num_items * type_size); } - FORCEINLINE OnTransfer Transfer() - { - return OnTransfer(*this); - }; + /** + * Get transfer object for current blob content + * + * @return the transfer object + */ + FORCEINLINE OnTransfer Transfer() { return OnTransfer(*this); } }; - #endif /* BLOB_HPP */ diff --git a/src/misc/str.hpp b/src/misc/str.hpp index bc1671c..921accb 100644 --- a/src/misc/str.hpp +++ b/src/misc/str.hpp @@ -18,9 +18,9 @@ #include "../string_func.h" /** Blob based case sensitive ANSI/UTF-8 string */ -struct CStrA : public CBlobT +struct CStrA : public SmallBlob { - typedef CBlobT base; ///< base class + typedef SmallBlob base; ///< base class /** Create an empty CStrT */ FORCEINLINE CStrA() diff --git a/src/pathfinder/yapf/yapf_costrail.hpp b/src/pathfinder/yapf/yapf_costrail.hpp index ea3941b..ccd483c 100644 --- a/src/pathfinder/yapf/yapf_costrail.hpp +++ b/src/pathfinder/yapf/yapf_costrail.hpp @@ -65,9 +65,9 @@ protected: * @note maximum cost doesn't work with caching enabled * @todo fix maximum cost failing with caching (e.g. FS#2900) */ - int m_max_cost; - CBlobT m_sig_look_ahead_costs; - bool m_disable_cache; + int m_max_cost; + SmallBlob m_sig_look_ahead_costs; + bool m_disable_cache; public: bool m_stopped_on_first_two_way_signal; -- 1.6.6.1