Skip to content
Iceyer edited this page Jan 31, 2018 · 22 revisions

Centents

#File name:

filename.cpp file filename.h file

#Include:

#include "file.h"
#include <systemPath/file.h>

#Class header Class header files should have

#ifndef CLASSNAME_H
#define CLASSNAME_H

...
#endif // CLASSNAME_H

#Class:

class DActionServer
{


public:
    DActionServer();
    int run(int port, char* argv[]);
    int run(int port);
    int signalServer();
    void test();
    void initDatabase();
    void selectDatabase(QString className, QString functionName);
    QByteArray getLowlevelSymbol(QString className, QString functionName);

     //Class Member
    int m_epollListeningFd;
    int m_epollConnectionsFd;
    int m_listeningFd;
    int m_numberOfRequests;

};

#class single inheritance:

class DAbsTime : public DValue
{
public:
    DAbsTime();
    void now();
    DAbsTime(int y, int m, int d, int h, int min, int s);
    void setDate(int y, int m, int d);
    void setTime(int h, int m, int s);
    QString encodeToString();
    void encodeFromString(QString paramString);
    QString getClassName();

    int m_year;
    int m_month;
    int m_day;
    int m_hour;
    int m_minute;
    int m_second;
};

#class multiple inheritance:

class DColor : public DValue, public QColor
{
      ......

};

#Struct:

struct DThreadData{
    long m_threadId;
    DActionServer * m_ptr;

};

#For:

for(int i = 0; i < size; i++)
{
    doSomeThing();
}

#Value

int m_size;
int m_userID;
int m_inputType

#Lambda

std::cout << [](int i) -> int { return std::abs(i); } (-2016);

#Function

/**
 * @brief read callback function for libcurl
 *
 * @param pointer of max size (size*nmemb) to write data to
 * @param size size parameter
 * @param nmemb memblock parameter
 * @param userdata pointer to user data to read data from
 *
 * @return (size * nmemb)
 */
size_t RestClient::readCallback(void *data, size_t size, size_t nmemb,
                            void *userdata)
{
  /** get upload struct */
  RestClient::upload_object* u;
  u = reinterpret_cast<RestClient::upload_object*>(userdata);
  /** set correct sizes */
  size_t curl_size = size * nmemb;
  size_t copy_size = (u->length < curl_size) ? u->length : curl_size;
  /** copy data to buffer */
  memcpy(data, u->data, copy_size);
  /** decrement length and increment data pointer */
  u->length -= copy_size;
  u->data += copy_size;
  /** return copied size */
  return copy_size;
}

#Typedef

typedef void (*DActionPoint)(DAction * action, DValue * value);

#Name Space

namespace YourNameSpace {
inline namespace Action {
  void DoSomething();
}
}
using namespace YourNameSpace;

Format c/c++ code style

We use astyle to format c/c++ code. The astyle config is:

indent=spaces=4
style=otbs
indent-labels
pad-oper
unpad-paren
pad-header
keep-one-line-statements
convert-tabs
indent-preprocessor
align-pointer=name
keep-one-line-blocks
keep-one-line-statements

How to use astyle with qtcreator

  1. Install astyle with sudo apt install astyle
  2. Enable beautifier plugin in qtcreator
  3. Config beautifier and enable astyle, add the config above as en customized style
  4. Bind a shortcut if you want