Built-in Data Types Basic Data Types¶
This article introduces the basic data types.
IException¶
ICore supports the C++ exception mechanism. The IException class declaration is as follows:
Notes¶
Please use exceptions cautiously during usage. Except in the following two scenarios, it is not recommended to use the exception mechanism.
- When the call chain is deep with potential cyclic calls, but the call entry point is clear, it is recommended to use the exception mechanism. This allows capturing exceptions at the call entry point and throwing exceptions at any node in the call chain. For example, during HTML template parsing.
- When external environments change, such as during database query operations. Under normal circumstances, this is not an issue. However, if problems arise, exceptions are more effective in addressing them.
Other scenarios do not recommend using exceptions. For more details on safety, please refer to the Security Policy documentation.
IRdbException¶
If users need to use IException, they should not use it directly. Instead, they should create subclasses and use the more semantically clear subclass type. Currently, the project supports the IRdbException subclass because database operations are unreliable and indeterminate.
Its usage is as follows:
In C++, thrown exceptions must be caught; otherwise, the entire program will crash. As shown above, IRdbException exceptions are caught during the HTTP request processing. If an exception is caught, a 500 status is returned to the user.
IJson¶
In ICore, we use nlohmann/json as our JSON tool. This is due to its advantages of high performance, ease of use, and comprehensive functionality. In ICore, we rename nlohmann/json to IJson. The code for IJson.h is as follows:
In the first line of code, we rename nlohmann/json to IJson. In the second line of code, we register IJson as a Qt meta-object type, enabling reflection usage of IJson.
Secondly, the naming convention of IJson aligns better with the naming conventions of ICore, making the code more natural and fluent.
In ICore programs, it is no longer recommended for users to use the nlohmann/json type to operate JSON data. Instead, they should use the shorter IJson type.
IJsonUtil¶
In ICore, to address the need for conversion between IJson and other data types, the IJsonUtil utility set is defined. It primarily includes two types of functions: fromJson and toJson.
fromJson¶
The declaration of IJsonUtil::fromJson is as follows:
From the above code, it can be seen that the basic signature of fromJson is:
The type T can be:
- Basic types: bool, QString, std::string, IString, IJson, QDate, QTime, QDateTime, QStringList.
- Numeric types, including integer and floating-point types.
- Bean types. For details on this type, refer to the Bean documentation.
- Sequence containers: std::list, std::vector, QList, QVector.
- Associative containers: std::map, QMap.
When the IJson conversion is successful, the return value is true. When it fails, the return value is false.
toJson¶
The basic signature of toJson is:
The type T can be:
- Basic types: bool, QString, std::string, IString, IJson, QDate, QTime, QDateTime, QStringList.
- Numeric types, including integer and floating-point types.
- Bean types. For details on this type, refer to the Bean documentation.
- Sequence containers: std::list, std::vector, QList, QVector.
- Associative containers: std::map, QMap.
toJson does not have scenarios where conversion fails.
IResult¶
IResult is a concise wrapper around std::optional.
The purpose is to standardize the writing style.