예제 #1
0
        public static void Serialize <TDoc, TCursor, TContext>(BitSet @this,
                                                               TagElementStream <TDoc, TCursor, string> s,
                                                               string elementName,
                                                               TContext ctxt, SerializeBitToTagElementStreamDelegate <TDoc, TCursor, TContext> streamElement,
                                                               int highestBitIndex = TypeExtensions.kNoneInt32)
            where TDoc : class
            where TCursor : class
        {
            Contract.Requires(s != null);
            Contract.Requires(streamElement != null);
            Contract.Requires(highestBitIndex.IsNoneOrPositive());
            Contract.Requires(highestBitIndex < @this.Length);

            if (highestBitIndex.IsNone())
            {
                highestBitIndex = @this.Length - 1;
            }

            if (s.IsReading)
            {
                int bit_index = 0;
                foreach (var node in s.ElementsByName(elementName))
                {
                    using (s.EnterCursorBookmark(node))
                    {
                        bit_index = streamElement(s, @this, TypeExtensions.kNoneInt32, ctxt);
                        if (bit_index.IsNone())
                        {
                            s.ThrowReadException(new System.IO.InvalidDataException(string.Format(Util.InvariantCultureInfo,
                                                                                                  "Element is not a valid {0} value", elementName)));
                        }

                        @this[bit_index] = true;
                    }
                }
            }
            else if (s.IsWriting)
            {
                foreach (int bit_index in @this.SetBitIndices)
                {
                    if (bit_index > highestBitIndex)
                    {
                        break;
                    }

                    using (s.EnterCursorBookmark(elementName))
                        streamElement(s, @this, bit_index, ctxt);
                }
            }
        }
예제 #2
0
        static void StreamElements <TDoc, TCursor>(TagElementStream <TDoc, TCursor, string> s,
                                                   ref Values.GroupTagData32[] tags)
            where TDoc : class
            where TCursor : class
        {
            const string k_element_name = "Group";

            if (s.IsReading)
            {
                var list = new List <Values.GroupTagData32>();

                foreach (var node in s.ElementsByName(k_element_name))
                {
                    using (s.EnterCursorBookmark(node))
                    {
                        Values.GroupTagData32 data = null;
                        Serialize(s, ref data);

                        list.Add(data);
                    }
                }

                tags = list.ToArray();
            }
            else if (s.IsWriting)
            {
                foreach (Values.GroupTagData32 data in tags)
                {
                    using (s.EnterCursorBookmark(k_element_name))
                    {
                        var temp = data;                         // can't pass a foreach value by ref
                        Serialize(s, ref temp);
                    }
                }
            }
        }