Isis 3 Programmer Reference
Resource.cpp
Go to the documentation of this file.
1 
24 #include "Resource.h"
25 
26 // Qt library
27 #include <QSharedPointer>
28 #include <QString>
29 
30 // boost library
31 #include <boost/foreach.hpp>
32 
33 // other ISIS
34 #include "IException.h"
35 #include "PvlContainer.h"
36 #include "PvlFlatMap.h"
37 #include "PvlKeyword.h"
38 #include "PvlObject.h"
39 
40 using namespace std;
41 
42 namespace Isis {
43 
48  Resource::Resource(): m_data( new ResourceData("Resource") ),
49  m_discard( false ) {
50  setName( m_data->m_name );
51  }
52 
53 
60  Resource::Resource(const QString &name) : m_data( new ResourceData(name) ),
61  m_discard( false ) {
62  setName(name);
63  }
64 
65 
73  Resource::Resource(const QString &name, const PvlFlatMap &profile) :
74  m_data( new ResourceData(name, profile) ),
75  m_discard(false) {
76  setName(name);
77  }
78 
79 
85  Resource::Resource(const Resource &other) : m_data( other.m_data ),
86  m_discard( other.m_discard ) {
87  }
88 
89 
98  Resource::Resource(const Resource &other, const bool deepcopy) :
99  m_data( new ResourceData( *other.m_data ) ),
100  m_discard( other.m_discard ) {
101  if ( deepcopy ) { m_data.detach(); }
102  }
103 
104 
112  Resource::Resource(const QString &name, const PvlContainer &profile) :
113  m_data( new ResourceData(name, PvlFlatMap(profile) ) ),
114  m_discard(false) {
115  setName(name);
116  }
117 
118 
123  }
124 
125 
131  QString Resource::name() const {
132  return ( m_data->m_name );
133  }
134 
135 
141  void Resource::setName(const QString &identity) {
142  m_data->m_name = identity;
143  add("Identity", identity);
144  return;
145  }
146 
147 
159  bool Resource::isEqual(const Resource &other) const {
160  return ( name().toLower() == other.name().toLower() );
161  }
162 
163 
172  bool Resource::exists(const QString &keywordName) const {
173  return ( m_data->m_keys.exists(keywordName) );
174  }
175 
176 
186  int Resource::count(const QString &keywordName) const {
187  return ( m_data->m_keys.count(keywordName) );
188  }
189 
190 
204  bool Resource::isNull(const QString &keywordName, const int index) const {
205  return ( m_data->m_keys.isNull(keywordName, index) );
206  }
207 
208 
215  const PvlFlatMap &Resource::keys() const {
216  return ( m_data->m_keys );
217  }
218 
219 
241  QString Resource::value(const QString &keywordName, const int &index) const {
242  return ( m_data->m_keys.get(keywordName, index) );
243  }
244 
245 
268  QString Resource::value(const QString &keywordName, const QString &defaultValue,
269  const int &index) const {
270  QString keywordValue(defaultValue);
271  if ( !isNull(keywordName, index) ) {
272  keywordValue = value(keywordName, index);
273  }
274  return (keywordValue);
275  }
276 
277 
287  PvlKeyword Resource::keyword(const QString &keywordName) const {
288  if ( m_data->m_keys.exists(keywordName) ) {
289  return ( m_data->m_keys.keyword(keywordName) );
290  }
291  // Return empty keyword
292  return (PvlKeyword(keywordName));
293  }
294 
295 
303  void Resource::add(const QString &keywordName, const QString &keywordValue) {
304  m_data->m_keys.add(keywordName, keywordValue);
305  return;
306  }
307 
308 
315  void Resource::add(const PvlKeyword &keyword) {
316  m_data->m_keys.add(keyword);
317  }
318 
319 
327  void Resource::add(const PvlFlatMap &keys) {
328  BOOST_FOREACH ( PvlKeyword keyword, keys ) {
329  add(keyword);
330  }
331  return;
332  }
333 
334 
344  void Resource::append(const QString &keywordName, const QString &keywordValue) {
345  m_data->m_keys.append(keywordName, keywordValue);
346  return;
347  }
348 
349 
358  int Resource::erase(const QString &keywordName) {
359  return ( m_data->m_keys.erase(keywordName) );
360  }
361 
362 
374  m_data->m_geom = SharedGisGeometry(geom);
375  return;
376  }
377 
378 
385  m_data->m_geom = geom;
386  return;
387  }
388 
389 
395  bool Resource::hasGeometry() const {
396  return ( !m_data->m_geom.isNull() );
397  }
398 
399 
407  if ( hasGeometry() ) {
408  return ( !m_data->m_geom->isEmpty() );
409  }
410 
411  return (false);
412  }
413 
414 
421  return ( m_data->m_geom );
422  }
423 
424 
429  m_discard = false;
430  }
431 
432 
438  bool Resource::isActive() const {
439  return ( !m_discard );
440  }
441 
442 
447  m_discard = true;
448  }
449 
450 
456  bool Resource::isDiscarded() const {
457  return ( m_discard );
458  }
459 
460 
469  bool Resource::hasAsset(const QString &assetName) const {
470  return ( m_data->m_assets.contains(assetName.toLower()) );
471  }
472 
473 
480  void Resource::addAsset(const QString &assetName, QVariant &assetValue) {
481  m_data->m_assets.insert(assetName.toLower(), assetValue);
482  return;
483  }
484 
485 
494  int Resource::removeAsset(const QString &assetName) {
495  return ( m_data->m_assets.remove(assetName.toLower()) );
496  }
497 
498 
505  int n = m_data->m_assets.size();
506  m_data->m_assets.clear();
507  return (n);
508  }
509 
510 
521  QVariant Resource::asset(const QString &assetName) const {
522  if ( !hasAsset(assetName) ) {
524  "Requested asset " + assetName + " does not exist.",
525  _FILEINFO_);
526  }
527  return ( m_data->m_assets.value(assetName.toLower()) );
528  }
529 
530 
544  Resource *resource = new Resource(*this, false);
545  resource->m_discard = m_discard;
546  return (resource);
547  }
548 
549 
565  Resource *Resource::clone(const QString &name, const bool &withAssets) const {
566  Resource *resource = new Resource(*this, true);
567  if ( !withAssets ) { resource->clearAssets(); }
568  resource->activate();
569  return (resource);
570  }
571 
572 
583  PvlObject Resource::toPvl(const QString &pvlName) const {
584  PvlObject object(pvlName);
585  PvlFlatMap::ConstPvlFlatMapIterator key = m_data->m_keys.begin();
586  while ( key != m_data->m_keys.end() ) {
587  object.addKeyword(key.value());
588  ++key;
589  }
590  return ( object );
591  }
592 
593 } //namespace Isis
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:186
QMap< QString, PvlKeyword >::const_iterator ConstPvlFlatMapIterator
A const iterator for the underling QMap that PvlFlatMap is built on.
Definition: PvlFlatMap.h:240
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
Contains more than one keyword-value pair.
Definition: PvlContainer.h:63
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:241
virtual ~Resource()
Destroys the Resource object.
Definition: Resource.cpp:122
PvlKeyword keyword(const QString &keywordName) const
Gets the PvlKeyword object with the given name, if it exists in this Resource.
Definition: Resource.cpp:287
virtual PvlObject toPvl(const QString &pvlName="Resource") const
Transfer all keywords in map to a PvlObject.
Definition: Resource.cpp:583
bool isActive() const
Accessor method to determine whether this Resource is to be discarded.
Definition: Resource.cpp:438
virtual Resource * clone(const QString &name, const bool &withAssets=false) const
Clone this resource for additional specialized use.
Definition: Resource.cpp:565
Namespace for the standard library.
bool isDiscarded() const
Accessor method to determine whether this Resource is to be discarded.
Definition: Resource.cpp:456
const PvlFlatMap & keys() const
Accessor method for the PVL keywords associated with this Resource.
Definition: Resource.cpp:215
int removeAsset(const QString &assetName)
Removes all of the assets in this Resource that are mapped to the given name.
Definition: Resource.cpp:494
SharedGisGeometry geometry() const
Accessor method for this Resource&#39;s GIS geometry.
Definition: Resource.cpp:420
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
bool m_discard
A flag to indicate whether the Resource is inactive.
Definition: Resource.h:177
QString name() const
Accessor for a string containing the Resource&#39;s name.
Definition: Resource.cpp:131
void append(const QString &keywordName, const QString &keywordValue)
Appends the given value to the PVL keyword with the given name.
Definition: Resource.cpp:344
bool hasValidGeometry() const
This method is used to determine whether a valid GIS geometry has been set for this Resource...
Definition: Resource.cpp:406
bool isEqual(const Resource &other) const
Checks for equality of another Resource.
Definition: Resource.cpp:159
Provides a flat map of PvlKeywords.
Definition: PvlFlatMap.h:236
QSharedPointer< GisGeometry > SharedGisGeometry
Definition for a SharedGisGeometry, a shared pointer to a GisGeometry.
Definition: GisGeometry.h:142
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:521
This class provides a resource of PVL keywords for Strategy classes.
Definition: Resource.h:71
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
void activate()
Activate a resource.
Definition: Resource.cpp:428
A single keyword-value pair.
Definition: PvlKeyword.h:98
virtual Resource * copy() const
Copy this resource for distinct management of its status.
Definition: Resource.cpp:543
int erase(const QString &keywordName)
Removes all of the PVL keywords in this Resource that are associated with the given name...
Definition: Resource.cpp:358
void addAsset(const QString &assetName, QVariant &assetValue)
Inserts an asset with the given name and value into this Resource&#39;s VariantList.
Definition: Resource.cpp:480
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:204
Shared Resource data pointer.
Definition: Resource.h:140
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:469
void discard()
Discard a resource.
Definition: Resource.cpp:446
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
bool hasGeometry() const
This method is used to determine whether the GIS geometry has been set for this Resource.
Definition: Resource.cpp:395
Resource()
Default constructor for a Resource object.
Definition: Resource.cpp:48
void add(const QString &keywordName, const QString &keywordValue)
Adds a PVL keyword with the given name and value to this Resource.
Definition: Resource.cpp:303
void setName(const QString &identity)
A mutator to set the Resource&#39;s name.
Definition: Resource.cpp:141
QExplicitlySharedDataPointer< ResourceData > m_data
Explicitly managed pointer to Resource data.
Definition: Resource.h:176
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
Encapsulation class provides support for GEOS-C API.
Definition: GisGeometry.h:67
bool exists(const QString &keywordName) const
Determines whether a PVL keyword with the given name is in this Resource.
Definition: Resource.cpp:172
int clearAssets()
Clears the assets from this Resource&#39;s VariantList.
Definition: Resource.cpp:504