投稿者「mynote」のアーカイブ

C#でoperator[]のオーバーロード

operator[]のオーバーロード

C#で自作クラスにoperator[]を追加したい場合は次のような感じで実装できます。

class Example
{
  int[] _ar = new int[10];                                                                       
  
  public int this[int i]
  {
    get => _ar[i];                                                                        
    set => _ar[i] = value;                                                                       
  }
}

Exampleクラスを使う方は次のような感じです。

var example = new Example();

example[2] = 345;

Debug.Log($"example[2] = {example[2]}");

実行結果

example[2] = 345

多次元配列

多次元配列のような感じで使いたい場合も次のような感じで実装できます。

class Example2
{
  int[,] _ar = new int[10, 10];

  public int this[int i, int j]
  {
    get => _ar[i, j];
    set => _ar[i, j] = value;
  }
}

Compiler Error CS0720

static classoperator[]を実装しようとするとCS0720というエラーになるようです。気を付けてください。

/* static */ class Example3
{
  public int this[int i]
  {
    get => 0;
  }
}

※配列の範囲外にアクセスしようとするとIndexOutOfRangeExceptionという例外が発生します。実際に使う場合は注意してください。

mplayerでDVD-Videoを扱うためのTIPS

Linuxで使える動画アプリmplayerに関するメモです。
Windowsバージョンもあります。詳細はリンクを参照してください。

DVD-Videoを再生する基本コマンド

単にDVD-Videoの再生したい場合

$ mplayer dvd://1

チャプター2から4のみ再生したい場合

$ mplayer dvd://1 -chapter 2-4

タイトル2を再生したい場合

$ mplayer dvd://2

メニューを使用したい場合

$ mplayer dvdnav://1 -mouse-movements                                                           

-mouse-movementsオプションを外した場合はキーボードのみの操作になります。

HDDにバックアップしたDVD-Videoの取り扱い

コピーガードのかかっていないDVD-Videoの場合

$ cp -r /mnt/dvd /path/to/directory
$ ls /path/to/directory
AUDIO_TS/  VIDEO_TS/

のような感じで普通のファイルと同様の手順でHDDにバックアップすることができます。

このバックアップしたファイルをDVD-Videoとして再生したい場合は-dvd-deviceオプションを使って次のようなコマンドを入力します。

$ mplayer dvd://1 -dvd-device /path/to/directory

DVD-Videoの内容をvobに変換

DVD-Videoの内容をvobファイルとしてdumpしたい場合は-dumpstream-dumpfileオプションを使って次のようなコマンドで出力できます。

$ mplayer dvd://1 -dumpstream -dumpfile dump.vob

-dumpstream-chapterオプションと併用できますので、例えばチャプターごとに分割したvobファイルを作成したい場合は

$ mplayer dvd://1 -chapter 1-1 -dumpstream -dumpfile chapter-1.vob
$ mplayer dvd://1 -chapter 2-2 -dumpstream -dumpfile chapter-2.vob

のような感じで出力できます。

ちなみに、収録されているチャプター数とかタイトル数などが不明な場合は-identifyオプションで調べることができます。

$ mplayer dvd://1 -identify

MPlayer homepage
http://www.mplayerhq.hu/

MPlayer & MEncoder Builds for Windows
https://mplayerwin.sourceforge.net/downloads.html

Unityで継承元のStartやUpdateを呼び出す方法

Unityでクラスの継承を利用する場合、単にvoid Start()void Update()と書くと継承元の実装は呼び出されない形になります。

サンプル1

public class Example1 : MonoBehaviour
{
  void Start()
  {
    Debug.Log("Start1");
  }
  void Update()
  {
    Debug.Log("Update1");
  }
}

public class Example2 : Example1
{
  void Start()
  {
    Debug.Log("Start2");
  }
  void Update()
  {
    Debug.Log("Update2");
  }
}

Example2の実行結果

Start2
Update2
Update2
Update2
...

このExample2から継承元Example1StartUpdateを呼び出したい場合はvirtual protectedoverride protectedを使って次のような感じで実装できるみたいです。

サンプル2

public class Example1 : MonoBehaviour
{
  virtual protected void Start()
  {
    Debug.Log("Start1");
  }
  virtual protected void Update()
  {
    Debug.Log("Update1");
  }
}

public class Example2 : Example1
{
  override protected void Start()
  {
    base.Start();
    Debug.Log("Start2");
  }
  override protected void Update()
  {
    base.Update();
    Debug.Log("Update2");
  }
}

Example2の実行結果

Start1
Start2
Update1
Update2
Update1
Update2
Update1
Update2
...

base.を使った継承元メソッドの呼び出しはStartUpdate以外でも使えます。必要な場所で試してみてください。