Nut/doc/sharedpointer.md

60 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

Nut can compile in *shared pointer* mode or *regular* mode
2020-03-17 02:39:36 +08:00
In *shared pointer* mode results of queries is QList<QSharedPointer<T>> and in *regular* mode results are QList<T*>
Almost in every case shared pointer mode is better, But nut support regular mode for backward comptability.
2020-08-12 22:32:32 +08:00
By default _Nut_ compiles in shared pointer mode, to switch to ols raw pointer mode you must define **NUT_RAW_POINTER** macro
Nut has template alias
```cpp
2020-08-12 22:32:32 +08:00
#ifdef NUT_RAW_POINTER
2020-03-17 02:40:08 +08:00
template <typename T>
2020-08-12 22:32:32 +08:00
using RowList = QList<T*>;
template <typename T>
2020-08-12 22:32:32 +08:00
using Row = T*;
#else
template <typename T>
2020-08-12 22:32:32 +08:00
using RowList = QList<QSharedPointer<T>>;
template <typename T>
2020-08-12 22:32:32 +08:00
using Row = QSharedPointer<T>;
#endif
```
In other words these types are defined by this table:
| Mode | Nut::Row | Nut::RowList |
|------ |----- |--------- |
|Regular|T* | QList\<T\*\> |
|Shared pointer|QSharedPointer\<T\> | QList\<QSharedPointer\<T\>\> |
For the integration of your source, you can use these aliases.
Ans also Nut::create<T>() method are defined for two mode
```cpp
2020-08-12 22:32:32 +08:00
#ifdef NUT_RAW_POINTER
template<class T>
2020-08-12 22:32:32 +08:00
inline Row<T> create() {
return new T;
}
#else
template<class T>
2020-08-12 22:32:32 +08:00
inline Row<T> create(QObject *parent) {
return QSharedPointer<T>(new T(parent));
}
#endif
```
So you can use the Nut::create function without considering in what way the library is being compiled. Example:
```cpp
auto post = Nut::create<Post>();
```
2020-08-12 22:32:32 +08:00
In above example if *NUT_RAW_POINTER* is defined *post* is *Post\** else is *QSharedPointer<Post>*
2020-08-12 22:32:32 +08:00
I recommand use shared pointer mode (default) always!