Tizen 2.1 base
[external/enchant.git] / src / bindings / Enchant.Net / SafeDictionaryHandle.cs
1 /* Copyright (c) 2007 Eric Scott Albright\r
2  * \r
3  * Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  * of this software and associated documentation files (the "Software"), to deal\r
5  * in the Software without restriction, including without limitation the rights\r
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  * copies of the Software, and to permit persons to whom the Software is\r
8  * furnished to do so, subject to the following conditions:\r
9  * \r
10  * The above copyright notice and this permission notice shall be included in\r
11  * all copies or substantial portions of the Software.\r
12  * \r
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
19  * THE SOFTWARE.\r
20  */\r
21 \r
22 using System;\r
23 using System.Runtime.ConstrainedExecution;\r
24 using System.Runtime.InteropServices;\r
25 \r
26 namespace Enchant\r
27 {\r
28         internal sealed class SafeDictionaryHandle : SafeHandle\r
29         {\r
30                 private readonly SafeBrokerHandle _broker;\r
31 \r
32                 public SafeDictionaryHandle(SafeBrokerHandle broker, IntPtr handle)\r
33                         : base(IntPtr.Zero, true)\r
34                 {\r
35                         _broker = broker;\r
36                         SetHandle(handle);\r
37                 }\r
38 \r
39 \r
40                 ///<summary>\r
41                 ///When overridden in a derived class, gets a value indicating whether the handle value is invalid.\r
42                 ///</summary>\r
43                 ///<returns>\r
44                 ///true if the handle is valid; otherwise, false.\r
45                 ///</returns>\r
46                 public override bool IsInvalid\r
47                 {\r
48                         get { return (handle == IntPtr.Zero); }\r
49                 }\r
50 \r
51                 ///<summary>\r
52                 ///When overridden in a derived class, executes the code required to free the handle.\r
53                 ///</summary>\r
54                 ///<returns>\r
55                 ///true if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a ReleaseHandleFailed Managed Debugging Assistant.\r
56                 ///</returns>\r
57                 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]\r
58                 protected override bool ReleaseHandle()\r
59                 {\r
60                         // The finalizer may finalize the broker and the dictionaries in any order\r
61                         // leading to a case where the broker may have already been released and thus\r
62                         // already cleaned up all its owned dictionaries. If that is the case, we don't\r
63                         // need to clean up.\r
64                         if (_broker.IsClosed)\r
65                                 return true;\r
66                         Bindings.enchant_broker_free_dict(_broker, handle);\r
67                         return true;\r
68                 }\r
69         }\r
70 }