PageRenderTime 49ms CodeModel.GetById 15ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llcommon/lleventcoro.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 146 lines | 78 code | 9 blank | 59 comment | 11 complexity | d583c16b248b6e5de673dd94e91e8df5 MD5 | raw file
  1/**
  2 * @file   lleventcoro.cpp
  3 * @author Nat Goodspeed
  4 * @date   2009-04-29
  5 * @brief  Implementation for lleventcoro.
  6 * 
  7 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29// Precompiled header
 30#include "linden_common.h"
 31// associated header
 32#include "lleventcoro.h"
 33// STL headers
 34#include <map>
 35// std headers
 36// external library headers
 37// other Linden headers
 38#include "llsdserialize.h"
 39#include "llerror.h"
 40#include "llcoros.h"
 41
 42std::string LLEventDetail::listenerNameForCoroImpl(const void* self_id)
 43{
 44    // First, if this coroutine was launched by LLCoros::launch(), find that name.
 45    std::string name(LLCoros::instance().getNameByID(self_id));
 46    if (! name.empty())
 47    {
 48        return name;
 49    }
 50    // Apparently this coroutine wasn't launched by LLCoros::launch(). Check
 51    // whether we have a memo for this self_id.
 52    typedef std::map<const void*, std::string> MapType;
 53    static MapType memo;
 54    MapType::const_iterator found = memo.find(self_id);
 55    if (found != memo.end())
 56    {
 57        // this coroutine instance has called us before, reuse same name
 58        return found->second;
 59    }
 60    // this is the first time we've been called for this coroutine instance
 61    name = LLEventPump::inventName("coro");
 62    memo[self_id] = name;
 63    LL_INFOS("LLEventCoro") << "listenerNameForCoroImpl(" << self_id << "): inventing coro name '"
 64                            << name << "'" << LL_ENDL;
 65    return name;
 66}
 67
 68void LLEventDetail::storeToLLSDPath(LLSD& dest, const LLSD& rawPath, const LLSD& value)
 69{
 70    if (rawPath.isUndefined())
 71    {
 72        // no-op case
 73        return;
 74    }
 75
 76    // Arrange to treat rawPath uniformly as an array. If it's not already an
 77    // array, store it as the only entry in one.
 78    LLSD path;
 79    if (rawPath.isArray())
 80    {
 81        path = rawPath;
 82    }
 83    else
 84    {
 85        path.append(rawPath);
 86    }
 87
 88    // Need to indicate a current destination -- but that current destination
 89    // needs to change as we step through the path array. Where normally we'd
 90    // use an LLSD& to capture a subscripted LLSD lvalue, this time we must
 91    // instead use a pointer -- since it must be reassigned.
 92    LLSD* pdest = &dest;
 93
 94    // Now loop through that array
 95    for (LLSD::Integer i = 0; i < path.size(); ++i)
 96    {
 97        if (path[i].isString())
 98        {
 99            // *pdest is an LLSD map
100            pdest = &((*pdest)[path[i].asString()]);
101        }
102        else if (path[i].isInteger())
103        {
104            // *pdest is an LLSD array
105            pdest = &((*pdest)[path[i].asInteger()]);
106        }
107        else
108        {
109            // What do we do with Real or Array or Map or ...?
110            // As it's a coder error -- not a user error -- rub the coder's
111            // face in it so it gets fixed.
112            LL_ERRS("lleventcoro") << "storeToLLSDPath(" << dest << ", " << rawPath << ", " << value
113                                   << "): path[" << i << "] bad type " << path[i].type() << LL_ENDL;
114        }
115    }
116
117    // Here *pdest is where we should store value.
118    *pdest = value;
119}
120
121LLSD errorException(const LLEventWithID& result, const std::string& desc)
122{
123    // If the result arrived on the error pump (pump 1), instead of
124    // returning it, deliver it via exception.
125    if (result.second)
126    {
127        throw LLErrorEvent(desc, result.first);
128    }
129    // That way, our caller knows a simple return must be from the reply
130    // pump (pump 0).
131    return result.first;
132}
133
134LLSD errorLog(const LLEventWithID& result, const std::string& desc)
135{
136    // If the result arrived on the error pump (pump 1), log it as a fatal
137    // error.
138    if (result.second)
139    {
140        LL_ERRS("errorLog") << desc << ":" << std::endl;
141        LLSDSerialize::toPrettyXML(result.first, LL_CONT);
142        LL_CONT << LL_ENDL;
143    }
144    // A simple return must therefore be from the reply pump (pump 0).
145    return result.first;
146}