DUDS
Distributed Update of Data from Something
BppStringCache.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2019 Jeff Jackowski
9  */
10 #ifndef BPPSTRINGCACHE_HPP
11 #define BPPSTRINGCACHE_HPP
13 #include <boost/multi_index_container.hpp>
14 #include <boost/multi_index/hashed_index.hpp>
15 #include <boost/multi_index/sequenced_index.hpp>
16 #include <boost/multi_index/member.hpp>
17 #include <boost/multi_index/composite_key.hpp>
18 
19 namespace duds { namespace ui { namespace graphics {
20 
28 class BppStringCache : boost::noncopyable {
36  struct BppString {
44  std::u32string text;
49  };
58  typedef boost::multi_index::multi_index_container<
59  BppString,
60  boost::multi_index::indexed_by<
61  boost::multi_index::hashed_non_unique<
62  boost::multi_index::tag<struct index_text>,
63  boost::multi_index::composite_key<
64  BppString,
65  boost::multi_index::member<
66  BppString,
67  std::u32string,
69  >,
70  boost::multi_index::member<
71  BppString,
74  >
75  >
76  >,
77  boost::multi_index::sequenced<
78  boost::multi_index::tag<struct index_seq>
79  >
80  >
81  > Cache;
89  unsigned int maxS;
93  unsigned int maxB;
97  unsigned int curB = 0;
102 public:
116  const BppFontSptr &font,
117  unsigned int maxBytes = 256 * 1024,
118  unsigned int maxStrings = -1
119  );
134  BppFontSptr &&font,
135  unsigned int maxBytes = 256 * 1024,
136  unsigned int maxStrings = -1
137  );
164  const std::string &path,
165  unsigned int maxBytes = 256 * 1024,
166  unsigned int maxStrings = -1
167  ) : BppStringCache(BppFont::make(path), maxBytes, maxStrings) { }
180  static std::shared_ptr<BppStringCache> make(
181  const BppFontSptr &font,
182  unsigned int maxBytes = 256 * 1024,
183  unsigned int maxStrings = -1
184  ) {
185  return std::make_shared<BppStringCache>(font, maxBytes, maxStrings);
186  }
200  static std::shared_ptr<BppStringCache> make(
201  BppFontSptr &&font,
202  unsigned int maxBytes = 256 * 1024,
203  unsigned int maxStrings = -1
204  ) {
205  return std::make_shared<BppStringCache>(
206  std::move(font),
207  maxBytes,
208  maxStrings
209  );
210  }
236  static std::shared_ptr<BppStringCache> make(
237  const std::string &path,
238  unsigned int maxBytes = 256 * 1024,
239  unsigned int maxStrings = -1
240  ) {
241  return std::make_shared<BppStringCache>(
242  BppFont::make(path),
243  maxBytes,
244  maxStrings
245  );
246  }
250  const BppFontSptr &font() const {
251  return fnt;
252  }
256  unsigned int maxBytes() const {
257  return maxB;
258  }
262  unsigned int maxStrings() const {
263  return maxS;
264  }
268  unsigned int bytes() const {
269  return curB;
270  }
274  unsigned int strings() const {
275  return cache.size();
276  }
280  void clear();
298  const std::string &str,
299  BppFont::Flags flags = BppFont::AlignLeft
300  );
320  const std::u32string &str,
321  BppFont::Flags flags = BppFont::AlignLeft
322  );
323 };
324 
328 typedef std::shared_ptr<BppStringCache> BppStringCacheSptr;
329 
330 } } }
331 
332 #endif // #ifndef BPPSTRINGCACHE_HPP
std::shared_ptr< BppStringCache > BppStringCacheSptr
A std::shared_ptr to a BppStringCache object.
const BppFontSptr & font() const
Returns the font object used by this cache to render text.
ConstBppImageSptr img
The rendered image of the text.
std::shared_ptr< BppFont > BppFontSptr
Definition: BppFont.hpp:263
unsigned int maxStrings() const
Returns the maximum number of cached images.
Stores an image and the data used to create it using the font.
boost::multi_index::multi_index_container< BppString, boost::multi_index::indexed_by< boost::multi_index::hashed_non_unique< boost::multi_index::tag< struct index_text >, boost::multi_index::composite_key< BppString, boost::multi_index::member< BppString, std::u32string, &BppString::text >, boost::multi_index::member< BppString, BppFont::Flags, &BppString::flags > > >, boost::multi_index::sequenced< boost::multi_index::tag< struct index_seq > > > > Cache
The container type for the rendered strings.
std::shared_ptr< const BppImage > ConstBppImageSptr
Definition: BppImage.hpp:1778
unsigned int maxBytes() const
Returns the maximum size of the cached images in bytes.
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
BppStringCache(const BppFontSptr &font, unsigned int maxBytes=256 *1024, unsigned int maxStrings=-1)
Creates a cache of rendered strings made using the given font.
unsigned int curB
The current size of all rendered text images in the cache.
void clear()
Clears all text images from the cache.
duds::general::Spinlock block
Used for thread safety.
static std::shared_ptr< BppFont > make()
Returns a shared pointer to a new BppFont object.
Definition: BppFont.hpp:71
unsigned int strings() const
Returns the number of currently stored cached strings.
BppStringCache(const std::string &path, unsigned int maxBytes=256 *1024, unsigned int maxStrings=-1)
Creates a cache of rendered strings made using a font created from the given image archive path...
static std::shared_ptr< BppStringCache > make(BppFontSptr &&font, unsigned int maxBytes=256 *1024, unsigned int maxStrings=-1)
Creates a cache of rendered strings made using the given font.
static std::shared_ptr< BppStringCache > make(const std::string &path, unsigned int maxBytes=256 *1024, unsigned int maxStrings=-1)
Creates a cache of rendered strings made using a font created from the given image archive path...
unsigned int bytes() const
Returns the total size in bytes of all the cached images.
A simple spinlock following the lockable and timed lockable concepts so that it can be used with std:...
Definition: Spinlock.hpp:52
duds::general::BitFlags< struct BppFontRenderingFlags > Flags
Option flags that affect how text is rendered.
Definition: BppFont.hpp:165
BppFont::Flags flags
The font rendering flags.
unsigned int maxB
The maximum size of rendered text images, in bytes, the cache may hold.
BppFontSptr fnt
The font to use for rendering.
General graphics related code.
Definition: HD44780.hpp:15
std::u32string text
The text rendered into img.
static constexpr Flags AlignLeft
Align each line to the left.
Definition: BppFont.hpp:184
Cache cache
The cache of rendered strings.
unsigned int maxS
The maximum number of strings the cache may hold.
static std::shared_ptr< BppStringCache > make(const BppFontSptr &font, unsigned int maxBytes=256 *1024, unsigned int maxStrings=-1)
Creates a cache of rendered strings made using the given font.
Maintains a cache for rendered strings that helps avoid re-rendering strings that may need to be show...
Renders strings using a font made of BppImage objects for glyphs.
Definition: BppFont.hpp:30