/
home
/
Programming
/
Style
/
Classes are central to developing everything as an API; a style that maximizes readability.
- #ifndef QOAUTH2_H
- #define QOAUTH2_H
- #include <QObject>
- class QOAuth2 : public QObject
- {
- Q_OBJECT
- public:
- explicit QOAuth2( QObject *parent = nullptr );
- ~QOAuth2();
- QString getToken( const QString &urlForToken
- = "https://www.googleapis.com/oauth2/v4/token" );
- /* Oauth2 */
- QString access_token ();
- QString client_id ();
- QString client_secret ();
- QString code ();
- QString error ();
- QString error_description();
- QString error_uri ();
- QString expires_in ();
- QString grant_type ();
- QString password ();
- QString redirect_uri ();
- QString refresh_token ();
- QString response_type ();
- QString scope ();
- QString state ();
- QString token_type ();
- QString username ();
- void setAccess_token ( const QString &access_token );
- void setClient_id ( const QString &client_id );
- void setClient_secret ( const QString &client_secret );
- void setCode ( const QString &code );
- void setError ( const QString &error );
- void setError_description ( const QString &error_description );
- void setError_uri ( const QString &error_uri );
- void setExpires_in ( const QString &expires_in );
- void setGrant_type ( const QString &grant_type );
- void setPassword ( const QString &password );
- void setRedirect_uri ( const QString &redirect_uri );
- void setRefresh_token ( const QString &refresh_token );
- void setResponse_type ( const QString &response_type );
- void setScope ( const QString &scope );
- void setState ( const QString &state );
- void setToken_type ( const QString &token_type );
- void setUsername ( const QString &username );
- signals:
- void access_tokenChanged ();
- void client_idChanged ();
- void client_secretChanged ();
- void codeChanged ();
- void errorChanged ();
- void error_descriptionChanged();
- void error_uriChanged ();
- void expires_inChanged ();
- void grant_typeChanged ();
- void passwordChanged ();
- void redirect_uriChanged ();
- void refresh_tokenChanged ();
- void response_typeChanged ();
- void scopeChanged ();
- void stateChanged ();
- void token_typeChanged ();
- void usernameChanged ();
- private:
- QString m_Access_token ;
- QString m_Client_id ;
- QString m_Client_secret ;
- QString m_Code ;
- QString m_Error ;
- QString m_Error_description;
- QString m_Error_uri ;
- QString m_Expires_in ;
- QString m_Grant_type ;
- QString m_Password ;
- QString m_Redirect_uri ;
- QString m_Refresh_token ;
- QString m_Response_type ;
- QString m_Scope ;
- QString m_State ;
- QString m_Token_type ;
- QString m_Username ;
- };
- #endif // QOAUTH2_H
- /* A class designed as an API */
- class Sneed : public QObject
- {
- Q_OBJECT
- public:
- int feed();
- signals:
- void feedChanged();
- private:
- int m_Feed;
- };
- /* Interfacing with that class's API */
- int main()
- {
- Sneed sneed;
- QObject::connect( &sneed, &Sneed::feedChanged, [&sneed](){
- // whenever m_Feed changes
- // this code will execute:
- qDebug() << sneed.feed();
- }
- }
- /* Bad */
- class Sneed
- {
- public:
- Feed m_Feed;
- Seed *m_Seed;
- static const inline Chuck s_Chuck{1};
- };
- /* Good */
- class Sneed
- {
- public:
- Feed feed() const;
- Seed *seed() const;
- void setFeed( const Feed &feed );
- void setSeed( Seed *seed );
- // Acceptable
- static const inline Chuck s_Chuck{1};
- private:
- Feed m_Feed;
- Seed *m_Seed{nullptr};
- };
- /* Bad */
- class Sneed
- {
- private:
- void clear();
- void reset();
- void update();
- void initiate();
- };
- /* Good */
- class Sneed
- {
- protected:
- void clear();
- void reset();
- void update();
- private:
- void initiate();
- };
- /* Bad */
- int fancy;
- class Sneed
- {
- public:
- Sneed() {
- Q_ASSERT( m_query.contains( QRegularExpression("(!i)(feed|seed)") ) );
- QTextStream(stdout) << 9001;
- }
- private:
- QDir m_seed;
- QFile *pHobo; // p is hungarian notation for pointer
- QString feed;
- QProcess m_Rsync;
- static int m_Chuck;
- QDatabase store;
- QString m_Query;
- };
- /* Good */
- int g_Fancy;
- class Sneed
- {
- public:
- Sneed()
- {
- Q_ASSERT( m_query.contains( re_FeedOrSeed ) );
- QTextStream(stdout) << mn_PowerLevel;
- }
- private:
- QDir d_Seed ; // d is for Directory
- QFile *f_Hobo ; // f is for File
- QString m_Feed ; // m is for Member
- QProcess p_rsync; // p is for Process
- static int s_Chuck; // s is for Static
- QDatabase db_Store; // db is for Database
- QString j_Query; // j is an informal custom prefix for Javascript
- QString m_Query; // Prefer m if it's context appears once
- static inline const int mn_PowerLevel{9001};
- static inline const QRegularExpression re_FeedOrSeed{"(!i)(feed|seed)"};
- };
- /* Bad */
- Sneed::Sneed()
- {
- QDir dir("/proc/");
- QFile file1( dir.absoluteFilePath("feed.txt") );
- QFile file2( dir.absoluteFilePath("seed.txt") );
- int m_Number = 911; // m_ here is heresy
- for ( int i = 0; i < --m_Number; i++ ) {
- QString str = QString::number(i);
- QProcess ps;
- ps.start( "ps", QStringList() << str );
- ps.waitForFinished();
- if ( file1.open( QIODevice::ReadOnly )
- && file2.open( QIODevice::WriteOnly ) ) {
- file2.write( file1.readAll() + ps.readAll() );
- }
- file1.close();
- file2.close();
- }
- qDebug() << m_Number;
- }
- /* Good */
- Sneed::Sneed()
- {
- QDir d("/proc/");
- QFile f_Feed( d.absoluteFilePath("feed.txt") );
- QFile f_Seed( d.absoluteFilePath("seed.txt") );
- int n = 911;
- for ( int i = 0; i < --n; i++ ) {
- QString s = QString::number(i);
- QProcess p;
- p.start( "ps", QStringList() << s );
- p.waitForFinished();
- if ( f_Feed.open( QIODevice::ReadOnly )
- && f_Seed.open( QIODevice::WriteOnly ) ) {
- f_Seed.write( f_Feed.readAll() + p.readAll() );
- }
- f_Feed.close();
- f_Seed.close();
- }
- qDebug() << n;
- }
- /* Good */
- class Sneed : public QObject
- {
- Q_OBJECT
- signals:
- void feedChanged();
- void finished( bool success );
- protected slots:
- void onFeedchanged();
- void onFinished( bool success );
- };
- /* Good */
- class Sneed : public QObject
- {
- Q_OBJECT
- public:
- void setFeed( QString s );
- void setSeed( QString s );
- signals:
- void feedChanged();
- void seedChanged();
- void started ();
- void paused ();
- void finished();
- void opened ();
- void closed ();
- void ready ();
- void errorOccured();
- };
- /* Good */
- class Sneed
- {
- public:
- QString output();
- private:
- struct Container
- {
- QTime time ;
- QString output;
- QThread *thread;
- }
- QList<Container> m_ContainerList;
- QTime m_Time;
- }
- /* Bad */
- Feed const *m_Feed;
- /* Good */
- const Feed *m_Feed;
- /* Bad */
- class Convenience : public QObject
- {
- Q_OBJECT
- public:
- enum Store {
- none = 0x0,
- sneed = 0x1,
- feed = 0x2,
- seed = 0x4,
- chuck = 0x8,
- hobo = 0x10,
- };
- Q_ENUM(Store)
- static const inline QMetaEnum s_MetaStore{ QMetaEnum::fromType<Store>() };
- Convenience( Store store )
- {
- switch(store) {
- case none: qDebug() << "none"; break;
- case sneed: qDebug() << "sneed"; break;
- case feed: qDebug() << "feed"; break;
- case seed: qDebug() << "seed"; break;
- case chuck: qDebug() << "chuck"; break;
- case hobo: qDebug() << "hobo"; break;
- default: return;
- }
- }
- };
- /* Good */
- class Convenience : public QObject
- {
- Q_OBJECT
- public:
- enum Store
- {
- None = 0,
- Sneed = 1 << 0 ,
- Feed = 1 << 1 ,
- Seed = 1 << 2 ,
- Chuck = 1 << 3 ,
- Hobo = 1 << 4 ,
- }; Q_ENUM(Store)
- static const inline QMetaEnum s_MetaStore{ QMetaEnum::fromType<Store>() };
- Convenience( Store store )
- {
- qDebug() << s_MetaStore.valueToKey(store);
- }
- };
- class Sneed
- {
- /* Bad */
- static bool s_Hobo;
- /* Good */
- static const bool s_Seed{true};
- static inline bool s_Feed{true};
- };
- if ( this->trySneed(60) ) { ... } // wait one minute
- if ( this->trySneed(-1) ) { ... } // wait forever
- if ( this->trySneed( 0) ) { ... } // Do not wait at all
- class Sneed
- {
- /* Bad */
- unsigned char m_uchar;
- signed char m_char;
- short m_16 ;
- long long int m_64 ;
- unsigned char m_u8 ;
- unsigned short m_u16;
- unsigned int m_u32;
- unsigned long m_u64;
- float m_float ;
- double m_double;
- /* fine */
- int m_int;
- /* Good */
- uchar m_uchar;
- qint8 m_char ;
- qint8 m_8 ;
- qint16 m_16 ;
- qint32 m_32 ;
- qint64 m_64 ;
- quint8 m_u8 ;
- quint16 m_u16 ;
- quint32 m_u32 ;
- quint64 m_u64 ;
- qreal m_real;
- };
- /* Good */
- template<typename T_Enum, typename T_Key, typename T_Value>
- class QDatabase
- {
- public:
- explicit QDatabase( const QString &applicationName, const SQL_Metadata options = SQL_Metadata(0) )
- : m_ApplicationName( applicationName )
- , d_Database( QDir::home().absoluteFilePath( ".local/share/qdatabase/" + applicationName ) )
- , m_FileName( d_Database.absoluteFilePath(
- this->demangle( typeid(T_Key ).name() )
- + QString(".")
- + this->demangle( typeid(T_Value).name() )
- + mn_Mime ) )
- , m_Metadata( options )
- {
- Q_ASSERT( QSqlDatabase::isDriverAvailable( mn_Driver ) );
- Q_ASSERT( s_MetaEnum.keyCount() != 0 );
- this->init();
- for ( int i = 0; i < s_MetaEnum.keyCount(); i++ ) {
- m_ColumnNames.append( s_MetaEnum.key(i) );
- }
- }
- /* [ etc... ] */
- }
- template<typename T>
- class Sneed
- {
- /* Bad */
- typedef QList<T> seed;
- typedef void (*feed)();
- /* Fine */
- typedef unsigned long long quint64;
- using quint64 = unsigned long long;
- /* Good */
- using seed = QList<T>;
- using feed = void (*)();
- template <typename T_Feed, typename T_Seed> using Chuck = QPair<T_Feed,T_Seed>;
- };