Thursday, June 23, 2022

C++: consteval in C++17

 

template <auto V>
static constexpr auto force_consteval = V; 
#define STRINGHASH(str) force_consteval<stringhash(str)> 

C++ get declared type info

Useful for automating some compile-time initialization in combination with consteval or constexpr:

#include "string.h"
#include "stdlib.h"
#include <iostream>

template <class T>
constexpr std::string_view type_name()
{
    using namespace std;
#ifdef __clang__
    string_view p = __PRETTY_FUNCTION__;
    return string_view(p.data() + 34, p.size() - 34 - 1);
#elif defined(__GNUC__)
    string_view p = __PRETTY_FUNCTION__;
#  if __cplusplus < 201402
    return string_view(p.data() + 36, p.size() - 36 - 1);
#  else
    return string_view(p.data() + 49, p.find(';', 49) - 49);
#  endif
#elif defined(_MSC_VER)
    string_view p = __FUNCSIG__;
    return string_view(p.data() + 84, p.size() - 84 - 7);
#endif
}

static char x[] = "Test";

int main() {
    std::cout << type_name<decltype(x)>() << "\r\n";
    std::cout << type_name<std::decay<decltype(x)>::type>();
}


Wednesday, March 16, 2022

Scope mutex locking on FreeRTOS

Class:

/** 
  * @brief The FreeRtosScopedLock class, provides an RAII 
  *        style approach to locking a FreeRTOS mutex, 
  *        similar to C++11 std::scoped_lock<> */class FreeRtosScopedLock

{

public:

    explicit FreeRtosScopedLock(SemaphoreHandle_t mutex) :

            mMutex(mutex)

    {

        xSemaphoreTakeRecursive(mMutex, portMAX_DELAY);

        //for demo, assume success

    }


    ~FreeRtosScopedLock()

    {

        xSemaphoreGiveRecursive(mMutex);

        //for demo, assume success

    }


private:

    SemaphoreHandle_t mMutex;

};

 

Usage example: 

bool AccessMyDeviceNewStyle()

{

    ScopedLock lockItDown(mDevMutex);


    if (!SomeGuardCheck())

    {

        return false;

    }


    if (!AnotherGuardCheck())

    {

        return false;

    }


    //Do Stuff


    return true;

}

Godbolt compile explorer 

 

References:

https://covemountainsoftware.com/2019/11/26/why-i-prefer-c-raii-all-the-things/

https://blogs.sw.siemens.com/embedded-software/2017/03/27/more-on-c-with-an-rtos/ 

https://embeddedartistry.com/blog/2018/02/08/implementing-stdmutex-with-freertos/

Monday, May 23, 2016

Rescue files from dying hard drive

For rescuing files with "least effort" strategy (copy what can be read and don't try endlessly to recover what can't be read) ddrescue and find commands come to rescue. The command below tries to copy all the files from the current location to the destination; if copying fails due to any error it erases the destination file, so everything copied is actually good data (not damaged).

# Makes the directory structure copy
find . -type d -exec mkdir /dst/dir/{} \;

# Tries to copy the files with 1 retry  and exit on error (and delete dst file if exit on error)
find . -type f -exec sh -c "dd_rescue -X1 -e1 {} /dst/dir/{} || rm /dst/dir/{}" \;

Note: this solution only works if your file system is still readable.

Based on the answer from: http://superuser.com/questions/159354/linux-tool-to-copy-files-directories-from-failing-hard-disk