GameMonkey Script

GameMonkey Script Forums
It is currently Sun Jan 20, 2019 11:39 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: gmMachine::GetTypeId
PostPosted: Wed Apr 22, 2009 5:56 pm 
Offline

Joined: Thu Jan 01, 2004 4:31 pm
Posts: 307
I've been needing to get the gmType for a type a couple of times lately for a library I'm writing; it's useful to see if something has already been added to the machine, for example. Here's my gmMachine mod:

Quote:
gmMachine.h
Code:
  /// \brief GetTypeId() will return the gmType for the specified type
  bool GetTypeId(const char *a_typename, gmType &a_type);



Quote:
gmMachine.cpp
Code:
bool gmMachine::GetTypeId(const char *a_typename, gmType &a_type)
{
  gmStringObject *str = AllocStringObject(a_typename);
  for(gmuint id = GM_NULL; id < m_types.Count(); ++id)
  {
   if (m_types[id].m_name == str)
   {
      a_type = id;
      return true;
   }
  }
  return false;
}



Top
 Profile  
Reply with quote  
 Post subject: Re: gmMachine::GetTypeId
PostPosted: Wed Apr 22, 2009 11:28 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
That sounds useful. I wonder if a preferred syntax would be:
Code:
gmType gmMachine::GetTypeId(const char *a_typename)

And we add to the type enum table (currently contains GM_NULL, GM_INT etc.) GM_INVALID_TYPE = -1 ?


Top
 Profile  
Reply with quote  
 Post subject: Re: gmMachine::GetTypeId
PostPosted: Thu Apr 23, 2009 6:55 am 
Offline

Joined: Thu Jan 01, 2004 4:31 pm
Posts: 307
Greg wrote:
That sounds useful. I wonder if a preferred syntax would be:
Code:
gmType gmMachine::GetTypeId(const char *a_typename)

And we add to the type enum table (currently contains GM_NULL, GM_INT etc.) GM_INVALID_TYPE = -1 ?



I did wonder if that would be better - what would be required to regression test the change in the enum?


Top
 Profile  
Reply with quote  
 Post subject: Re: gmMachine::GetTypeId
PostPosted: Thu Apr 23, 2009 7:19 am 
Offline

Joined: Thu Jan 01, 2004 4:31 pm
Posts: 307
Also, a method that takes in a gmStringObject* might be useful while I think about it - it'll let you use it with an already allocated string.


Top
 Profile  
Reply with quote  
 Post subject: Re: gmMachine::GetTypeId
PostPosted: Thu Apr 23, 2009 9:56 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
downgraded wrote:
...I did wonder if that would be better - what would be required to regression test the change in the enum?

I don't know how this change would effect common use of typeIds. I expect it would be a low risk. If such an ID was passed back to gmMachine it should assert on an invalid array index, requiring the caller to handle the case. The compilers integrity should not be compromised since this is a new read only value, not a value stored anywhere.

Since you are the creator of this specific function gmMachine::GetTypeId() and its syntax, there is no risk to you as the syntax change would locate all references at compile time.

downgraded wrote:
Also, a method that takes in a gmStringObject* might be useful while I think about it - it'll let you use it with an already allocated string.
That might be very reasonable for you to add if names are regularly coming from script strings. I certainly have used cached gmStringObjects to speed access or perform quick compares to specific script resources. I would only add such things on a case by case basis otherwise all APIs would be littered with application specific optimizations.


Top
 Profile  
Reply with quote  
 Post subject: Re: gmMachine::GetTypeId
PostPosted: Thu Apr 23, 2009 12:45 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
Sorry, I wasn't paying attention and didn't study the implementation.
Perhaps forget the gmStringObject entirely and just use:
Code:
gmType gmMachine::GetTypeId(const char * a_typename) const
{
  for(gmuint id = GM_NULL; id < m_types.Count(); ++id)
  {
    if( strcmp((const char *)m_types[id].m_name, a_typename) == 0 )
    {
      return id;
    }
  }
  return GM_INVALID_TYPE;
}


Edit: Added const since read only function.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group