swift
processinfo.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_MISC_PROCESSINFO_H
7 #define SWIFT_MISC_PROCESSINFO_H
8 
9 #include <QCoreApplication>
10 #include <QString>
11 
12 #include "misc/valueobject.h"
13 
15 
16 namespace swift::misc
17 {
21  class SWIFT_MISC_EXPORT CProcessInfo : public CValueObject<CProcessInfo>
22  {
23  public:
26 
28  explicit CProcessInfo(qint64 pid) : m_pid(pid), m_name(processNameFromId(pid)) {}
29 
31  CProcessInfo(qint64 pid, const QString &name) : m_pid(pid), m_name(name) {}
32 
34  static CProcessInfo currentProcess() { return CProcessInfo(QCoreApplication::applicationPid()); }
35 
37  bool exists() const { return !m_name.isEmpty() && *this == CProcessInfo(m_pid); }
38 
40  qint64 processId() const { return m_pid; }
41 
43  bool isNull() const { return m_pid == 0 && m_name.isEmpty(); }
44 
46  const QString &processName() const { return m_name; }
47 
49  QString convertToQString(bool i18n = false) const;
50 
51  private:
52  static QString processNameFromId(qint64 pid);
53 
54  qint64 m_pid = 0;
55  QString m_name;
56 
59  SWIFT_METAMEMBER(pid),
60  SWIFT_METAMEMBER(name));
61  };
62 } // namespace swift::misc
63 
64 Q_DECLARE_METATYPE(swift::misc::CProcessInfo)
65 
66 #endif // SWIFT_MISC_PROCESSINFO_H
Value class identifying a process, with a PID and a name.
Definition: processinfo.h:22
CProcessInfo(qint64 pid)
Construct an object identifying the process with the given PID.
Definition: processinfo.h:28
bool exists() const
True if this object identifies a process that exists.
Definition: processinfo.h:37
bool isNull() const
Empty process info.
Definition: processinfo.h:43
CProcessInfo(qint64 pid, const QString &name)
Construct an object identifying a process with the given PID and name.
Definition: processinfo.h:31
CProcessInfo()
Construct an object identifying an invalid process.
Definition: processinfo.h:25
qint64 processId() const
Get the pid.
Definition: processinfo.h:40
static CProcessInfo currentProcess()
Return an object identifying the current process.
Definition: processinfo.h:34
const QString & processName() const
Get the process name.
Definition: processinfo.h:46
Mix of the most commonly used mixin classes.
Definition: valueobject.h:114
#define SWIFT_METAMEMBER(MEMBER,...)
Macro to define an element within a metaclass.
Definition: metaclass.h:73
#define SWIFT_METACLASS(CLASS,...)
Macro to define a nested metaclass that describes the attributes of its enclosing class.
Definition: metaclass.h:53
Free functions in swift::misc.
#define SWIFT_MISC_EXPORT
Export a class or function from the library.
#define SWIFT_DECLARE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template declaration of mixins for a CValueObject subclass to be placed near the top of the ...
Definition: valueobject.h:65