Changeset 893 – HoverRace

Changeset 893

Show
Ignore:
Timestamp:
03/07/10 00:47:52 (5 months ago)
Author:
zoogie
Message:

Allow multiple output streams to be attached to a single scripting env.

This is to support multiple consoles attached to one env.

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/client/Game2/Console.cpp

    r879 r893  
    8585void Console::InitEnv(Script::Env *scripting) 
    8686{ 
    87     scripting->SetOutput(logStream); 
     87    scripting->AddOutput(logStream); 
    8888} 
    8989 
  • trunk/engine/Script/Env.cpp

    r705 r893  
    2424 
    2525#include <iostream> 
     26 
     27#include <boost/foreach.hpp> 
    2628 
    2729#include "../Util/OS.h" 
     
    143145 * @param out The output stream (wrapped in a shared pointer). 
    144146 *            May be @c NULL to use the system default. 
    145  */ 
    146 void Env::SetOutput(boost::shared_ptr<std::ostream> out) 
    147 { 
    148     this->out = out; 
     147 * @return A handle for removing the stream later. 
     148 */ 
     149Env::OutHandle Env::AddOutput(boost::shared_ptr<std::ostream> out) 
     150{ 
     151    outs.push_back(out); 
     152    return --(outs.end()); 
     153} 
     154 
     155void Env::RemoveOutput(const OutHandle &handle) 
     156{ 
     157    outs.erase(handle); 
    149158} 
    150159 
     
    221230{ 
    222231    Env *self = static_cast<Env*>(lua_touserdata(state, lua_upvalueindex(1))); 
    223     std::ostream &oss = (self->out == NULL) ? std::cout : *(self->out); 
     232    //std::ostream &oss = (self->out == NULL) ? std::cout : *(self->out); 
     233    //bool hasOut = !outs.empty(); 
    224234 
    225235    int numParams = lua_gettop(state); 
     
    246256            continue; 
    247257        } 
    248         if (i > 1) oss << '\t'; 
    249         oss << s; 
     258        BOOST_FOREACH(boost::shared_ptr<std::ostream> &oss, self->outs) { 
     259            if (i > 1) *oss << '\t'; 
     260            *oss << s; 
     261        } 
    250262        lua_pop(state, 1); 
    251263    } 
    252     oss << std::endl; 
     264    BOOST_FOREACH(boost::shared_ptr<std::ostream> &oss, self->outs) { 
     265        *oss << std::endl; 
     266    } 
    253267 
    254268    return 0; 
  • trunk/engine/Script/Env.h

    r703 r893  
    2323#pragma once 
    2424 
    25 #include <queue> 
     25#include <list> 
    2626 
    2727#include <lua.hpp> 
     
    6464        void Reset(); 
    6565        void ActivateSandbox(); 
    66         void SetOutput(boost::shared_ptr<std::ostream> out); 
     66 
     67    private: 
     68        typedef std::list<boost::shared_ptr<std::ostream> > outs_t; 
     69    public: 
     70        typedef outs_t::iterator OutHandle; 
     71        OutHandle AddOutput(boost::shared_ptr<std::ostream> out); 
     72        void RemoveOutput(const OutHandle &handle); 
    6773 
    6874        std::string GetVersionString() const; 
     
    7884 
    7985    private: 
    80         boost::shared_ptr<std::ostream> out; 
     86        outs_t outs; 
    8187        lua_State *state; 
    8288};