Skip to content

Commit

Permalink
Add MPIO error class
Browse files Browse the repository at this point in the history
  • Loading branch information
cfnptr committed Nov 3, 2024
1 parent f1d8d7b commit 39e5075
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
16 changes: 8 additions & 8 deletions wrappers/cpp/mpio/directory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
**********************************************************************************************************************/

#pragma once
#include "mpio/error.hpp"
#include <filesystem>

extern "C"
{
#include "mpio/directory.h"
}

#include <filesystem>

namespace mpio
{

Expand All @@ -45,13 +45,13 @@ class Directory
*
* @param isShared is data directory shared between multiple users
* @return Data directory path on success.
* @throw runtime_error if failed to get data directory.
* @throw Error if failed to get data directory.
*/
static filesystem::path getDataPath(bool isShared = false)
{
auto dataPath = getDataDirectory(isShared);
if (!dataPath)
throw runtime_error("Failed to get data directory.");
throw Error("Failed to get data directory.");
auto path = filesystem::path(dataPath);
free(dataPath);
return path;
Expand All @@ -64,13 +64,13 @@ class Directory
* @param appName target application name string
* @param isShared is data directory shared between multiple users
* @return Data directory path on success.
* @throw runtime_error if failed to get app data directory.
* @throw Error if failed to get app data directory.
*/
static filesystem::path getAppDataPath(const string& appName, bool isShared = false)
{
auto dataPath = getAppDataDirectory(appName.c_str(), isShared);
if (!dataPath)
throw runtime_error("Failed to get app data directory.");
throw Error("Failed to get app data directory.");
auto path = filesystem::path(dataPath);
free(dataPath);
return path;
Expand All @@ -81,13 +81,13 @@ class Directory
* @details See the @ref getResourcesDirectory().
*
* @return Resources directory path on success.
* @throw runtime_error if failed to get resources directory.
* @throw Error if failed to get resources directory.
*/
static filesystem::path getResourcesPath()
{
auto dataPath = getResourcesDirectory();
if (!dataPath)
throw runtime_error("Failed to get resources directory.");
throw Error("Failed to get resources directory.");
auto path = filesystem::path(dataPath);
free(dataPath);
return path;
Expand Down
48 changes: 48 additions & 0 deletions wrappers/cpp/mpio/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021-2024 Nikita Fediuchin. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/***********************************************************************************************************************
* @file
* @brief Common MPIO error (exception) functions.
**********************************************************************************************************************/

#pragma once
#include <string>
#include <stdexcept>

namespace mpio
{

using namespace std;

/**
* @brief MPIO error (exception) class.
*/
class Error : public exception
{
string message;
public:
/**
* @brief Creates a new MPIO error (exception). instance
* @param message target error message
*/
Error(const string& message) : message(message) { }

/**
* @brief Returns MPIO error message C-string.
*/
const char* what() const noexcept override { return message.c_str(); }
};

} // mpio
26 changes: 13 additions & 13 deletions wrappers/cpp/mpio/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
**********************************************************************************************************************/

#pragma once
#include <string>
#include "mpio/error.hpp"

extern "C"
{
Expand Down Expand Up @@ -50,78 +50,78 @@ class OS
/**
* @brief Returns system logical CPU count. (MT-Safe)
* @details See the @ref getLogicalCpuCount().
* @throw runtime_error if failed to get logical CPU count.
* @throw Error if failed to get logical CPU count.
*/
static int getLogicalCpuCount()
{
auto cpuCount = ::getLogicalCpuCount();
if (cpuCount <= 0)
throw runtime_error("Failed to get logical CPU count.");
throw Error("Failed to get logical CPU count.");
return cpuCount;
}

/**
* @brief Returns system physical CPU count. (MT-Safe)
* @details See the @ref getPhysicalCpuCount().
* @throw runtime_error if failed to get physical CPU count.
* @throw Error if failed to get physical CPU count.
*/
static int getPhysicalCpuCount()
{
auto cpuCount = ::getPhysicalCpuCount();
if (cpuCount <= 0)
throw runtime_error("Failed to get physical CPU count.");
throw Error("Failed to get physical CPU count.");
return cpuCount;
}

/**
* @brief Returns system performance CPU count. (MT-Safe)
* @details See the @ref getPerformanceCpuCount().
* @throw runtime_error if failed to get performance CPU count.
* @throw Error if failed to get performance CPU count.
*/
static int getPerformanceCpuCount()
{
auto cpuCount = ::getPerformanceCpuCount();
if (cpuCount <= 0)
throw runtime_error("Failed to get performance CPU count.");
throw Error("Failed to get performance CPU count.");
return cpuCount;
}

/**
* @brief Returns system total physical RAM size. (MT-Safe)
* @details See the @ref getTotalRamSize().
* @throw runtime_error if failed to get total RAM size.
* @throw Error if failed to get total RAM size.
*/
static int64_t getTotalRamSize()
{
auto ramSize = ::getTotalRamSize();
if (ramSize <= 0)
throw runtime_error("Failed to get total RAM size.");
throw Error("Failed to get total RAM size.");
return ramSize;
}

/**
* @brief Returns system free physical RAM size. (MT-Safe)
* @details See the @ref getFreeRamSize().
* @throw runtime_error if failed to get free RAM size.
* @throw Error if failed to get free RAM size.
*/
static int64_t getFreeRamSize()
{
auto ramSize = ::getFreeRamSize();
if (ramSize <= 0)
throw runtime_error("Failed to get free RAM size.");
throw Error("Failed to get free RAM size.");
return ramSize;
}

/**
* @brief Returns system CPU name string. (MT-Safe)
* @details See the @ref getCpuName().
* @throw runtime_error if failed to get CPU name.
* @throw Error if failed to get CPU name.
*/
static string getCpuName()
{
auto cpuName = ::getCpuName();
if (!cpuName)
throw runtime_error("Failed to get CPU name.");
throw Error("Failed to get CPU name.");
auto cpuString = string(cpuName);
free(cpuName);
return cpuString;
Expand Down

0 comments on commit 39e5075

Please sign in to comment.