Category Archives: Programming languages

Sending XRP with JavaScript

const read = require('read').read;
const send = require('./send');

async function asyncMain()
{
  const amount = await read({
    prompt: "Amount: "
  });

  const password = await read({
    prompt: "Password: ",
    silent: true,
    replace: "*" //optional, will print out an asterisk for every typed character 
  });

  // console.log("Amount: " + amount);
  // console.log("Your password: " + password);
(more…)

Make QML menu width fit the content

Found an implementation here and added two pixels:

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Menu {
    width: {
        var result = 0;
        var padding = 0;
        for (var i = 0; i < count; ++i) {
            var item = itemAt(i);
            result = Math.max(item.contentItem.implicitWidth, result);
            padding = Math.max(item.padding, padding);
        }
        // It looks like two pixels are missing to remove the ellipsis.
        // My first idea was that it is leftInset + rightInset, but it does not work.
        var missing = 2;
        return result + padding * 2 + missing;
    }
}
(more…)

Move assignment operator in C++

Implicitly-declared move assignment operator

If no user-defined move assignment operators are provided for a class type, and all of the following is true:

(more…)

Examples of C++/Objective-C interop

From QT’s Purchasing:

//SKProductsRequestDelegate
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray<SKProduct *> *products = response.products;
    SKProduct *product = [products count] == 1 ? [[products firstObject] retain] : nil;

    if (product == nil) {
        //Invalid product ID
        NSString *invalidId = [response.invalidProductIdentifiers firstObject];
        QMetaObject::invokeMethod(backend, "registerQueryFailure", Qt::AutoConnection, Q_ARG(QString, QString::fromNSString(invalidId)));
    } else {
        //Valid product query
        //Create a IosInAppPurchaseProduct
        IosInAppPurchaseProduct *validProduct = new IosInAppPurchaseProduct(product, backend->productTypeForProductId(QString::fromNSString([product productIdentifier])));
        if (validProduct->thread() != backend->thread()) {
            validProduct->moveToThread(backend->thread());
            QMetaObject::invokeMethod(backend, "setParentToBackend", Qt::AutoConnection, Q_ARG(QObject*, validProduct));
        }
        QMetaObject::invokeMethod(backend, "registerProduct", Qt::AutoConnection, Q_ARG(IosInAppPurchaseProduct*, validProduct));
    }

    [request release];
}
(more…)

Copy a file with CMake

Copy Podfile at the build stage:

message("Copy ${CMAKE_CURRENT_SOURCE_DIR}/Podfile -> ${CMAKE_BINARY_DIR}")
add_custom_target(copy_podfile
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${CMAKE_CURRENT_SOURCE_DIR}/Podfile"
        "${CMAKE_BINARY_DIR}"
    COMMENT "Copying Podfile"
)
add_dependencies(${PROJECT_NAME} copy_podfile)

Copy Podfile at the configuration stage:

execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
    "${CMAKE_CURRENT_SOURCE_DIR}/Podfile"
    "${CMAKE_BINARY_DIR}"
)

Using Swift in a QT6 app

I simply opened arboreus_examples/qt6/CMake/UsingSwift/UsingSwift_v3/CMakeLists.txt in QT Creator and was able to build it and run.

In its main.cpp it creates a Swift wrapper and calls method:

ASwift* oSwift = new ASwift(&oEngine);
oSwift->mInit();
_A_DEBUG << oSwift->mString();
(more…)

Version tolerant serialization in C++

Last time I have been working on a C++ binary serialization framework that allows to serialize simple data structures with a few lines of code. First, you add AWL_REFLECT macro to all your structures as follows:

#include "Awl/Reflection.h"
#include <string>
#include <vector>
#include <set>

struct A
{
    int a;
    bool b;
    std::string c;
    double d;

    AWL_REFLECT(a, b, c, d)
};
(more…)

What is the type of a string literal in C++?

The code below compiles and the asserts do not fail:

#include <iostream>

int main()
{
    auto a = "str";
    const auto& ar = "str";
    const char* b = "str";
    auto const &c = a;
    auto* d = &a;
    const auto e = 2;

    static_assert(std::is_same_v<const char(&)[4], decltype("str")>);
    static_assert(std::is_same_v<const char*, decltype(a)>);
    static_assert(std::is_same_v<const char(&)[4], decltype(ar)>);
    static_assert(std::is_same_v<const char* const&, decltype(c)>);
    static_assert(std::is_same_v<const char**, decltype(d)>);
    static_assert(std::is_same_v<const int, decltype(e)>);

    std::cout << "a: " << typeid(a).name() << std::endl;
    std::cout << "ar: " << typeid(ar).name() << std::endl;
    std::cout << "b: " << typeid(b).name() << std::endl;
    
    return 0;
}

And a and b have identical typeids.

An example of overloading operator << in C++

The code below is compiled successfully with both GCC and MSVC:

#include <iostream>
#include <sstream>

template <class C>
class basic_format
{
public:
    
    template <typename T>
    basic_format & operator << (const T & val)
    {
        out << val;
        return *this;
    }

    std::basic_string<C> str() const { return out.str(); }

    operator std::basic_string<C>() const { return str(); }

private:
    
    std::basic_ostringstream<C> out;
};
(more…)

“Cannot anchor to a null item” waring in QML

The following QML code

Callout
{
    id: call

    //margins should bound but not assigned
    anchors.topMargin: root.above ? undefined : yMargin
    anchors.bottomMargin: root.above ? yMargin : undefined

    ...
}

produced “Cannot anchor to a null item” warning and worked incorrectly.

I tried to replace it with AnchorChanges and PropertyChanges:

(more…)