PageRenderTime 34ms CodeModel.GetById 22ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llfixedbuffer.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 97 lines | 56 code | 16 blank | 25 comment | 2 complexity | 8c455ef7a045f5c24c62df46bbeb26dd MD5 | raw file
 1/** 
 2 * @file llfixedbuffer.cpp
 3 *
 4 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 5 * Second Life Viewer Source Code
 6 * Copyright (C) 2010, Linden Research, Inc.
 7 * 
 8 * This library is free software; you can redistribute it and/or
 9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation;
11 * version 2.1 of the License only.
12 * 
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 * 
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 * 
22 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
23 * $/LicenseInfo$
24 */
25#include "linden_common.h"
26
27#include "llfixedbuffer.h"
28
29////////////////////////////////////////////////////////////////////////////
30
31LLFixedBuffer::LLFixedBuffer(const U32 max_lines)
32	: LLLineBuffer(),
33	  mMaxLines(max_lines),
34	  mMutex(NULL)
35{
36	mTimer.reset();
37}
38
39LLFixedBuffer::~LLFixedBuffer()
40{
41	clear();
42}
43
44void LLFixedBuffer::clear()
45{
46	mMutex.lock() ;
47	mLines.clear();
48	mAddTimes.clear();
49	mLineLengths.clear();
50	mMutex.unlock() ;
51
52	mTimer.reset();
53}
54
55
56void LLFixedBuffer::addLine(const std::string& utf8line)
57{
58	LLWString wstring = utf8str_to_wstring(utf8line);
59	addWLine(wstring);
60}
61
62void LLFixedBuffer::addWLine(const LLWString& line)
63{
64	if (line.empty())
65	{
66		return;
67	}
68
69	removeExtraLines();
70
71	mMutex.lock() ;
72	mLines.push_back(line);
73	mLineLengths.push_back((S32)line.length());
74	mAddTimes.push_back(mTimer.getElapsedTimeF32());
75	mMutex.unlock() ;
76}
77
78
79void LLFixedBuffer::setMaxLines(S32 max_lines)
80{
81	mMaxLines = max_lines;
82
83	removeExtraLines();
84}
85
86
87void LLFixedBuffer::removeExtraLines()
88{
89	mMutex.lock() ;
90	while ((S32)mLines.size() > llmax((S32)0, (S32)(mMaxLines - 1)))
91	{
92		mLines.pop_front();
93		mAddTimes.pop_front();
94		mLineLengths.pop_front();
95	}
96	mMutex.unlock() ;
97}