Beispiel #1
0
        public static OpenOption New(Guid guid,
                                     OpenOptionDeserializeCallback deserializeCallback,
                                     OpenOptionSerializeCallback serializeCallback,
                                     object defaultValue = null, string friendlyName = null)
        {
            Throw.If.Null(deserializeCallback, "deserializeCallback");
            Throw.If.Null(serializeCallback, "serializeCallback");

            OpenOption option;

            lock (_options)
            {
                if (_options.ContainsKey(guid))
                {
                    throw new ArgumentException();
                }

                option = new OpenOption()
                {
                    Guid                 = guid,
                    DefaultValue         = defaultValue,
                    FriendlyName         = friendlyName ?? guid.ToString("B"),
                    _deserializeCallback = deserializeCallback,
                    _serializeCallback   = serializeCallback
                };
                _options.Add(guid, option);
            }
            return(option);
        }
        /// <summary>
        /// Gets the current value of an option.
        /// </summary>
        /// <param name="option">The option.</param>
        /// <returns>The option's value.</returns>
        public object GetOption(OpenOption option)
        {
            Throw.If.Null(option, "option");

            object value;

            return(_options.TryGetValue(option, out value) ? value : option.DefaultValue);
        }
        /// <summary>
        /// Sets the current value of an option.
        /// </summary>
        /// <param name="option">The option.</param>
        /// <param name="value">The value to set it to.</param>
        public void SetOption(OpenOption option, object value)
        {
            Throw.If.Null(option, "option");

            if (value != null)
            {
                _options[option] = value;
            }
            else
            {
                _options.Remove(option);
            }
        }
Beispiel #4
0
        static OpenOption()
        {
            _options = new Dictionary <Guid, OpenOption>();

            Exclusive = OpenOption.New(new Guid("{49DB23CD-727E-4788-BBAD-7D67ACCBC469}"),
                                       deserializeCallback: DeserializeBoolean,
                                       serializeCallback: SerializeBoolean,
                                       defaultValue: false,
                                       friendlyName: "Exclusive");
            Interruptible = OpenOption.New(new Guid("{55C9673C-A49C-4190-B0BC-294020EAAE54}"),
                                           deserializeCallback: DeserializeBoolean,
                                           serializeCallback: SerializeBoolean,
                                           defaultValue: false,
                                           friendlyName: "Interruptible");
            Priority = OpenOption.New(new Guid("{3C065A90-A685-44BD-BE06-50EDACF51F11}"),
                                      deserializeCallback: buffer =>
            {
                if (buffer.Length < 1)
                {
                    return(null);
                }
                return((object)(OpenPriority)buffer[0]);
            },
                                      serializeCallback: value =>
            {
                return(new[] { (byte)(OpenPriority)value });
            },
                                      defaultValue: OpenPriority.Normal,
                                      friendlyName: "Priority");
            TimeoutIfInterruptible = OpenOption.New(new Guid("{C8F9B70B-302F-4326-B28D-E823C4E6131E}"),
                                                    deserializeCallback: DeserializeInt32,
                                                    serializeCallback: SerializeInt32,
                                                    defaultValue: 3000,
                                                    friendlyName: "TimeoutIfInterruptible");
            TimeoutIfTransient = OpenOption.New(new Guid("{0A918B9F-6FF5-4A14-A945-78685B37BF40}"),
                                                deserializeCallback: DeserializeInt32,
                                                serializeCallback: SerializeInt32,
                                                defaultValue: 30000,
                                                friendlyName: "TimeoutIfTransient");
            Transient = OpenOption.New(new Guid("{C564DE4B-A9A8-4F5F-A7E4-1A14AF9BEFEC}"),
                                       deserializeCallback: DeserializeBoolean,
                                       serializeCallback: SerializeBoolean,
                                       defaultValue: false,
                                       friendlyName: "Transient");
            BleService = OpenOption.New(new Guid("{A0E7B2C1-656D-40FB-9C29-3CD28F54D45D}"),
                                        deserializeCallback: _ => { throw new NotImplementedException(); },
                                        serializeCallback: _ => { throw new NotImplementedException(); },
                                        defaultValue: null,
                                        friendlyName: "BLE Service");
        }
        /// <summary>
        /// Checks if an option has been set.
        /// </summary>
        /// <param name="option">The option.</param>
        /// <returns><c>true</c> if the option has been set.</returns>
        public bool IsOptionSet(OpenOption option)
        {
            Throw.If.Null(option, "option");

            return(_options.ContainsKey(option));
        }