Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Jul 7, 2024
1 parent 5fe0b1d commit 5250f90
Show file tree
Hide file tree
Showing 28 changed files with 401 additions and 483 deletions.
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ recursive-include src/clp/components/core/src/clp *.h
recursive-include src/clp/components/core/src/clp *.hpp
recursive-include src/clp/components/core/src/clp *.inc
recursive-include src/clp_ffi_py *.hpp
recursive-include src/clp_ffi_py *.inc
recursive-include clp_ffi_py *.py
recursive-include clp_ffi_py *.pyi
recursive-include clp_ffi_py py.typed
Expand Down
11 changes: 5 additions & 6 deletions src/clp_ffi_py/ExceptionFFI.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#ifndef CLP_FFI_PY_EXCEPTION_FFI
#define CLP_FFI_PY_EXCEPTION_FFI
#ifndef CLP_FFI_PY_IR_EXCEPTION_FFI
#define CLP_FFI_PY_IR_EXCEPTION_FFI

#include <string>

#include <clp/TraceableException.hpp>

namespace clp_ffi_py {
/**
* A class that represents a traceable exception during the native code
* execution. Note: for exceptions of CPython execution, please use CPython
* interface to set the exception instead.
* A class that represents a traceable exception during the native code execution. Note: for
* exceptions of CPython execution, please use CPython interface to set the exception instead.
*/
class ExceptionFFI : public clp::TraceableException {
public:
Expand All @@ -29,4 +28,4 @@ class ExceptionFFI : public clp::TraceableException {
};
} // namespace clp_ffi_py

#endif // CLP_FFI_PY_EXCEPTION_FFI
#endif // CLP_FFI_PY_IR_EXCEPTION_FFI
50 changes: 24 additions & 26 deletions src/clp_ffi_py/PyObjectCast.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#ifndef CLP_FFI_PY_PY_OBJECT_CAST_HPP
#define CLP_FFI_PY_PY_OBJECT_CAST_HPP
#ifndef CLP_FFI_PY_IR_PY_OBJECT_CAST_HPP
#define CLP_FFI_PY_IR_PY_OBJECT_CAST_HPP

#include <clp_ffi_py/Python.hpp> // Must always be included before any other header files

#include <type_traits>

namespace clp_ffi_py {

/**
* Casts a given function pointer to a PyCFunction.
* The main purpose of this function is to silence clang-tidy checks on using
* `reinterpret_cast`. It does not perform any further function type checking.
* The main purpose of this function is to silence clang-tidy checks on using `reinterpret_cast`. It
* does not perform any further function type checking.
* @tparam Src The source function pointer type.
* @param src The source function pointer.
* @return PyCFunction using reinterpret_cast.
Expand All @@ -23,9 +22,9 @@ auto py_c_function_cast(Src src) noexcept -> PyCFunction {
}

/**
* Casts a given function pointer to a `getbufferproc` CPython function.
* The main purpose of this function is to silence clang-tidy checks on using
* `reinterpret_cast`. It does not perform any further function type checking.
* Casts a given function pointer to a `getbufferproc` CPython function. The main purpose of this
* function is to silence clang-tidy checks on using `reinterpret_cast`. It does not perform any
* further function type checking.
* @tparam Src The source function pointer type.
* @param src The source function pointer.
* @return `getbufferproc` using reinterpret_cast.
Expand All @@ -38,9 +37,9 @@ auto py_getbufferproc_cast(Src src) noexcept -> getbufferproc {
}

/**
* Casts a given function pointer to a `releasebufferproc` CPython function.
* The main purpose of this function is to silence clang-tidy checks on using
* `reinterpret_cast`. It does not perform any further function type checking.
* Casts a given function pointer to a `releasebufferproc` CPython function. The main purpose of
* this function is to silence clang-tidy checks on using `reinterpret_cast`. It does not perform
* any further function type checking.
* @tparam Src The source function pointer type.
* @param src The source function pointer.
* @return `releasebufferproc` using reinterpret_cast.
Expand All @@ -53,8 +52,8 @@ auto py_releasebufferproc_cast(Src src) noexcept -> releasebufferproc {
}

/**
* This template struct is used as a compile-time flag that indicates whether
* type T is a PyObject or not. By default, `cValue` is set to false.
* This template struct is used as a compile-time flag that indicates whether type T is a PyObject
* or not. By default, `cValue` is set to false.
* @tparam T
*/
template <typename T>
Expand All @@ -63,18 +62,16 @@ struct is_python_object {
};

/**
* This template const expression is a wrapper of underlying `cValue` stored in
* `is_python_object`, which is used to determine whether a type T is a valid
* Python object type.
* This template const expression is a wrapper of underlying `cValue` stored in `is_python_object`,
* which is used to determine whether a type T is a valid Python object type.
* @tparam T
*/
template <typename T> // NOLINTNEXTLINE(readability-identifier-naming)
constexpr bool is_python_object_v{is_python_object<T>::cValue};

/**
* The macro to create a specialization of is_python_object for a given type T.
* Only types that are marked with this macro will be considered as a valid
* Python object type.
* The macro to create a specialization of is_python_object for a given type T. Only types that are
* marked with this macro will be considered as a valid Python object type.
*/
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define CLP_FFI_PY_MARK_AS_PYOBJECT(T) \
Expand All @@ -85,12 +82,12 @@ constexpr bool is_python_object_v{is_python_object<T>::cValue};

/**
* Casts a given Src pointer to a Dst pointer. It should behave as followed:
* 1. If Src and Dst are the same type, return `src` if their type is a valid
* Python object type, or they are both PyObject.
* 2. If Dst is PyObject, the cast is valid if Src is a PyObject. A pointer,
* Python*, should be returned.
* 3. If Src is PyObject, the cast is valid if Dst is a valid Python object
* type. A pointer, Dst*, should be returned.
* 1. If Src and Dst are the same type, return `src` if their type is a valid Python object type, or
* they are both PyObject.
* 2. If Dst is PyObject, the cast is valid if Src is a PyObject. A pointer, Python*, should be
* returned.
* 3. If Src is PyObject, the cast is valid if Dst is a valid Python object type. A pointer, Dst*,
* should be returned.
* 4. Any other cases are considered as invalid cast.
* @tparam Dst The destination type. Must be given explicitly.
* @tparam Src The source type. Can be given implicitly.
Expand Down Expand Up @@ -129,4 +126,5 @@ CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyMetadata);
CLP_FFI_PY_MARK_AS_PYOBJECT(ir::native::PyQuery);
CLP_FFI_PY_MARK_AS_PYOBJECT(PyTypeObject);
} // namespace clp_ffi_py
#endif

#endif // CLP_FFI_PY_IR_PY_OBJECT_CAST_HPP
32 changes: 15 additions & 17 deletions src/clp_ffi_py/PyObjectUtils.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef CLP_FFI_PY_PY_OBJECT_UTILS_HPP
#define CLP_FFI_PY_PY_OBJECT_UTILS_HPP
#ifndef CLP_FFI_PY_IR_PY_OBJECT_UTILS_HPP
#define CLP_FFI_PY_IR_PY_OBJECT_UTILS_HPP

#include <clp_ffi_py/Python.hpp> // Must always be included before any other header files

#include <memory>

namespace clp_ffi_py {
/**
* A specialized deleter for PyObjectPtr which decrements the pointed PyObject
* reference count when it is destroyed.
* A specialized deleter for PyObjectPtr which decrements the pointed PyObject reference count when
* it is destroyed.
* @tparam PyObjectType
*/
template <typename PyObjectType>
Expand All @@ -19,8 +19,7 @@ class PyObjectDeleter {
};

/**
* An empty deleter that has an empty implementation to ensure object trivial
* destruction.
* An empty deleter that has an empty implementation to ensure object trivial destruction.
* @tparam PyObjectType
*/
template <typename PyObjectType>
Expand All @@ -30,25 +29,24 @@ class PyObjectTrivialDeleter {
};

/**
* A type of smart pointer that maintains a reference to a Python object for the
* duration of its lifetime.
* A type of smart pointer that maintains a reference to a Python object for the duration of its
* lifetime.
* @tparam PyObjectType
*/
template <typename PyObjectType>
using PyObjectPtr = std::unique_ptr<PyObjectType, PyObjectDeleter<PyObjectType>>;

/**
* A smart pointer to be used for raw Python object pointers that have static
* storage duration. It holds a reference of the underlying Python object.
* Compared to PyObjectPtr, when destructed, this smart pointer does not
* decrease the reference count of the contained Python object. Since this
* pointer has static storage duration, it's possible that the Python
* interpreter exits before this pointer's destructor is called; attempting to
* decrement the reference count (maintained by the interpreter) in this
* situation would lead to undefined behaviour.
* A smart pointer to be used for raw Python object pointers that have static storage duration. It
* holds a reference of the underlying Python object. Compared to PyObjectPtr, when destructed, this
* smart pointer does not decrease the reference count of the contained Python object. Since this
* pointer has static storage duration, it's possible that the Python interpreter exits before this
* pointer's destructor is called; attempting to decrement the reference count (maintained by the
* interpreter) in this situation would lead to undefined behaviour.
* @tparam PyObjectType
*/
template <typename PyObjectType>
using PyObjectStaticPtr = std::unique_ptr<PyObjectType, PyObjectTrivialDeleter<PyObjectType>>;
} // namespace clp_ffi_py
#endif // CLP_FFI_PY_PY_OBJECT_PTR_HPP

#endif // CLP_FFI_PY_IR_PY_OBJECT_UTILS_HPP
19 changes: 9 additions & 10 deletions src/clp_ffi_py/Py_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef CLP_FFI_PY_PY_UTILS_HPP
#define CLP_FFI_PY_PY_UTILS_HPP
#ifndef CLP_FFI_PY_IR_PY_UTILS_HPP
#define CLP_FFI_PY_IR_PY_UTILS_HPP

#include <clp_ffi_py/Python.hpp> // Must always be included before any other header files

#include <clp/ffi/encoding_methods.hpp>

namespace clp_ffi_py {
/**
* Initializes CPython interface to Python level utility functions implemented
* in submodule `clp_ffi_py.utils`.
* Initializes CPython interface to Python level utility functions implemented in submodule
* `clp_ffi_py.utils`.
* @return true on success.
* @return false on failure with the relevant Python exception and error set.
*/
Expand All @@ -18,20 +18,19 @@ auto py_utils_init() -> bool;
* CPython wrapper of clp_ffi_py.utils.get_formatted_timestamp.
* @param timestamp
* @param tzinfo Python tzinfo object that specifies timezone information.
* @return a new reference of a PyObject string that stores the formatted
* timestamp.
* @return a new reference of a PyObject string that stores the formatted timestamp.
* @return nullptr on failure with the relevant Python exception and error set.
*/
auto py_utils_get_formatted_timestamp(clp::ir::epoch_time_ms_t timestamp, PyObject* timezone)
-> PyObject*;

/**
* CPython wrapper of clp_ffi_py.utils.get_timezone_from_timezone_id
* CPython wrapper of clp_ffi_py.utils.get_timezone_from_timezone_id.
* @param timezone_id
* @return a new reference of a Python tzinfo object that matches the input
* timezone id.
* @return a new reference of a Python tzinfo object that matches the input timezone id.
* @return nullptr on failure with the relevant Python exception and error set.
*/
auto py_utils_get_timezone_from_timezone_id(std::string const& timezone_id) -> PyObject*;
} // namespace clp_ffi_py
#endif // CLP_FFI_PY_PY_UTILS_HPP

#endif // CLP_FFI_PY_IR_PY_UTILS_HPP
6 changes: 3 additions & 3 deletions src/clp_ffi_py/error_messages.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CLP_FFI_PY_ERROR_MESSAGES
#define CLP_FFI_PY_ERROR_MESSAGES
#ifndef CLP_FFI_PY_IR_ERROR_MESSAGES
#define CLP_FFI_PY_IR_ERROR_MESSAGES

namespace clp_ffi_py {
constexpr char const* const cOutofMemoryError = "Failed to allocate memory.";
Expand All @@ -11,4 +11,4 @@ constexpr char const* const cTimezoneObjectNotInitialzed
= "Timezone (tzinfo) object is not yet initialized.";
} // namespace clp_ffi_py

#endif // CLP_FFI_PY_ERROR_MESSAGES
#endif // CLP_FFI_PY_IR_ERROR_MESSAGES
13 changes: 6 additions & 7 deletions src/clp_ffi_py/ir/native/LogEvent.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#ifndef CLP_FFI_PY_LOG_EVENT_HPP
#define CLP_FFI_PY_LOG_EVENT_HPP
#ifndef CLP_FFI_PY_IR_NATIVE_LOG_EVENT_HPP
#define CLP_FFI_PY_IR_NATIVE_LOG_EVENT_HPP

#include <optional>

#include <clp/ffi/encoding_methods.hpp>

namespace clp_ffi_py::ir::native {
/**
* A class that represents a decoded IR log event. Contains ways to access (get
* or set) the log message, the timestamp, and the log event index.
* A class that represents a decoded IR log event. Contains ways to access (get or set) the log
* message, the timestamp, and the log event index.
*/
class LogEvent {
public:
LogEvent() = delete;

/**
* Constructs a new log event and leaves the formatted timestamp empty by
* default.
* Constructs a new log event and leaves the formatted timestamp empty by default.
* @param log_message
* @param timestamp
* @param index
Expand Down Expand Up @@ -75,4 +74,4 @@ class LogEvent {
};
} // namespace clp_ffi_py::ir::native

#endif // CLP_FFI_PY_LOG_EVENT_HPP
#endif // CLP_FFI_PY_IR_NATIVE_LOG_EVENT_HPP
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace clp_ffi_py::ir::native {
namespace {
/**
* Validates whether the JSON object contains the given key and has a string
* data associated to this particular key.
* Validates whether the JSON object contains the given key and has a string data associated to this
* particular key.
* @param json_data JSON object to be validated.
* @param key The key to access the data field.
* @return true if the data is valid.
Expand Down
33 changes: 16 additions & 17 deletions src/clp_ffi_py/ir/native/Metadata.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CLP_FFI_PY_METADATA_HPP
#define CLP_FFI_PY_METADATA_HPP
#ifndef CLP_FFI_PY_IR_NATIVE_METADATA_HPP
#define CLP_FFI_PY_IR_NATIVE_METADATA_HPP

#include <utility>

Expand All @@ -8,30 +8,28 @@

namespace clp_ffi_py::ir::native {
/**
* A class that represents a decoded IR preamble. Contains ways to access (get)
* metadata such as the timestamp format. After construction, the metadata is
* readonly. */
* A class that represents a decoded IR preamble. Contains ways to access (get) metadata such as the
* timestamp format. After construction, the metadata is readonly.
*/
class Metadata {
public:
/**
* Constructs a new Metadata object by reading values from a JSON object
* decoded from the preamble. This constructor will validate the JSON data
* and throw exceptions when failing to extract required values.
* Constructs a new Metadata object by reading values from a JSON object decoded from the
* preamble. This constructor will validate the JSON data and throw exceptions when failing to
* extract required values.
* @param metadata JSON data that contains the metadata.
* @param is_four_byte_encoding
*/
explicit Metadata(nlohmann::json const& metadata, bool is_four_byte_encoding);

/**
* Constructs a new Metadata object from the provided fields. Currently,
* `m_is_four_byte_encoding` is set to true by default since it is the only
* format supported.
* @param ref_timestamp The reference timestamp used to calculate the
* timestamp of the first log message in the IR stream.
* @param timestamp_format Timestamp format to use when generating the logs
* with a reader.
* @param timezone Timezone in TZID format to use when generating the
* timestamp from Unix epoch time.
* `m_is_four_byte_encoding` is set to true by default since it is the only format supported.
* @param ref_timestamp The reference timestamp used to calculate the timestamp of the first log
* message in the IR stream.
* @param timestamp_format Timestamp format to use when generating the logs with a reader.
* @param timezone Timezone in TZID format to use when generating the timestamp from Unix epoch
* time.
*/
explicit Metadata(
clp::ir::epoch_time_ms_t ref_timestamp,
Expand Down Expand Up @@ -64,4 +62,5 @@ class Metadata {
std::string m_timezone_id;
};
} // namespace clp_ffi_py::ir::native
#endif // CLP_FFI_PY_METADATA_HPP

#endif // CLP_FFI_PY_IR_NATIVE_METADATA_HPP
Loading

0 comments on commit 5250f90

Please sign in to comment.