Skip to content

Commit

Permalink
Add Conf error class
Browse files Browse the repository at this point in the history
  • Loading branch information
cfnptr committed Nov 3, 2024
1 parent 604d3f3 commit 145c142
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 43 deletions.
2 changes: 1 addition & 1 deletion libraries/mpio
49 changes: 49 additions & 0 deletions wrappers/cpp/conf/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 Conf error (exception) functions.
**********************************************************************************************************************/

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

namespace conf
{

using namespace std;

/**
* @brief Conf error (exception) class.
*/
class Error : public exception
{
string message;
size_t line = 0;
public:
/**
* @brief Creates a new Conf error (exception) instance.
* @param message target error message
*/
Error(const string& message, size_t line = 0) : message(message), line(line) { }

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

} // mpio
19 changes: 5 additions & 14 deletions wrappers/cpp/conf/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
**********************************************************************************************************************/

#pragma once
#include <string>
#include <exception>
#include "conf/error.hpp"
#include <filesystem>
#include <string_view>

Expand All @@ -32,8 +31,6 @@ extern "C"
namespace conf
{

using namespace std;

/***********************************************************************************************************************
* @brief Conf reader instance handle.
* @details See the @ref reader.h
Expand All @@ -47,35 +44,29 @@ class Reader final
* @brief Creates a new Conf file reader instance.
* @details See the @ref createFileConfReader().
* @param[in] filePath target Conf file path string
* @throw runtime_error with a @ref ConfResult string on failure.
* @throw Error with a @ref ConfResult string and line number on failure.
*/
Reader(const filesystem::path& filePath)
{
size_t errorLine = 0;
auto string = filePath.generic_string();
auto result = createFileConfReader(string.c_str(), &instance, &errorLine);
if (result != SUCCESS_CONF_RESULT)
{
throw runtime_error(confResultToString(result) + (" at line " +
to_string(errorLine) + ", path: " + filePath.generic_string()));
}
throw Error(confResultToString(result), errorLine);
}

/**
* @brief Creates a new Conf data reader instance.
* @details See the @ref createDataConfReader().
* @param[in] data target Conf data string
* @throw runtime_error with a @ref ConfResult string on failure.
* @throw Error with a @ref ConfResult string and line number on failure.
*/
Reader(const char* data)
{
size_t errorLine = 0;
auto result = createDataConfReader(data, &instance, &errorLine);
if (result != SUCCESS_CONF_RESULT)
{
throw runtime_error(confResultToString(result) +
(" at line " + to_string(errorLine)));
}
throw Error(confResultToString(result), errorLine);
}

/**
Expand Down
50 changes: 22 additions & 28 deletions wrappers/cpp/conf/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
**********************************************************************************************************************/

#pragma once
#include <string>
#include <exception>
#include "conf/error.hpp"
#include <filesystem>
#include <string_view>

Expand All @@ -32,8 +31,6 @@ extern "C"
namespace conf
{

using namespace std;

/***********************************************************************************************************************
* @brief Conf writer instance handle.
* @details See the @ref writer.h
Expand All @@ -47,17 +44,14 @@ class Writer final
* @brief Creates a new Conf file writer instance.
* @details See the @ref createFileConfWriter().
* @param[in] filePath target Conf file path string
* @throw runtime_error with a @ref ConfResult string on failure.
* @throw Error with a @ref ConfResult string on failure.
*/
Writer(const filesystem::path& filePath)
{
auto pathString = filePath.generic_string();
auto result = createFileConfWriter(pathString.c_str(), &instance);
if (result != SUCCESS_CONF_RESULT)
{
throw runtime_error(confResultToString(result) +
(", path:" + filePath.generic_string()));
}
throw Error(confResultToString(result));
}

/**
Expand All @@ -70,23 +64,23 @@ class Writer final
* @brief Writes a comment to the config.
* @details See the @ref writeConfComment().
* @param[in] comment target comment string
* @throw runtime_error on a failure.
* @throw Error on a comment write failure.
*/
void writeComment(const string& comment)
{
if (!writeConfComment(instance, comment.c_str()))
throw runtime_error("Failed to write a comment");
throw Error("Failed to write a comment");
}

/**
* @brief Writes a new line to the config. ('\\n')
* @details See the @ref writeConfNewLine().
* @throw runtime_error on a failure.
* @throw Error on a new line write failure.
*/
void writeNewLine()
{
if (!writeConfNewLine(instance))
throw runtime_error("Failed to write a new line");
throw Error("Failed to write a new line");
}

/**
Expand All @@ -96,12 +90,12 @@ class Writer final
* @param[in] key target item key string
* @param value integer item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, int64_t value)
{
if (!writeConfInt(instance, key.c_str(), value))
throw runtime_error("Failed to write a integer item");
throw Error("Failed to write a integer item");
}

/**
Expand All @@ -111,7 +105,7 @@ class Writer final
* @param[in] key target item key string
* @param value integer item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, int32_t value) { write(key, (int64_t)value); }

Expand All @@ -122,7 +116,7 @@ class Writer final
* @param[in] key target item key string
* @param value integer item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, uint32_t value) { write(key, (int64_t)value); }

Expand All @@ -133,7 +127,7 @@ class Writer final
* @param[in] key target item key string
* @param value integer item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, int16_t value) { write(key, (int64_t)value); }

Expand All @@ -144,7 +138,7 @@ class Writer final
* @param[in] key target item key string
* @param value integer item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, uint16_t value) { write(key, (int64_t)value); }

Expand All @@ -155,7 +149,7 @@ class Writer final
* @param[in] key target item key string
* @param value integer item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, int8_t value) { write(key, (int64_t)value); }

Expand All @@ -166,7 +160,7 @@ class Writer final
* @param[in] key target item key string
* @param value integer item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, uint8_t value) { write(key, (int64_t)value); }

Expand All @@ -178,12 +172,12 @@ class Writer final
* @param value floating item value
* @param precision number of digits after the decimal point, or 0 (auto detect).
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, double value, uint8_t precision = 0)
{
if (!writeConfFloat(instance, key.data(), value, precision))
throw runtime_error("Failed to write a floating item");
throw Error("Failed to write a floating item");
}

/**
Expand All @@ -194,7 +188,7 @@ class Writer final
* @param value floating item value
* @param precision number of digits after the decimal point, or 0 (auto detect).
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, float value, uint8_t precision = 0)
{
Expand All @@ -208,12 +202,12 @@ class Writer final
* @param[in] key target item key string
* @param value boolean item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, bool value)
{
if (!writeConfBool(instance, key.c_str(), value))
throw runtime_error("Failed to write a boolean item");
throw Error("Failed to write a boolean item");
}

/**
Expand All @@ -223,12 +217,12 @@ class Writer final
* @param[in] key target item key string
* @param[in] value string item value
*
* @throw runtime_error on a failure.
* @throw Error on a write failure.
*/
void write(const string& key, string_view value)
{
if (!writeConfString(instance, key.c_str(), value.data(), value.size()))
throw runtime_error("Failed to write a string item");
throw Error("Failed to write a string item");
}
};

Expand Down

0 comments on commit 145c142

Please sign in to comment.