1999Q1/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: [MUD&#45;Dev] Re: World&#45;file parsing and RTTI? -->
<!--X-From-R13: [nex Uevggre <znexNreqbf.Egnasbeq.SRG> -->
<!--X-Date: Wed, 10 Feb 1999 14:55:46 &#45;0800 -->
<!--X-Message-Id: 199902102254.OAA08051#erdos,Stanford.EDU -->
<!--X-Content-Type: text -->
<!--X-Reference: Pine.LNX.4.04.9902100044360.430&#45;100000#t8o3p24,telia.com -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, [MUD-Dev] Re: World-file parsing and RTTI?</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:mark#erdos,Stanford.EDU">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00376.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00378.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00376.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00375.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00377">Author</A>
&nbsp;|&nbsp;<A HREF="#00377">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00377">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>[MUD-Dev] Re: World-file parsing and RTTI?</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
<LI><em>Subject</em>: [MUD-Dev] Re: World-file parsing and RTTI?</LI>
<LI><em>From</em>: Mark Gritter &lt;<A HREF="mailto:mark#erdos,Stanford.EDU">mark#erdos,Stanford.EDU</A>&gt;</LI>
<LI><em>Date</em>: Wed, 10 Feb 1999 14:54:17 -0800 (PST)</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>
Joachim Pileborg writes:
&gt; 
&gt; For my MUD, I have a simple yet pretty good (IMHO :) format for world-files.
&gt; A file contains one or more objects (monsters, locations, etc.), and each
&gt; object have a list of properties (key-value pairs).  Right now I have a
&gt; simple lexer in flex that search for certain keywords like "monster",
&gt; "location", "item".  When the parser finds such a keyword I create a new
&gt; instance of that specific C++ class.

I'm not clear here on what the strategy is: you create an instance of
the C++ class with the same name?  What does your table contain as the
"value" associated with a particular keyword?

&gt; My goal for the parser is to make it as flexible as possible, all anyone
&gt; using my code should have to do, is to add a line to a table.
&gt; Now comes the question:  What is the best way to do this?  I have though
&gt; about using RTTI, but to my knowledge there exists no platform/compiler
&gt; independant system.  I could limit the MUD to just be compileable with gcc
&gt; or egcs, but I don't like limiting myself in any way.
&gt; Discussion?
&gt; 

RTTI is part of the standard.  Anything in ISO C++ that g++/egcs can handle 
should be available in commercial compilers by now.  (I hope.)

The "standard" solution to adding this flexiblity is to use factory classes 
rather than trying to use some language feature to create the right type.
I'll sketch an example below of how I've done this on other projects.
(Not MUDs... yet.)

class EntityFactory {
 public:
  /* Load an entity from the file.  Subclasses of EntityFactory
   * handle different types.
   */
  virtual Ptr&lt;Entity&gt; loadEntity(istream &amp;in) = 0;
};

/* Depending on how your parser works, istream &amp;in might be replaced
 * by list&lt;KeywordValuePairs&gt; or something else appropriate for creating
 * the entity.
 *
 * "WorldContext" specifies where the new entity goes in some way.
 */
void Parser::handleKeyword(string keyword, istream &amp;in, WorldContext &amp;world) {
  Ptr&lt;EntityFactory&gt; factory = keywordTable-&gt;lookup(keyword);
  Ptr&lt;Entity&gt; newEntity = factory-&gt;loadEntity(in):
  world.addEntity(newEntity);
}

class MonsterFactory : public EntityFactory {
 public:
  virtual Ptr&lt;Entity&gt; loadEntity(istream &amp;in) { ... }
};

class WeaponFactory : public EntityFactory {
 public:
  virtual Ptr&lt;Entity&gt; loadEntity(istream &amp;in) { ... }
};

void init(void) {
  keywordTable-&gt;add("monster", new MonsterFactory());
  keywordTable-&gt;add("weapon", new WeaponFactory());
  ...
}

This allows new types to be added by creating the new factory and putting
it in the keywordTable so that the parser can find it.  RTTI might be used
by "WorldContext" to determine what sort of thing got returned from the
parser, but something like:

if ((foo = dynamic_cast&lt;Weapon *&gt;(newEntity))) { ... }
else if ((bar = dynamic_cast&lt;Player *&gt;(newEntity))) { ... }
...

should probably be avoided in favor of double-dispatch if there are going to
be a lot of cases.

(Currently, I'm using something like this to parse DNS packets: each type
of resource record has its own factory which knows how to interpret the
contents and return a class of the right type.  Any operations are done
using double-dispatch on all the RRs in a Message object.)

Mark Gritter
mgritter#cs,stanford.edu




</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<!--X-Follow-Ups-End-->
<!--X-References-->
<UL><LI><STRONG>References</STRONG>:
<UL>
<LI><STRONG><A NAME="00376" HREF="msg00376.html">[MUD-Dev] World-file parsing and RTTI?</A></STRONG>
<UL><LI><EM>From:</EM> The Arrow &lt;arrow#trelleborg,mail.telia.com&gt;</LI></UL></LI>
</UL></LI></UL>
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00376.html">[MUD-Dev] World-file parsing and RTTI?</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00378.html">[MUD-Dev] pet peeves</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00376.html">[MUD-Dev] World-file parsing and RTTI?</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00375.html">[MUD-Dev] Re: optimizing code</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00377"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00377"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><strong><A NAME="00378" HREF="msg00378.html">[MUD-Dev] pet peeves</A></strong>, 
diablo <a href="mailto:diablo#best,com">diablo#best,com</a>, Thu 11 Feb 1999, 21:29 GMT
<UL>
<LI><strong><A NAME="00379" HREF="msg00379.html">[MUD-Dev] Re: pet peeves</A></strong>, 
diablo <a href="mailto:diablo#best,com">diablo#best,com</a>, Thu 11 Feb 1999, 21:51 GMT
<UL>
<LI><strong><A NAME="00381" HREF="msg00381.html">[MUD-Dev] Re: pet peeves</A></strong>, 
Richard Woolcock <a href="mailto:KaVir#dial,pipex.com">KaVir#dial,pipex.com</a>, Thu 11 Feb 1999, 23:02 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
<LI><strong><A NAME="00376" HREF="msg00376.html">[MUD-Dev] World-file parsing and RTTI?</A></strong>, 
The Arrow <a href="mailto:arrow#trelleborg,mail.telia.com">arrow#trelleborg,mail.telia.com</a>, Wed 10 Feb 1999, 18:45 GMT
<UL>
<LI><strong><A NAME="00377" HREF="msg00377.html">[MUD-Dev] Re: World-file parsing and RTTI?</A></strong>, 
Mark Gritter <a href="mailto:mark#erdos,Stanford.EDU">mark#erdos,Stanford.EDU</a>, Wed 10 Feb 1999, 22:55 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00375" HREF="msg00375.html">[MUD-Dev] Re: optimizing code</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Wed 10 Feb 1999, 02:41 GMT
<LI><strong><A NAME="00371" HREF="msg00371.html">[MUD-Dev] code profiling</A></strong>, 
Chris Gray <a href="mailto:cg#ami-cg,GraySage.Edmonton.AB.CA">cg#ami-cg,GraySage.Edmonton.AB.CA</a>, Tue 09 Feb 1999, 06:00 GMT
<UL>
<LI><strong><A NAME="00373" HREF="msg00373.html">[MUD-Dev] optimizing code</A></strong>, 
diablo <a href="mailto:diablo#best,com">diablo#best,com</a>, Tue 09 Feb 1999, 06:31 GMT
<UL>
<LI><strong><A NAME="00374" HREF="msg00374.html">[MUD-Dev] Re: optimizing code</A></strong>, 
Hans-Henrik Staerfeldt <a href="mailto:hhs#cbs,dtu.dk">hhs#cbs,dtu.dk</a>, Tue 09 Feb 1999, 14:58 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>