<commit_before>= C/C++ tips
:hp-tags: c, c++, tips

==== What's here ?

Here you will find some tips n tricks around C++.


==== Ternary

[source,cpp]
----
bool test = true;

(test) ? std::cout << "yes" : std::cout << "no"; // will print yes

----

==== Get int* len on linux and windows

[source,cpp]
----
#include <stdio.h>
#include <malloc.h>

#ifdef linux
#define array_size(x) malloc_usable_size(x)
#endif

#ifdef _WIN32
#define array_size(x) _msize(x)
#endif

int main(int argc, char**argv)
{
    int         len;
    int         *array = malloc(sizeof(int)*10);

    len = array_size(array);
    len = len / 4; 				// because array_size return size_t

    printf("len : %d\n", len);
    return(0);
}
----

==== Two ways to iterate

[source,cpp]
----
std::vector<double> latWayPoints;
// fill vector

for (std::vector<double>::iterator it = latWaypoints.begin(); it != latWaypoints.end(); ++it)
    qDebug() << ">> latWay : " << *it;

for (double lon: lonWaypoints)
    qDebug() << ">> latWay : " << lon;
----

==== cpp method callable from qml
[source,cpp]
----

// .h
Q_INVOKABLE void my_function();


//.cpp
void my_class::my_function() 
{
  // do stuff
}

//.qml
onMyEvent: {
    item.my_function();
}
----

==== cast

dynamic_cast pour les classes héritées surtout, 
static_cast pour les types de base,
reinterpret_cast pour forcer un cast de n'importe quoi vers n'importe quoi
et const_cast pour pouvoir passer des string en entrée à des vieilles fonctions C qui veulent des char*

==== Concat arrays C style

[source,cpp]
----
void CryptoStream::concatArrays(unsigned char * &src, int dstOffset, unsigned char * dst)
{
    int j = 0;
    int i = 0;
    int srcLen =  strlen(reinterpret_cast<char *>(src));
    int dstLen =  strlen(reinterpret_cast<char *>(dst));
    unsigned char * tmp = new unsigned char [srcLen + dstLen + 1];


    for (i = 0; i < srcLen; ++i)
    {
       tmp[i] = src[i];
    }

    i = dstOffset;

    while (j < dstLen && i < (srcLen + dstLen + 1))
    {
        tmp[i] = dst[j];
        i++;
        j++;
    }

    delete [] src;
    src = tmp;
}<commit_msg>Update 2016-10-30-CC-tips.adoc<commit_after>= C/C++ tips
:hp-tags: c, c++, tips

==== What's here ?

Here you will find some tips n tricks around C++.


==== Ternary

[source,cpp]
----
bool test = true;

(test) ? std::cout << "yes" : std::cout << "no"; // will print yes

----

==== Get int* len on linux and windows

[source,cpp]
----
#include <stdio.h>
#include <malloc.h>

#ifdef linux
#define array_size(x) malloc_usable_size(x)
#endif

#ifdef _WIN32
#define array_size(x) _msize(x)
#endif

int main(int argc, char**argv)
{
    int         len;
    int         *array = malloc(sizeof(int)*10);

    len = array_size(array);
    len = len / 4; 				// because array_size return size_t

    printf("len : %d\n", len);
    return(0);
}
----

==== Two ways to iterate

[source,cpp]
----
std::vector<double> latWayPoints;
// fill vector

for (std::vector<double>::iterator it = latWaypoints.begin(); it != latWaypoints.end(); ++it)
    qDebug() << ">> latWay : " << *it;

for (double lon: lonWaypoints)
    qDebug() << ">> latWay : " << lon;
----

==== cpp method callable from qml
[source,cpp]
----

// .h
Q_INVOKABLE void my_function();


//.cpp
void my_class::my_function() 
{
  // do stuff
}

//.qml
onMyEvent: {
    item.my_function();
}
----

==== cast

dynamic_cast pour les classes héritées surtout, 
static_cast pour les types de base,
reinterpret_cast pour forcer un cast de n'importe quoi vers n'importe quoi
et const_cast pour pouvoir passer des string en entrée à des vieilles fonctions C qui veulent des char*

==== Concat arrays C style

[source,cpp]
----
void concatArrays(unsigned char * &src, int dstOffset, unsigned char * dst)
{
    int j = 0;
    int i = 0;
    int srcLen =  strlen(reinterpret_cast<char *>(src));
    int dstLen =  strlen(reinterpret_cast<char *>(dst));
    unsigned char * tmp = new unsigned char [srcLen + dstLen + 1];


    for (i = 0; i < srcLen; ++i)
    {
       tmp[i] = src[i];
    }

    i = dstOffset;

    while (j < dstLen && i < (srcLen + dstLen + 1))
    {
        tmp[i] = dst[j];
        i++;
        j++;
    }

    delete [] src;
    src = tmp;
}
{'fraction_non_alphanumeric': 0.16932603000714455, 'fraction_numerical': 0.00714455822814956, 'mean_word_length': 2.8147138964577656, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1529920', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1520, 'n_words': 453}