From 6cb3f9cbd9279c3201fea35e79506ddbac136fb2 Mon Sep 17 00:00:00 2001 From: skidd13 Date: Fri, 19 Mar 2010 22:04:21 +0100 Subject: [PATCH 2/9] -Codechange: Unify method naming of CBlobT and CStrA --- src/misc/array.hpp | 2 +- src/misc/blob.hpp | 36 +++++++++++++------------------ src/misc/dbg_helpers.cpp | 14 ++++++------ src/misc/dbg_helpers.h | 8 +++--- src/misc/str.hpp | 24 ++++++++++---------- src/pathfinder/yapf/yapf_costrail.hpp | 10 ++++---- src/pathfinder/yapf/yapf_node_rail.hpp | 2 +- src/pathfinder/yapf/yapf_rail.cpp | 4 +- 8 files changed, 47 insertions(+), 53 deletions(-) diff --git a/src/misc/array.hpp b/src/misc/array.hpp index 78dd009..2d1a3cb 100644 --- a/src/misc/array.hpp +++ b/src/misc/array.hpp @@ -83,7 +83,7 @@ public: for (uint i = 0; i < num_items; i++) { const T& item = (*this)[i]; name.Format("item[%d]", i); - dmp.WriteStructT(name.Data(), &item); + dmp.WriteStructT(name.Begin(), &item); } } }; diff --git a/src/misc/blob.hpp b/src/misc/blob.hpp index 06e9be9..4feb116 100644 --- a/src/misc/blob.hpp +++ b/src/misc/blob.hpp @@ -77,7 +77,7 @@ public: /** copy constructor */ FORCEINLINE ByteBlob(const ByteBlob &src) { InitEmpty(); - AppendRaw(src.Begin(), src.Length()); + Append(src.Begin(), src.Length()); } /** move constructor - take ownership of blob data */ @@ -214,7 +214,7 @@ public: } /** append new bytes at the end of existing data bytes - reallocates if necessary */ - FORCEINLINE void AppendRaw(const void *p, uint num_bytes) + FORCEINLINE void Append(const void *p, uint num_bytes) { assert(p != NULL); if (num_bytes != 0) @@ -311,65 +311,59 @@ public: Free(); } - /** Check the validity of item index (only in debug mode) */ - FORCEINLINE void CheckIdx(uint index) const - { - assert(index < Size()); - } - /** Return pointer to the first data item - non-const version */ - FORCEINLINE T *Data() + FORCEINLINE T *Begin() { return (T*)base::Begin(); } /** Return pointer to the first data item - const version */ - FORCEINLINE const T *Data() const + FORCEINLINE const T *Begin() const { return (const T*)base::Begin(); } /** Return pointer to the index-th data item - non-const version */ - FORCEINLINE T *Data(uint index) + FORCEINLINE T *Get(uint index) { - CheckIdx(index); - return (Data() + index); + assert(index < Length()); + return (Begin() + index); } /** Return pointer to the index-th data item - const version */ - FORCEINLINE const T *Data(uint index) const + FORCEINLINE const T *Get(uint index) const { - CheckIdx(index); - return (Data() + index); + assert(index < Length()); + return (Begin() + index); } /** Return number of items in the Blob */ - FORCEINLINE uint Size() const + FORCEINLINE uint Length() const { return (base::Length() / type_size); } /** Return total number of items that can fit in the Blob without buffer reallocation */ - FORCEINLINE uint MaxSize() const + FORCEINLINE uint Capacity() const { return (base::Capacity() / type_size); } /** Return number of additional items that can fit in the Blob without buffer reallocation */ - FORCEINLINE uint GetReserve() const + FORCEINLINE uint Reserve() const { return ((base::Capacity() - base::Length()) / type_size); } /** Grow number of data items in Blob by given number - doesn't construct items */ - FORCEINLINE T *GrowSizeNC(uint num_items) + 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 *MakeFreeSpace(uint num_items) + FORCEINLINE T *Prepare(uint num_items) { return (T*)base::Prepare(num_items * type_size); } diff --git a/src/misc/dbg_helpers.cpp b/src/misc/dbg_helpers.cpp index 78b2ec4..d20c38e 100644 --- a/src/misc/dbg_helpers.cpp +++ b/src/misc/dbg_helpers.cpp @@ -31,7 +31,7 @@ CStrA ValueStr(Trackdir td) CStrA ValueStr(TrackdirBits td_bits) { CStrA out; - out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data()); + out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Begin()); return out.Transfer(); } @@ -110,7 +110,7 @@ void DumpTarget::WriteIndent() { int num_spaces = 2 * m_indent; if (num_spaces > 0) { - memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces); + memset(m_out.Append(num_spaces), ' ', num_spaces); } } @@ -122,7 +122,7 @@ void DumpTarget::WriteLine(const char *format, ...) va_start(args, format); m_out.AddFormatL(format, args); va_end(args); - m_out.AppendStr("\n"); + m_out.Append("\n"); } /** Write 'name = value' with indent and new-line. */ @@ -136,7 +136,7 @@ void DumpTarget::WriteValue(const char *name, const char *value_str) void DumpTarget::WriteTile(const char *name, TileIndex tile) { WriteIndent(); - m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data()); + m_out.AddFormat("%s = %s\n", name, TileStr(tile).Begin()); } /** @@ -146,11 +146,11 @@ void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr) { /* make composite name */ CStrA cur_name = GetCurrentStructName().Transfer(); - if (cur_name.Size() > 0) { + if (cur_name.Length() > 0) { /* add name delimiter (we use structured names) */ - cur_name.AppendStr("."); + cur_name.Append("."); } - cur_name.AppendStr(name); + cur_name.Append(name); /* put the name onto stack (as current struct name) */ m_cur_struct.push(cur_name); diff --git a/src/misc/dbg_helpers.h b/src/misc/dbg_helpers.h index 379b2e8..aabdf8d 100644 --- a/src/misc/dbg_helpers.h +++ b/src/misc/dbg_helpers.h @@ -79,10 +79,10 @@ inline CStrA ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const cha } else { for (size_t i = 0; i < ArrayT::length; i++) { if ((value & (1 << i)) == 0) continue; - out.AddFormat("%s%s", (out.Size() > 0 ? "+" : ""), t[i]); + out.AddFormat("%s%s", (out.Length() > 0 ? "+" : ""), t[i]); value &= ~(E)(1 << i); } - if (value != 0) out.AddFormat("%s%s", (out.Size() > 0 ? "+" : ""), t_unk); + if (value != 0) out.AddFormat("%s%s", (out.Length() > 0 ? "+" : ""), t_unk); } return out.Transfer(); } @@ -144,7 +144,7 @@ struct DumpTarget { /** Dump given enum value (as a number and as named value) */ template void WriteEnumT(const char *name, E e) { - WriteValue(name, ValueStr(e).Data()); + WriteValue(name, ValueStr(e).Begin()); } void BeginStruct(size_t type_id, const char *name, const void *ptr); @@ -163,7 +163,7 @@ struct DumpTarget { CStrA known_as; if (FindKnownName(type_id, s, known_as)) { /* We already know this one, no need to dump it. */ - WriteLine("%s = known_as.%s", name, known_as.Data()); + WriteLine("%s = known_as.%s", name, known_as.Begin()); } else { /* Still unknown, dump it */ BeginStruct(type_id, name, s); diff --git a/src/misc/str.hpp b/src/misc/str.hpp index 7385b54..bc1671c 100644 --- a/src/misc/str.hpp +++ b/src/misc/str.hpp @@ -40,18 +40,18 @@ struct CStrA : public CBlobT } /** Grow the actual buffer and fix the trailing zero at the end. */ - FORCEINLINE char *GrowSizeNC(uint count) + FORCEINLINE char *Append(uint count) { - char *ret = base::GrowSizeNC(count); + char *ret = base::Append(count); base::FixTail(); return ret; } /** Append zero-ended C string. */ - FORCEINLINE void AppendStr(const char *str) + FORCEINLINE void Append(const char *str) { if (!StrEmpty(str)) { - base::AppendRaw(str, strlen(str)); + ByteBlob::Append(str, strlen(str)); base::FixTail(); } } @@ -60,7 +60,7 @@ struct CStrA : public CBlobT FORCEINLINE void Append(const CStrA &src) { if (src.Length() > 0) { - base::AppendRaw(src.Data(), src.Size()); + ByteBlob::Append(src.Begin(), src.Length()); base::FixTail(); } } @@ -69,7 +69,7 @@ struct CStrA : public CBlobT FORCEINLINE CStrA &operator = (const char *src) { base::Clear(); - AppendStr(src); + Append(src); return *this; } @@ -78,7 +78,7 @@ struct CStrA : public CBlobT { if (&src != this) { base::Clear(); - base::AppendRaw(src.Data(), src.Size()); + ByteBlob::Append(src.Begin(), src.Length()); base::FixTail(); } return *this; @@ -87,7 +87,7 @@ struct CStrA : public CBlobT /** Lower-than operator (to support stl collections) */ FORCEINLINE bool operator < (const CStrA &other) const { - return strcmp(base::Data(), other.Data()) < 0; + return strcmp(base::Begin(), other.Begin()) < 0; } /** Add formated string (like vsprintf) at the end of existing contents. */ @@ -98,9 +98,9 @@ struct CStrA : public CBlobT int ret; int err = 0; for (;;) { - char *buf = base::MakeFreeSpace(addSize); - ret = vsnprintf(buf, base::GetReserve(), format, args); - if (ret >= (int)base::GetReserve()) { + char *buf = base::Prepare(addSize); + ret = vsnprintf(buf, base::Reserve(), format, args); + if (ret >= (int)base::Reserve()) { /* Greater return than given count means needed buffer size. */ addSize = ret + 1; continue; @@ -118,7 +118,7 @@ struct CStrA : public CBlobT addSize *= 2; } if (ret > 0) { - base::GrowSizeNC(ret); + base::Append(ret); } base::FixTail(); return ret; diff --git a/src/pathfinder/yapf/yapf_costrail.hpp b/src/pathfinder/yapf/yapf_costrail.hpp index 6c4603c..b479095 100644 --- a/src/pathfinder/yapf/yapf_costrail.hpp +++ b/src/pathfinder/yapf/yapf_costrail.hpp @@ -84,7 +84,7 @@ protected: int p0 = Yapf().PfGetSettings().rail_look_ahead_signal_p0; int p1 = Yapf().PfGetSettings().rail_look_ahead_signal_p1; int p2 = Yapf().PfGetSettings().rail_look_ahead_signal_p2; - int *pen = m_sig_look_ahead_costs.GrowSizeNC(Yapf().PfGetSettings().rail_look_ahead_max_signals); + int *pen = m_sig_look_ahead_costs.Append(Yapf().PfGetSettings().rail_look_ahead_max_signals); for (uint i = 0; i < Yapf().PfGetSettings().rail_look_ahead_max_signals; i++) { pen[i] = p0 + i * (p1 + i * p2); } @@ -168,7 +168,7 @@ public: /** The cost for reserved tiles, including skipped ones. */ FORCEINLINE int ReservationCost(Node& n, TileIndex tile, Trackdir trackdir, int skipped) { - if (n.m_num_signals_passed >= m_sig_look_ahead_costs.Size() / 2) return 0; + if (n.m_num_signals_passed >= m_sig_look_ahead_costs.Length() / 2) return 0; if (!IsPbsSignal(n.m_last_signal_type)) return 0; if (IsRailStationTile(tile) && IsAnyStationTileReserved(tile, trackdir, skipped)) { @@ -200,7 +200,7 @@ public: n.m_last_signal_type = sig_type; /* cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is */ - int look_ahead_cost = (n.m_num_signals_passed < m_sig_look_ahead_costs.Size()) ? m_sig_look_ahead_costs.Data()[n.m_num_signals_passed] : 0; + int look_ahead_cost = (n.m_num_signals_passed < m_sig_look_ahead_costs.Length()) ? m_sig_look_ahead_costs.Begin()[n.m_num_signals_passed] : 0; if (sig_state != SIGNAL_STATE_RED) { /* green signal */ n.flags_u.flags_s.m_last_signal_was_red = false; @@ -460,7 +460,7 @@ no_entry_cost: // jump here at the beginning if the node has no parent (it is th /* Apply min/max speed penalties only when inside the look-ahead radius. Otherwise * it would cause desync in MP. */ - if (n.m_num_signals_passed < m_sig_look_ahead_costs.Size()) + if (n.m_num_signals_passed < m_sig_look_ahead_costs.Length()) { int min_speed = 0; int max_speed = tf->GetSpeedLimit(&min_speed); @@ -605,7 +605,7 @@ no_entry_cost: // jump here at the beginning if the node has no parent (it is th { return !m_disable_cache && (n.m_parent != NULL) - && (n.m_parent->m_num_signals_passed >= m_sig_look_ahead_costs.Size()); + && (n.m_parent->m_num_signals_passed >= m_sig_look_ahead_costs.Length()); } FORCEINLINE void ConnectNodeToCachedData(Node& n, CachedData& ci) diff --git a/src/pathfinder/yapf/yapf_node_rail.hpp b/src/pathfinder/yapf/yapf_node_rail.hpp index 41ad8f8..c114ce4 100644 --- a/src/pathfinder/yapf/yapf_node_rail.hpp +++ b/src/pathfinder/yapf/yapf_node_rail.hpp @@ -126,7 +126,7 @@ inline CStrA ValueStr(EndSegmentReasonBits bits) }; CStrA out; - out.Format("0x%04X (%s)", bits, ComposeNameT(bits, end_segment_reason_names, "UNK", ESRB_NONE, "NONE").Data()); + out.Format("0x%04X (%s)", bits, ComposeNameT(bits, end_segment_reason_names, "UNK", ESRB_NONE, "NONE").Begin()); return out.Transfer(); } diff --git a/src/pathfinder/yapf/yapf_rail.cpp b/src/pathfinder/yapf/yapf_rail.cpp index 4365ac5..f847cb4 100644 --- a/src/pathfinder/yapf/yapf_rail.cpp +++ b/src/pathfinder/yapf/yapf_rail.cpp @@ -28,8 +28,8 @@ template void DumpState(Tpf &pf1, Tpf &pf2) pf2.DumpBase(dmp2); FILE *f1 = fopen("yapf1.txt", "wt"); FILE *f2 = fopen("yapf2.txt", "wt"); - fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1); - fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2); + fwrite(dmp1.m_out.Begin(), 1, dmp1.m_out.Length(), f1); + fwrite(dmp2.m_out.Begin(), 1, dmp2.m_out.Length(), f2); fclose(f1); fclose(f2); } -- 1.6.6.1