diff --git a/libs/client/CMakeLists.txt b/libs/client/CMakeLists.txt index e640dfc375bff99f7a45aefc4e29e2f33efccc3b..701f42bcdbd63cca61d47e2345ee36cdc1150bf3 100644 --- a/libs/client/CMakeLists.txt +++ b/libs/client/CMakeLists.txt @@ -10,6 +10,7 @@ set (CLIENT_LIB_SRC_FILES MockClientAPI.cpp SecurityIdentity.cpp StorageClass.cpp + StorageClassAndUsageCount.cpp UserIdentity.cpp) add_library (ctaclient SHARED diff --git a/libs/client/StorageClassAndUsageCount.cpp b/libs/client/StorageClassAndUsageCount.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d26df6319bf3ee520e05b3b1cf709d461398b152 --- /dev/null +++ b/libs/client/StorageClassAndUsageCount.cpp @@ -0,0 +1,61 @@ +#define __STDC_LIMIT_MACROS + +#include "StorageClassAndUsageCount.hpp" + +//------------------------------------------------------------------------------ +// constructor +//------------------------------------------------------------------------------ +cta::StorageClassAndUsageCount::StorageClassAndUsageCount(): + m_usageCount(0) { +} + +//------------------------------------------------------------------------------ +// constructor +//------------------------------------------------------------------------------ +cta::StorageClassAndUsageCount::StorageClassAndUsageCount( + const StorageClass &storageClass): + m_storageClass(storageClass), + m_usageCount(0) { +} + +//------------------------------------------------------------------------------ +// getStorageClass +//------------------------------------------------------------------------------ +const cta::StorageClass &cta::StorageClassAndUsageCount::getStorageClass() + const throw() { + return m_storageClass; +} + +//------------------------------------------------------------------------------ +// getUsageCount +//------------------------------------------------------------------------------ +uint64_t cta::StorageClassAndUsageCount::getUsageCount() const throw() { + return m_usageCount; +} + +//------------------------------------------------------------------------------ +// incUsageCount +//------------------------------------------------------------------------------ +void cta::StorageClassAndUsageCount::incUsageCount() { + if(UINT64_MAX == m_usageCount) { + std::ostringstream message; + message << "Cannot increment usage count of storage class " << + m_storageClass.name << " because its maximum value of UINT64_MAX has " + "already been reached"; + throw Exception(message.str()); + } + m_usageCount++; +} + +//------------------------------------------------------------------------------ +// decUsageCount +//------------------------------------------------------------------------------ +void cta::StorageClassAndUsageCount::decUsageCount() { + if(0 == m_usageCount) { + std::ostringstream message; + message << "Cannot decrement usage count of storage class " << + m_storageClass.name << " because it is already at zero"; + throw Exception(message.str()); + } + m_usageCount--; +} diff --git a/libs/client/StorageClassAndUsageCount.hpp b/libs/client/StorageClassAndUsageCount.hpp new file mode 100644 index 0000000000000000000000000000000000000000..91b3ea05d3e0c9939e686db66210cd81eb4bb9c3 --- /dev/null +++ b/libs/client/StorageClassAndUsageCount.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include "Exception.hpp" +#include "StorageClass.hpp" + +#include <sstream> + +namespace cta { + +/** + * Class used to store a storage class and it usage count. + */ +class StorageClassAndUsageCount { +public: + + /** + * Constructor. + * + * Initialisez the usage count to 0. + */ + StorageClassAndUsageCount(); + + /** + * Constructor. + * + * Initialisez the usage count to 0. + */ + StorageClassAndUsageCount(const StorageClass &storageClass); + + /** + * Returns the storage class. + */ + const StorageClass &getStorageClass() const throw(); + + /** + * Returns the usage count. + */ + uint64_t getUsageCount() const throw(); + + /** + * Increments the usage count by 1. + */ + void incUsageCount(); + + /** + * Decrements the usage count by 1. + */ + void decUsageCount(); + +private: + + /** + * The storage class. + */ + StorageClass m_storageClass; + + /** + * The usage count. + */ + uint64_t m_usageCount; + +}; // class StorageClassAndUsageCount + +} // namespace cta