[Aptitude-devel] r3117 - in branches/aptitude-0.3/aptitude: . src/vscreen
Daniel Burrows
dburrows@costa.debian.org
Wed, 27 Apr 2005 02:02:16 +0000
Author: dburrows
Date: Wed Apr 27 02:02:13 2005
New Revision: 3117
Added:
branches/aptitude-0.3/aptitude/src/vscreen/fragment_cache.cc
branches/aptitude-0.3/aptitude/src/vscreen/fragment_cache.h
Modified:
branches/aptitude-0.3/aptitude/ChangeLog
branches/aptitude-0.3/aptitude/src/vscreen/Makefile.am
Log:
Add a caching fragment box.
Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog (original)
+++ branches/aptitude-0.3/aptitude/ChangeLog Wed Apr 27 02:02:13 2005
@@ -1,5 +1,10 @@
2005-04-26 Daniel Burrows <dburrows@debian.org>
+ * src/vscreen/fragment_cache.cc, src/vscreen/fragment_cache.h:
+
+ Add a special intermediate fragment widget that caches the
+ output of its contents.
+
* src/vscreen/vs_togglebutton.cc, src/vscreen/vs_togglebutton.h:
Fix the display of toggle buttons; in particular, make sure the
Modified: branches/aptitude-0.3/aptitude/src/vscreen/Makefile.am
==============================================================================
--- branches/aptitude-0.3/aptitude/src/vscreen/Makefile.am (original)
+++ branches/aptitude-0.3/aptitude/src/vscreen/Makefile.am Wed Apr 27 02:02:13 2005
@@ -18,6 +18,8 @@
curses++.h \
fragment.h \
fragment.cc \
+ fragment_cache.h\
+ fragment_cache.cc\
fragment_contents.h \
slotarg.h \
vscreen.cc \
Added: branches/aptitude-0.3/aptitude/src/vscreen/fragment_cache.cc
==============================================================================
--- (empty file)
+++ branches/aptitude-0.3/aptitude/src/vscreen/fragment_cache.cc Wed Apr 27 02:02:13 2005
@@ -0,0 +1,101 @@
+// fragment_cache.cc
+//
+// Copyright (C) 2005 Daniel Burrows
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; see the file COPYING. If not, write to
+// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+#include "fragment_cache.h"
+
+fragment_cache::fragment_cache(fragment *_contents)
+ :contents(_contents), cached_lines_valid(false),
+ cached_max_width_valid(false), cached_trailing_width_valid(false),
+ cached_final_nl_valid(false)
+{
+}
+
+fragment_cache::~fragment_cache()
+{
+ delete contents;
+}
+
+void fragment_cache::invalidate()
+{
+ cached_lines_valid=cached_max_width_valid=false;
+ cached_trailing_width_valid=cached_final_nl=false;
+}
+
+fragment_contents fragment_cache::layout(size_t firstw, size_t restw)
+{
+ if(!cached_lines_valid ||
+ cached_lines_first_width != firstw ||
+ cached_lines_rest_width != restw)
+ {
+ cached_lines=contents->layout(firstw, restw);
+ cached_lines_first_width=firstw;
+ cached_lines_rest_width=restw;
+ cached_lines_valid=true;
+ }
+
+ return cached_lines;
+}
+
+void fragment_cache::set_attr(int attr)
+{
+ contents->set_attr(attr);
+
+ invalidate();
+}
+
+size_t fragment_cache::max_width(size_t first_indent, size_t rest_indent)
+{
+ if(!cached_max_width_valid ||
+ first_indent != cached_max_width_first_indent ||
+ rest_indent != cached_max_width_rest_indent)
+ {
+ cached_max_width=contents->max_width(first_indent, rest_indent);
+ cached_max_width_first_indent=first_indent;
+ cached_max_width_rest_indent=rest_indent;
+ cached_max_width_valid=true;
+ }
+
+ return cached_max_width;
+}
+
+size_t fragment_cache::trailing_width(size_t first_indent, size_t rest_indent)
+{
+ if(!cached_trailing_width_valid ||
+ first_indent != cached_trailing_width_first_indent ||
+ rest_indent != cached_trailing_width_rest_indent)
+ {
+ cached_trailing_width=contents->trailing_width(first_indent, rest_indent);
+ cached_trailing_width_first_indent=first_indent;
+ cached_trailing_width_rest_indent=rest_indent;
+ cached_trailing_width_valid=true;
+ }
+
+ return cached_trailing_width;
+}
+
+bool fragment_cache::final_newline()
+{
+ if(!cached_final_nl_valid)
+ {
+ cached_final_nl=contents->final_newline();
+ cached_final_nl_valid=true;
+ }
+
+ return cached_final_nl;
+}
Added: branches/aptitude-0.3/aptitude/src/vscreen/fragment_cache.h
==============================================================================
--- (empty file)
+++ branches/aptitude-0.3/aptitude/src/vscreen/fragment_cache.h Wed Apr 27 02:02:13 2005
@@ -0,0 +1,80 @@
+// fragment_cache.h -*-c++-*-
+//
+// Copyright (C) 2005 Daniel Burrows
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as
+// published by the Free Software Foundation; either version 2 of
+// the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; see the file COPYING. If not, write to
+// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+//
+// A special fragment class that caches its contents.
+
+#ifndef FRAGMENT_CACHE_H
+#define FRAGMENT_CACHE_H
+
+#include "fragment.h"
+
+/** A fragment that caches its contents; a cached result is used if
+ * the same width is passed to the layout routine twice in a row.
+ * Obviously this should only be done if you know that the contents
+ * are static.
+ */
+class fragment_cache:public fragment
+{
+ fragment *contents;
+
+ /** The last cached result. */
+ fragment_contents cached_lines;
+
+ /** If cached_lines is valid, cached_lines was formatted for these
+ * widths.
+ */
+ size_t cached_lines_first_width, cached_lines_rest_width;
+
+ /** The cached max_width value. */
+ size_t cached_max_width;
+
+ /** For what indents is cached_max_width valid? */
+ size_t cached_max_width_first_indent, cached_max_width_rest_indent;
+
+ /** The cached trailing_width value. */
+ size_t cached_trailing_width;
+
+ /** For what indents is cached_trailing_width valid? */
+ size_t cached_trailing_width_first_indent, cached_trailing_width_rest_indent;
+
+ /** The cached final_newline value. */
+ bool cached_final_nl:1;
+
+ /** If \b true, the corresponding property is valid. */
+ bool cached_lines_valid:1, cached_max_width_valid:1;
+ /** If \b true, the corresponding property is valid. */
+ bool cached_trailing_width_valid:1, cached_final_nl_valid:1;
+public:
+ fragment_cache(fragment *_contents);
+ ~fragment_cache();
+
+ void invalidate();
+
+ fragment_contents layout(size_t firstw, size_t restw);
+
+ void set_attr(int attr);
+
+ size_t max_width(size_t first_indent, size_t rest_indent);
+ size_t trailing_width(size_t first_indent, size_t rest_indent);
+
+ bool final_newline();
+};
+
+
+#endif