Пример #1
0
        protected void MarshalTop(object obj, WriteCache cache)
        {
            IWriteHandler handler = GetHandler(obj);

            if (handler == null)
            {
                throw new NotSupportedException(
                          string.Format("Cannot marshal type {0} ({1})", obj != null ? obj.GetType() : null, obj));
            }

            string tag = handler.Tag(obj);

            if (tag == null)
            {
                throw new NotSupportedException(
                          string.Format("Cannot marshal type {0} ({1})", obj != null ? obj.GetType() : null, obj));
            }

            if (tag.Length == 1)
            {
                obj = new Quote(obj);
            }

            Marshal(obj, false, cache);
        }
Пример #2
0
        private IWriteHandler CheckBaseTypes(Type type, IEnumerable <Type> baseTypes)
        {
            IDictionary <Type, IWriteHandler> possibles = new Dictionary <Type, IWriteHandler>();

            foreach (Type item in baseTypes)
            {
                // TODO Better way to decide the most appropriate possibility
                if (possibles.Count < 1)
                {
                    IWriteHandler h;
                    if (handlers.TryGetValue(item, out h))
                    {
                        possibles.Add(item, h);
                    }
                }
            }

            switch (possibles.Count)
            {
            case 0: return(null);

            case 1:
            {
                IWriteHandler h = possibles.First().Value;
                handlers = handlers.Add(type, h);
                return(h);
            }

            default:
                throw new TransitException("More thane one match for " + type);
            }
        }
Пример #3
0
        protected void Marshal(object o, bool asDictionaryKey, WriteCache cache)
        {
            bool supported = false;

            IWriteHandler h = GetHandler(o);

            if (h != null)
            {
                string t = h.Tag(o);
                if (t != null)
                {
                    supported = true;
                    if (t.Length == 1)
                    {
                        switch (t[0])
                        {
                        case '_': EmitNull(asDictionaryKey, cache); break;

                        case 's': EmitString(null, null, Escape((string)h.Representation(o)), asDictionaryKey, cache); break;

                        case '?': EmitBoolean((bool)h.Representation(o), asDictionaryKey, cache); break;

                        case 'i': EmitInteger(h.Representation(o), asDictionaryKey, cache); break;

                        case 'd': EmitDouble(h.Representation(o), asDictionaryKey, cache); break;

                        case 'b': EmitBinary(h.Representation(o), asDictionaryKey, cache); break;

                        case '\'': EmitTagged(t, h.Representation(o), false, cache); break;

                        default: EmitEncoded(t, h, o, asDictionaryKey, cache); break;
                        }
                    }
                    else
                    {
                        if (t.Equals("array"))
                        {
                            EmitList(h.Representation(o), asDictionaryKey, cache);
                        }
                        else
                        if (t.Equals("map"))
                        {
                            EmitDictionary(h.Representation(o), asDictionaryKey, cache);
                        }
                        else
                        {
                            EmitEncoded(t, h, o, asDictionaryKey, cache);
                        }
                    }
                    FlushWriter();
                }
            }

            if (!supported)
            {
                throw new NotSupportedException("Not supported: " + o.GetType());
            }
        }
Пример #4
0
 public static string WritePrintableToString(IEDNWritable printable, IWriteHandler handler)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         printable.WriteEDN(ms, handler);
         ms.Position = 0;
         var sr = new StreamReader(ms);
         return(sr.ReadToEnd());
     }
 }
Пример #5
0
 public static string WritePrintableToString(IEDNWritable printable, IWriteHandler handler)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         printable.WriteEDN(ms, handler);
         ms.Position = 0;
         var sr = new StreamReader(ms);
         return sr.ReadToEnd();
     }
 }
Пример #6
0
        public string GetTag(object obj)
        {
            IWriteHandler handler = GetHandler(obj);

            if (handler == null)
            {
                return(null);
            }

            return(handler.Tag(obj));
        }
Пример #7
0
        private IWriteHandler GetHandler(object obj)
        {
            Type          type    = (obj != null) ? obj.GetType() : typeof(NullType);
            IWriteHandler handler = null;

            if (!handlers.TryGetValue(type, out handler))
            {
                handler = CheckBaseClasses(type);

                if (handler == null)
                {
                    handler = CheckBaseGenericInterfaces(type);

                    if (handler == null)
                    {
                        handler = CheckBaseInterfaces(type);
                    }
                }
            }

            return(handler);
        }
Пример #8
0
 protected void EmitEncoded(string t, IWriteHandler handler, object obj, bool asDictionaryKey, WriteCache cache)
 {
     if (t.Length == 1)
     {
         object r = handler.Representation(obj);
         if (r is string)
         {
             EmitString(Constants.EscStr, t, (string)r, asDictionaryKey, cache);
         }
         else
         if (PrefersStrings() || asDictionaryKey)
         {
             string sr = handler.StringRepresentation(obj);
             if (sr != null)
             {
                 EmitString(Constants.EscStr, t, sr, asDictionaryKey, cache);
             }
             else
             {
                 throw new TransitException("Cannot be encoded as a string " + obj);
             }
         }
         else
         {
             EmitTagged(t, r, asDictionaryKey, cache);
         }
     }
     else
     {
         if (asDictionaryKey)
         {
             throw new TransitException("Cannot be used as a map key " + obj);
         }
         else
         {
             EmitTagged(t, handler.Representation(obj), asDictionaryKey, cache);
         }
     }
 }
Пример #9
0
        public void DelegatePerformanceTest()
        {
            int runs = 5000000;

            StopWatch sw;

            sw = new StopWatch();
            using (sw.Start("Time:{0}"))
            {
                for (int i = 0; i < runs; i++)
                {
                    PerformanceTestTarget(1, null, null);
                }
            }
//            Trace.WriteLine( string.Format("Time:{0}", sw.Elapsed.TotalSeconds) );

            WriteHandler writeHandler = new WriteHandler(PerformanceTestTarget);

            using (sw.Start("Time:{0}"))
            {
                for (int i = 0; i < runs; i++)
                {
                    writeHandler(1, null, null);
                }
            }
//            Trace.WriteLine(string.Format("Time:{0}", sw.Elapsed.TotalSeconds));

            IWriteHandler writeHandler2 = this;

            using (sw.Start("Time:{0}"))
            {
                for (int i = 0; i < runs; i++)
                {
                    writeHandler2.PerformanceTestTarget(1, null, null);
                }
            }
//            Trace.WriteLine(string.Format("Time:{0}", sw.Elapsed.TotalSeconds));
        }
Пример #10
0
 public string WriteEDN(IWriteHandler handler)
 {
     return Utils.getSymbolString(prefix, name);
 }
Пример #11
0
 public string WriteEDN(IWriteHandler handler)
 {
     return(Utils.WritePrintableToString(this, handler));
 }
Пример #12
0
 public string WriteEDN(IWriteHandler handler)
 {
     return ":" + this.ToString();
 }
 protected void EmitEncoded(string t, IWriteHandler handler, object obj, bool asDictionaryKey, WriteCache cache)
 {
     if (t.Length == 1)
     {
         object r = handler.Representation(obj);
         if (r is string)
         {
             EmitString(Constants.EscStr, t, (string)r, asDictionaryKey, cache);
         }
         else
             if (PrefersStrings() || asDictionaryKey)
             {
                 string sr = handler.StringRepresentation(obj);
                 if (sr != null)
                 {
                     EmitString(Constants.EscStr, t, sr, asDictionaryKey, cache);
                 }
                 else
                 {
                     throw new TransitException("Cannot be encoded as a string " + obj);
                 }
             }
             else
             {
                 EmitTagged(t, r, asDictionaryKey, cache);
             }
     }
     else
     {
         if (asDictionaryKey)
         {
             throw new TransitException("Cannot be used as a map key " + obj);
         }
         else
         {
             EmitTagged(t, handler.Representation(obj), asDictionaryKey, cache);
         }
     }
 }
Пример #14
0
 public void WriteEDN(System.IO.Stream stream, IWriteHandler handler)
 {
     stream.Write(Utils.openSetBytes, 0, Utils.openSetBytes.Length);
     handler.handleEnumerable(this, stream, this);
     stream.Write(Utils.closeSetBytes, 0, Utils.closeSetBytes.Length);
 }
Пример #15
0
 public string WriteEDN(IWriteHandler handler)
 {
     return(Utils.getSymbolString(prefix, name));
 }
 protected CreateService(IWriteHandler <TModel> handler)
 {
     Handler = handler;
 }
Пример #17
0
 public void WriteEDN(System.IO.Stream stream, IWriteHandler handler)
 {
     Utils.WriteEDNToStream(this.ToString(), stream);
 }
Пример #18
0
 public void WriteEDN(System.IO.Stream stream, IWriteHandler handler)
 {
     Utils.WriteEDNToStream(this.ToString(), stream);
 }
Пример #19
0
        public Workbook ExportData(IWriteHandler handler, Dictionary <string, object> param)
        {
            var wb = handler.ExecuteWork(_workbook, param);

            return(wb);
        }
Пример #20
0
 public string WriteEDN(IWriteHandler handler)
 {
     return(":" + this.ToString());
 }