A great introduction to Rust

Rust is a modern program language designed for system programming. Just like C++ it aims to provide powerful abstractions at zero cost. In addition it also offers a type system that protects the programmer from hitting some very common concurrency bugs. This great talk explains how it works.

Advertisements
Posted in Uncategorized | Leave a comment

Running applications on iOS 10.3 devices using El Capitan

If you are using an old Mac like I do (Macbook Pro mid-2009), then you might also be stuck with macOS El Capitan and you can’t upgrade to macOS Sierra. Sure it feels sad to be left behind, but it still quite impressive that Apple supports 7 year old macs (https://support.apple.com/mac) with their latest operating system.

I recently tried to build and run a iOS game that I developed last year. Unfortunately I was unable to transfer it to my iPhone 7. Xcode complained that Xcode did not support the device files on my device which runs the latest version of iOS. Easy I thought, let’s update Xcode. It turns out that most recent versions of Xcode do not support El Capitan, the latest version of macOS I can install on my mac. Crap!

Would I really need to buy a new Mac just to compile and run my own game on my own device? Fortunately this is not the case. There is a hack that solves the issue! You just need to install the support files from the latest version of Xcode and drop them into the old version of Xcode

  1. Exit Xcode 8.2
  2. Download Xcode8.3.2.xip from Apple (https://developer.apple.com/services-account/download?path=/Developer_Tools/Xcode_8.3.2/Xcode8.3.2.xip)
  3. Unzip ~/Downloads/Xcode8.3.2.xip by double-clicking on it in Finder.
  4. Copy the folder 10.3 (14E269) from ~/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
    to
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
  5. Start Xcode ūüôā
Posted in Uncategorized | Leave a comment

Pongiliner is available on Google Play and App Store

My very simple game Pongiliner has finally been released on

Google Play

https://play.google.com/store/apps/details?id=com.rumberg.pongiliner&hl=en

and

App Store

https://appsto.re/se/ILVnfb.i

The game itself is very simple and I wrote it mainly to learn OpenGL ES 3.0 and some of the new features in C++14. I have not used Unity, Cocos2d-X or similar frameworks. This decision has delayed the development of the game, but I think that I have learned more during the journey than I would have otherwise.

By writing the game in C++ I could share about 99% of the code between Android and iOS.

Posted in Uncategorized | Leave a comment

Backup with attic

For the last couple of years my backup system of choice has been Bup (https://github.com/bup/bup). It is based on git and handles incremental backups and deduplication by writing small chunks of checksummed data as git packfiles. In the beginning it was very fast and worked really well. Unfortunately you can’t remove data from bup and that has lead to a continuously growing backup database. These days the repository is really slow and when my last backup took 13 hours to complete I decided to look for an alternative.

My first option was to simply start using Bup from a clean database. Unfortunately I quickly ran into problems while executing the first backup. At random instances during the backup Bup was suddently unable to send some packages from my laptop to my backup server. I tried to restart the backup multiple times, I even reinitialised the backup on the server side but nothing helped. I finally gave up.

Then I started to look for alternatives and I soon discovered Attic (https://attic-backup.org/index.html). It supports reduplication just like Bup, but it also offers encryption AND you can remove old data from a backup set. Moreover it is easy to create independent backup repositories which is good because it keeps each database smaller which allows for better performance.

If you want to try out Attic, you can use my backup script as a starting point.

#!/bin/bash

set -e

IP=<ENTER YOUR SERVER IP>
USER=<Your server user>

year=2016

function backup()
{
   dir=$1
   name=$2

   BASE_PATH=/home/${USER}/attic/${year}
   SERVERURL=${USER}@${IP}

   ATTICDIR_PATH=${BASE_PATH}/${name}.attic
   REMOTE_FULL_SSHPATH=${SERVERURL}:${ATTICDIR_PATH}
   ARCHIVE=${REMOTE_FULL_SSHPATH}::`hostname`-`date +%Y%m%d-%H%M

   # Create dir if does not exist
   ssh $SERVERURL "mkdir -p ${BASE_PATH}"

   if ssh $SERVERURL "[ -d ${ATTICDIR_PATH} ]";
   then
      echo "Backing up $dir to $name"
   else
      echo "Creating $name for $dir"
      attic init ${REMOTE_FULL_SSHPATH} -e passphrase
   fi

   echo attic create --stats ${REMOTE_FULL_SSHPATH} ${dir}
   attic create --stats ${ARCHIVE} ${dir}
}

echo "Enter password: "
read -s pass

export ATTIC_PASSPHRASE=${pass}
echo

backup ~/Pictures "photos"
backup ~/Dropbox "dropbox"
backup ~/Desktop "desktop"
backup ~/Documents "documents"
backup ~/Development "development"
backup ~/Music "music"
backup ~/ownCloud "owncloud"

Posted in Uncategorized | Leave a comment

Android local module based on custom target

Android has makefile macros for creating local modules  written in C, C++ and Java. A module declaration can look something like this.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mymodule
LOCAL_SRC_FILES := a.cpp b.cpp
LOCAL_LDLIBS := -lGLESv3 -llog
LOCAL_CXXFLAGS := -std=c++14
include $(BUILD_SHARED_LIBRARY)

The syntax is simple, but not very flexible.

Today I needed to create a custom target in an Android makefile. At first I thought that it would be as simple as creating a standard Makefile target, but in my particular situation I found that it had to be structured as a local module. I finally found a solution by invoking the BUILD_PHONY_PACKAGE module template.

custom_myfile_target: 
        echo Run custom code here
include $(CLEAR_VARS)
LOCAL_MODULE := mymodule
LOCAL_ADDITIONAL_DEPENDENCIES := custom_myfile_target
include $(BUILD_PHONY_PACKAGE)
custom_myfile_target is a regular GNU makefile target which can be extended as such.
Posted in Uncategorized | Leave a comment

ValWrapper – a named value wrapper in C++

I recently found myself writing a C++ class where the constructor needed three¬†arguments of type “int”. The arguments where not related so I could not pass¬†them as a color, point3d or something similar.

The class looked like this

struct AnimationConfig {
 public:
  AnimationConfig(int numFrames, int frameDelay, int numAnimations)
        : numFrames(numFrames),
          frameDelay(frameDelay),
          numAnimations(numAnimations) {
    }

    int numFrames;
    int frameDelay;
    int numAnimations;
};

and an instance could be created in this way

AnimationConfig aniConfig(5, 100, 3);

This code looks horrible. What is 5, 100 and 3? The code could be improved by adding constants or helper variables like this

const auto numFrames = 5;
const auto frameDelay = 100;
const auto numAnimations = 3;
AnimationConfig aniConfig(numFrames, frameDelay, numAnimations);

This is okay for a single instance, but what if we need to create several instances? This would require a lot of variables. Another option would be to create an uninitialized instance and assign each member implicitly. The is just as verbose as the previous alternative and there is a risk that the user forgets to initialize a certain member.

I thought for a while and then I came up with that I consider to be a much¬†better alternative – a ValWrapper class. The idea is that we create a new class that is named after the parameter that we wish to pass. Its constructor is marked as “explicit” so you need to cast the value you are passing (this is actually a feature) and you must initialize each such variable.

#pragma once
template <typename T> struct ValWrapper {
    explicit ValWrapper(T value) : mValue(value) {
    }
    operator T() const {
        return mValue;
    }
  private:
    T mValue;
};

#define DECL_VALWRAPPER(name, realtype)                 \
    class name : public ValWrapper<realtype> {          \
        using ValWrapper<realtype>::ValWrapper;         \
    };

How would AnimationConfig look if it used ValWrapper? It would look this way

struct AnimationConfig {
  public:
    DECL_VALWRAPPER(NumFrames, size_t);
    DECL_VALWRAPPER(FrameDelayMs, size_t)
    DECL_VALWRAPPER(NumAnimations, size_t);
    
    AnimationConfig(NumFrames numFrames, 
                    FrameDelayMs frameDelay,
                    NumAnimations numAnimations)
        : numFrames(numFrames),
          frameDelay(frameDelay),
          numAnimations(numAnimations) {
    }

    NumFrames numFrames;
    FrameDelayMs frameDelay;
    NumAnimations numAnimations;
};

Now it is no longer possible to construct an AnimationConfig as follows.

AnimationConfig aniConfig(5, 100, 3);

Instead you need to create it this way

Aniconfig aniconfig(AnimationConfig::NumFrames(5),
                    AnimationConfig::FrameDelayMs(100),
                    AnimationConfig::NumAnimations(3));

I consider this to be much more readable. Since ValWrapper is a template it can work with any type, not just integers.

 

Posted in Uncategorized | Leave a comment

Using multiple interfaces in C

In my previous post I described one way of implementing interfaces and classes in C.
Some of my readers may wonder why I added the baseptr member.

Why do

static int dog_getage(Animal super)
{
	Dog self = (Dog) super->baseptr;
	return self->age;
}

when it’s easier to do

static int dog_getage(Animal super)
{
	Dog self = (Dog) super;
	return self->age;
}

With only single interface inheritance as in the previous post then

	super == self == super->baseptr

and

	super->baseptr

is redundant. But if we wish to allow for multiple interfaces then
this raw form of upcasting does not work.

Let’s assume that we need to add a class “Lamp” which implements two interfaces
“Switch” and “Regulator”. The Switch interface has the methods

void set_enabled(bool)
bool get_enabled()

The Regulator interface also has two methods

void set_level(int)
int get_level();

where the argument ranges between 0 and 99.

This can be implemented in C by creating a structure

struct Lamp_s {
	struct Switch_s switch_base;
	struct Regulator_s regulator_base;

	/* Members go here */
	bool enabled;
	int level;
};

where Switch_s is defined as

struct Switch_s {
	struct SwitchVtbl_s* vtbl;
	void* baseptr;
};

and Regulator_s is defined as

struct Regulator_s {
	struct RegulatorVtbl_s* vtbl;
	void* baseptr;
};

The constructor of Lamp may look like

Lamp lamp_create()
{
	Lamp self = calloc(sizeof(struct Lamp_s), 1);
	if (self)
	{
		/* Initialize vtbl */
		self->switch_base->vtbl       = &switch_vtbl;
		self->switch_base->baseptr    = self;
		self->regulator_base->vtbl    = &regulator_vtbl;
		self->regulator_base->baseptr = self;

		self->enabled = DEFAULT_MODE;
		self->level = DEFAULT_LEVEL;

		return self;
	}

	return NULL;
}

where switch_vtbl and regulator_vtbl are member function tables
just like in the original Animal/Dog example. We then need to provide two accessors in Lamp for the Regulator and Switch interfaces.

Switch lamp_get_switch(Lamp self);

Regulator lamp_get_regulator(Lamp self);

The complete code for the Switch, Regulator, Lamp and an example of how they can be used (main.c) comes here

main.c

#include <stdio.h>

#include "lamp.h"

int main()
{
	Lamp l = lamp_create();

	Switch    s = lamp_get_switch(l);
	Regulator r = lamp_get_regulator(l);

	printf("State of switch: %d\n", switch_get_enabled(s));
	printf("Regulator level: %d\n", regulator_get_level(r));

	switch_set_enabled(s, true); // Turn on
	regulator_set_level(r, 40); // Adjust level to 40%

	printf("State of switch: %d\n", switch_get_enabled(s));
	printf("Regulator level: %d\n", regulator_get_level(r));

	switch_destroy(s); // Not required but harmless
	regulator_destroy(r); // Not required but harmless

	lamp_destroy(l);
}

lamp.c

	
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "lamp.h"

#define DEFAULT_MODE  false
#define DEFAULT_LEVEL 50

struct Lamp_s {
       struct Switch_s    switch_base;
       struct Regulator_s regulator_base;

       /* Members go here */
       bool enabled;
       int  level;
};

static void lamp_set_enabled(Switch super, bool enabled)
{
	Lamp self = (Lamp) super->baseptr;

	printf("%s: Setting enabled=%d\n", __func__, enabled);

	self->enabled = enabled;
}

static bool lamp_get_enabled(Switch super)
{
	Lamp self = (Lamp) super->baseptr;

	return self->enabled;
}

static void lamp_set_level(Regulator super, int level)
{
	Lamp self = (Lamp) super->baseptr;

	printf("%s: Setting level=%d\n", __func__, level);

	self->level = level;
}

static int lamp_get_level(Regulator super)
{
	Lamp self = (Lamp) super->baseptr;

	return self->level;
}

struct RegulatorVtbl_s regulator_vtbl = 
{
	.set_level = lamp_set_level,
	.get_level = lamp_get_level
};

struct SwitchVtbl_s switch_vtbl =
{
	.set_enabled = lamp_set_enabled,
	.get_enabled = lamp_get_enabled
};

Lamp lamp_create()
{
	Lamp self = calloc(sizeof(struct Lamp_s), 1);
	if (self)
	{
		/* Initialize vtbl */
		self->switch_base.vtbl	     = &switch_vtbl;
		self->switch_base.baseptr    = self;
		self->regulator_base.vtbl    = &regulator_vtbl;
		self->regulator_base.baseptr = self;

		self->enabled = DEFAULT_MODE;
		self->level   = DEFAULT_LEVEL;

		return self;
	}

	return NULL;
}

Switch lamp_get_switch(Lamp self)
{
	return &self->switch_base;
}

Regulator lamp_get_regulator(Lamp self)
{
	return &self->regulator_base;
}

void lamp_destroy(Lamp self)
{
	if (self)
	{
		free(self);
	}
}

lamp.h

#ifndef LAMP_H
#define LAMP_H

#include "regulator.h"
#include "switch.h"

typedef struct Lamp_s* Lamp;

Lamp lamp_create();

Switch lamp_get_switch(Lamp self);

Regulator lamp_get_regulator(Lamp self);

void lamp_destroy(Lamp self);

#endif /* LAMP_H */

regulator.c

#include <stdio.h>

#include "regulator.h"

void regulator_set_level(Regulator super, int level)
{
	super->vtbl->set_level(super, level);
}

int  regulator_get_level(Regulator super)
{
	return super->vtbl->get_level(super);
}

void regulator_destroy(Regulator super)
{
	/* Don't require a destructor */
	if (super->vtbl->destroy)
	{
		super->vtbl->destroy(super);
	}
	else
	{
		printf("%s: No destructor defined\n", __func__);
	}
}

regulator.h

#ifndef REGULATOR_H
#define REGULATOR_H

typedef struct Regulator_s* Regulator;

struct RegulatorVtbl_s
{
	void (*set_level)(Regulator super, int level);
	int  (*get_level)(Regulator super);
	void (*destroy)(Regulator super);
};

struct Regulator_s
{
	struct RegulatorVtbl_s* vtbl;
	void*		        baseptr;
};

void regulator_set_level(Regulator super, int level);

int regulator_get_level(Regulator super);

void regulator_destroy(Regulator super);

#endif /* REGULATOR_H */

switch.c

#include <stdio.h>

#include "switch.h"

void switch_set_enabled(Switch super, bool enable)
{
	super->vtbl->set_enabled(super, enable);
}

bool switch_get_enabled(Switch super)
{
	return super->vtbl->get_enabled(super);
}

void switch_destroy(Switch super)
{
	/* Don't require a destructor */
	if (super->vtbl->destroy)
	{
		super->vtbl->destroy(super);
	}
	else
	{
		printf("%s: No destructor defined\n", __func__);
	}
}

switch.h

#ifndef SWITCH_H
#define SWITCH_H

#include <stdbool.h>

typedef struct Switch_s* Switch;

struct SwitchVtbl_s
{
	void (*set_enabled)(Switch super, bool enable);
	bool (*get_enabled)(Switch super);
	void (*destroy)(Switch super);
};

struct Switch_s
{
	struct SwitchVtbl_s* vtbl;
	void*		baseptr;
};

void switch_set_enabled(Switch super, bool enable);

bool switch_get_enabled(Switch super);

void switch_destroy(Switch super);

#endif /* SWITCH_H */
Posted in Uncategorized | Tagged , , , , , , , | Leave a comment