DUDS
Distributed Update of Data from Something
BppImageArchive.cpp
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) 2018 Jeff Jackowski
9  */
13 #include <duds/general/Errors.hpp>
14 
15 namespace duds { namespace ui { namespace graphics {
16 
17 void BppImageArchive::load(const std::string &path) {
18  std::ifstream is(path);
19  if (!is.good()) {
22  );
23  }
24  try {
25  load(is);
26  } catch (boost::exception &be) {
27  // add the file name to the error metadata
28  be << ImageArchiveFileName(path);
29  throw;
30  }
31 }
32 
33 void BppImageArchive::load(std::istream &is) {
34  BppImageArchiveSequence bias(is);
35  bias.readHeader();
37  for (; iter != bias.end(); ++iter) {
38  std::lock_guard<duds::general::Spinlock> lock(block);
39  arc[iter->first] = iter->second;
40  }
41 }
42 
43 void BppImageArchive::add(const std::string &name, const BppImageSptr &img) {
44  std::lock_guard<duds::general::Spinlock> lock(block);
45  arc[name] = img;
46 }
47 
48 void BppImageArchive::add(const std::string &name, BppImageSptr &&img) {
49  std::lock_guard<duds::general::Spinlock> lock(block);
50  arc[name] = std::move(img);
51 }
52 
53 const BppImageSptr &BppImageArchive::get(const std::string &name) const {
54  std::lock_guard<duds::general::Spinlock> lock(block);
55  ImageMap::const_iterator
56  iter = arc.find(name);
57  if (iter != arc.end()) {
58  return iter->second;
59  }
61 }
62 
63 BppImageSptr BppImageArchive::tryGet(const std::string &name) const {
64  std::lock_guard<duds::general::Spinlock> lock(block);
65  ImageMap::const_iterator
66  iter = arc.find(name);
67  if (iter != arc.end()) {
68  return iter->second;
69  }
70  return BppImageSptr();
71 }
72 
73 } } }
iterator begin()
Parses the first image in the stream and returns an iterator to that data.
void add(const std::string &name, const BppImageSptr &img)
Adds an image to the archive.
void load(const std::string &path)
Loads images from an image archive in the specified file.
duds::general::Spinlock block
Used for thread safety.
boost::error_info< struct Info_ArchiveImageName, std::string > ImageArchiveImageName
The name of the image involved in an ImageArchiveError.
An input iterator that will parse and supply image data from the stream.
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
The base class for errors resulting from the attempt to read an image archive stream or file...
const BppImageSptr & get(const std::string &name) const
Returns the image with the given name.
static constexpr iterator end()
Returns the end iterator.
boost::error_info< struct Info_ImageArcFileName, std::string > ImageArchiveFileName
The name of the image archive file involved in an ImageArchiveStreamError.
void readHeader()
Parses the headers used in BppImageArchive files.
std::shared_ptr< BppImage > BppImageSptr
Definition: BppImage.hpp:1777
Provides an input iterator to a sequence of named bit-per-pixel images read from an input stream...
ImageMap arc
The images keyed by name.
General graphics related code.
Definition: HD44780.hpp:15
An image was requested that the archive does not contain.
BppImageSptr tryGet(const std::string &name) const
Returns the image with the given name.
General errors.
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48