Category Archives: Programming languages

A simple C++ serialization framework

I implemented a simple C++ binary serialization framework that makes a structure or class serializable by adding a macro that usually takes one line of code as shown in the example below:

struct A
{
    bool x;
    int y;

    AWL_SERIALIZABLE(x, y)
};

There is also a macro that makes a structure or a class equatable:

AWL_MEMBERWISE_EQUATABLE(A)
(more…)

Parsing GUID in C++ with a regular expression

The code below demonstrates how to parse GUID in C++ using a regular expression:

#include <iostream>
#include <string>
#include <regex>

int main()
{
    static const std::wregex regex(L"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");
    
    std::wstring sample = L"850fe1da-0ea6-c1a8-9810-0c1cece30698";

    std::match_results<std::wstring::const_iterator> match;

    if (std::regex_match(sample, match, regex))
    {
        std::wcout << L"matches" << std::endl;
    }
    else
    {
        std::wcout << L"does not match" << std::endl;
    }

    return 0;
}
(more…)

A simple example demonstrating how std::forward works in C++

For example, std::forward comes to play if I implement a template container with two ‘insert‘ function overloads that take the parameters of type const T & and T && respectively. I implement all the private insertion logic with T&&, but when I need to know the original value type passed to ‘insert‘ method I use std::forward as shown in the example below:

#include <iostream>
#include <string>

class Value
{
public:

    Value(const char * sz) : m_s(sz)
    {
    }

    Value(const Value & other) : m_s(other.m_s)
    {
        std::cout << "copy constructor: " << m_s << std::endl;
    }

    Value(Value && other) : m_s(std::move(other.m_s))
    {
        std::cout << "move constructor: " << m_s << std::endl;
    }

    const std::string & ToString() const
    {
        return m_s;
    }

private:

    std::string m_s;
};
(more…)

The string representation of a type is implementation defined in C++

The string representation of a type is implementation defined in C++, for example the following code produce the different output with MSVC, GCC and CLang:

#include <string>
#include <iostream>

struct A {};
class B {};

namespace ns
{
    struct X {};
}

int main()
{
    std::cout << typeid(A).name() << ", " << typeid(B).name() << ", " << typeid(ns::X).name() << ", " << typeid(std::string).name() << std::endl;
    return 0;
}
(more…)

Handling errors at compile time in C++

We had a discussion with colleagues on why in the following code we cannot simply use

static_assert(false)

but need to do a trick with ‘always_false’:

#include <type_traits>

template<typename>
struct always_false : std::false_type {};

template<typename Type>
constexpr int Get()
{
    if constexpr (std::is_same_v<Type, int>)
    {
        return 1;
    }
    else if constexpr (std::is_same_v<Type, bool>)
    {
        return 2;
    }
    else {
        static_assert(always_false<Type>::value);
    }
}
(more…)

Conversion of S75 charts to GeoJSON with GDAL org2org utility.

The original S57 map consists of the following layers:

ogrinfo E:\temp\ENC_ROOT\US4CA11M\US4CA11M.000
INFO: Open of `E:\temp\ENC_ROOT\US4CA11M\US4CA11M.000'
      using driver `S57' successful.
1: DSID (None)
2: Point (Point)
3: Line (Line String)
4: Area (Polygon)
5: Meta (None)

So I was able to convert each layer separately one by one without errors:

ogr2ogr -f GeoJSON US1HA02M-Area.json E:\temp\ENC_ROOT\US1HA02M\US1HA02M.000 Area -lco RFC7946=YES -skipfailures
ogr2ogr -f GeoJSON US1HA02M-Point.json E:\temp\ENC_ROOT\US1HA02M\US1HA02M.000 Point -lco RFC7946=YES -skipfailures
ogr2ogr -f GeoJSON US1HA02M-Line.json E:\temp\ENC_ROOT\US1HA02M\US1HA02M.000 Line -lco RFC7946=YES -skipfailures
ogr2ogr -f GeoJSON US1HA02M-DSID.json E:\temp\ENC_ROOT\US1HA02M\US1HA02M.000 DSID -lco RFC7946=YES -skipfailures
Warning 1: No SRS set on layer. Assuming it is long/lat on WGS84 ellipsoid
ogr2ogr -f GeoJSON US1HA02M-Meta.json E:\temp\ENC_ROOT\US1HA02M\US1HA02M.000 Meta -lco RFC7946=YES -skipfailures
Warning 1: No SRS set on layer. Assuming it is long/lat on WGS84 ellipsoid

(more…)

Setting up Vue.js development tools on Windows 10.

I installed Node.js and Vue.js cli with the following commands

npm install -g @vue/cli
npm install -g @vue/cli-init

and a sample application with

vue init webpack-simple my-vue-app
cd my-vue-app
npm install
npm run dev

(more…)

PHP script that saves client IP address to a file.

Below I wrote down a simple PHP script that saves client IP address to a file. If the IP address of your home machine periodically changes, you can store it on a web server once a minute by scheduling a task like this:

sudo crontab -u <user> -e
* * * * * wget -q -O /dev/null -o /dev/null "https://yourwebsite.com/ip.php?rig=rig1&password=XXXXX"

where ip.php is the following PHP script:
(more…)

How to run WIX bootstrapper application UI with elevated privileges?

WIX bootstrapper application (BA) can easily determine if it runs as admin with the following code:

using System;
using System.Diagnostics;
using System.Security.Principal;

static bool IsAdmin()
{
    WindowsIdentity id = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(id);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

and if it does not, run a new instance as admin and exit:

static void RunAsAdmin()
{
    ProcessStartInfo proc = new ProcessStartInfo
    {
        UseShellExecute = true,
        WorkingDirectory = Environment.CurrentDirectory,
        FileName = Process.GetCurrentProcess().MainModule.FileName,
        Verb = "runas"
    };

    Process.Start(proc);
}

(more…)

Exponential growth policy of std::vector

Once std::vector is filled (size() equals to capacity()), a subsequent push_back(…) results in an exponential expansion of the vector capacity. The following table shows that the expansion happens when the index reaches a power of two:

index: 0, capacity: 1, address: 0x1fa6c20
index: 1, capacity: 2, address: 0x1fa6c40
index: 2, capacity: 4, address: 0x1fa6c20
index: 4, capacity: 8, address: 0x1fa6c60
index: 8, capacity: 16, address: 0x1fa6c90
index: 16, capacity: 32, address: 0x1fa6ce0
index: 32, capacity: 64, address: 0x1fa6d70
index: 64, capacity: 128, address: 0x1fa6e80
index: 128, capacity: 256, address: 0x1fa7090
index: 256, capacity: 512, address: 0x1fa74a0
index: 512, capacity: 1024, address: 0x1fa7cb0
index: 1024, capacity: 2048, address: 0x1fa8cc0
index: 2048, capacity: 4096, address: 0x1faacd0
index: 4096, capacity: 8192, address: 0x1faece0
index: 8192, capacity: 16384, address: 0x1fb6cf0
index: 16384, capacity: 32768, address: 0x1fc6d00
index: 32768, capacity: 65536, address: 0x1fe6d10
index: 65536, capacity: 131072, address: 0x2026d20
index: 131072, capacity: 262144, address: 0x20a6d30
index: 262144, capacity: 524288, address: 0x21a6d40
index: 524288, capacity: 1048576, address: 0x23a6d50
index: 1048576, capacity: 2097152, address: 0x27a6d60
index: 2097152, capacity: 4194304, address: 0x2fa6d70
index: 4194304, capacity: 8388608, address: 0x7f8e9225f010
index: 8388608, capacity: 16777216, address: 0x7f8e8e25e010
index: 16777216, capacity: 33554432, address: 0x7f8e8625d010
index: 33554432, capacity: 67108864, address: 0x7f8e7625c010
index: 67108864, capacity: 134217728, address: 0x7f8e5625b010
index: 134217728, capacity: 268435456, address: 0x7f8e1625a010
index: 268435456, capacity: 536870912, address: 0x7f8d96259010
index: 536870912, capacity: 1073741824, address: 0x7f8c96258010

(more…)