Isis 3 Programmer Reference
Resource.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "Resource.h"
8 
9 // Qt library
10 #include <QSharedPointer>
11 #include <QString>
12 
13 // boost library
14 #include <boost/foreach.hpp>
15 
16 // other ISIS
17 #include "IException.h"
18 #include "PvlContainer.h"
19 #include "PvlFlatMap.h"
20 #include "PvlKeyword.h"
21 #include "PvlObject.h"
22 
23 using namespace std;
24 
25 namespace Isis {
26 
31  Resource::Resource(): m_data( new ResourceData("Resource") ),
32  m_discard( false ) {
33  setName( m_data->m_name );
34  }
35 
36 
43  Resource::Resource(const QString &name) : m_data( new ResourceData(name) ),
44  m_discard( false ) {
45  setName(name);
46  }
47 
48 
56  Resource::Resource(const QString &name, const PvlFlatMap &profile) :
57  m_data( new ResourceData(name, profile) ),
58  m_discard(false) {
59  setName(name);
60  }
61 
62 
68  Resource::Resource(const Resource &other) : m_data( other.m_data ),
69  m_discard( other.m_discard ) {
70  }
71 
72 
81  Resource::Resource(const Resource &other, const bool deepcopy) :
82  m_data( new ResourceData( *other.m_data ) ),
83  m_discard( other.m_discard ) {
84  if ( deepcopy ) { m_data.detach(); }
85  }
86 
87 
95  Resource::Resource(const QString &name, const PvlContainer &profile) :
96  m_data( new ResourceData(name, PvlFlatMap(profile) ) ),
97  m_discard(false) {
98  setName(name);
99  }
100 
101 
106  }
107 
108 
114  QString Resource::name() const {
115  return ( m_data->m_name );
116  }
117 
118 
124  void Resource::setName(const QString &identity) {
125  m_data->m_name = identity;
126  add("Identity", identity);
127  return;
128  }
129 
130 
142  bool Resource::isEqual(const Resource &other) const {
143  return ( name().toLower() == other.name().toLower() );
144  }
145 
146 
155  bool Resource::exists(const QString &keywordName) const {
156  return ( m_data->m_keys.exists(keywordName) );
157  }
158 
159 
169  int Resource::count(const QString &keywordName) const {
170  return ( m_data->m_keys.count(keywordName) );
171  }
172 
173 
187  bool Resource::isNull(const QString &keywordName, const int index) const {
188  return ( m_data->m_keys.isNull(keywordName, index) );
189  }
190 
191 
198  const PvlFlatMap &Resource::keys() const {
199  return ( m_data->m_keys );
200  }
201 
202 
224  QString Resource::value(const QString &keywordName, const int &index) const {
225  return ( m_data->m_keys.get(keywordName, index) );
226  }
227 
228 
251  QString Resource::value(const QString &keywordName, const QString &defaultValue,
252  const int &index) const {
253  QString keywordValue(defaultValue);
254  if ( !isNull(keywordName, index) ) {
255  keywordValue = value(keywordName, index);
256  }
257  return (keywordValue);
258  }
259 
260 
270  PvlKeyword Resource::keyword(const QString &keywordName) const {
271  if ( m_data->m_keys.exists(keywordName) ) {
272  return ( m_data->m_keys.keyword(keywordName) );
273  }
274  // Return empty keyword
275  return (PvlKeyword(keywordName));
276  }
277 
278 
286  void Resource::add(const QString &keywordName, const QString &keywordValue) {
287  m_data->m_keys.add(keywordName, keywordValue);
288  return;
289  }
290 
291 
298  void Resource::add(const PvlKeyword &keyword) {
299  m_data->m_keys.add(keyword);
300  }
301 
302 
310  void Resource::add(const PvlFlatMap &keys) {
311  BOOST_FOREACH ( PvlKeyword keyword, keys ) {
312  add(keyword);
313  }
314  return;
315  }
316 
317 
327  void Resource::append(const QString &keywordName, const QString &keywordValue) {
328  m_data->m_keys.append(keywordName, keywordValue);
329  return;
330  }
331 
332 
341  int Resource::erase(const QString &keywordName) {
342  return ( m_data->m_keys.erase(keywordName) );
343  }
344 
345 
357  m_data->m_geom = SharedGisGeometry(geom);
358  return;
359  }
360 
361 
368  m_data->m_geom = geom;
369  return;
370  }
371 
372 
378  bool Resource::hasGeometry() const {
379  return ( !m_data->m_geom.isNull() );
380  }
381 
382 
390  if ( hasGeometry() ) {
391  return ( !m_data->m_geom->isEmpty() );
392  }
393 
394  return (false);
395  }
396 
397 
404  return ( m_data->m_geom );
405  }
406 
407 
412  m_discard = false;
413  }
414 
415 
421  bool Resource::isActive() const {
422  return ( !m_discard );
423  }
424 
425 
430  m_discard = true;
431  }
432 
433 
439  bool Resource::isDiscarded() const {
440  return ( m_discard );
441  }
442 
443 
452  bool Resource::hasAsset(const QString &assetName) const {
453  return ( m_data->m_assets.contains(assetName.toLower()) );
454  }
455 
456 
463  void Resource::addAsset(const QString &assetName, QVariant &assetValue) {
464  m_data->m_assets.insert(assetName.toLower(), assetValue);
465  return;
466  }
467 
468 
477  int Resource::removeAsset(const QString &assetName) {
478  return ( m_data->m_assets.remove(assetName.toLower()) );
479  }
480 
481 
488  int n = m_data->m_assets.size();
489  m_data->m_assets.clear();
490  return (n);
491  }
492 
493 
504  QVariant Resource::asset(const QString &assetName) const {
505  if ( !hasAsset(assetName) ) {
507  "Requested asset " + assetName + " does not exist.",
508  _FILEINFO_);
509  }
510  return ( m_data->m_assets.value(assetName.toLower()) );
511  }
512 
513 
527  Resource *resource = new Resource(*this, false);
528  resource->m_discard = m_discard;
529  return (resource);
530  }
531 
532 
548  Resource *Resource::clone(const QString &name, const bool &withAssets) const {
549  Resource *resource = new Resource(*this, true);
550  if ( !withAssets ) { resource->clearAssets(); }
551  resource->activate();
552  return (resource);
553  }
554 
555 
566  PvlObject Resource::toPvl(const QString &pvlName) const {
567  PvlObject object(pvlName);
568  PvlFlatMap::ConstPvlFlatMapIterator key = m_data->m_keys.begin();
569  while ( key != m_data->m_keys.end() ) {
570  object.addKeyword(key.value());
571  ++key;
572  }
573  return ( object );
574  }
575 
576 } //namespace Isis
Isis::Resource::m_discard
bool m_discard
A flag to indicate whether the Resource is inactive.
Definition: Resource.h:160
Isis::Resource::activate
void activate()
Activate a resource.
Definition: Resource.cpp:411
Isis::Resource::isActive
bool isActive() const
Accessor method to determine whether this Resource is to be discarded.
Definition: Resource.cpp:421
Isis::Resource::keys
const PvlFlatMap & keys() const
Accessor method for the PVL keywords associated with this Resource.
Definition: Resource.cpp:198
Isis::Resource::addAsset
void addAsset(const QString &assetName, QVariant &assetValue)
Inserts an asset with the given name and value into this Resource's VariantList.
Definition: Resource.cpp:463
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
Isis::Resource::exists
bool exists(const QString &keywordName) const
Determines whether a PVL keyword with the given name is in this Resource.
Definition: Resource.cpp:155
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
Isis::Resource::m_data
QExplicitlySharedDataPointer< ResourceData > m_data
Explicitly managed pointer to Resource data.
Definition: Resource.h:159
Isis::Resource::discard
void discard()
Discard a resource.
Definition: Resource.cpp:429
Isis::PvlFlatMap
Provides a flat map of PvlKeywords.
Definition: PvlFlatMap.h:218
Isis::Resource::count
int count(const QString &keywordName) const
Counts the number of values the PVL keyword with the given name has, if it exists in this Resource.
Definition: Resource.cpp:169
Isis::PvlFlatMap::ConstPvlFlatMapIterator
QMap< QString, PvlKeyword >::const_iterator ConstPvlFlatMapIterator
A const iterator for the underling QMap that PvlFlatMap is built on.
Definition: PvlFlatMap.h:222
Isis::Resource::clone
virtual Resource * clone(const QString &name, const bool &withAssets=false) const
Clone this resource for additional specialized use.
Definition: Resource.cpp:548
Isis::Resource::isDiscarded
bool isDiscarded() const
Accessor method to determine whether this Resource is to be discarded.
Definition: Resource.cpp:439
QSharedPointer< GisGeometry >
Isis::Resource::value
QString value(const QString &keywordName, const int &keywordIndex=0) const
Gets the value of the PVL keyword with the given name at the given index.
Definition: Resource.cpp:224
Isis::Resource::~Resource
virtual ~Resource()
Destroys the Resource object.
Definition: Resource.cpp:105
Isis::Resource::hasGeometry
bool hasGeometry() const
This method is used to determine whether the GIS geometry has been set for this Resource.
Definition: Resource.cpp:378
Isis::Resource::setName
void setName(const QString &identity)
A mutator to set the Resource's name.
Definition: Resource.cpp:124
Isis::Resource::isEqual
bool isEqual(const Resource &other) const
Checks for equality of another Resource.
Definition: Resource.cpp:142
Isis::Resource::removeAsset
int removeAsset(const QString &assetName)
Removes all of the assets in this Resource that are mapped to the given name.
Definition: Resource.cpp:477
Isis::GisGeometry
Encapsulation class provides support for GEOS-C API.
Definition: GisGeometry.h:50
Isis::Resource::clearAssets
int clearAssets()
Clears the assets from this Resource's VariantList.
Definition: Resource.cpp:487
Isis::Resource::name
QString name() const
Accessor for a string containing the Resource's name.
Definition: Resource.cpp:114
Isis::Resource::add
void add(const QString &keywordName, const QString &keywordValue)
Adds a PVL keyword with the given name and value to this Resource.
Definition: Resource.cpp:286
Isis::Resource::asset
QVariant asset(const QString &assetName) const
Retrieves the value of the asset in this Resource that is mapped to the given name.
Definition: Resource.cpp:504
Isis::Resource::Resource
Resource()
Default constructor for a Resource object.
Definition: Resource.cpp:31
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::SharedGisGeometry
QSharedPointer< GisGeometry > SharedGisGeometry
Definition for a SharedGisGeometry, a shared pointer to a GisGeometry.
Definition: GisGeometry.h:125
Isis::Resource::ResourceData
Shared Resource data pointer.
Definition: Resource.h:123
Isis::Resource
This class provides a resource of PVL keywords for Strategy classes.
Definition: Resource.h:54
Isis::Resource::keyword
PvlKeyword keyword(const QString &keywordName) const
Gets the PvlKeyword object with the given name, if it exists in this Resource.
Definition: Resource.cpp:270
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
std
Namespace for the standard library.
Isis::Resource::append
void append(const QString &keywordName, const QString &keywordValue)
Appends the given value to the PVL keyword with the given name.
Definition: Resource.cpp:327
Isis::Resource::erase
int erase(const QString &keywordName)
Removes all of the PVL keywords in this Resource that are associated with the given name.
Definition: Resource.cpp:341
Isis::PvlContainer
Contains more than one keyword-value pair.
Definition: PvlContainer.h:49
Isis::Resource::hasAsset
bool hasAsset(const QString &assetName) const
This method is used to determine whether an asset with the given name is in this Resource.
Definition: Resource.cpp:452
Isis::Resource::geometry
SharedGisGeometry geometry() const
Accessor method for this Resource's GIS geometry.
Definition: Resource.cpp:403
Isis::Resource::isNull
bool isNull(const QString &keywordName, const int keywordIndex=0) const
Determines whether the PVL keyword with the given name at the given index is null.
Definition: Resource.cpp:187
Isis::Resource::copy
virtual Resource * copy() const
Copy this resource for distinct management of its status.
Definition: Resource.cpp:526
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::Resource::toPvl
virtual PvlObject toPvl(const QString &pvlName="Resource") const
Transfer all keywords in map to a PvlObject.
Definition: Resource.cpp:566
Isis::Resource::hasValidGeometry
bool hasValidGeometry() const
This method is used to determine whether a valid GIS geometry has been set for this Resource.
Definition: Resource.cpp:389