diff datagear/command/CommandType.cs @ 17:4a3115ba746d

fix CommandType enum
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 24 Nov 2020 03:11:50 +0900
parents 7352793b5dbe
children 8fe565f8acb8
line wrap: on
line diff
--- a/datagear/command/CommandType.cs	Sun Nov 22 01:52:22 2020 +0900
+++ b/datagear/command/CommandType.cs	Tue Nov 24 03:11:50 2020 +0900
@@ -2,43 +2,31 @@
 using System.Collections.Generic;
 
 namespace Christie_net.datagear.command {
-public enum CommandType {
-    PUT,
-    TAKE,
-    PEEK,
-    REMOTETAKE,
-    REMOTEPEEK,
-    REPLY,
-    CLOSE,
-    FINISH
-}
+public class CommandType {
+    private static int lastId = 0; // コマンドの総数
+    public readonly int id = ++lastId; // コマンドのid コンストラクタが呼ばれるたびにlastIdが++される
+    public static readonly Dictionary<int, CommandType> hash = new Dictionary<int, CommandType>();
 
-// C#ではenumに関数は生やせないためヘルパークラスを実装
-public class CommandTypeEtx {
-    public CommandType commandType;
-    public int id; // command ID
-    public static Dictionary<int, CommandType> hash = new Dictionary<int, CommandType>();  // コマンド対応表
-    private static int lastID = 0; // total command number
-
-    // コマンドごとにコンストラクタ内の処理をする必要がある
-    public CommandTypeEtx(CommandType type) {
-        commandType = type;
-        StoreValues();
-        id = IncrementLastID();
+    private CommandType() {
+        hash.Add(this.id, this);
     }
 
-    private static int IncrementLastID() {
-        return ++lastID;
-    }
-
-    public static CommandType GetCommandTypeFromID(int id) {
+    /// <summary>
+    /// idよりCommandTypeを返す
+    /// </summary>
+    /// <param name="id"></param>
+    /// <returns></returns>
+    public static CommandType GetCommandTypeFromId(int id) {
         return hash[id];
     }
 
-    public static void StoreValues() {
-        foreach (CommandType value in Enum.GetValues(typeof(CommandType))) {
-            hash.Add(Convert.ToInt32(value), value);
-        }
-    }
+    public static readonly CommandType PUT = new CommandType();
+    public static readonly CommandType TAKE = new CommandType();
+    public static readonly CommandType PEEK = new CommandType();
+    public static readonly CommandType REMOTETAKE = new CommandType();
+    public static readonly CommandType REMOTEPEEK = new CommandType();
+    public static readonly CommandType REPLY = new CommandType();
+    public static readonly CommandType CLOSE = new CommandType();
+    public static readonly CommandType FINISH = new CommandType();
 }
 }
\ No newline at end of file