Customize C++ syntax

Customize C++ syntax

2012-06-22

​‌‍​‌​‌‍​‍

The C + + is a programming language resulting from C. It was created in 1995 by Bjarne Stroustrup, a PhD in computer science. The C + + language is known for its performance and ease of use. It manages both object-oriented programming and conventional structured programming.

The language inherits development tools from the C. I’ll talk about one of them, the preprocessor.

If you have already developed in C or C + + you’ve probably faced it. It’s the set of instructions preceded by a #. In your development you’ve probably encountered the #include “stdafx.h” is a preprocessor.

The preprocessor is, as its name suggests, a statement that will be done before compiling the program. The # include statement merge the files together before compiling them. There is another very useful command is #define, it allows you to define variables that will be replaced by their actual values at the end of the compilation. It also allows for conditions compilation for example compile an administrator and a user version using same source code.

In this code snippet the drawAdmin () function will only be called when the program is compiled with a ADMIN variable definition.

#define creates variables but also assign a value. For example, C + + does not support the elseif keyword available in php however, it is possible to define it anyway.

#define elseif else if

Similarly the NULL keyword does not exist in C + +, but it is not a problem.

#define NULL 0

You can go further by creating functions. For example, it can be bored to copy the same code somewhat long to write a loop, you can simplify the process this way.

#define REP(n) for(Int i=(n); --i>= 0 ;)

And so you speed up writing down your iterations.

I put at your disposal a list of the preprocessors that I use. Just choose yours.

//types
typedef float    flt;
typedef double   dbl;

typedef int8_t   I8;
typedef uint8_t  U8, Byte;
typedef int16_t  I, I16, Int;
typedef uint16_t U, U16, UInt;
typedef int32_t  I32;
typedef uint32_t U32;
typedef int64_t  I64;
typedef uint64_t U64;

//Shortcut
#define T      (*this) //Warning T is a reference not a pointer
#define elseif else if
#define NULL   0

#define REP(n)      for(U32 i=(n); --i>=0;)//Loop from i to 0
#define FREP(n)     for(U32 i=0; i<(n); i++)//Loop from 0 to i-1
#define REPD(i, n)  for(U32 i=(n); --i>=0;)//Loop from i à 0, allow you to change the name of the iteration variable
#define FREPD(i, n) for(U32 i=0; i<(n); i++)//Loop from 0 to i-1, allow you to change the name of the iteration variable

/*--------Exemple--------
  struct obj
  {
    int elm()
    {
        return T._elm;
    }
    void elm(int elm)
    {
        T._elm = elm;
    }
  private:
    I32 _elm;
  }

  void main()
  {
    //...
    FREP(nbUsers)
    {
        REPD(j, Users[i]->nbGroupes())
        {
            FREPD(k,Users[i]->Groupes[j]->nbChildren())
            {
                Users[i]->Groupes[j]->Children[k]->action();
            }
        }
    }
  }
  */