ぷろみん

プログラミング的な内容を扱ってます

シングルトンを使うのはやめよう

概要

この記事はC++の作者Bjarne Stroustrup等によって書かれたコーディングガイドラインAvoid singletonsの箇所の翻訳です。

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Ri-singleton

シングルトンを使うのはやめよう

理由

シングルトンは基本的に変装したグローバルオブジェクトに過ぎないため。

class Singleton {
    // シングルトンオブジェクトが確実に1つだけ作られて
    // プロパティの初期化が行われるようにする
};

シングルトンという発想には多くの亜種がある。 それが問題の1つである。

メモ

あなたがグローバルオブジェクトを変更したくない場合、constまたはconstexprを宣言する。

例外

最初の使用での初期化を行うための純粋なシングルトン(設計を考えなくて良いような)の場合は使用できる。

X& myX()
{
    static X my_x {3};
    return my_x;
}

これは初期化順を制御する最も効果的な解決策の1つだ。 マルチスレッド環境において静的オブジェクトはレースコンディションを引き起こさない。(うっかりそのコンストラクタ内から共有オブジェクトにアクセスしない限り)

もし、あなたが他の大勢のようにシングルトンをオブジェクトを1つだけ作成するためのクラスと定義するのなら、myXのような関数はシングルトンではない。そして、この便利なテクニックはシングルトンを無くすルールの例外ではない。

実施

一般化することは非常に難しい。

  • singletonを含む名前を持つクラスを探す。
  • 作成された単一オブジェクトを探す。(オブジェクトを数えたりコンストラクタを調査する)
  • クラスXの持つpublic static関数が内部に静的なクラスXを持ち、それのポインタや参照を返している場合、それを禁止する。

DirectX12をビルド可能なプロジェクトを作る

前回

ようやくWindows10にしたのでDX12プログラミングを始めてみた - ぷろみん

ビルド

New Project -> Win32 Project -> Empty Project

新しく生成されたフォルダに前回ビルドに成功したサンプルの.cppと.hをコピーしてきます。

Solution Explorerのファイルが並んでいるアイコン(Show All File)をクリックし、先ほどコピーしたファイルを出現させます。
そして、全選択してから右クリックInclude In Projectします。

1からソースを書く場合は省略できますが、d3dx12.hSDKに含まれていないので注意が必要です。

How to: Use the Windows 10 SDK in a Windows Desktop Application

を参考にRetarget SDK Versionすると前回設定した項目が入力可能になります。

プロジェクトのプロパティを開き利用したいバージョンを指定しましょう。
そして、プロジェクトのプロパティの右上にあるConfiguration ManagerからActive solution platformの箇所をx64に変更します。

最後にLinker -> Input -> Additional Dependenciesにd3d12.libdxgi.libを加えるとビルドできます。

ようやくWindows10にしたのでDX12プログラミングを始めてみた

概要

Windows10にしたらまず最初にすべき事といえば・・・
そう、DirectX12プログラミングですよね。
今回はその導入をやってみたのでメモしておきます。

Visual Studioのアップグレード

始めから入ってない人もここで入れるので一緒です。

Direct3D12 開発環境構築 -プレビュー版- :: ☆PROJECT ASURA☆

ここの手順に従ってインストーラーをDLして実行し、アップグレードが終わった後に変更からWindows SDK10を導入します。

アップグレードの場合は(もしくは最新のビルドでは不要)Windows 10 Developer Preview Toolsはインストール済みでした。

動作確認

Git for WindowsCygwinMinGW等でgitを入れます。

$ git clone https://github.com/Microsoft/DirectX-Graphics-Samples
$ cd DirectX-Graphics-Samples/Samples/D3D12HelloWorld/src

D3D12HelloWorld.slnからVSを起動させます。
そのままではビルドに失敗するので
Solutin Explorerのプロジェクトを右クリックしてプロパティを選び、
GeneralにてTarget Platform Versionを新しい物へ変更します。
私の場合は10.0.10240.0から10.0.10586.0へ変更しました。

HelloWindowのプロジェクトが生成した実行ファイルをビルドしてウィンドウが表示されたらOKです。

プロジェクトの作成下見

new projectするとDirectX 12 Appといういかにもなテンプレートが追加されていますが、C++/CLIぽかったのでスルーします。
いつも通りのWin32 Projectで作成します。

パス

DirectX12関連のパスをメモしておきます。
Solutin Explorerのプロジェクトを右クリックしてプロパティを選び
VC++DirectoriesにてIncludeとLibraryを追加します。

Include Directories

C:\Program Files (x86)\Windows Kits\10\Include\10.0.platform番号\um
C:\Program Files (x86)\Windows Kits\10\Include\10.0.platform番号\shared

Library Directories

C:\Program Files (x86)\Windows Kits\10\Lib\10.0.platform番号\um\x86

x64だとリンカエラー出ました。

git-flowとhubコマンドを使ってCUIでgithubを活用する

github/hubコマンドを使ってみた - ぷろみんの続きです

概要

git-flowとhubを使います。

インストール

$ sudo apt-get install -y git-flow

初期化

$ mkdir foo
$ cd foo
# hub導入済み
$ git init -g
$ git flow init
# 質問には全部エンター
$ git create

$ echo '# foo' >> README.md
$ git add .
$ git commit -m 'Initial commit'
$ git push --all

開発

# issue create [-m <MESSAGE>|-f <FILE>] [-l <LABEL-1>,<LABEL-2>...,<LABEL-N>]
$ git issue create -m 'LICENSEを追加する'
# issueを解決するブランチを作成する
$ git flow feature start add_license

licenseの追加にはliceを使ってみようかと思います。

$ sudo pip install lice
$ lice mit > LICENSE

featureブランチでissueを解決し、プッシュします。

$ git add .
$ git commit -m 'close #1'
$ git flow feature publish add_license

そして、その変更を取り込んでもらえるようにpull-requestを送ります。

$ git pull-request -m "Add license" -b develop -h feature/add_license

問題無ければマージします。

$ git checkout develop
$ git merge https://github.com/YOUR_USER/CURRENT_REPO/pull/2
$ git push origin develop

ここもhubさんの力でgit pr-merge 2とかできたら良かったんですけどねー。
issueが全て片付いたので、masterへマージします。

$ git checkout master
$ git merge develop
$ git push origin master

githubで確認すると無事にissueがcloseされました。

参考

git-flow cheatsheet

( WIP: 更新済み ) Ubuntu + Vagrant + Ansible + Serverspec + Capistrano 3 + Git-Flow を使って Nginx + Rails + Unicorn で走るサービスの面倒な作業を自動化しよう! - コードレシピ

terminal - git auto-complete for *branches* at the command line? - Ask Different

licenses/lice · GitHub

github/hubコマンドを使ってみた

概要

githubCUIで操作できるhubコマンドの使い方や困った事をメモしました。

環境

debian

インストール

まず、ビルドにgoが必要なのでインストールします。
私の場合はansibleで入れました。
こんなややこしい方法取らずに適当な方法でインストールした方が楽です。

# パッケージマネージャから入れる方法
sudo apt-get install -y software-properties-common
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

# pipから入れる方法
sudo apt-get install -y python-pip
sudo pip install ansible
$ mkdir -p develope/roles
$ git clone https://github.com/jlund/ansible-go develope/roles/ansible-go
$ vim develope/site.yml
# develope/site.yml
---
- hosts: all
  sudo: yes
  roles:
    - ansible-go
$ vim hosts
# ansible_connectionを付けない場合自身のssh鍵が必要
localhost ansible_connection=local
# 接続確認を無効化したい場合
$ vim ansible.cfg
[defaults]
host_key_checking = false
$ ansible-playbook -i hosts develope/site.yml
# 再読み込みしないとgoへのパスが通らないので
$ exec $SHELL -l

次はhubのビルド

$ git clone https://github.com/github/hub.git
$ cd hub
$ ./script/build
$ sudo cp hub /usr/local/bin
# hubにgitエイリアスをかける
$ echo 'eval "$(hub alias -s)"' >> ~/.bash_profile
$ exec $SHELL -l
$ git version
git version 2.1.4
hub version 2.2.0-96-gaa63aa6

push

$ mkdir foo
$ cd foo
$ git init -g
$ echo '# foo' >> README.md
$ git create
# ここで色々聞かれる

ユーザ名とパスワード、two-factor authenticationを有効にしている場合はSMS等で送られてくるコードを入力します。すると

https://github.com/settings/tokens

に新しくアクセストークンが追加されています。
fooレポジトリも作成されています。
また、~/.config/hubも生成されています。このファイルにより次回からはgit createの際に認証が不要になります。
ファイルの内容は以下です。

github.com:
- user: YOUR_USER
  oauth_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  protocol: https

早速pushしていきましょう。

$ git config --global user.name "YOUR_USER"
$ git config --global user.email "YOUR_EMAIL"
$ git add .
$ git commit -m 'Initial commit'
$ git push origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

アクセストークン作ったのならそれでpushして欲しかったのですが、そこはカバーしてくれない様子です。以下の様にすればアクセストークンでpushできるようになるはずですが、せっかく名前とレポジトリを自動取得できるようにしたのに、手入力したくありません。

git remote set-url origin https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@github.com/YOUR_USER/CURRENT_REPO.git

諦めてssh接続します。
ssh-keygenid_rsa.pubを生成し、内容をgithubに登録します。

その後に再トライです。

$ git push origin master
ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

レポジトリの場所が間違っている様子なので、どこにアクセスしているか確認します。

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@github.com:/foo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

url = git@github.com:/foo.gitとユーザ名が抜けてしまっているみたいです。仕方ないので作り直します。

$ rm -rf .git
$ git init -g
$ git add .
$ git commit -m 'Initial commit'
$ git push origin master

今度は上手くいきました。

次回はgit-flow入れてhubを活用してみようと思います。

gitの最新版をビルドする

環境

https://atlas.hashicorp.com/ARTACK/boxes/debian-jessieで作成されたdebianのboxを使っています。

$ sudo aptitude update
$ sudo aptitude -y install git
$ sudo aptitude -y install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
$ git clone https://github.com/git/git

$ cd git
$ git tag | tail
v2.5.0-rc0
v2.5.0-rc1
v2.5.0-rc2
v2.5.0-rc3
v2.5.1
v2.5.2
v2.5.3
v2.6.0-rc0
v2.6.0-rc1
v2.6.0-rc2
# 2.5.3をインストールすることにする
$ git checkout refs/tags/v2.5.3

# 後はビルドするだけなのでバージョンの古いgitをアンインストールする
$ sudo aptitude -y purge git

$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install

# 環境変数を適用する為にシェルを再起動
$ exec -l $SHELL
$ git --version
git version 2.5.3

Git - Gitのインストールを参考にインストールしましたが、v2は何が違うのでしょうか?

debianでglfwをコンパイルしてwindows向けバイナリを作成する

概要

windows環境での話です。
最近msys-gitやmysy2を使ったりで環境変数やら実行バイナリがごちゃごちゃしてきてしまったので、実験用の環境が欲しくなったので仮想化してみます。

事前準備

管理者権限が必要なパッケージもあります。

  • chocolatly
    https://chocolatey.org/
    サイトに記載されているコマンドを打てばインストールできます。

  • virtual box

$choco install virtualbox
$choco install vagrant
$choco install virtualbox.extensionpack

もしかしたらOpenSSHも必要かもしれません。

choco install openssh

sshはパスが自動で付かないかもしれません。

Windows 8.1ではじめるイマドキの開発環境 - Qiita

vagrant

http://www.vagrantbox.es/から好きなboxを引っ張ってきます。 とりあえずDebianでいきます。

#powershell か msys-mingw
$mkdir debian
$cd debian
$vagrant box add https://atlas.hashicorp.com/ARTACK/boxes/debian-jessie
$vagrant box list
ARTACK/debian-jessie (virtualbox, 8.1.0)
$vagrant init ARTACK/debian-jessie
$vagrant up
$vagrant ssh

Oracle VM VirtualBox Extension Packを入れていないとvagrant upの際に以下のエラーがでます。

The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'poweroff' state. Please verify everything is configured
properly and try again.

If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, run `vagrant up` while the
VirtualBox GUI is open.

The primary issue for this error is that the provider you're using
is not properly configured. This is very rarely a Vagrant issue.

言われた通りGUIで起動してみます。
するとImplementation of the USB 2.0 controller not found!というエラーが出ます。

ビルド

ここからはvagrantで起動したdebian上での実行例です。

# 必要なパッケージを揃える
$sudo aptitude update
$sudo aptitude -V -D -y install git
$sudo aptitude -V -D -y install cmake
$sudo aptitude -V -D -y install mingw-w64

# mingw版のglfwをビルド
$git clone https://github.com/glfw/glfw
$cd glfw
$cmake -DCMAKE_TOOLCHAIN_FILE=CMake/x86_64-w64-mingw32.cmake .
$make
$sudo make install

# http://www.glfw.org/docs/latest/quick.htmlにあるサンプルをhello.cppとして保存

$x86_64-w64-mingw32-g++ -I/usr/local/include hello.cpp -L/usr/local/lib -lglfw3 -lopengl32 -lwinmm -lgdi32
# windowsとの共有フォルダに実行ファイルを送る
$mv a.exe /vagrant/a.exe

windows側からa.exeを実行すると無事に実行できました。

後片付け

遊び終わったら環境を片付けましょう。

# debian
$exit

# windows
$vagrant halt
$vagrant destroy --force