http://git-wip-us.apache.org/repos/asf/thrift/blob/d5436f5c/lib/csharp/test/ThriftTest/TestServer.cs ---------------------------------------------------------------------- diff --git a/lib/csharp/test/ThriftTest/TestServer.cs b/lib/csharp/test/ThriftTest/TestServer.cs index 92ef374..d944e72 100644 --- a/lib/csharp/test/ThriftTest/TestServer.cs +++ b/lib/csharp/test/ThriftTest/TestServer.cs @@ -32,8 +32,8 @@ using Thrift.Server; namespace Test { - public class TestServer - { + public class TestServer + { public class TradeServerEventHandler : TServerEventHandler { public int callCount = 0; @@ -56,395 +56,395 @@ namespace Test } }; - public class TestHandler : ThriftTest.Iface - { - public TServer server; - - public TestHandler() { } - - public void testVoid() - { - Console.WriteLine("testVoid()"); - } - - public string testString(string thing) - { - Console.WriteLine("teststring(\"" + thing + "\")"); - return thing; - } - - public sbyte testByte(sbyte thing) - { - Console.WriteLine("testByte(" + thing + ")"); - return thing; - } - - public int testI32(int thing) - { - Console.WriteLine("testI32(" + thing + ")"); - return thing; - } - - public long testI64(long thing) - { - Console.WriteLine("testI64(" + thing + ")"); - return thing; - } - - public double testDouble(double thing) - { - Console.WriteLine("testDouble(" + thing + ")"); - return thing; - } - - public Xtruct testStruct(Xtruct thing) - { - Console.WriteLine("testStruct({" + - "\"" + thing.String_thing + "\", " + - thing.Byte_thing + ", " + - thing.I32_thing + ", " + - thing.I64_thing + "})"); - return thing; - } - - public Xtruct2 testNest(Xtruct2 nest) - { - Xtruct thing = nest.Struct_thing; - Console.WriteLine("testNest({" + - nest.Byte_thing + ", {" + - "\"" + thing.String_thing + "\", " + - thing.Byte_thing + ", " + - thing.I32_thing + ", " + - thing.I64_thing + "}, " + - nest.I32_thing + "})"); - return nest; - } - - public Dictionary<int, int> testMap(Dictionary<int, int> thing) - { - Console.WriteLine("testMap({"); - bool first = true; - foreach (int key in thing.Keys) - { - if (first) - { - first = false; - } - else - { - Console.WriteLine(", "); - } - Console.WriteLine(key + " => " + thing[key]); - } - Console.WriteLine("})"); - return thing; - } - - public Dictionary<string, string> testStringMap(Dictionary<string, string> thing) - { - Console.WriteLine("testStringMap({"); - bool first = true; - foreach (string key in thing.Keys) - { - if (first) - { - first = false; - } - else - { - Console.WriteLine(", "); - } - Console.WriteLine(key + " => " + thing[key]); - } - Console.WriteLine("})"); - return thing; - } - - public THashSet<int> testSet(THashSet<int> thing) - { - Console.WriteLine("testSet({"); - bool first = true; - foreach (int elem in thing) - { - if (first) - { - first = false; - } - else - { - Console.WriteLine(", "); - } - Console.WriteLine(elem); - } - Console.WriteLine("})"); - return thing; - } - - public List<int> testList(List<int> thing) - { - Console.WriteLine("testList({"); - bool first = true; - foreach (int elem in thing) - { - if (first) - { - first = false; - } - else - { - Console.WriteLine(", "); - } - Console.WriteLine(elem); - } - Console.WriteLine("})"); - return thing; - } - - public Numberz testEnum(Numberz thing) - { - Console.WriteLine("testEnum(" + thing + ")"); - return thing; - } - - public long testTypedef(long thing) - { - Console.WriteLine("testTypedef(" + thing + ")"); - return thing; - } - - public Dictionary<int, Dictionary<int, int>> testMapMap(int hello) - { - Console.WriteLine("testMapMap(" + hello + ")"); - Dictionary<int, Dictionary<int, int>> mapmap = - new Dictionary<int, Dictionary<int, int>>(); - - Dictionary<int, int> pos = new Dictionary<int, int>(); - Dictionary<int, int> neg = new Dictionary<int, int>(); - for (int i = 1; i < 5; i++) - { - pos[i] = i; - neg[-i] = -i; - } - - mapmap[4] = pos; - mapmap[-4] = neg; - - return mapmap; - } - - public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument) - { - Console.WriteLine("testInsanity()"); - - Xtruct hello = new Xtruct(); - hello.String_thing = "Hello2"; - hello.Byte_thing = 2; - hello.I32_thing = 2; - hello.I64_thing = 2; - - Xtruct goodbye = new Xtruct(); - goodbye.String_thing = "Goodbye4"; - goodbye.Byte_thing = (sbyte)4; - goodbye.I32_thing = 4; - goodbye.I64_thing = (long)4; - - Insanity crazy = new Insanity(); - crazy.UserMap = new Dictionary<Numberz, long>(); - crazy.UserMap[Numberz.EIGHT] = (long)8; - crazy.Xtructs = new List<Xtruct>(); - crazy.Xtructs.Add(goodbye); - - Insanity looney = new Insanity(); - crazy.UserMap[Numberz.FIVE] = (long)5; - crazy.Xtructs.Add(hello); - - Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>(); - Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ; - - first_map[Numberz.TWO] = crazy; - first_map[Numberz.THREE] = crazy; - - second_map[Numberz.SIX] = looney; - - Dictionary<long, Dictionary<Numberz, Insanity>> insane = - new Dictionary<long, Dictionary<Numberz, Insanity>>(); - insane[(long)1] = first_map; - insane[(long)2] = second_map; - - return insane; - } - - public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5) - { - Console.WriteLine("testMulti()"); - - Xtruct hello = new Xtruct(); ; - hello.String_thing = "Hello2"; - hello.Byte_thing = arg0; - hello.I32_thing = arg1; - hello.I64_thing = arg2; - return hello; - } - - public void testException(string arg) - { - Console.WriteLine("testException(" + arg + ")"); - if (arg == "Xception") - { - Xception x = new Xception(); - x.ErrorCode = 1001; - x.Message = "This is an Xception"; - throw x; - } - return; - } - - public Xtruct testMultiException(string arg0, string arg1) - { - Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")"); - if (arg0 == "Xception") - { - Xception x = new Xception(); - x.ErrorCode = 1001; - x.Message = "This is an Xception"; - throw x; - } - else if (arg0 == "Xception2") - { - Xception2 x = new Xception2(); - x.ErrorCode = 2002; - x.Struct_thing = new Xtruct(); - x.Struct_thing.String_thing = "This is an Xception2"; - throw x; - } - - Xtruct result = new Xtruct(); - result.String_thing = arg1; - return result; - } - - public void testStop() - { - if (server != null) - { - server.Stop(); - } - } - - public void testOneway(int arg) - { - Console.WriteLine("testOneway(" + arg + "), sleeping..."); - System.Threading.Thread.Sleep(arg * 1000); - Console.WriteLine("testOneway finished"); - } - - } // class TestHandler - - public static void Execute(string[] args) - { - try - { - bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false; - int port = 9090; - string pipe = null; - for (int i = 0; i < args.Length; i++) - { - if (args[i] == "-pipe") // -pipe name - { - pipe = args[++i]; - } - else if (args[i].Contains("--port=")) - { - port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1)); - } - else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") - { - useBufferedSockets = true; - } - else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") - { - useFramed = true; - } - else if (args[i] == "--compact" || args[i] == "--protocol=compact") - { - compact = true; - } - else if (args[i] == "--json" || args[i] == "--protocol=json") - { - json = true; - } - else if (args[i] == "--ssl") - { - useEncryption = true; - } - } - - // Processor - TestHandler testHandler = new TestHandler(); - ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler); - - // Transport - TServerTransport trans; - if( pipe != null) - { - trans = new TNamedPipeServerTransport(pipe); - } - else - { - if (useEncryption) - { - trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2("../../../../../keys/server.pem")); - } - else - { - trans = new TServerSocket(port, 0, useBufferedSockets); - } - } - - TProtocolFactory proto; - if ( compact ) - proto = new TCompactProtocol.Factory(); - else if ( json ) - proto = new TJSONProtocol.Factory(); - else - proto = new TBinaryProtocol.Factory(); - - // Simple Server - TServer serverEngine; - if ( useFramed ) - serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto); - else - serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto); - - // ThreadPool Server - // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket); - - // Threaded Server - // serverEngine = new TThreadedServer(testProcessor, tServerSocket); + public class TestHandler : ThriftTest.Iface + { + public TServer server; + + public TestHandler() { } + + public void testVoid() + { + Console.WriteLine("testVoid()"); + } + + public string testString(string thing) + { + Console.WriteLine("teststring(\"" + thing + "\")"); + return thing; + } + + public sbyte testByte(sbyte thing) + { + Console.WriteLine("testByte(" + thing + ")"); + return thing; + } + + public int testI32(int thing) + { + Console.WriteLine("testI32(" + thing + ")"); + return thing; + } + + public long testI64(long thing) + { + Console.WriteLine("testI64(" + thing + ")"); + return thing; + } + + public double testDouble(double thing) + { + Console.WriteLine("testDouble(" + thing + ")"); + return thing; + } + + public Xtruct testStruct(Xtruct thing) + { + Console.WriteLine("testStruct({" + + "\"" + thing.String_thing + "\", " + + thing.Byte_thing + ", " + + thing.I32_thing + ", " + + thing.I64_thing + "})"); + return thing; + } + + public Xtruct2 testNest(Xtruct2 nest) + { + Xtruct thing = nest.Struct_thing; + Console.WriteLine("testNest({" + + nest.Byte_thing + ", {" + + "\"" + thing.String_thing + "\", " + + thing.Byte_thing + ", " + + thing.I32_thing + ", " + + thing.I64_thing + "}, " + + nest.I32_thing + "})"); + return nest; + } + + public Dictionary<int, int> testMap(Dictionary<int, int> thing) + { + Console.WriteLine("testMap({"); + bool first = true; + foreach (int key in thing.Keys) + { + if (first) + { + first = false; + } + else + { + Console.WriteLine(", "); + } + Console.WriteLine(key + " => " + thing[key]); + } + Console.WriteLine("})"); + return thing; + } + + public Dictionary<string, string> testStringMap(Dictionary<string, string> thing) + { + Console.WriteLine("testStringMap({"); + bool first = true; + foreach (string key in thing.Keys) + { + if (first) + { + first = false; + } + else + { + Console.WriteLine(", "); + } + Console.WriteLine(key + " => " + thing[key]); + } + Console.WriteLine("})"); + return thing; + } + + public THashSet<int> testSet(THashSet<int> thing) + { + Console.WriteLine("testSet({"); + bool first = true; + foreach (int elem in thing) + { + if (first) + { + first = false; + } + else + { + Console.WriteLine(", "); + } + Console.WriteLine(elem); + } + Console.WriteLine("})"); + return thing; + } + + public List<int> testList(List<int> thing) + { + Console.WriteLine("testList({"); + bool first = true; + foreach (int elem in thing) + { + if (first) + { + first = false; + } + else + { + Console.WriteLine(", "); + } + Console.WriteLine(elem); + } + Console.WriteLine("})"); + return thing; + } + + public Numberz testEnum(Numberz thing) + { + Console.WriteLine("testEnum(" + thing + ")"); + return thing; + } + + public long testTypedef(long thing) + { + Console.WriteLine("testTypedef(" + thing + ")"); + return thing; + } + + public Dictionary<int, Dictionary<int, int>> testMapMap(int hello) + { + Console.WriteLine("testMapMap(" + hello + ")"); + Dictionary<int, Dictionary<int, int>> mapmap = + new Dictionary<int, Dictionary<int, int>>(); + + Dictionary<int, int> pos = new Dictionary<int, int>(); + Dictionary<int, int> neg = new Dictionary<int, int>(); + for (int i = 1; i < 5; i++) + { + pos[i] = i; + neg[-i] = -i; + } + + mapmap[4] = pos; + mapmap[-4] = neg; + + return mapmap; + } + + public Dictionary<long, Dictionary<Numberz, Insanity>> testInsanity(Insanity argument) + { + Console.WriteLine("testInsanity()"); + + Xtruct hello = new Xtruct(); + hello.String_thing = "Hello2"; + hello.Byte_thing = 2; + hello.I32_thing = 2; + hello.I64_thing = 2; + + Xtruct goodbye = new Xtruct(); + goodbye.String_thing = "Goodbye4"; + goodbye.Byte_thing = (sbyte)4; + goodbye.I32_thing = 4; + goodbye.I64_thing = (long)4; + + Insanity crazy = new Insanity(); + crazy.UserMap = new Dictionary<Numberz, long>(); + crazy.UserMap[Numberz.EIGHT] = (long)8; + crazy.Xtructs = new List<Xtruct>(); + crazy.Xtructs.Add(goodbye); + + Insanity looney = new Insanity(); + crazy.UserMap[Numberz.FIVE] = (long)5; + crazy.Xtructs.Add(hello); + + Dictionary<Numberz, Insanity> first_map = new Dictionary<Numberz, Insanity>(); + Dictionary<Numberz, Insanity> second_map = new Dictionary<Numberz, Insanity>(); ; + + first_map[Numberz.TWO] = crazy; + first_map[Numberz.THREE] = crazy; + + second_map[Numberz.SIX] = looney; + + Dictionary<long, Dictionary<Numberz, Insanity>> insane = + new Dictionary<long, Dictionary<Numberz, Insanity>>(); + insane[(long)1] = first_map; + insane[(long)2] = second_map; + + return insane; + } + + public Xtruct testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5) + { + Console.WriteLine("testMulti()"); + + Xtruct hello = new Xtruct(); ; + hello.String_thing = "Hello2"; + hello.Byte_thing = arg0; + hello.I32_thing = arg1; + hello.I64_thing = arg2; + return hello; + } + + public void testException(string arg) + { + Console.WriteLine("testException(" + arg + ")"); + if (arg == "Xception") + { + Xception x = new Xception(); + x.ErrorCode = 1001; + x.Message = "This is an Xception"; + throw x; + } + return; + } + + public Xtruct testMultiException(string arg0, string arg1) + { + Console.WriteLine("testMultiException(" + arg0 + ", " + arg1 + ")"); + if (arg0 == "Xception") + { + Xception x = new Xception(); + x.ErrorCode = 1001; + x.Message = "This is an Xception"; + throw x; + } + else if (arg0 == "Xception2") + { + Xception2 x = new Xception2(); + x.ErrorCode = 2002; + x.Struct_thing = new Xtruct(); + x.Struct_thing.String_thing = "This is an Xception2"; + throw x; + } + + Xtruct result = new Xtruct(); + result.String_thing = arg1; + return result; + } + + public void testStop() + { + if (server != null) + { + server.Stop(); + } + } + + public void testOneway(int arg) + { + Console.WriteLine("testOneway(" + arg + "), sleeping..."); + System.Threading.Thread.Sleep(arg * 1000); + Console.WriteLine("testOneway finished"); + } + + } // class TestHandler + + public static void Execute(string[] args) + { + try + { + bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false; + int port = 9090; + string pipe = null; + for (int i = 0; i < args.Length; i++) + { + if (args[i] == "-pipe") // -pipe name + { + pipe = args[++i]; + } + else if (args[i].Contains("--port=")) + { + port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1)); + } + else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") + { + useBufferedSockets = true; + } + else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") + { + useFramed = true; + } + else if (args[i] == "--compact" || args[i] == "--protocol=compact") + { + compact = true; + } + else if (args[i] == "--json" || args[i] == "--protocol=json") + { + json = true; + } + else if (args[i] == "--ssl") + { + useEncryption = true; + } + } + + // Processor + TestHandler testHandler = new TestHandler(); + ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler); + + // Transport + TServerTransport trans; + if( pipe != null) + { + trans = new TNamedPipeServerTransport(pipe); + } + else + { + if (useEncryption) + { + trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2("../../../../../keys/server.pem")); + } + else + { + trans = new TServerSocket(port, 0, useBufferedSockets); + } + } + + TProtocolFactory proto; + if ( compact ) + proto = new TCompactProtocol.Factory(); + else if ( json ) + proto = new TJSONProtocol.Factory(); + else + proto = new TBinaryProtocol.Factory(); + + // Simple Server + TServer serverEngine; + if ( useFramed ) + serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto); + else + serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto); + + // ThreadPool Server + // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket); + + // Threaded Server + // serverEngine = new TThreadedServer(testProcessor, tServerSocket); //Server event handler TradeServerEventHandler serverEvents = new TradeServerEventHandler(); serverEngine.setEventHandler(serverEvents); - testHandler.server = serverEngine; - - // Run it - string where = ( pipe != null ? "on pipe "+pipe : "on port " + port); - Console.WriteLine("Starting the server " + where + - (useBufferedSockets ? " with buffered socket" : "") + - (useFramed ? " with framed transport" : "") + - (useEncryption ? " with encryption" : "") + - (compact ? " with compact protocol" : "") + - (json ? " with json protocol" : "") + - "..."); - serverEngine.Serve(); - - } - catch (Exception x) - { - Console.Error.Write(x); - } - Console.WriteLine("done."); - } - } + testHandler.server = serverEngine; + + // Run it + string where = ( pipe != null ? "on pipe "+pipe : "on port " + port); + Console.WriteLine("Starting the server " + where + + (useBufferedSockets ? " with buffered socket" : "") + + (useFramed ? " with framed transport" : "") + + (useEncryption ? " with encryption" : "") + + (compact ? " with compact protocol" : "") + + (json ? " with json protocol" : "") + + "..."); + serverEngine.Serve(); + + } + catch (Exception x) + { + Console.Error.Write(x); + } + Console.WriteLine("done."); + } + } }
http://git-wip-us.apache.org/repos/asf/thrift/blob/d5436f5c/lib/delphi/src/Thrift.Collections.pas ---------------------------------------------------------------------- diff --git a/lib/delphi/src/Thrift.Collections.pas b/lib/delphi/src/Thrift.Collections.pas index 3a0274c..b2206cb 100644 --- a/lib/delphi/src/Thrift.Collections.pas +++ b/lib/delphi/src/Thrift.Collections.pas @@ -1,619 +1,619 @@ -(* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *) - -unit Thrift.Collections; - -interface - -uses - Generics.Collections, Generics.Defaults, Thrift.Utils; - -type - -{$IF CompilerVersion < 21.0} - TArray<T> = array of T; -{$IFEND} - - IThriftContainer = interface - ['{93DEF5A0-D162-461A-AB22-5B4EE0734050}'] - function ToString: string; - end; - - IThriftDictionary<TKey,TValue> = interface(IThriftContainer) - ['{25EDD506-F9D1-4008-A40F-5940364B7E46}'] - function GetEnumerator: TEnumerator<TPair<TKey,TValue>>; - - function GetKeys: TDictionary<TKey,TValue>.TKeyCollection; - function GetValues: TDictionary<TKey,TValue>.TValueCollection; - function GetItem(const Key: TKey): TValue; - procedure SetItem(const Key: TKey; const Value: TValue); - function GetCount: Integer; - - procedure Add(const Key: TKey; const Value: TValue); - procedure Remove(const Key: TKey); -{$IF CompilerVersion >= 21.0} - function ExtractPair(const Key: TKey): TPair<TKey,TValue>; -{$IFEND} - procedure Clear; - procedure TrimExcess; - function TryGetValue(const Key: TKey; out Value: TValue): Boolean; - procedure AddOrSetValue(const Key: TKey; const Value: TValue); - function ContainsKey(const Key: TKey): Boolean; - function ContainsValue(const Value: TValue): Boolean; - function ToArray: TArray<TPair<TKey,TValue>>; - - property Items[const Key: TKey]: TValue read GetItem write SetItem; default; - property Count: Integer read GetCount; - property Keys: TDictionary<TKey,TValue>.TKeyCollection read GetKeys; - property Values: TDictionary<TKey,TValue>.TValueCollection read GetValues; - end; - - TThriftDictionaryImpl<TKey,TValue> = class( TInterfacedObject, IThriftDictionary<TKey,TValue>) - private - FDictionaly : TDictionary<TKey,TValue>; - protected - function GetEnumerator: TEnumerator<TPair<TKey,TValue>>; - - function GetKeys: TDictionary<TKey,TValue>.TKeyCollection; - function GetValues: TDictionary<TKey,TValue>.TValueCollection; - function GetItem(const Key: TKey): TValue; - procedure SetItem(const Key: TKey; const Value: TValue); - function GetCount: Integer; - - procedure Add(const Key: TKey; const Value: TValue); - procedure Remove(const Key: TKey); -{$IF CompilerVersion >= 21.0} - function ExtractPair(const Key: TKey): TPair<TKey,TValue>; -{$IFEND} - procedure Clear; - procedure TrimExcess; - function TryGetValue(const Key: TKey; out Value: TValue): Boolean; - procedure AddOrSetValue(const Key: TKey; const Value: TValue); - function ContainsKey(const Key: TKey): Boolean; - function ContainsValue(const Value: TValue): Boolean; - function ToArray: TArray<TPair<TKey,TValue>>; - property Items[const Key: TKey]: TValue read GetItem write SetItem; default; - property Count: Integer read GetCount; - property Keys: TDictionary<TKey,TValue>.TKeyCollection read GetKeys; - property Values: TDictionary<TKey,TValue>.TValueCollection read GetValues; - public - constructor Create(ACapacity: Integer = 0); - destructor Destroy; override; - end; - - IThriftList<T> = interface(IThriftContainer) - ['{29BEEE31-9CB4-401B-AA04-5148A75F473B}'] - function GetEnumerator: TEnumerator<T>; - function GetCapacity: Integer; - procedure SetCapacity(Value: Integer); - function GetCount: Integer; - procedure SetCount(Value: Integer); - function GetItem(Index: Integer): T; - procedure SetItem(Index: Integer; const Value: T); - function Add(const Value: T): Integer; - procedure AddRange(const Values: array of T); overload; - procedure AddRange(const Collection: IEnumerable<T>); overload; - procedure AddRange(Collection: TEnumerable<T>); overload; - procedure Insert(Index: Integer; const Value: T); - procedure InsertRange(Index: Integer; const Values: array of T); overload; - procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload; - procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload; - function Remove(const Value: T): Integer; - procedure Delete(Index: Integer); - procedure DeleteRange(AIndex, ACount: Integer); - function Extract(const Value: T): T; -{$IF CompilerVersion >= 21.0} - procedure Exchange(Index1, Index2: Integer); - procedure Move(CurIndex, NewIndex: Integer); - function First: T; - function Last: T; -{$IFEND} - procedure Clear; - function Contains(const Value: T): Boolean; - function IndexOf(const Value: T): Integer; - function LastIndexOf(const Value: T): Integer; - procedure Reverse; - procedure Sort; overload; - procedure Sort(const AComparer: IComparer<T>); overload; - function BinarySearch(const Item: T; out Index: Integer): Boolean; overload; - function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload; - procedure TrimExcess; - function ToArray: TArray<T>; - property Capacity: Integer read GetCapacity write SetCapacity; - property Count: Integer read GetCount write SetCount; - property Items[Index: Integer]: T read GetItem write SetItem; default; - end; - - TThriftListImpl<T> = class( TInterfacedObject, IThriftList<T>) - private - FList : TList<T>; - protected - function GetEnumerator: TEnumerator<T>; - function GetCapacity: Integer; - procedure SetCapacity(Value: Integer); - function GetCount: Integer; - procedure SetCount(Value: Integer); - function GetItem(Index: Integer): T; - procedure SetItem(Index: Integer; const Value: T); - function Add(const Value: T): Integer; - procedure AddRange(const Values: array of T); overload; - procedure AddRange(const Collection: IEnumerable<T>); overload; - procedure AddRange(Collection: TEnumerable<T>); overload; - procedure Insert(Index: Integer; const Value: T); - procedure InsertRange(Index: Integer; const Values: array of T); overload; - procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload; - procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload; - function Remove(const Value: T): Integer; - procedure Delete(Index: Integer); - procedure DeleteRange(AIndex, ACount: Integer); - function Extract(const Value: T): T; -{$IF CompilerVersion >= 21.0} - procedure Exchange(Index1, Index2: Integer); - procedure Move(CurIndex, NewIndex: Integer); - function First: T; - function Last: T; -{$IFEND} - procedure Clear; - function Contains(const Value: T): Boolean; - function IndexOf(const Value: T): Integer; - function LastIndexOf(const Value: T): Integer; - procedure Reverse; - procedure Sort; overload; - procedure Sort(const AComparer: IComparer<T>); overload; - function BinarySearch(const Item: T; out Index: Integer): Boolean; overload; - function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload; - procedure TrimExcess; - function ToArray: TArray<T>; - property Capacity: Integer read GetCapacity write SetCapacity; - property Count: Integer read GetCount write SetCount; - property Items[Index: Integer]: T read GetItem write SetItem; default; - public - constructor Create; - destructor Destroy; override; - end; - - IHashSet<TValue> = interface(IThriftContainer) - ['{0923A3B5-D4D4-48A8-91AD-40238E2EAD66}'] - function GetEnumerator: TEnumerator<TValue>; - function GetIsReadOnly: Boolean; - function GetCount: Integer; - property Count: Integer read GetCount; - property IsReadOnly: Boolean read GetIsReadOnly; - procedure Add( const item: TValue); - procedure Clear; - function Contains( const item: TValue): Boolean; - procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer); - function Remove( const item: TValue ): Boolean; - end; - - THashSetImpl<TValue> = class( TInterfacedObject, IHashSet<TValue>) - private - FDictionary : IThriftDictionary<TValue,Integer>; - FIsReadOnly: Boolean; - protected - function GetEnumerator: TEnumerator<TValue>; - function GetIsReadOnly: Boolean; - function GetCount: Integer; - property Count: Integer read GetCount; - property IsReadOnly: Boolean read FIsReadOnly; - procedure Add( const item: TValue); - procedure Clear; - function Contains( const item: TValue): Boolean; - procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer); - function Remove( const item: TValue ): Boolean; - public - constructor Create; - end; - -implementation - -{ THashSetImpl<TValue> } - -procedure THashSetImpl<TValue>.Add( const item: TValue); -begin - if not FDictionary.ContainsKey(item) then - begin - FDictionary.Add( item, 0); - end; -end; - -procedure THashSetImpl<TValue>.Clear; -begin - FDictionary.Clear; -end; - -function THashSetImpl<TValue>.Contains( const item: TValue): Boolean; -begin - Result := FDictionary.ContainsKey(item); -end; - -procedure THashSetImpl<TValue>.CopyTo(var A: TArray<TValue>; arrayIndex: Integer); -var - i : Integer; - Enumlator : TEnumerator<TValue>; -begin - Enumlator := GetEnumerator; - while Enumlator.MoveNext do - begin - A[arrayIndex] := Enumlator.Current; - Inc(arrayIndex); - end; -end; - -constructor THashSetImpl<TValue>.Create; -begin - inherited; - FDictionary := TThriftDictionaryImpl<TValue,Integer>.Create; -end; - -function THashSetImpl<TValue>.GetCount: Integer; -begin - Result := FDictionary.Count; -end; - -function THashSetImpl<TValue>.GetEnumerator: TEnumerator<TValue>; -begin - Result := FDictionary.Keys.GetEnumerator; -end; - -function THashSetImpl<TValue>.GetIsReadOnly: Boolean; -begin - Result := FIsReadOnly; -end; - -function THashSetImpl<TValue>.Remove( const item: TValue): Boolean; -begin - Result := False; - if FDictionary.ContainsKey( item ) then - begin - FDictionary.Remove( item ); - Result := not FDictionary.ContainsKey( item ); - end; -end; - -{ TThriftDictionaryImpl<TKey, TValue> } - -procedure TThriftDictionaryImpl<TKey, TValue>.Add(const Key: TKey; - const Value: TValue); -begin - FDictionaly.Add( Key, Value); -end; - -procedure TThriftDictionaryImpl<TKey, TValue>.AddOrSetValue(const Key: TKey; - const Value: TValue); -begin - FDictionaly.AddOrSetValue( Key, Value); -end; - -procedure TThriftDictionaryImpl<TKey, TValue>.Clear; -begin - FDictionaly.Clear; -end; - -function TThriftDictionaryImpl<TKey, TValue>.ContainsKey( - const Key: TKey): Boolean; -begin - Result := FDictionaly.ContainsKey( Key ); -end; - -function TThriftDictionaryImpl<TKey, TValue>.ContainsValue( - const Value: TValue): Boolean; -begin - Result := FDictionaly.ContainsValue( Value ); -end; - -constructor TThriftDictionaryImpl<TKey, TValue>.Create(ACapacity: Integer); -begin - inherited Create; - FDictionaly := TDictionary<TKey,TValue>.Create( ACapacity ); -end; - -destructor TThriftDictionaryImpl<TKey, TValue>.Destroy; -begin - FDictionaly.Free; - inherited; -end; - -{$IF CompilerVersion >= 21.0} -function TThriftDictionaryImpl<TKey, TValue>.ExtractPair( const Key: TKey): TPair<TKey, TValue>; -begin - Result := FDictionaly.ExtractPair( Key); -end; -{$IFEND} - -function TThriftDictionaryImpl<TKey, TValue>.GetCount: Integer; -begin - Result := FDictionaly.Count; -end; - -function TThriftDictionaryImpl<TKey, TValue>.GetEnumerator: TEnumerator<TPair<TKey, TValue>>; -begin - Result := FDictionaly.GetEnumerator; -end; - -function TThriftDictionaryImpl<TKey, TValue>.GetItem(const Key: TKey): TValue; -begin - Result := FDictionaly.Items[Key]; -end; - -function TThriftDictionaryImpl<TKey, TValue>.GetKeys: TDictionary<TKey, TValue>.TKeyCollection; -begin - Result := FDictionaly.Keys; -end; - -function TThriftDictionaryImpl<TKey, TValue>.GetValues: TDictionary<TKey, TValue>.TValueCollection; -begin - Result := FDictionaly.Values; -end; - -procedure TThriftDictionaryImpl<TKey, TValue>.Remove(const Key: TKey); -begin - FDictionaly.Remove( Key ); -end; - -procedure TThriftDictionaryImpl<TKey, TValue>.SetItem(const Key: TKey; - const Value: TValue); -begin - FDictionaly.AddOrSetValue( Key, Value); -end; - -function TThriftDictionaryImpl<TKey, TValue>.ToArray: TArray<TPair<TKey, TValue>>; -{$IF CompilerVersion < 22.0} -var - x : TPair<TKey, TValue>; - i : Integer; -{$IFEND} -begin -{$IF CompilerVersion < 22.0} - SetLength(Result, Count); - i := 0; - for x in FDictionaly do - begin - Result[i] := x; - Inc( i ); - end; -{$ELSE} - Result := FDictionaly.ToArray; -{$IFEND} -end; - -procedure TThriftDictionaryImpl<TKey, TValue>.TrimExcess; -begin - FDictionaly.TrimExcess; -end; - -function TThriftDictionaryImpl<TKey, TValue>.TryGetValue(const Key: TKey; - out Value: TValue): Boolean; -begin - Result := FDictionaly.TryGetValue( Key, Value); -end; - -{ TThriftListImpl<T> } - -function TThriftListImpl<T>.Add(const Value: T): Integer; -begin - Result := FList.Add( Value ); -end; - -procedure TThriftListImpl<T>.AddRange(Collection: TEnumerable<T>); -begin - FList.AddRange( Collection ); -end; - -procedure TThriftListImpl<T>.AddRange(const Collection: IEnumerable<T>); -begin - FList.AddRange( Collection ); -end; - -procedure TThriftListImpl<T>.AddRange(const Values: array of T); -begin - FList.AddRange( Values ); -end; - -function TThriftListImpl<T>.BinarySearch(const Item: T; - out Index: Integer): Boolean; -begin - Result := FList.BinarySearch( Item, Index); -end; - -function TThriftListImpl<T>.BinarySearch(const Item: T; out Index: Integer; - const AComparer: IComparer<T>): Boolean; -begin - Result := FList.BinarySearch( Item, Index, AComparer); -end; - -procedure TThriftListImpl<T>.Clear; -begin - FList.Clear; -end; - -function TThriftListImpl<T>.Contains(const Value: T): Boolean; -begin - Result := FList.Contains( Value ); -end; - -constructor TThriftListImpl<T>.Create; -begin - inherited; - FList := TList<T>.Create; -end; - -procedure TThriftListImpl<T>.Delete(Index: Integer); -begin - FList.Delete( Index ) -end; - -procedure TThriftListImpl<T>.DeleteRange(AIndex, ACount: Integer); -begin - FList.DeleteRange( AIndex, ACount) -end; - -destructor TThriftListImpl<T>.Destroy; -begin - FList.Free; - inherited; -end; - -{$IF CompilerVersion >= 21.0} -procedure TThriftListImpl<T>.Exchange(Index1, Index2: Integer); -begin - FList.Exchange( Index1, Index2 ) -end; -{$IFEND} - -function TThriftListImpl<T>.Extract(const Value: T): T; -begin - Result := FList.Extract( Value ) -end; - -{$IF CompilerVersion >= 21.0} -function TThriftListImpl<T>.First: T; -begin - Result := FList.First; -end; -{$IFEND} - -function TThriftListImpl<T>.GetCapacity: Integer; -begin - Result := FList.Capacity; -end; - -function TThriftListImpl<T>.GetCount: Integer; -begin - Result := FList.Count; -end; - -function TThriftListImpl<T>.GetEnumerator: TEnumerator<T>; -begin - Result := FList.GetEnumerator; -end; - -function TThriftListImpl<T>.GetItem(Index: Integer): T; -begin - Result := FList[Index]; -end; - -function TThriftListImpl<T>.IndexOf(const Value: T): Integer; -begin - Result := FList.IndexOf( Value ); -end; - -procedure TThriftListImpl<T>.Insert(Index: Integer; const Value: T); -begin - FList.Insert( Index, Value); -end; - -procedure TThriftListImpl<T>.InsertRange(Index: Integer; - const Collection: TEnumerable<T>); -begin - FList.InsertRange( Index, Collection ); -end; - -procedure TThriftListImpl<T>.InsertRange(Index: Integer; - const Values: array of T); -begin - FList.InsertRange( Index, Values); -end; - -procedure TThriftListImpl<T>.InsertRange(Index: Integer; - const Collection: IEnumerable<T>); -begin - FList.InsertRange( Index, Collection ); -end; - -{$IF CompilerVersion >= 21.0} -function TThriftListImpl<T>.Last: T; -begin - Result := FList.Last; -end; -{$IFEND} - -function TThriftListImpl<T>.LastIndexOf(const Value: T): Integer; -begin - Result := FList.LastIndexOf( Value ); -end; - -{$IF CompilerVersion >= 21.0} -procedure TThriftListImpl<T>.Move(CurIndex, NewIndex: Integer); -begin - FList.Move( CurIndex, NewIndex); -end; -{$IFEND} - -function TThriftListImpl<T>.Remove(const Value: T): Integer; -begin - Result := FList.Remove( Value ); -end; - -procedure TThriftListImpl<T>.Reverse; -begin - FList.Reverse; -end; - -procedure TThriftListImpl<T>.SetCapacity(Value: Integer); -begin - FList.Capacity := Value; -end; - -procedure TThriftListImpl<T>.SetCount(Value: Integer); -begin - FList.Count := Value; -end; - -procedure TThriftListImpl<T>.SetItem(Index: Integer; const Value: T); -begin - FList[Index] := Value; -end; - -procedure TThriftListImpl<T>.Sort; -begin - FList.Sort; -end; - -procedure TThriftListImpl<T>.Sort(const AComparer: IComparer<T>); -begin - FList.Sort; -end; - -function TThriftListImpl<T>.ToArray: TArray<T>; -{$IF CompilerVersion < 22.0} -var - x : T; - i : Integer; -{$IFEND} -begin -{$IF CompilerVersion < 22.0} - SetLength(Result, Count); - i := 0; - for x in FList do - begin - Result[i] := x; - Inc( i ); - end; -{$ELSE} - Result := FList.ToArray; -{$IFEND} -end; - -procedure TThriftListImpl<T>.TrimExcess; -begin - FList.TrimExcess; -end; - -end. +(* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *) + +unit Thrift.Collections; + +interface + +uses + Generics.Collections, Generics.Defaults, Thrift.Utils; + +type + +{$IF CompilerVersion < 21.0} + TArray<T> = array of T; +{$IFEND} + + IThriftContainer = interface + ['{93DEF5A0-D162-461A-AB22-5B4EE0734050}'] + function ToString: string; + end; + + IThriftDictionary<TKey,TValue> = interface(IThriftContainer) + ['{25EDD506-F9D1-4008-A40F-5940364B7E46}'] + function GetEnumerator: TEnumerator<TPair<TKey,TValue>>; + + function GetKeys: TDictionary<TKey,TValue>.TKeyCollection; + function GetValues: TDictionary<TKey,TValue>.TValueCollection; + function GetItem(const Key: TKey): TValue; + procedure SetItem(const Key: TKey; const Value: TValue); + function GetCount: Integer; + + procedure Add(const Key: TKey; const Value: TValue); + procedure Remove(const Key: TKey); +{$IF CompilerVersion >= 21.0} + function ExtractPair(const Key: TKey): TPair<TKey,TValue>; +{$IFEND} + procedure Clear; + procedure TrimExcess; + function TryGetValue(const Key: TKey; out Value: TValue): Boolean; + procedure AddOrSetValue(const Key: TKey; const Value: TValue); + function ContainsKey(const Key: TKey): Boolean; + function ContainsValue(const Value: TValue): Boolean; + function ToArray: TArray<TPair<TKey,TValue>>; + + property Items[const Key: TKey]: TValue read GetItem write SetItem; default; + property Count: Integer read GetCount; + property Keys: TDictionary<TKey,TValue>.TKeyCollection read GetKeys; + property Values: TDictionary<TKey,TValue>.TValueCollection read GetValues; + end; + + TThriftDictionaryImpl<TKey,TValue> = class( TInterfacedObject, IThriftDictionary<TKey,TValue>) + private + FDictionaly : TDictionary<TKey,TValue>; + protected + function GetEnumerator: TEnumerator<TPair<TKey,TValue>>; + + function GetKeys: TDictionary<TKey,TValue>.TKeyCollection; + function GetValues: TDictionary<TKey,TValue>.TValueCollection; + function GetItem(const Key: TKey): TValue; + procedure SetItem(const Key: TKey; const Value: TValue); + function GetCount: Integer; + + procedure Add(const Key: TKey; const Value: TValue); + procedure Remove(const Key: TKey); +{$IF CompilerVersion >= 21.0} + function ExtractPair(const Key: TKey): TPair<TKey,TValue>; +{$IFEND} + procedure Clear; + procedure TrimExcess; + function TryGetValue(const Key: TKey; out Value: TValue): Boolean; + procedure AddOrSetValue(const Key: TKey; const Value: TValue); + function ContainsKey(const Key: TKey): Boolean; + function ContainsValue(const Value: TValue): Boolean; + function ToArray: TArray<TPair<TKey,TValue>>; + property Items[const Key: TKey]: TValue read GetItem write SetItem; default; + property Count: Integer read GetCount; + property Keys: TDictionary<TKey,TValue>.TKeyCollection read GetKeys; + property Values: TDictionary<TKey,TValue>.TValueCollection read GetValues; + public + constructor Create(ACapacity: Integer = 0); + destructor Destroy; override; + end; + + IThriftList<T> = interface(IThriftContainer) + ['{29BEEE31-9CB4-401B-AA04-5148A75F473B}'] + function GetEnumerator: TEnumerator<T>; + function GetCapacity: Integer; + procedure SetCapacity(Value: Integer); + function GetCount: Integer; + procedure SetCount(Value: Integer); + function GetItem(Index: Integer): T; + procedure SetItem(Index: Integer; const Value: T); + function Add(const Value: T): Integer; + procedure AddRange(const Values: array of T); overload; + procedure AddRange(const Collection: IEnumerable<T>); overload; + procedure AddRange(Collection: TEnumerable<T>); overload; + procedure Insert(Index: Integer; const Value: T); + procedure InsertRange(Index: Integer; const Values: array of T); overload; + procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload; + procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload; + function Remove(const Value: T): Integer; + procedure Delete(Index: Integer); + procedure DeleteRange(AIndex, ACount: Integer); + function Extract(const Value: T): T; +{$IF CompilerVersion >= 21.0} + procedure Exchange(Index1, Index2: Integer); + procedure Move(CurIndex, NewIndex: Integer); + function First: T; + function Last: T; +{$IFEND} + procedure Clear; + function Contains(const Value: T): Boolean; + function IndexOf(const Value: T): Integer; + function LastIndexOf(const Value: T): Integer; + procedure Reverse; + procedure Sort; overload; + procedure Sort(const AComparer: IComparer<T>); overload; + function BinarySearch(const Item: T; out Index: Integer): Boolean; overload; + function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload; + procedure TrimExcess; + function ToArray: TArray<T>; + property Capacity: Integer read GetCapacity write SetCapacity; + property Count: Integer read GetCount write SetCount; + property Items[Index: Integer]: T read GetItem write SetItem; default; + end; + + TThriftListImpl<T> = class( TInterfacedObject, IThriftList<T>) + private + FList : TList<T>; + protected + function GetEnumerator: TEnumerator<T>; + function GetCapacity: Integer; + procedure SetCapacity(Value: Integer); + function GetCount: Integer; + procedure SetCount(Value: Integer); + function GetItem(Index: Integer): T; + procedure SetItem(Index: Integer; const Value: T); + function Add(const Value: T): Integer; + procedure AddRange(const Values: array of T); overload; + procedure AddRange(const Collection: IEnumerable<T>); overload; + procedure AddRange(Collection: TEnumerable<T>); overload; + procedure Insert(Index: Integer; const Value: T); + procedure InsertRange(Index: Integer; const Values: array of T); overload; + procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload; + procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload; + function Remove(const Value: T): Integer; + procedure Delete(Index: Integer); + procedure DeleteRange(AIndex, ACount: Integer); + function Extract(const Value: T): T; +{$IF CompilerVersion >= 21.0} + procedure Exchange(Index1, Index2: Integer); + procedure Move(CurIndex, NewIndex: Integer); + function First: T; + function Last: T; +{$IFEND} + procedure Clear; + function Contains(const Value: T): Boolean; + function IndexOf(const Value: T): Integer; + function LastIndexOf(const Value: T): Integer; + procedure Reverse; + procedure Sort; overload; + procedure Sort(const AComparer: IComparer<T>); overload; + function BinarySearch(const Item: T; out Index: Integer): Boolean; overload; + function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload; + procedure TrimExcess; + function ToArray: TArray<T>; + property Capacity: Integer read GetCapacity write SetCapacity; + property Count: Integer read GetCount write SetCount; + property Items[Index: Integer]: T read GetItem write SetItem; default; + public + constructor Create; + destructor Destroy; override; + end; + + IHashSet<TValue> = interface(IThriftContainer) + ['{0923A3B5-D4D4-48A8-91AD-40238E2EAD66}'] + function GetEnumerator: TEnumerator<TValue>; + function GetIsReadOnly: Boolean; + function GetCount: Integer; + property Count: Integer read GetCount; + property IsReadOnly: Boolean read GetIsReadOnly; + procedure Add( const item: TValue); + procedure Clear; + function Contains( const item: TValue): Boolean; + procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer); + function Remove( const item: TValue ): Boolean; + end; + + THashSetImpl<TValue> = class( TInterfacedObject, IHashSet<TValue>) + private + FDictionary : IThriftDictionary<TValue,Integer>; + FIsReadOnly: Boolean; + protected + function GetEnumerator: TEnumerator<TValue>; + function GetIsReadOnly: Boolean; + function GetCount: Integer; + property Count: Integer read GetCount; + property IsReadOnly: Boolean read FIsReadOnly; + procedure Add( const item: TValue); + procedure Clear; + function Contains( const item: TValue): Boolean; + procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer); + function Remove( const item: TValue ): Boolean; + public + constructor Create; + end; + +implementation + +{ THashSetImpl<TValue> } + +procedure THashSetImpl<TValue>.Add( const item: TValue); +begin + if not FDictionary.ContainsKey(item) then + begin + FDictionary.Add( item, 0); + end; +end; + +procedure THashSetImpl<TValue>.Clear; +begin + FDictionary.Clear; +end; + +function THashSetImpl<TValue>.Contains( const item: TValue): Boolean; +begin + Result := FDictionary.ContainsKey(item); +end; + +procedure THashSetImpl<TValue>.CopyTo(var A: TArray<TValue>; arrayIndex: Integer); +var + i : Integer; + Enumlator : TEnumerator<TValue>; +begin + Enumlator := GetEnumerator; + while Enumlator.MoveNext do + begin + A[arrayIndex] := Enumlator.Current; + Inc(arrayIndex); + end; +end; + +constructor THashSetImpl<TValue>.Create; +begin + inherited; + FDictionary := TThriftDictionaryImpl<TValue,Integer>.Create; +end; + +function THashSetImpl<TValue>.GetCount: Integer; +begin + Result := FDictionary.Count; +end; + +function THashSetImpl<TValue>.GetEnumerator: TEnumerator<TValue>; +begin + Result := FDictionary.Keys.GetEnumerator; +end; + +function THashSetImpl<TValue>.GetIsReadOnly: Boolean; +begin + Result := FIsReadOnly; +end; + +function THashSetImpl<TValue>.Remove( const item: TValue): Boolean; +begin + Result := False; + if FDictionary.ContainsKey( item ) then + begin + FDictionary.Remove( item ); + Result := not FDictionary.ContainsKey( item ); + end; +end; + +{ TThriftDictionaryImpl<TKey, TValue> } + +procedure TThriftDictionaryImpl<TKey, TValue>.Add(const Key: TKey; + const Value: TValue); +begin + FDictionaly.Add( Key, Value); +end; + +procedure TThriftDictionaryImpl<TKey, TValue>.AddOrSetValue(const Key: TKey; + const Value: TValue); +begin + FDictionaly.AddOrSetValue( Key, Value); +end; + +procedure TThriftDictionaryImpl<TKey, TValue>.Clear; +begin + FDictionaly.Clear; +end; + +function TThriftDictionaryImpl<TKey, TValue>.ContainsKey( + const Key: TKey): Boolean; +begin + Result := FDictionaly.ContainsKey( Key ); +end; + +function TThriftDictionaryImpl<TKey, TValue>.ContainsValue( + const Value: TValue): Boolean; +begin + Result := FDictionaly.ContainsValue( Value ); +end; + +constructor TThriftDictionaryImpl<TKey, TValue>.Create(ACapacity: Integer); +begin + inherited Create; + FDictionaly := TDictionary<TKey,TValue>.Create( ACapacity ); +end; + +destructor TThriftDictionaryImpl<TKey, TValue>.Destroy; +begin + FDictionaly.Free; + inherited; +end; + +{$IF CompilerVersion >= 21.0} +function TThriftDictionaryImpl<TKey, TValue>.ExtractPair( const Key: TKey): TPair<TKey, TValue>; +begin + Result := FDictionaly.ExtractPair( Key); +end; +{$IFEND} + +function TThriftDictionaryImpl<TKey, TValue>.GetCount: Integer; +begin + Result := FDictionaly.Count; +end; + +function TThriftDictionaryImpl<TKey, TValue>.GetEnumerator: TEnumerator<TPair<TKey, TValue>>; +begin + Result := FDictionaly.GetEnumerator; +end; + +function TThriftDictionaryImpl<TKey, TValue>.GetItem(const Key: TKey): TValue; +begin + Result := FDictionaly.Items[Key]; +end; + +function TThriftDictionaryImpl<TKey, TValue>.GetKeys: TDictionary<TKey, TValue>.TKeyCollection; +begin + Result := FDictionaly.Keys; +end; + +function TThriftDictionaryImpl<TKey, TValue>.GetValues: TDictionary<TKey, TValue>.TValueCollection; +begin + Result := FDictionaly.Values; +end; + +procedure TThriftDictionaryImpl<TKey, TValue>.Remove(const Key: TKey); +begin + FDictionaly.Remove( Key ); +end; + +procedure TThriftDictionaryImpl<TKey, TValue>.SetItem(const Key: TKey; + const Value: TValue); +begin + FDictionaly.AddOrSetValue( Key, Value); +end; + +function TThriftDictionaryImpl<TKey, TValue>.ToArray: TArray<TPair<TKey, TValue>>; +{$IF CompilerVersion < 22.0} +var + x : TPair<TKey, TValue>; + i : Integer; +{$IFEND} +begin +{$IF CompilerVersion < 22.0} + SetLength(Result, Count); + i := 0; + for x in FDictionaly do + begin + Result[i] := x; + Inc( i ); + end; +{$ELSE} + Result := FDictionaly.ToArray; +{$IFEND} +end; + +procedure TThriftDictionaryImpl<TKey, TValue>.TrimExcess; +begin + FDictionaly.TrimExcess; +end; + +function TThriftDictionaryImpl<TKey, TValue>.TryGetValue(const Key: TKey; + out Value: TValue): Boolean; +begin + Result := FDictionaly.TryGetValue( Key, Value); +end; + +{ TThriftListImpl<T> } + +function TThriftListImpl<T>.Add(const Value: T): Integer; +begin + Result := FList.Add( Value ); +end; + +procedure TThriftListImpl<T>.AddRange(Collection: TEnumerable<T>); +begin + FList.AddRange( Collection ); +end; + +procedure TThriftListImpl<T>.AddRange(const Collection: IEnumerable<T>); +begin + FList.AddRange( Collection ); +end; + +procedure TThriftListImpl<T>.AddRange(const Values: array of T); +begin + FList.AddRange( Values ); +end; + +function TThriftListImpl<T>.BinarySearch(const Item: T; + out Index: Integer): Boolean; +begin + Result := FList.BinarySearch( Item, Index); +end; + +function TThriftListImpl<T>.BinarySearch(const Item: T; out Index: Integer; + const AComparer: IComparer<T>): Boolean; +begin + Result := FList.BinarySearch( Item, Index, AComparer); +end; + +procedure TThriftListImpl<T>.Clear; +begin + FList.Clear; +end; + +function TThriftListImpl<T>.Contains(const Value: T): Boolean; +begin + Result := FList.Contains( Value ); +end; + +constructor TThriftListImpl<T>.Create; +begin + inherited; + FList := TList<T>.Create; +end; + +procedure TThriftListImpl<T>.Delete(Index: Integer); +begin + FList.Delete( Index ) +end; + +procedure TThriftListImpl<T>.DeleteRange(AIndex, ACount: Integer); +begin + FList.DeleteRange( AIndex, ACount) +end; + +destructor TThriftListImpl<T>.Destroy; +begin + FList.Free; + inherited; +end; + +{$IF CompilerVersion >= 21.0} +procedure TThriftListImpl<T>.Exchange(Index1, Index2: Integer); +begin + FList.Exchange( Index1, Index2 ) +end; +{$IFEND} + +function TThriftListImpl<T>.Extract(const Value: T): T; +begin + Result := FList.Extract( Value ) +end; + +{$IF CompilerVersion >= 21.0} +function TThriftListImpl<T>.First: T; +begin + Result := FList.First; +end; +{$IFEND} + +function TThriftListImpl<T>.GetCapacity: Integer; +begin + Result := FList.Capacity; +end; + +function TThriftListImpl<T>.GetCount: Integer; +begin + Result := FList.Count; +end; + +function TThriftListImpl<T>.GetEnumerator: TEnumerator<T>; +begin + Result := FList.GetEnumerator; +end; + +function TThriftListImpl<T>.GetItem(Index: Integer): T; +begin + Result := FList[Index]; +end; + +function TThriftListImpl<T>.IndexOf(const Value: T): Integer; +begin + Result := FList.IndexOf( Value ); +end; + +procedure TThriftListImpl<T>.Insert(Index: Integer; const Value: T); +begin + FList.Insert( Index, Value); +end; + +procedure TThriftListImpl<T>.InsertRange(Index: Integer; + const Collection: TEnumerable<T>); +begin + FList.InsertRange( Index, Collection ); +end; + +procedure TThriftListImpl<T>.InsertRange(Index: Integer; + const Values: array of T); +begin + FList.InsertRange( Index, Values); +end; + +procedure TThriftListImpl<T>.InsertRange(Index: Integer; + const Collection: IEnumerable<T>); +begin + FList.InsertRange( Index, Collection ); +end; + +{$IF CompilerVersion >= 21.0} +function TThriftListImpl<T>.Last: T; +begin + Result := FList.Last; +end; +{$IFEND} + +function TThriftListImpl<T>.LastIndexOf(const Value: T): Integer; +begin + Result := FList.LastIndexOf( Value ); +end; + +{$IF CompilerVersion >= 21.0} +procedure TThriftListImpl<T>.Move(CurIndex, NewIndex: Integer); +begin + FList.Move( CurIndex, NewIndex); +end; +{$IFEND} + +function TThriftListImpl<T>.Remove(const Value: T): Integer; +begin + Result := FList.Remove( Value ); +end; + +procedure TThriftListImpl<T>.Reverse; +begin + FList.Reverse; +end; + +procedure TThriftListImpl<T>.SetCapacity(Value: Integer); +begin + FList.Capacity := Value; +end; + +procedure TThriftListImpl<T>.SetCount(Value: Integer); +begin + FList.Count := Value; +end; + +procedure TThriftListImpl<T>.SetItem(Index: Integer; const Value: T); +begin + FList[Index] := Value; +end; + +procedure TThriftListImpl<T>.Sort; +begin + FList.Sort; +end; + +procedure TThriftListImpl<T>.Sort(const AComparer: IComparer<T>); +begin + FList.Sort; +end; + +function TThriftListImpl<T>.ToArray: TArray<T>; +{$IF CompilerVersion < 22.0} +var + x : T; + i : Integer; +{$IFEND} +begin +{$IF CompilerVersion < 22.0} + SetLength(Result, Count); + i := 0; + for x in FList do + begin + Result[i] := x; + Inc( i ); + end; +{$ELSE} + Result := FList.ToArray; +{$IFEND} +end; + +procedure TThriftListImpl<T>.TrimExcess; +begin + FList.TrimExcess; +end; + +end. http://git-wip-us.apache.org/repos/asf/thrift/blob/d5436f5c/lib/delphi/src/Thrift.Console.pas ---------------------------------------------------------------------- diff --git a/lib/delphi/src/Thrift.Console.pas b/lib/delphi/src/Thrift.Console.pas index 5d1345b..a52eeb9 100644 --- a/lib/delphi/src/Thrift.Console.pas +++ b/lib/delphi/src/Thrift.Console.pas @@ -1,133 +1,133 @@ -(* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *) - -unit Thrift.Console; - -interface - -uses - StdCtrls; - -type - TThriftConsole = class - public - procedure Write( const S: string); virtual; - procedure WriteLine( const S: string); virtual; - end; - - TGUIConsole = class( TThriftConsole ) - private - FLineBreak : Boolean; - FMemo : TMemo; - - procedure InternalWrite( const S: string; bWriteLine: Boolean); - public - procedure Write( const S: string); override; - procedure WriteLine( const S: string); override; - constructor Create( AMemo: TMemo); - end; - -function Console: TThriftConsole; -procedure ChangeConsole( AConsole: TThriftConsole ); -procedure RestoreConsoleToDefault; - -implementation - -var - FDefaultConsole : TThriftConsole; - FConsole : TThriftConsole; - -function Console: TThriftConsole; -begin - Result := FConsole; -end; - -{ TThriftConsole } - -procedure TThriftConsole.Write(const S: string); -begin - System.Write( S ); -end; - -procedure TThriftConsole.WriteLine(const S: string); -begin - System.Writeln( S ); -end; - -procedure ChangeConsole( AConsole: TThriftConsole ); -begin - FConsole := AConsole; -end; - -procedure RestoreConsoleToDefault; -begin - FConsole := FDefaultConsole; -end; - -{ TGUIConsole } - -constructor TGUIConsole.Create( AMemo: TMemo); -begin - inherited Create; - FMemo := AMemo; - FLineBreak := True; -end; - -procedure TGUIConsole.InternalWrite(const S: string; bWriteLine: Boolean); -var - idx : Integer; -begin - if FLineBreak then - begin - FMemo.Lines.Add( S ); - end else - begin - idx := FMemo.Lines.Count - 1; - if idx < 0 then - begin - FMemo.Lines.Add( S ); - end; - FMemo.Lines[idx] := FMemo.Lines[idx] + S; - end; - FLineBreak := bWriteLine; -end; - -procedure TGUIConsole.Write(const S: string); -begin - InternalWrite( S, False); -end; - -procedure TGUIConsole.WriteLine(const S: string); -begin - InternalWrite( S, True); -end; - -initialization -begin - FDefaultConsole := TThriftConsole.Create; - FConsole := FDefaultConsole; -end; - -finalization -begin - FDefaultConsole.Free; -end; - -end. - +(* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *) + +unit Thrift.Console; + +interface + +uses + StdCtrls; + +type + TThriftConsole = class + public + procedure Write( const S: string); virtual; + procedure WriteLine( const S: string); virtual; + end; + + TGUIConsole = class( TThriftConsole ) + private + FLineBreak : Boolean; + FMemo : TMemo; + + procedure InternalWrite( const S: string; bWriteLine: Boolean); + public + procedure Write( const S: string); override; + procedure WriteLine( const S: string); override; + constructor Create( AMemo: TMemo); + end; + +function Console: TThriftConsole; +procedure ChangeConsole( AConsole: TThriftConsole ); +procedure RestoreConsoleToDefault; + +implementation + +var + FDefaultConsole : TThriftConsole; + FConsole : TThriftConsole; + +function Console: TThriftConsole; +begin + Result := FConsole; +end; + +{ TThriftConsole } + +procedure TThriftConsole.Write(const S: string); +begin + System.Write( S ); +end; + +procedure TThriftConsole.WriteLine(const S: string); +begin + System.Writeln( S ); +end; + +procedure ChangeConsole( AConsole: TThriftConsole ); +begin + FConsole := AConsole; +end; + +procedure RestoreConsoleToDefault; +begin + FConsole := FDefaultConsole; +end; + +{ TGUIConsole } + +constructor TGUIConsole.Create( AMemo: TMemo); +begin + inherited Create; + FMemo := AMemo; + FLineBreak := True; +end; + +procedure TGUIConsole.InternalWrite(const S: string; bWriteLine: Boolean); +var + idx : Integer; +begin + if FLineBreak then + begin + FMemo.Lines.Add( S ); + end else + begin + idx := FMemo.Lines.Count - 1; + if idx < 0 then + begin + FMemo.Lines.Add( S ); + end; + FMemo.Lines[idx] := FMemo.Lines[idx] + S; + end; + FLineBreak := bWriteLine; +end; + +procedure TGUIConsole.Write(const S: string); +begin + InternalWrite( S, False); +end; + +procedure TGUIConsole.WriteLine(const S: string); +begin + InternalWrite( S, True); +end; + +initialization +begin + FDefaultConsole := TThriftConsole.Create; + FConsole := FDefaultConsole; +end; + +finalization +begin + FDefaultConsole.Free; +end; + +end. + http://git-wip-us.apache.org/repos/asf/thrift/blob/d5436f5c/lib/delphi/src/Thrift.Processor.Multiplex.pas ---------------------------------------------------------------------- diff --git a/lib/delphi/src/Thrift.Processor.Multiplex.pas b/lib/delphi/src/Thrift.Processor.Multiplex.pas index 198bed8..f6d9446 100644 --- a/lib/delphi/src/Thrift.Processor.Multiplex.pas +++ b/lib/delphi/src/Thrift.Processor.Multiplex.pas @@ -1,22 +1,22 @@ (* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *) - + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *) + unit Thrift.Processor.Multiplex; @@ -53,7 +53,7 @@ uses type IMultiplexedProcessor = interface( IProcessor) - ['{810FF32D-22A2-4D58-B129-B0590703ECEC}'] + ['{810FF32D-22A2-4D58-B129-B0590703ECEC}'] // Register a service with this TMultiplexedProcessor. This allows us // to broker requests to individual services by using the service name // to select them at request time. @@ -150,14 +150,14 @@ begin appex := TApplicationException.Create( extype, etxt); try newMsg := TMessageImpl.Create( msg.Name, TMessageType.Exception, msg.SeqID); - + oprot.WriteMessageBegin(newMsg); appex.Write(oprot); oprot.WriteMessageEnd(); oprot.Transport.Flush(); finally - appex.Free; + appex.Free; end; end; @@ -180,9 +180,9 @@ begin Error( oprot, msg, TApplicationException.TExceptionType.InvalidMessageType, ERROR_INVALID_MSGTYPE); - Exit( FALSE); - end; - + Exit( FALSE); + end; + // Extract the service name idx := Pos( TMultiplexedProtocol.SEPARATOR, msg.Name); if idx < 1 then begin @@ -190,7 +190,7 @@ begin TApplicationException.TExceptionType.InvalidProtocol, Format(ERROR_INCOMPATIBLE_PROT,[msg.Name])); Exit( FALSE); - end; + end; // Create a new TMessage, something that can be consumed by any TProtocol sService := Copy( msg.Name, 1, idx-1); @@ -200,7 +200,7 @@ begin TApplicationException.TExceptionType.InternalError, Format(ERROR_UNKNOWN_SERVICE,[sService])); Exit( FALSE); - end; + end; // Create a new TMessage, removing the service name Inc( idx, Length(TMultiplexedProtocol.SEPARATOR)); http://git-wip-us.apache.org/repos/asf/thrift/blob/d5436f5c/lib/delphi/src/Thrift.Protocol.Multiplex.pas ---------------------------------------------------------------------- diff --git a/lib/delphi/src/Thrift.Protocol.Multiplex.pas b/lib/delphi/src/Thrift.Protocol.Multiplex.pas index 2cd2401..18b39b5 100644 --- a/lib/delphi/src/Thrift.Protocol.Multiplex.pas +++ b/lib/delphi/src/Thrift.Protocol.Multiplex.pas @@ -1,22 +1,22 @@ (* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - *) - + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *) + unit Thrift.Protocol.Multiplex; interface
