Jump to content
The Dark Mod Forums

StringOutputStream


Recommended Posts

Greebo -- I notice you have changed StringOutputStream to a typedef for std::ostringstream, which in theory is a good idea (i.e. using STL classes instead of homegrown rubbish). However I have encountered some errors.

 

1. Calls to std::ostringstream(int) constructor (corresponding to the "capacity" argument of StringOutputStream) do not work, since there is no such constructor.

2. std::ostringstream does not have a c_str() method, you have to use str() to get a string and then call c_str() on this.

3. StringOutputStream has overloaded operator<< functions for (stupid) custom classes like DirectoryCleaned, which do not work for ostringstream.

 

Assuming you didn't see these errors before committing, could this be a Win32/Linux issue? I can probably fix most of them, but I have no idea how many there are going to be.

Link to comment
Share on other sites

Sorry, didn't see your post in time. I'm still in the middle of the refactoring process and there is a lot of old code lying around, either unused or commented out. I intended to switch to Linux and fix the remaining issues (the sconscript will have to be updated as well), but I'm not that far yet.

Link to comment
Share on other sites

On the subject of refactoring, I am confused by how the "in loop increment" in XMLRegistry::removeKeyObserver() could be necessary to avoid a crash, since after looking at the code several times the two expressions seem to be functionally identical:

 

if (i->second == observer {
_keyObservers.erase(i++);
}
else {
i++;
}

 

is the same as

 

if (i->second == observer {
_keyObservers.erase(i);
i++;
}
else {
i++;
}

 

which is the same as

 

if (i->second == observer {
_keyObservers.erase(i);
}
i++;

 

which is equivalent (although potentially slower) than

 

if (i->second == observer {
_keyObservers.erase(i);
}
++i;

 

which is the same as putting the ++i in the final part of the enclosing for loop.

 

Is this a Windows issue, or am I just missing something really obvious?

Link to comment
Share on other sites

The VC++ implementation of the STL iterators raise an exception if you try to dereference/increase an invalid iterator, which is the case here. The erase(i) method invalidates the given iterator (the element being pointed at is no longer existing after the call) and it can no longer be used. The post-increment i++ makes sure that the iterator is incremented before it is becoming invalid.

 

I'm not an expert when it comes to STL (I haven't got around to read my copy of Effective STL yet), but I think this is ok and not a glitch of the MSVC implementation. I'm surprised that gcc doesn't throw an error here when executing that code. I guess it just increases the encapsulated pointer without asking whether it is invalidated or not.

 

(On a related note, the VC++ debug mode automatically has turned on the switch _HAS_ITERATOR_DEBUGGING, which is brutally picky when it comes to invalid iterators). It's not active in release builds (and it makes STL operations really slow in debug builds), but so far all of the issues raised by this setting were valid.)

Link to comment
Share on other sites

I'm mostly through with my small refactoring project.

 

TextFileOutputStream: replaced by std::ofstream

StringOutputStream: replaced by std::ostringstream

TextOutputStream: replaced by std::ostream

 

The advantage is that globalOutputStream() accepts all std::* types now - no more stupid c_str() calls when inserting a std::string to the output stream.

 

Moreover, I could redirect the std::cout/cerr streams to our console and logfile with a few lines of code, so the actual OS console window (in Windows or Linux) is not needed anymore.

 

Another thing: The Console and the LogFile classes derive from an abstract LogDevice, which can be attached to the LogWriter. With this approach, it's possible to attach any number of LogDevices to our logging system and the LogWriter itself doesn't need to check for the existence of any LogDevice - it just iterates over all registered classes and that's it.

 

I'm currently removing all references to the old StringOutputStream, etc. classes, which is taking a while as I'm recompiling all the code with each step, to be sure that I'm not missing anything.

Link to comment
Share on other sites

Sounds like a good piece of legacy code disposal. To be honest I can't actually think of an instance where I have seen a StringOutputStream that was actually necessary in the first place -- most of the uses could be replaced with simple std::string appends.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recent Status Updates

    • nbohr1more

      The FAQ wiki is almost a proper FAQ now. Probably need to spin-off a bunch of the "remedies" for playing older TDM versions into their own article.
      · 1 reply
    • nbohr1more

      Was checking out old translation packs and decided to fire up TDM 1.07. Rightful Property with sub-20 FPS areas yay! ( same areas run at 180FPS with cranked eye candy on 2.12 )
      · 3 replies
    • taffernicus

      i am so euphoric to see new FMs keep coming out and I am keen to try it out in my leisure time, then suddenly my PC is spouting a couple of S.M.A.R.T errors...
      tbf i cannot afford myself to miss my network emulator image file&progress, important ebooks, hyper-v checkpoint & hyper-v export and the precious thief & TDM gamesaves. Don't fall yourself into & lay your hands on crappy SSD
       
      · 7 replies
    • OrbWeaver

      Does anyone actually use the Normalise button in the Surface inspector? Even after looking at the code I'm not quite sure what it's for.
      · 7 replies
    • Ansome

      Turns out my 15th anniversary mission idea has already been done once or twice before! I've been beaten to the punch once again, but I suppose that's to be expected when there's over 170 FMs out there, eh? I'm not complaining though, I love learning new tricks and taking inspiration from past FMs. Best of luck on your own fan missions!
      · 4 replies
×
×
  • Create New...