Remove MenuManager use of memtables (bug 5899 part 2, r=fyren).
authorDavid Anderson <dvander@alliedmods.net>
Sat Aug 31 11:50:28 2013 -0700 (2013-08-31)
changeset 411134b8688e3bec
parent 4110 f1f4eb1d9274
child 4112 31f38e6aae4e
Remove MenuManager use of memtables (bug 5899 part 2, r=fyren).
core/Database.h
core/MenuManager.h
core/MenuStyle_Base.cpp
core/MenuStyle_Base.h
public/amtl/am-string.h
public/amtl/am-vector.h
     1.1 --- a/core/Database.h	Sat Aug 31 11:50:11 2013 -0700
     1.2 +++ b/core/Database.h	Sat Aug 31 11:50:28 2013 -0700
     1.3 @@ -38,7 +38,6 @@
     1.4  #include <sh_string.h>
     1.5  #include <sh_list.h>
     1.6  #include <ITextParsers.h>
     1.7 -#include "sm_memtable.h"
     1.8  #include <IThreader.h>
     1.9  #include <IPluginSys.h>
    1.10  #include <am-thread-utils.h>
     2.1 --- a/core/MenuManager.h	Sat Aug 31 11:50:11 2013 -0700
     2.2 +++ b/core/MenuManager.h	Sat Aug 31 11:50:28 2013 -0700
     2.3 @@ -37,7 +37,6 @@
     2.4  #include <sh_stack.h>
     2.5  #include <sh_list.h>
     2.6  #include <sh_string.h>
     2.7 -#include "sm_memtable.h"
     2.8  #include "sm_globals.h"
     2.9  
    2.10  using namespace SourceMod;
     3.1 --- a/core/MenuStyle_Base.cpp	Sat Aug 31 11:50:11 2013 -0700
     3.2 +++ b/core/MenuStyle_Base.cpp	Sat Aug 31 11:50:28 2013 -0700
     3.3 @@ -1,5 +1,5 @@
     3.4  /**
     3.5 - * vim: set ts=4 :
     3.6 + * vim: set ts=4 sw=4 tw=99 noet :
     3.7   * =============================================================================
     3.8   * SourceMod
     3.9   * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
    3.10 @@ -604,7 +604,7 @@
    3.11  }
    3.12  
    3.13  CBaseMenu::CBaseMenu(IMenuHandler *pHandler, IMenuStyle *pStyle, IdentityToken_t *pOwner) : 
    3.14 -m_pStyle(pStyle), m_Strings(512), m_Pagination(7), m_bShouldDelete(false), m_bCancelling(false), 
    3.15 +m_pStyle(pStyle), m_Pagination(7), m_bShouldDelete(false), m_bCancelling(false), 
    3.16  m_pOwner(pOwner ? pOwner : g_pCoreIdent), m_bDeleting(false), m_bWillFreeHandle(false), 
    3.17  m_hHandle(BAD_HANDLE), m_pHandler(pHandler), m_nFlags(MENUFLAG_BUTTON_EXIT)
    3.18  {
    3.19 @@ -627,94 +627,74 @@
    3.20  bool CBaseMenu::AppendItem(const char *info, const ItemDrawInfo &draw)
    3.21  {
    3.22  	if (m_Pagination == (unsigned)MENU_NO_PAGINATION
    3.23 -		&& m_items.size() >= m_pStyle->GetMaxPageItems())
    3.24 +		&& m_items.length() >= m_pStyle->GetMaxPageItems())
    3.25  	{
    3.26  		return false;
    3.27  	}
    3.28  
    3.29  	CItem item;
    3.30  
    3.31 -	item.infoString = m_Strings.AddString(info);
    3.32 +	item.info = info;
    3.33  	if (draw.display)
    3.34 -	{
    3.35 -		item.displayString = m_Strings.AddString(draw.display);
    3.36 -	}
    3.37 +		item.display = draw.display;
    3.38  	item.style = draw.style;
    3.39  
    3.40 -	m_items.push_back(item);
    3.41 -
    3.42 +	m_items.append(item);
    3.43  	return true;
    3.44  }
    3.45  
    3.46  bool CBaseMenu::InsertItem(unsigned int position, const char *info, const ItemDrawInfo &draw)
    3.47  {
    3.48 -	if (m_Pagination == (unsigned)MENU_NO_PAGINATION
    3.49 -		&& m_items.size() >= m_pStyle->GetMaxPageItems())
    3.50 -	{
    3.51 -		return false;
    3.52 -	}
    3.53 -
    3.54 -	if (position >= m_items.size())
    3.55 +	if (m_Pagination == (unsigned)MENU_NO_PAGINATION &&
    3.56 +	    m_items.length() >= m_pStyle->GetMaxPageItems())
    3.57  	{
    3.58  		return false;
    3.59  	}
    3.60  
    3.61 +	if (position >= m_items.length())
    3.62 +		return false;
    3.63 +
    3.64  	CItem item;
    3.65 -	item.infoString = m_Strings.AddString(info);
    3.66 +	item.info = info;
    3.67  	if (draw.display)
    3.68 -	{
    3.69 -		item.displayString = m_Strings.AddString(draw.display);
    3.70 -	}
    3.71 +		item.display = draw.display;
    3.72  	item.style = draw.style;
    3.73  
    3.74 -	CVector<CItem>::iterator iter = m_items.iterAt(position);
    3.75 -	m_items.insert(iter, item);
    3.76 -
    3.77 +	m_items.insert(position, item);
    3.78  	return true;
    3.79  }
    3.80  
    3.81  bool CBaseMenu::RemoveItem(unsigned int position)
    3.82  {
    3.83 -	if (position >= m_items.size())
    3.84 -	{
    3.85 +	if (position >= m_items.length())
    3.86  		return false;
    3.87 -	}
    3.88  
    3.89 -	m_items.erase(m_items.iterAt(position));
    3.90 -
    3.91 -	if (m_items.size() == 0)
    3.92 -	{
    3.93 -		m_Strings.Reset();
    3.94 -	}
    3.95 -
    3.96 +	m_items.remove(position);
    3.97  	return true;
    3.98  }
    3.99  
   3.100  void CBaseMenu::RemoveAllItems()
   3.101  {
   3.102  	m_items.clear();
   3.103 -	m_Strings.Reset();
   3.104  }
   3.105  
   3.106  const char *CBaseMenu::GetItemInfo(unsigned int position, ItemDrawInfo *draw/* =NULL */)
   3.107  {
   3.108 -	if (position >= m_items.size())
   3.109 -	{
   3.110 +	if (position >= m_items.length())
   3.111  		return NULL;
   3.112 -	}
   3.113  
   3.114  	if (draw)
   3.115  	{
   3.116 -		draw->display = m_Strings.GetString(m_items[position].displayString);
   3.117 +		draw->display = m_items[position].display.chars();
   3.118  		draw->style = m_items[position].style;
   3.119  	}
   3.120  
   3.121 -	return m_Strings.GetString(m_items[position].infoString);
   3.122 +	return m_items[position].info.chars();
   3.123  }
   3.124  
   3.125  unsigned int CBaseMenu::GetItemCount()
   3.126  {
   3.127 -	return m_items.size();
   3.128 +	return m_items.length();
   3.129  }
   3.130  
   3.131  bool CBaseMenu::SetPagination(unsigned int itemsPerPage)
   3.132 @@ -747,12 +727,12 @@
   3.133  
   3.134  void CBaseMenu::SetDefaultTitle(const char *message)
   3.135  {
   3.136 -	m_Title.assign(message);
   3.137 +	m_Title = message;
   3.138  }
   3.139  
   3.140  const char *CBaseMenu::GetDefaultTitle()
   3.141  {
   3.142 -	return m_Title.c_str();
   3.143 +	return m_Title.chars();
   3.144  }
   3.145  
   3.146  void CBaseMenu::Cancel()
   3.147 @@ -844,7 +824,5 @@
   3.148  
   3.149  unsigned int CBaseMenu::GetBaseMemUsage()
   3.150  {
   3.151 -	return m_Title.size() 
   3.152 -		+ m_Strings.GetMemTable()->GetMemUsage()
   3.153 -		+ (m_items.size() * sizeof(CItem));
   3.154 +	return m_Title.length() + (m_items.length() * sizeof(CItem));
   3.155  }
     4.1 --- a/core/MenuStyle_Base.h	Sat Aug 31 11:50:11 2013 -0700
     4.2 +++ b/core/MenuStyle_Base.h	Sat Aug 31 11:50:28 2013 -0700
     4.3 @@ -1,5 +1,5 @@
     4.4  /**
     4.5 - * vim: set ts=4 :
     4.6 + * vim: set ts=4 sw=4 tw=99 noet :
     4.7   * =============================================================================
     4.8   * SourceMod
     4.9   * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
    4.10 @@ -34,27 +34,32 @@
    4.11  
    4.12  #include <IMenuManager.h>
    4.13  #include <IPlayerHelpers.h>
    4.14 -#include <sh_string.h>
    4.15 -#include <sh_vector.h>
    4.16 -#include "sm_memtable.h"
    4.17 +#include <am-string.h>
    4.18 +#include <am-vector.h>
    4.19  #include "sm_fastlink.h"
    4.20  
    4.21  using namespace SourceMod;
    4.22 -using namespace SourceHook;
    4.23  
    4.24  class CItem
    4.25  {
    4.26  public:
    4.27  	CItem()
    4.28  	{
    4.29 -		infoString = -1;
    4.30 -		displayString = -1;
    4.31 +		info.setVoid();
    4.32 +		display.setVoid();
    4.33  		style = 0;
    4.34  		access = 0;
    4.35  	}
    4.36 +	CItem(ke::Moveable<CItem> other)
    4.37 +	: info(ke::Move(other->info)),
    4.38 +	  display(ke::Move(other->display))
    4.39 +	{
    4.40 +		style = other->style;
    4.41 +		access = other->access;
    4.42 +	}
    4.43  public:
    4.44 -	int infoString;
    4.45 -	int displayString;
    4.46 +	ke::AString info;
    4.47 +	ke::AString display;
    4.48  	unsigned int style;
    4.49  	unsigned int access;
    4.50  };
    4.51 @@ -139,11 +144,10 @@
    4.52  private:
    4.53  	void InternalDelete();
    4.54  protected:
    4.55 -	String m_Title;
    4.56 +	ke::AString m_Title;
    4.57  	IMenuStyle *m_pStyle;
    4.58 -	BaseStringTable m_Strings;
    4.59  	unsigned int m_Pagination;
    4.60 -	CVector<CItem> m_items;
    4.61 +	ke::Vector<CItem> m_items;
    4.62  	bool m_bShouldDelete;
    4.63  	bool m_bCancelling;
    4.64  	IdentityToken_t *m_pOwner;
     5.1 --- a/public/amtl/am-string.h	Sat Aug 31 11:50:11 2013 -0700
     5.2 +++ b/public/amtl/am-string.h	Sat Aug 31 11:50:28 2013 -0700
     5.3 @@ -104,17 +104,29 @@
     5.4      return chars()[index];
     5.5    }
     5.6  
     5.7 +  void setVoid() {
     5.8 +    chars_ = NULL;
     5.9 +    length_ = kInvalidLength;
    5.10 +  }
    5.11 +
    5.12 +  bool isVoid() const {
    5.13 +    return length_ == kInvalidLength;
    5.14 +  }
    5.15 +
    5.16    size_t length() const {
    5.17 +    assert(!isVoid());
    5.18      return length_;
    5.19    }
    5.20  
    5.21    const char *chars() const {
    5.22      if (!chars_)
    5.23 -      return "";
    5.24 +      return isVoid() ? NULL : "";
    5.25      return chars_;
    5.26    }
    5.27  
    5.28   private:
    5.29 +  static const size_t kInvalidLength = (size_t)-1;
    5.30 +
    5.31    void set(const char *str, size_t length) {
    5.32      chars_ = new char[length + 1];
    5.33      length_ = length;
     6.1 --- a/public/amtl/am-vector.h	Sat Aug 31 11:50:11 2013 -0700
     6.2 +++ b/public/amtl/am-vector.h	Sat Aug 31 11:50:28 2013 -0700
     6.3 @@ -85,6 +85,29 @@
     6.4      nitems_++;
     6.5    }
     6.6  
     6.7 +  // Shift all elements including |at| up by one, and insert |item| at the
     6.8 +  // given position. This is a linear-time operation.
     6.9 +  bool insert(size_t at, const T &item) {
    6.10 +    if (!moveUp(at))
    6.11 +      return false;
    6.12 +    new (&data_[at]) T(item);
    6.13 +    return true;
    6.14 +  }
    6.15 +  bool insert(size_t at, Moveable<T> item) {
    6.16 +    if (!moveUp(at))
    6.17 +      return false;
    6.18 +    new (&data_[at]) T(item);
    6.19 +    return true;
    6.20 +  }
    6.21 +
    6.22 +  // Shift all elements at the given position down, removing the given
    6.23 +  // element. This is a linear-time operation.
    6.24 +  void remove(size_t at) {
    6.25 +    for (size_t i = at; i < length() - 1; i++)
    6.26 +      data_[i] = T(Moveable<T>(data_[i + 1]));
    6.27 +    pop();
    6.28 +  }
    6.29 +
    6.30    T popCopy() {
    6.31      T t = at(length() - 1);
    6.32      pop();
    6.33 @@ -154,6 +177,15 @@
    6.34      maxsize_ = 0;
    6.35    }
    6.36  
    6.37 +  bool moveUp(size_t at) {
    6.38 +    if (!growIfNeeded(1))
    6.39 +      return false;
    6.40 +    nitems_++;
    6.41 +    for (size_t i = nitems_ - 1; i > at; i--)
    6.42 +      data_[i] = T(Moveable<T>(data_[i - 1]));
    6.43 +    return true;
    6.44 +  }
    6.45 +
    6.46    bool growIfNeeded(size_t needed)
    6.47    {
    6.48      if (!IsUintPtrAddSafe(nitems_, needed)) {