You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by jf...@apache.org on 2011/10/18 16:35:26 UTC

svn commit: r1185688 [3/4] - in /thrift/trunk: ./ compiler/cpp/ compiler/cpp/src/ compiler/cpp/src/generate/ lib/delphi/ lib/delphi/src/ lib/delphi/test/ test/

Modified: thrift/trunk/compiler/cpp/src/main.cc
URL: http://svn.apache.org/viewvc/thrift/trunk/compiler/cpp/src/main.cc?rev=1185688&r1=1185687&r2=1185688&view=diff
==============================================================================
--- thrift/trunk/compiler/cpp/src/main.cc (original)
+++ thrift/trunk/compiler/cpp/src/main.cc Tue Oct 18 14:35:26 2011
@@ -184,6 +184,7 @@ bool gen_ocaml = false;
 bool gen_hs = false;
 bool gen_cocoa = false;
 bool gen_csharp = false;
+bool gen_delphi = false;
 bool gen_st = false;
 bool gen_recurse = false;
 
@@ -1045,6 +1046,8 @@ int main(int argc, char** argv) {
         gen_st = true;
       } else if (strcmp(arg, "-csharp") == 0) {
         gen_csharp = true;
+      } else if (strcmp(arg, "-delphi") == 0) {
+        gen_delphi = true;
       } else if (strcmp(arg, "-cpp_use_include_prefix") == 0) {
         g_cpp_use_include_prefix = true;
       } else if (strcmp(arg, "-I") == 0) {
@@ -1124,6 +1127,10 @@ int main(int argc, char** argv) {
     pwarning(1, "-csharp is deprecated.  Use --gen csharp");
     generator_strings.push_back("csharp");
   }
+  if (gen_delphi) {
+    pwarning(1, "-delphi is deprecated.  Use --gen delphi");
+    generator_strings.push_back("delphi");
+  }
   if (gen_py) {
     pwarning(1, "-py is deprecated.  Use --gen py");
     generator_strings.push_back("py");

Modified: thrift/trunk/compiler/cpp/src/thriftl.ll
URL: http://svn.apache.org/viewvc/thrift/trunk/compiler/cpp/src/thriftl.ll?rev=1185688&r1=1185687&r2=1185688&view=diff
==============================================================================
--- thrift/trunk/compiler/cpp/src/thriftl.ll (original)
+++ thrift/trunk/compiler/cpp/src/thriftl.ll Tue Oct 18 14:35:26 2011
@@ -120,6 +120,7 @@ literal_begin (['\"])
 "java_package"       { return tok_java_package;         }
 "cocoa_prefix"       { return tok_cocoa_prefix;         }
 "csharp_namespace"   { return tok_csharp_namespace;     }
+"delphi_namespace"   { return tok_delphi_namespace;     }
 "php_namespace"      { return tok_php_namespace;        }
 "py_module"          { return tok_py_module;            }
 "perl_package"       { return tok_perl_package;         }

Modified: thrift/trunk/compiler/cpp/src/thrifty.yy
URL: http://svn.apache.org/viewvc/thrift/trunk/compiler/cpp/src/thrifty.yy?rev=1185688&r1=1185687&r2=1185688&view=diff
==============================================================================
--- thrift/trunk/compiler/cpp/src/thrifty.yy (original)
+++ thrift/trunk/compiler/cpp/src/thrifty.yy Tue Oct 18 14:35:26 2011
@@ -110,6 +110,7 @@ const int struct_is_union = 1;
 %token tok_smalltalk_prefix
 %token tok_cocoa_prefix
 %token tok_csharp_namespace
+%token tok_delphi_namespace
 
 /**
  * Base datatype keywords
@@ -393,6 +394,15 @@ Header:
        g_program->set_namespace("csharp", $2);
      }
    }
+/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
+| tok_delphi_namespace tok_identifier
+   {
+     pwarning(1, "'delphi_namespace' is deprecated. Use 'namespace delphi' instead");
+     pdebug("Header -> tok_delphi_namespace tok_identifier");
+     if (g_parse_mode == PROGRAM) {
+       g_program->set_namespace("delphi", $2);
+     }
+   }
 
 Include:
   tok_include tok_literal

Modified: thrift/trunk/configure.ac
URL: http://svn.apache.org/viewvc/thrift/trunk/configure.ac?rev=1185688&r1=1185687&r2=1185688&view=diff
==============================================================================
--- thrift/trunk/configure.ac (original)
+++ thrift/trunk/configure.ac Tue Oct 18 14:35:26 2011
@@ -528,7 +528,6 @@ fi
 if test "$have_ruby" = "yes" ; then
   echo
   echo "Using Ruby ................... : $RUBY"
-  echo "Using rspec .................. : $RSPEC"
 fi
 if test "$have_haskell" = "yes" ; then
   echo

Added: thrift/trunk/lib/delphi/src/Thrift.Collections.pas
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/delphi/src/Thrift.Collections.pas?rev=1185688&view=auto
==============================================================================
--- thrift/trunk/lib/delphi/src/Thrift.Collections.pas (added)
+++ thrift/trunk/lib/delphi/src/Thrift.Collections.pas Tue Oct 18 14:35:26 2011
@@ -0,0 +1,618 @@
+(*
+ * 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( item: TValue);
+    procedure Clear;
+    function Contains( item: TValue): Boolean;
+    procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
+    function Remove( 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( item: TValue);
+    procedure Clear;
+    function Contains( item: TValue): Boolean;
+    procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
+    function Remove( item: TValue ): Boolean;
+  public
+    constructor Create;
+  end;
+
+implementation
+
+{ THashSetImpl<TValue> }
+
+procedure THashSetImpl<TValue>.Add(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(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(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
+  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
+  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.

Added: thrift/trunk/lib/delphi/src/Thrift.Console.pas
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/delphi/src/Thrift.Console.pas?rev=1185688&view=auto
==============================================================================
--- thrift/trunk/lib/delphi/src/Thrift.Console.pas (added)
+++ thrift/trunk/lib/delphi/src/Thrift.Console.pas Tue Oct 18 14:35:26 2011
@@ -0,0 +1,132 @@
+(*
+ * 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
+  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.
+

Added: thrift/trunk/lib/delphi/src/Thrift.Protocol.pas
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/delphi/src/Thrift.Protocol.pas?rev=1185688&view=auto
==============================================================================
--- thrift/trunk/lib/delphi/src/Thrift.Protocol.pas (added)
+++ thrift/trunk/lib/delphi/src/Thrift.Protocol.pas Tue Oct 18 14:35:26 2011
@@ -0,0 +1,1178 @@
+(*
+ * 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.
+ *)
+
+{$SCOPEDENUMS ON}
+
+unit Thrift.Protocol;
+
+interface
+
+uses
+  Classes,
+  SysUtils,
+  Contnrs,
+  Thrift.Stream,
+  Thrift.Collections,
+  Thrift.Transport;
+
+type
+
+  TType = (
+    Stop = 0,
+    Void = 1,
+    Bool_ = 2,
+    Byte_ = 3,
+    Double_ = 4,
+    I16 = 6,
+    I32 = 8,
+    I64 = 10,
+    String_ = 11,
+    Struct = 12,
+    Map = 13,
+    Set_ = 14,
+    List = 15
+  );
+
+  TMessageType = (
+    Call = 1,
+    Reply = 2,
+    Exception = 3,
+    Oneway = 4
+  );
+
+  IProtocol = interface;
+  IStruct = interface;
+
+  IProtocolFactory = interface
+    ['{7CD64A10-4E9F-4E99-93BF-708A31F4A67B}']
+    function GetProtocol( trans: ITransport): IProtocol;
+  end;
+
+  TThriftStringBuilder = class( TStringBuilder)
+  public
+    function Append(const Value: TBytes): TStringBuilder; overload;
+    function Append(const Value: IThriftContainer): TStringBuilder; overload;
+  end;
+
+  TProtocolException = class( Exception )
+  public
+    const
+      UNKNOWN : Integer = 0;
+      INVALID_DATA : Integer = 1;
+      NEGATIVE_SIZE : Integer = 2;
+      SIZE_LIMIT : Integer = 3;
+      BAD_VERSION : Integer = 4;
+      NOT_IMPLEMENTED : Integer = 5;
+  protected
+    FType : Integer;
+  public
+    constructor Create; overload;
+    constructor Create( type_: Integer ); overload;
+    constructor Create( type_: Integer; const msg: string); overload;
+  end;
+
+  IMap = interface
+    ['{30531D97-7E06-4233-B800-C3F53CCD23E7}']
+    function GetKeyType: TType;
+    procedure SetKeyType( Value: TType);
+    function GetValueType: TType;
+    procedure SetValueType( Value: TType);
+    function GetCount: Integer;
+    procedure SetCount( Value: Integer);
+    property KeyType: TType read GetKeyType write SetKeyType;
+    property ValueType: TType read GetValueType write SetValueType;
+    property Count: Integer read GetCount write SetCount;
+  end;
+
+  TMapImpl = class( TInterfacedObject, IMap)
+  private
+    FValueType: TType;
+    FKeyType: TType;
+    FCount: Integer;
+  protected
+    function GetKeyType: TType;
+    procedure SetKeyType( Value: TType);
+    function GetValueType: TType;
+    procedure SetValueType( Value: TType);
+    function GetCount: Integer;
+    procedure SetCount( Value: Integer);
+  public
+    constructor Create( AValueType: TType; AKeyType: TType; ACount: Integer); overload;
+    constructor Create; overload;
+  end;
+
+  IList = interface
+    ['{6763E1EA-A934-4472-904F-0083980B9B87}']
+    function GetElementType: TType;
+    procedure SetElementType( Value: TType);
+    function GetCount: Integer;
+    procedure SetCount( Value: Integer);
+    property ElementType: TType read GetElementType write SetElementType;
+    property Count: Integer read GetCount write SetCount;
+  end;
+
+  TListImpl = class( TInterfacedObject, IList)
+  private
+    FElementType: TType;
+    FCount : Integer;
+  protected
+    function GetElementType: TType;
+    procedure SetElementType( Value: TType);
+    function GetCount: Integer;
+    procedure SetCount( Value: Integer);
+  public
+    constructor Create( AElementType: TType; ACount: Integer); overload;
+    constructor Create; overload;
+  end;
+
+  ISet = interface
+    ['{A8671700-7514-4C1E-8A05-62786872005F}']
+    function GetElementType: TType;
+    procedure SetElementType( Value: TType);
+    function GetCount: Integer;
+    procedure SetCount( Value: Integer);
+    property ElementType: TType read GetElementType write SetElementType;
+    property Count: Integer read GetCount write SetCount;
+  end;
+
+  TSetImpl = class( TInterfacedObject, ISet)
+  private
+    FCount: Integer;
+    FElementType: TType;
+  protected
+    function GetElementType: TType;
+    procedure SetElementType( Value: TType);
+    function GetCount: Integer;
+    procedure SetCount( Value: Integer);
+  public
+    constructor Create( AElementType: TType; ACount: Integer); overload;
+    constructor Create; overload;
+  end;
+
+  IMessage = interface
+    ['{9E368B4A-B1FA-43E7-8CF5-56C66D256CA7}']
+    function GetName: string;
+    procedure SetName( const Value: string);
+    function GetType: TMessageType;
+    procedure SetType( Value: TMessageType);
+    function GetSeqID: Integer;
+    procedure SetSeqID( Value: Integer);
+    property Name: string read GetName write SetName;
+    property Type_: TMessageType read GetType write SetType;
+    property SeqID: Integer read GetSeqID write SetSeqID;
+  end;
+
+  TMessageImpl = class( TInterfacedObject, IMessage )
+  private
+    FName: string;
+    FMessageType: TMessageType;
+    FSeqID: Integer;
+  protected
+    function GetName: string;
+    procedure SetName( const Value: string);
+    function GetType: TMessageType;
+    procedure SetType( Value: TMessageType);
+    function GetSeqID: Integer;
+    procedure SetSeqID( Value: Integer);
+  public
+    property Name: string read FName write FName;
+    property Type_: TMessageType read FMessageType write FMessageType;
+    property SeqID: Integer read FSeqID write FSeqID;
+    constructor Create( AName: string; AMessageType: TMessageType; ASeqID: Integer); overload;
+    constructor Create; overload;
+  end;
+
+  IField = interface
+    ['{F0D43BE5-7883-442E-83FF-0580CC632B72}']
+    function GetName: string;
+    procedure SetName( const Value: string);
+    function GetType: TType;
+    procedure SetType( Value: TType);
+    function GetId: SmallInt;
+    procedure SetId( Value: SmallInt);
+    property Name: string read GetName write SetName;
+    property Type_: TType read GetType write SetType;
+    property Id: SmallInt read GetId write SetId;
+  end;
+
+  TFieldImpl = class( TInterfacedObject, IField)
+  private
+    FName : string;
+    FType : TType;
+    FId   : SmallInt;
+  protected
+    function GetName: string;
+    procedure SetName( const Value: string);
+    function GetType: TType;
+    procedure SetType( Value: TType);
+    function GetId: SmallInt;
+    procedure SetId( Value: SmallInt);
+  public
+    constructor Create( const AName: string; const AType: TType; AId: SmallInt); overload;
+    constructor Create; overload;
+  end;
+
+  TProtocolUtil = class
+  public
+    class procedure Skip( prot: IProtocol; type_: TType);
+  end;
+
+  IProtocol = interface
+    ['{FD95C151-1527-4C96-8134-B902BFC4B4FC}']
+    function GetTransport: ITransport;
+    procedure WriteMessageBegin( message: IMessage);
+    procedure WriteMessageEnd;
+    procedure WriteStructBegin(struc: IStruct);
+    procedure WriteStructEnd;
+    procedure WriteFieldBegin(field: IField);
+    procedure WriteFieldEnd;
+    procedure WriteFieldStop;
+    procedure WriteMapBegin(map: IMap);
+    procedure WriteMapEnd;
+    procedure WriteListBegin( list: IList);
+    procedure WriteListEnd();
+    procedure WriteSetBegin( set_: ISet );
+    procedure WriteSetEnd();
+    procedure WriteBool( b: Boolean);
+    procedure WriteByte( b: ShortInt);
+    procedure WriteI16( i16: SmallInt);
+    procedure WriteI32( i32: Integer);
+    procedure WriteI64( i64: Int64);
+    procedure WriteDouble( d: Double);
+    procedure WriteString( const s: string );
+    procedure WriteAnsiString( const s: AnsiString);
+    procedure WriteBinary( const b: TBytes);
+
+    function ReadMessageBegin: IMessage;
+    procedure ReadMessageEnd();
+    function ReadStructBegin: IStruct;
+    procedure ReadStructEnd;
+    function ReadFieldBegin: IField;
+    procedure ReadFieldEnd();
+    function ReadMapBegin: IMap;
+    procedure ReadMapEnd();
+    function ReadListBegin: IList;
+    procedure ReadListEnd();
+    function ReadSetBegin: ISet;
+    procedure ReadSetEnd();
+    function ReadBool: Boolean;
+    function ReadByte: ShortInt;
+    function ReadI16: SmallInt;
+    function ReadI32: Integer;
+    function ReadI64: Int64;
+    function ReadDouble:Double;
+    function ReadBinary: TBytes;
+    function ReadString: string;
+    function ReadAnsiString: AnsiString;
+    property Transport: ITransport read GetTransport;
+  end;
+
+  TProtocolImpl = class abstract( TInterfacedObject, IProtocol)
+  protected
+    FTrans : ITransport;
+    function GetTransport: ITransport;
+  public
+    procedure WriteMessageBegin( message: IMessage); virtual; abstract;
+    procedure WriteMessageEnd; virtual; abstract;
+    procedure WriteStructBegin(struc: IStruct); virtual; abstract;
+    procedure WriteStructEnd; virtual; abstract;
+    procedure WriteFieldBegin(field: IField); virtual; abstract;
+    procedure WriteFieldEnd; virtual; abstract;
+    procedure WriteFieldStop; virtual; abstract;
+    procedure WriteMapBegin(map: IMap); virtual; abstract;
+    procedure WriteMapEnd; virtual; abstract;
+    procedure WriteListBegin( list: IList); virtual; abstract;
+    procedure WriteListEnd(); virtual; abstract;
+    procedure WriteSetBegin( set_: ISet ); virtual; abstract;
+    procedure WriteSetEnd(); virtual; abstract;
+    procedure WriteBool( b: Boolean); virtual; abstract;
+    procedure WriteByte( b: ShortInt); virtual; abstract;
+    procedure WriteI16( i16: SmallInt); virtual; abstract;
+    procedure WriteI32( i32: Integer); virtual; abstract;
+    procedure WriteI64( i64: Int64); virtual; abstract;
+    procedure WriteDouble( d: Double); virtual; abstract;
+    procedure WriteString( const s: string ); virtual;
+    procedure WriteAnsiString( const s: AnsiString); virtual;
+    procedure WriteBinary( const b: TBytes); virtual; abstract;
+
+    function ReadMessageBegin: IMessage; virtual; abstract;
+    procedure ReadMessageEnd(); virtual; abstract;
+    function ReadStructBegin: IStruct; virtual; abstract;
+    procedure ReadStructEnd; virtual; abstract;
+    function ReadFieldBegin: IField; virtual; abstract;
+    procedure ReadFieldEnd(); virtual; abstract;
+    function ReadMapBegin: IMap; virtual; abstract;
+    procedure ReadMapEnd(); virtual; abstract;
+    function ReadListBegin: IList; virtual; abstract;
+    procedure ReadListEnd(); virtual; abstract;
+    function ReadSetBegin: ISet; virtual; abstract;
+    procedure ReadSetEnd(); virtual; abstract;
+    function ReadBool: Boolean; virtual; abstract;
+    function ReadByte: ShortInt; virtual; abstract;
+    function ReadI16: SmallInt; virtual; abstract;
+    function ReadI32: Integer; virtual; abstract;
+    function ReadI64: Int64; virtual; abstract;
+    function ReadDouble:Double; virtual; abstract;
+    function ReadBinary: TBytes; virtual; abstract;
+    function ReadString: string; virtual;
+    function ReadAnsiString: AnsiString; virtual;
+
+    property Transport: ITransport read GetTransport;
+
+    constructor Create( trans: ITransport );
+  end;
+
+  IBase = interface
+    ['{08D9BAA8-5EAA-410F-B50B-AC2E6E5E4155}']
+    function ToString: string;
+    procedure Read( iprot: IProtocol);
+    procedure Write( iprot: IProtocol);
+  end;
+
+  IStruct = interface
+    ['{5DCE39AA-C916-4BC7-A79B-96A0C36B2220}']
+    procedure SetName(const Value: string);
+    function GetName: string;
+    property Name: string read GetName write SetName;
+  end;
+
+  TStructImpl = class( TInterfacedObject, IStruct )
+  private
+    FName: string;
+  protected
+    function GetName: string;
+    procedure SetName(const Value: string);
+  public
+    constructor Create( const AName: string);
+  end;
+
+  TBinaryProtocolImpl = class( TProtocolImpl )
+  protected
+    const
+      VERSION_MASK : Cardinal = $ffff0000;
+      VERSION_1 : Cardinal = $80010000;
+  protected
+    FStrictRead : Boolean;
+    FStrictWrite : Boolean;
+    FReadLength : Integer;
+    FCheckReadLength : Boolean;
+
+  private
+    function ReadAll( var buf: TBytes; off: Integer; len: Integer ): Integer;
+    function ReadStringBody( size: Integer): string;
+    procedure CheckReadLength( len: Integer );
+  public
+
+    type
+      TFactory = class( TInterfacedObject, IProtocolFactory)
+      protected
+        FStrictRead : Boolean;
+        FStrictWrite : Boolean;
+      public
+        function GetProtocol(trans: ITransport): IProtocol;
+        constructor Create( AStrictRead, AStrictWrite: Boolean ); overload;
+        constructor Create; overload;
+      end;
+
+    constructor Create( trans: ITransport); overload;
+    constructor Create( trans: ITransport; strictRead: Boolean; strictWrite: Boolean); overload;
+
+    procedure WriteMessageBegin( message: IMessage); override;
+    procedure WriteMessageEnd; override;
+    procedure WriteStructBegin(struc: IStruct); override;
+    procedure WriteStructEnd; override;
+    procedure WriteFieldBegin(field: IField); override;
+    procedure WriteFieldEnd; override;
+    procedure WriteFieldStop; override;
+    procedure WriteMapBegin(map: IMap); override;
+    procedure WriteMapEnd; override;
+    procedure WriteListBegin( list: IList); override;
+    procedure WriteListEnd(); override;
+    procedure WriteSetBegin( set_: ISet ); override;
+    procedure WriteSetEnd(); override;
+    procedure WriteBool( b: Boolean); override;
+    procedure WriteByte( b: ShortInt); override;
+    procedure WriteI16( i16: SmallInt); override;
+    procedure WriteI32( i32: Integer); override;
+    procedure WriteI64( i64: Int64); override;
+    procedure WriteDouble( d: Double); override;
+    procedure WriteBinary( const b: TBytes); override;
+
+    function ReadMessageBegin: IMessage; override;
+    procedure ReadMessageEnd(); override;
+    function ReadStructBegin: IStruct; override;
+    procedure ReadStructEnd; override;
+    function ReadFieldBegin: IField; override;
+    procedure ReadFieldEnd(); override;
+    function ReadMapBegin: IMap; override;
+    procedure ReadMapEnd(); override;
+    function ReadListBegin: IList; override;
+    procedure ReadListEnd(); override;
+    function ReadSetBegin: ISet; override;
+    procedure ReadSetEnd(); override;
+    function ReadBool: Boolean; override;
+    function ReadByte: ShortInt; override;
+    function ReadI16: SmallInt; override;
+    function ReadI32: Integer; override;
+    function ReadI64: Int64; override;
+    function ReadDouble:Double; override;
+    function ReadBinary: TBytes; override;
+
+    procedure SetReadLength( readLength: Integer );
+  end;
+
+implementation
+
+function ConvertInt64ToDouble( n: Int64): Double;
+begin
+  ASSERT( SizeOf(n) = SizeOf(Result));
+  System.Move( n, Result, SizeOf(Result));
+end;
+
+function ConvertDoubleToInt64( d: Double): Int64;
+begin
+  ASSERT( SizeOf(d) = SizeOf(Result));
+  System.Move( d, Result, SizeOf(Result));
+end;
+
+{ TFieldImpl }
+
+constructor TFieldImpl.Create(const AName: string; const AType: TType;
+  AId: SmallInt);
+begin
+  FName := AName;
+  FType := AType;
+  FId := AId;
+end;
+
+constructor TFieldImpl.Create;
+begin
+  FName := '';
+  FType := Low(TType);
+  FId   := 0;
+end;
+
+function TFieldImpl.GetId: SmallInt;
+begin
+  Result := FId;
+end;
+
+function TFieldImpl.GetName: string;
+begin
+  Result := FName;
+end;
+
+function TFieldImpl.GetType: TType;
+begin
+  Result := FType;
+end;
+
+procedure TFieldImpl.SetId(Value: SmallInt);
+begin
+  FId := Value;
+end;
+
+procedure TFieldImpl.SetName(const Value: string);
+begin
+  FName := Value;
+end;
+
+procedure TFieldImpl.SetType(Value: TType);
+begin
+  FType := Value;
+end;
+
+{ TProtocolImpl }
+
+constructor TProtocolImpl.Create(trans: ITransport);
+begin
+  inherited Create;
+  FTrans := trans;
+end;
+
+function TProtocolImpl.GetTransport: ITransport;
+begin
+  Result := FTrans;
+end;
+
+function TProtocolImpl.ReadAnsiString: AnsiString;
+var
+  b : TBytes;
+  len : Integer;
+begin
+  Result := '';
+  b := ReadBinary;
+  len := Length( b );
+  if len > 0 then
+  begin
+    SetLength( Result, len);
+    System.Move( b[0], Pointer(Result)^, len );
+  end;
+end;
+
+function TProtocolImpl.ReadString: string;
+begin
+  Result := TEncoding.UTF8.GetString( ReadBinary );
+end;
+
+procedure TProtocolImpl.WriteAnsiString(const s: AnsiString);
+var
+  b : TBytes;
+  len : Integer;
+begin
+  len := Length(s);
+  SetLength( b, len);
+  if len > 0 then
+  begin
+    System.Move( Pointer(s)^, b[0], len );
+  end;
+  WriteBinary( b );
+end;
+
+procedure TProtocolImpl.WriteString(const s: string);
+var
+  b : TBytes;
+begin
+  b := TEncoding.UTF8.GetBytes(s);
+  WriteBinary( b );
+end;
+
+{ TProtocolUtil }
+
+class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType);
+begin
+
+end;
+
+{ TStructImpl }
+
+constructor TStructImpl.Create(const AName: string);
+begin
+  inherited Create;
+  FName := AName;
+end;
+
+function TStructImpl.GetName: string;
+begin
+  Result := FName;
+end;
+
+procedure TStructImpl.SetName(const Value: string);
+begin
+  FName := Value;
+end;
+
+{ TMapImpl }
+
+constructor TMapImpl.Create(AValueType, AKeyType: TType; ACount: Integer);
+begin
+  inherited Create;
+  FValueType := AValueType;
+  FKeyType := AKeyType;
+  FCount := ACount;
+end;
+
+constructor TMapImpl.Create;
+begin
+
+end;
+
+function TMapImpl.GetCount: Integer;
+begin
+  Result := FCount;
+end;
+
+function TMapImpl.GetKeyType: TType;
+begin
+  Result := FKeyType;
+end;
+
+function TMapImpl.GetValueType: TType;
+begin
+  Result := FValueType;
+end;
+
+procedure TMapImpl.SetCount(Value: Integer);
+begin
+  FCount := Value;
+end;
+
+procedure TMapImpl.SetKeyType(Value: TType);
+begin
+  FKeyType := Value;
+end;
+
+procedure TMapImpl.SetValueType(Value: TType);
+begin
+  FValueType := Value;
+end;
+
+{ IMessage }
+
+constructor TMessageImpl.Create(AName: string; AMessageType: TMessageType;
+  ASeqID: Integer);
+begin
+  inherited Create;
+  FName := AName;
+  FMessageType := AMessageType;
+  FSeqID := ASeqID;
+end;
+
+constructor TMessageImpl.Create;
+begin
+  inherited;
+end;
+
+function TMessageImpl.GetName: string;
+begin
+  Result := FName;
+end;
+
+function TMessageImpl.GetSeqID: Integer;
+begin
+  Result := FSeqID;
+end;
+
+function TMessageImpl.GetType: TMessageType;
+begin
+  Result := FMessageType;
+end;
+
+procedure TMessageImpl.SetName(const Value: string);
+begin
+  FName := Value;
+end;
+
+procedure TMessageImpl.SetSeqID(Value: Integer);
+begin
+  FSeqID := Value;
+end;
+
+procedure TMessageImpl.SetType(Value: TMessageType);
+begin
+  FMessageType := Value;
+end;
+
+{ ISet }
+
+constructor TSetImpl.Create( AElementType: TType; ACount: Integer);
+begin
+  inherited Create;
+  FCount := ACount;
+  FElementType := AElementType;
+end;
+
+constructor TSetImpl.Create;
+begin
+
+end;
+
+function TSetImpl.GetCount: Integer;
+begin
+  Result := FCount;
+end;
+
+function TSetImpl.GetElementType: TType;
+begin
+  Result := FElementType;
+end;
+
+procedure TSetImpl.SetCount(Value: Integer);
+begin
+  FCount := Value;
+end;
+
+procedure TSetImpl.SetElementType(Value: TType);
+begin
+  FElementType := Value;
+end;
+
+{ IList }
+
+constructor TListImpl.Create( AElementType: TType; ACount: Integer);
+begin
+  inherited Create;
+  FCount := ACount;
+  FElementType := AElementType;
+end;
+
+constructor TListImpl.Create;
+begin
+
+end;
+
+function TListImpl.GetCount: Integer;
+begin
+  Result := FCount;
+end;
+
+function TListImpl.GetElementType: TType;
+begin
+  Result := FElementType;
+end;
+
+procedure TListImpl.SetCount(Value: Integer);
+begin
+  FCount := Value;
+end;
+
+procedure TListImpl.SetElementType(Value: TType);
+begin
+  FElementType := Value;
+end;
+
+{ TBinaryProtocolImpl }
+
+constructor TBinaryProtocolImpl.Create( trans: ITransport);
+begin
+  Create( trans, False, True);
+end;
+
+procedure TBinaryProtocolImpl.CheckReadLength(len: Integer);
+begin
+  if FCheckReadLength then
+  begin
+    Dec( FReadLength, len);
+    if FReadLength < 0 then
+    begin
+      raise Exception.Create( 'Message length exceeded: ' + IntToStr( len ) );
+    end;
+  end;
+end;
+
+constructor TBinaryProtocolImpl.Create(trans: ITransport; strictRead,
+  strictWrite: Boolean);
+begin
+  inherited Create( trans );
+  FStrictRead := strictRead;
+  FStrictWrite := strictWrite;
+end;
+
+function TBinaryProtocolImpl.ReadAll( var buf: TBytes; off,
+  len: Integer): Integer;
+begin
+  CheckReadLength( len );
+  Result := FTrans.ReadAll( buf, off, len );
+end;
+
+function TBinaryProtocolImpl.ReadBinary: TBytes;
+var
+  size : Integer;
+  buf : TBytes;
+begin
+  size := ReadI32;
+  CheckReadLength( size );
+  SetLength( buf, size );
+  FTrans.ReadAll( buf, 0, size);
+  Result := buf;
+end;
+
+function TBinaryProtocolImpl.ReadBool: Boolean;
+begin
+  Result := ReadByte = 1;
+end;
+
+function TBinaryProtocolImpl.ReadByte: ShortInt;
+var
+  bin : TBytes;
+begin
+  SetLength( bin, 1);
+  ReadAll( bin, 0, 1 );
+  Result := ShortInt( bin[0]);
+end;
+
+function TBinaryProtocolImpl.ReadDouble: Double;
+begin
+  Result := ConvertInt64ToDouble( ReadI64 )
+end;
+
+function TBinaryProtocolImpl.ReadFieldBegin: IField;
+var
+  field : IField;
+begin
+  field := TFieldImpl.Create;
+  field.Type_ := TType( ReadByte);
+  if ( field.Type_ <> TType.Stop ) then
+  begin
+    field.Id := ReadI16;
+  end;
+  Result := field;
+end;
+
+procedure TBinaryProtocolImpl.ReadFieldEnd;
+begin
+
+end;
+
+function TBinaryProtocolImpl.ReadI16: SmallInt;
+var
+  i16in : TBytes;
+begin
+  SetLength( i16in, 2 );
+  ReadAll( i16in, 0, 2);
+  Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF));
+end;
+
+function TBinaryProtocolImpl.ReadI32: Integer;
+var
+  i32in : TBytes;
+begin
+  SetLength( i32in, 4 );
+  ReadAll( i32in, 0, 4);
+
+  Result := Integer(
+    ((i32in[0] and $FF) shl 24) or
+    ((i32in[1] and $FF) shl 16) or
+    ((i32in[2] and $FF) shl 8) or
+     (i32in[3] and $FF));
+
+end;
+
+function TBinaryProtocolImpl.ReadI64: Int64;
+var
+  i64in : TBytes;
+begin
+  SetLength( i64in, 8);
+  ReadAll( i64in, 0, 8);
+  Result :=
+    (Int64( i64in[0] and $FF) shl 56) or
+    (Int64( i64in[1] and $FF) shl 48) or
+    (Int64( i64in[2] and $FF) shl 40) or
+    (Int64( i64in[3] and $FF) shl 32) or
+    (Int64( i64in[4] and $FF) shl 24) or
+    (Int64( i64in[5] and $FF) shl 16) or
+    (Int64( i64in[6] and $FF) shl 8) or
+    (Int64( i64in[7] and $FF));
+end;
+
+function TBinaryProtocolImpl.ReadListBegin: IList;
+var
+  list : IList;
+begin
+  list := TListImpl.Create;
+  list.ElementType := TType( ReadByte );
+  list.Count := ReadI32;
+  Result := list;
+end;
+
+procedure TBinaryProtocolImpl.ReadListEnd;
+begin
+
+end;
+
+function TBinaryProtocolImpl.ReadMapBegin: IMap;
+var
+  map : IMap;
+begin
+  map := TMapImpl.Create;
+  map.KeyType := TType( ReadByte );
+  map.ValueType := TType( ReadByte );
+  map.Count := ReadI32;
+  Result := map;
+end;
+
+procedure TBinaryProtocolImpl.ReadMapEnd;
+begin
+
+end;
+
+function TBinaryProtocolImpl.ReadMessageBegin: IMessage;
+var
+  size : Integer;
+  version : Integer;
+  message : IMessage;
+begin
+  message := TMessageImpl.Create;
+  size := ReadI32;
+  if (size < 0) then
+  begin
+    version := size and Integer( VERSION_MASK);
+    if ( version <> Integer( VERSION_1)) then
+    begin
+      raise TProtocolException.Create(TProtocolException.BAD_VERSION, 'Bad version in ReadMessageBegin: ' + IntToStr(version) );
+    end;
+    message.Type_ := TMessageType( size and $000000ff);
+    message.Name := ReadString;
+    message.SeqID := ReadI32;
+  end else
+  begin
+    if FStrictRead then
+    begin
+      raise TProtocolException.Create( TProtocolException.BAD_VERSION, 'Missing version in readMessageBegin, old client?' );
+    end;
+    message.Name := ReadStringBody( size );
+    message.Type_ := TMessageType( ReadByte );
+    message.SeqID := ReadI32;
+  end;
+  Result := message;
+end;
+
+procedure TBinaryProtocolImpl.ReadMessageEnd;
+begin
+  inherited;
+
+end;
+
+function TBinaryProtocolImpl.ReadSetBegin: ISet;
+var
+  set_ : ISet;
+begin
+  set_ := TSetImpl.Create;
+  set_.ElementType := TType( ReadByte );
+  set_.Count := ReadI32;
+  Result := set_;
+end;
+
+procedure TBinaryProtocolImpl.ReadSetEnd;
+begin
+
+end;
+
+function TBinaryProtocolImpl.ReadStringBody( size: Integer): string;
+var
+  buf : TBytes;
+begin
+  CheckReadLength( size );
+  SetLength( buf, size );
+  FTrans.ReadAll( buf, 0, size );
+  Result := TEncoding.UTF8.GetString( buf);
+end;
+
+function TBinaryProtocolImpl.ReadStructBegin: IStruct;
+begin
+  Result := TStructImpl.Create('');
+end;
+
+procedure TBinaryProtocolImpl.ReadStructEnd;
+begin
+  inherited;
+
+end;
+
+procedure TBinaryProtocolImpl.SetReadLength(readLength: Integer);
+begin
+  FReadLength := readLength;
+  FCheckReadLength := True;
+end;
+
+procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);
+begin
+  WriteI32( Length(b));
+  FTrans.Write(b, 0, Length( b));
+end;
+
+procedure TBinaryProtocolImpl.WriteBool(b: Boolean);
+begin
+  if b then
+  begin
+    WriteByte( 1 );
+  end else
+  begin
+    WriteByte( 0 );
+  end;
+end;
+
+procedure TBinaryProtocolImpl.WriteByte(b: ShortInt);
+var
+  a : TBytes;
+begin
+  SetLength( a, 1);
+  a[0] := Byte( b );
+  FTrans.Write( a, 0, 1 );
+end;
+
+procedure TBinaryProtocolImpl.WriteDouble(d: Double);
+begin
+  WriteI64(ConvertDoubleToInt64(d));
+end;
+
+procedure TBinaryProtocolImpl.WriteFieldBegin(field: IField);
+begin
+  WriteByte(ShortInt(field.Type_));
+  WriteI16(field.ID);
+end;
+
+procedure TBinaryProtocolImpl.WriteFieldEnd;
+begin
+
+end;
+
+procedure TBinaryProtocolImpl.WriteFieldStop;
+begin
+  WriteByte(ShortInt(TType.Stop));
+end;
+
+procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt);
+var
+  i16out : TBytes;
+begin
+  SetLength( i16out, 2);
+  i16out[0] := Byte($FF and (i16 shr 8));
+  i16out[1] := Byte($FF and i16);
+  FTrans.Write( i16out );
+end;
+
+procedure TBinaryProtocolImpl.WriteI32(i32: Integer);
+var
+  i32out : TBytes;
+begin
+  SetLength( i32out, 4);
+  i32out[0] := Byte($FF and (i32 shr 24));
+  i32out[1] := Byte($FF and (i32 shr 16));
+  i32out[2] := Byte($FF and (i32 shr 8));
+  i32out[3] := Byte($FF and i32);
+  FTrans.Write( i32out, 0, 4);
+end;
+
+procedure TBinaryProtocolImpl.WriteI64(i64: Int64);
+var
+  i64out : TBytes;
+begin
+  SetLength( i64out, 8);
+  i64out[0] := Byte($FF and (i64 shr 56));
+  i64out[1] := Byte($FF and (i64 shr 48));
+  i64out[2] := Byte($FF and (i64 shr 40));
+  i64out[3] := Byte($FF and (i64 shr 32));
+  i64out[4] := Byte($FF and (i64 shr 24));
+  i64out[5] := Byte($FF and (i64 shr 16));
+  i64out[6] := Byte($FF and (i64 shr 8));
+  i64out[7] := Byte($FF and i64);
+  FTrans.Write( i64out, 0, 8);
+end;
+
+procedure TBinaryProtocolImpl.WriteListBegin(list: IList);
+begin
+  WriteByte(ShortInt(list.ElementType));
+  WriteI32(list.Count);
+end;
+
+procedure TBinaryProtocolImpl.WriteListEnd;
+begin
+
+end;
+
+procedure TBinaryProtocolImpl.WriteMapBegin(map: IMap);
+begin
+  WriteByte(ShortInt(map.KeyType));
+  WriteByte(ShortInt(map.ValueType));
+  WriteI32(map.Count);
+end;
+
+procedure TBinaryProtocolImpl.WriteMapEnd;
+begin
+
+end;
+
+procedure TBinaryProtocolImpl.WriteMessageBegin( message: IMessage);
+var
+  version : Cardinal;
+begin
+  if FStrictWrite then
+  begin
+    version := VERSION_1 or Cardinal( message.Type_);
+    WriteI32( Integer( version) );
+    WriteString( message.Name);
+  	WriteI32(message.SeqID);
+  end else
+  begin
+    WriteString(message.Name);
+    WriteByte(ShortInt(message.Type_));
+    WriteI32(message.SeqID);
+  end;
+end;
+
+procedure TBinaryProtocolImpl.WriteMessageEnd;
+begin
+
+end;
+
+procedure TBinaryProtocolImpl.WriteSetBegin(set_: ISet);
+begin
+  WriteByte(ShortInt(set_.ElementType));
+  WriteI32(set_.Count);
+end;
+
+procedure TBinaryProtocolImpl.WriteSetEnd;
+begin
+
+end;
+
+procedure TBinaryProtocolImpl.WriteStructBegin(struc: IStruct);
+begin
+
+end;
+
+procedure TBinaryProtocolImpl.WriteStructEnd;
+begin
+
+end;
+
+{ TProtocolException }
+
+constructor TProtocolException.Create;
+begin
+  inherited Create('');
+  FType := UNKNOWN;
+end;
+
+constructor TProtocolException.Create(type_: Integer);
+begin
+  inherited Create('');
+  FType := type_;
+end;
+
+constructor TProtocolException.Create(type_: Integer; const msg: string);
+begin
+  inherited Create( msg );
+  FType := type_;
+end;
+
+{ TThriftStringBuilder }
+
+function TThriftStringBuilder.Append(const Value: TBytes): TStringBuilder;
+begin
+  Result := Append( string( RawByteString(Value)) );
+end;
+
+function TThriftStringBuilder.Append(
+  const Value: IThriftContainer): TStringBuilder;
+begin
+  Result := Append( Value.ToString );
+end;
+
+{ TBinaryProtocolImpl.TFactory }
+
+constructor TBinaryProtocolImpl.TFactory.Create(AStrictRead, AStrictWrite: Boolean);
+begin
+  FStrictRead := AStrictRead;
+  FStrictWrite := AStrictWrite;
+end;
+
+constructor TBinaryProtocolImpl.TFactory.Create;
+begin
+  Create( False, True )
+end;
+
+function TBinaryProtocolImpl.TFactory.GetProtocol(trans: ITransport): IProtocol;
+begin
+  Result := TBinaryProtocolImpl.Create( trans );
+end;
+
+end.
+

Added: thrift/trunk/lib/delphi/src/Thrift.Server.pas
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/delphi/src/Thrift.Server.pas?rev=1185688&view=auto
==============================================================================
--- thrift/trunk/lib/delphi/src/Thrift.Server.pas (added)
+++ thrift/trunk/lib/delphi/src/Thrift.Server.pas Tue Oct 18 14:35:26 2011
@@ -0,0 +1,325 @@
+(*
+ * 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.Server;
+
+interface
+
+uses
+  SysUtils,
+  Thrift,
+  Thrift.Protocol,
+  Thrift.Transport;
+
+type
+  IServer = interface
+    ['{CF9F56C6-BB39-4C7D-877B-43B416572CE6}']
+    procedure Serve;
+    procedure Stop;
+  end;
+
+  TServerImpl = class abstract( TInterfacedObject, IServer )
+  public
+    type
+      TLogDelegate = reference to procedure( str: string);
+  protected
+    FProcessor : IProcessor;
+    FServerTransport : IServerTransport;
+    FInputTransportFactory : ITransportFactory;
+    FOutputTransportFactory : ITransportFactory;
+    FInputProtocolFactory : IProtocolFactory;
+    FOutputProtocolFactory : IProtocolFactory;
+    FLogDelegate : TLogDelegate;
+
+    class procedure DefaultLogDelegate( str: string);
+
+    procedure Serve; virtual; abstract;
+    procedure Stop; virtual; abstract;
+  public
+    constructor Create(
+      AProcessor :IProcessor;
+      AServerTransport: IServerTransport;
+      AInputTransportFactory : ITransportFactory;
+      AOutputTransportFactory : ITransportFactory;
+      AInputProtocolFactory : IProtocolFactory;
+      AOutputProtocolFactory : IProtocolFactory;
+      ALogDelegate : TLogDelegate
+      ); overload;
+
+    constructor Create( AProcessor :IProcessor;
+      AServerTransport: IServerTransport); overload;
+
+    constructor Create(
+      AProcessor :IProcessor;
+      AServerTransport: IServerTransport;
+      ALogDelegate: TLogDelegate
+      ); overload;
+
+    constructor Create(
+      AProcessor :IProcessor;
+      AServerTransport: IServerTransport;
+      ATransportFactory : ITransportFactory
+      ); overload;
+
+    constructor Create(
+      AProcessor :IProcessor;
+      AServerTransport: IServerTransport;
+      ATransportFactory : ITransportFactory;
+      AProtocolFactory : IProtocolFactory
+      ); overload;
+  end;
+
+  TSimpleServer = class( TServerImpl)
+  private
+    FStop : Boolean;
+  public
+    constructor Create( AProcessor: IProcessor; AServerTransport: IServerTransport); overload;
+    constructor Create( AProcessor: IProcessor; AServerTransport: IServerTransport;
+      ALogDel: TServerImpl.TLogDelegate); overload;
+    constructor Create( AProcessor: IProcessor; AServerTransport: IServerTransport;
+      ATransportFactory: ITransportFactory); overload;
+    constructor Create( AProcessor: IProcessor; AServerTransport: IServerTransport;
+      ATransportFactory: ITransportFactory; AProtocolFactory: IProtocolFactory); overload;
+
+    procedure Serve; override;
+    procedure Stop; override;
+  end;
+
+
+implementation
+
+{ TServerImpl }
+
+constructor TServerImpl.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport; ALogDelegate: TLogDelegate);
+var
+  InputFactory, OutputFactory : IProtocolFactory;
+  InputTransFactory, OutputTransFactory : ITransportFactory;
+
+begin
+  InputFactory := TBinaryProtocolImpl.TFactory.Create;
+  OutputFactory := TBinaryProtocolImpl.TFactory.Create;
+  InputTransFactory := TTransportFactoryImpl.Create;
+  OutputTransFactory := TTransportFactoryImpl.Create;
+
+  Create(
+    AProcessor,
+    AServerTransport,
+    InputTransFactory,
+    OutputTransFactory,
+    InputFactory,
+    OutputFactory,
+    ALogDelegate
+  );
+end;
+
+constructor TServerImpl.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport);
+var
+  InputFactory, OutputFactory : IProtocolFactory;
+  InputTransFactory, OutputTransFactory : ITransportFactory;
+
+begin
+  InputFactory := TBinaryProtocolImpl.TFactory.Create;
+  OutputFactory := TBinaryProtocolImpl.TFactory.Create;
+  InputTransFactory := TTransportFactoryImpl.Create;
+  OutputTransFactory := TTransportFactoryImpl.Create;
+
+  Create(
+    AProcessor,
+    AServerTransport,
+    InputTransFactory,
+    OutputTransFactory,
+    InputFactory,
+    OutputFactory,
+    DefaultLogDelegate
+  );
+end;
+
+constructor TServerImpl.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport; ATransportFactory: ITransportFactory);
+var
+  InputProtocolFactory : IProtocolFactory;
+  OutputProtocolFactory : IProtocolFactory;
+begin
+  InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
+  OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
+
+  Create( AProcessor, AServerTransport, ATransportFactory, ATransportFactory,
+    InputProtocolFactory, OutputProtocolFactory, DefaultLogDelegate);
+end;
+
+constructor TServerImpl.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport; AInputTransportFactory,
+  AOutputTransportFactory: ITransportFactory; AInputProtocolFactory,
+  AOutputProtocolFactory: IProtocolFactory;
+  ALogDelegate : TLogDelegate);
+begin
+  FProcessor := AProcessor;
+  FServerTransport := AServerTransport;
+  FInputTransportFactory := AInputTransportFactory;
+  FOutputTransportFactory := AOutputTransportFactory;
+  FInputProtocolFactory := AInputProtocolFactory;
+  FOutputProtocolFactory := AOutputProtocolFactory;
+  FLogDelegate := ALogDelegate;
+end;
+
+class procedure TServerImpl.DefaultLogDelegate( str: string);
+begin
+  Writeln( str );
+end;
+
+constructor TServerImpl.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport; ATransportFactory: ITransportFactory;
+  AProtocolFactory: IProtocolFactory);
+begin
+
+end;
+
+{ TSimpleServer }
+
+constructor TSimpleServer.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport);
+var
+  InputProtocolFactory : IProtocolFactory;
+  OutputProtocolFactory : IProtocolFactory;
+  InputTransportFactory : ITransportFactory;
+  OutputTransportFactory : ITransportFactory;
+begin
+  InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
+  OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
+  InputTransportFactory := TTransportFactoryImpl.Create;
+  OutputTransportFactory := TTransportFactoryImpl.Create;
+
+  inherited Create( AProcessor, AServerTransport, InputTransportFactory,
+    OutputTransportFactory, InputProtocolFactory, OutputProtocolFactory, DefaultLogDelegate);
+end;
+
+constructor TSimpleServer.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport; ALogDel: TServerImpl.TLogDelegate);
+var
+  InputProtocolFactory : IProtocolFactory;
+  OutputProtocolFactory : IProtocolFactory;
+  InputTransportFactory : ITransportFactory;
+  OutputTransportFactory : ITransportFactory;
+begin
+  InputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
+  OutputProtocolFactory := TBinaryProtocolImpl.TFactory.Create;
+  InputTransportFactory := TTransportFactoryImpl.Create;
+  OutputTransportFactory := TTransportFactoryImpl.Create;
+
+  inherited Create( AProcessor, AServerTransport, InputTransportFactory,
+    OutputTransportFactory, InputProtocolFactory, OutputProtocolFactory, ALogDel);
+end;
+
+constructor TSimpleServer.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport; ATransportFactory: ITransportFactory);
+begin
+  inherited Create( AProcessor, AServerTransport, ATransportFactory,
+    ATransportFactory, TBinaryProtocolImpl.TFactory.Create, TBinaryProtocolImpl.TFactory.Create, DefaultLogDelegate);
+end;
+
+constructor TSimpleServer.Create(AProcessor: IProcessor;
+  AServerTransport: IServerTransport; ATransportFactory: ITransportFactory;
+  AProtocolFactory: IProtocolFactory);
+begin
+  inherited Create( AProcessor, AServerTransport, ATransportFactory,
+    ATransportFactory, AProtocolFactory, AProtocolFactory, DefaultLogDelegate);
+end;
+
+procedure TSimpleServer.Serve;
+var
+  client : ITransport;
+  InputTransport : ITransport;
+  OutputTransport : ITransport;
+  InputProtocol : IProtocol;
+  OutputProtocol : IProtocol;
+begin
+  try
+    FServerTransport.Listen;
+  except
+    on E: Exception do
+    begin
+      FLogDelegate( E.ToString);
+    end;
+  end;
+
+  client := nil;
+  InputTransport := nil;
+  OutputTransport := nil;
+  InputProtocol := nil;
+  OutputProtocol := nil;
+
+  while (not FStop) do
+  begin
+    try
+      client := FServerTransport.Accept;
+      FLogDelegate( 'Client Connected!');
+      InputTransport := FInputTransportFactory.GetTransport( client );
+      OutputTransport := FOutputTransportFactory.GetTransport( client );
+      InputProtocol := FInputProtocolFactory.GetProtocol( InputTransport );
+      OutputProtocol := FOutputProtocolFactory.GetProtocol( OutputTransport );
+      while ( FProcessor.Process( InputProtocol, OutputProtocol )) do
+      begin
+        if FStop then Break;
+      end;
+    except
+      on E: TTransportException do
+      begin
+        if FStop then
+        begin
+          FLogDelegate('TSimpleServer was shutting down, caught ' + E.ClassName);
+        end;
+      end;
+      on E: Exception do
+      begin
+        FLogDelegate( E.ToString );
+      end;
+    end;
+    if InputTransport <> nil then
+    begin
+      InputTransport.Close;
+    end;
+    if OutputTransport <> nil then
+    begin
+      OutputTransport.Close;
+    end;
+  end;
+
+  if FStop then
+  begin
+    try
+      FServerTransport.Close;
+    except
+      on E: TTransportException do
+      begin
+        FLogDelegate('TServerTranport failed on close: ' + E.Message);
+      end;
+    end;
+    FStop := False;
+  end;
+end;
+
+procedure TSimpleServer.Stop;
+begin
+  FStop := True;
+  FServerTransport.Close;
+end;
+
+end.

Added: thrift/trunk/lib/delphi/src/Thrift.Stream.pas
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/delphi/src/Thrift.Stream.pas?rev=1185688&view=auto
==============================================================================
--- thrift/trunk/lib/delphi/src/Thrift.Stream.pas (added)
+++ thrift/trunk/lib/delphi/src/Thrift.Stream.pas Tue Oct 18 14:35:26 2011
@@ -0,0 +1,298 @@
+(*
+ * 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.Stream;
+
+interface
+
+uses
+  Classes,
+  SysUtils,
+  SysConst,
+  RTLConsts,
+  Thrift.Utils,
+  ActiveX;
+
+type
+
+  IThriftStream = interface
+    ['{732621B3-F697-4D76-A1B0-B4DD5A8E4018}']
+    procedure Write( const buffer: TBytes; offset: Integer; count: Integer);
+    function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer;
+    procedure Open;
+    procedure Close;
+    procedure Flush;
+    function IsOpen: Boolean;
+    function ToArray: TBytes;
+  end;
+
+  TThriftStreamImpl = class( TInterfacedObject, IThriftStream)
+  private
+    procedure CheckSizeAndOffset( const buffer: TBytes; offset: Integer; count: Integer);
+  protected
+    procedure Write( const buffer: TBytes; offset: Integer; count: Integer); virtual;
+    function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; virtual;
+    procedure Open; virtual; abstract;
+    procedure Close; virtual; abstract;
+    procedure Flush; virtual; abstract;
+    function IsOpen: Boolean; virtual; abstract;
+    function ToArray: TBytes; virtual; abstract;
+  end;
+
+  TThriftStreamAdapterDelphi = class( TThriftStreamImpl )
+  private
+    FStream : TStream;
+    FOwnsStream : Boolean;
+  protected
+    procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
+    function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
+    procedure Open; override;
+    procedure Close; override;
+    procedure Flush; override;
+    function IsOpen: Boolean; override;
+    function ToArray: TBytes; override;
+  public
+    constructor Create( AStream: TStream; AOwnsStream : Boolean);
+    destructor Destroy; override;
+  end;
+
+  TThriftStreamAdapterCOM = class( TThriftStreamImpl)
+  private
+    FStream : IStream;
+  protected
+    procedure Write( const buffer: TBytes; offset: Integer; count: Integer); override;
+    function Read( var buffer: TBytes; offset: Integer; count: Integer): Integer; override;
+    procedure Open; override;
+    procedure Close; override;
+    procedure Flush; override;
+    function IsOpen: Boolean; override;
+    function ToArray: TBytes; override;
+  public
+    constructor Create( AStream: IStream);
+  end;
+
+implementation
+
+{ TThriftStreamAdapterCOM }
+
+procedure TThriftStreamAdapterCOM.Close;
+begin
+  FStream := nil;
+end;
+
+constructor TThriftStreamAdapterCOM.Create(AStream: IStream);
+begin
+  FStream := AStream;
+end;
+
+procedure TThriftStreamAdapterCOM.Flush;
+begin
+  if IsOpen then
+  begin
+    if FStream <> nil then
+    begin
+      FStream.Commit( STGC_DEFAULT );
+    end;
+  end;
+end;
+
+function TThriftStreamAdapterCOM.IsOpen: Boolean;
+begin
+  Result := FStream <> nil;
+end;
+
+procedure TThriftStreamAdapterCOM.Open;
+begin
+
+end;
+
+function TThriftStreamAdapterCOM.Read( var buffer: TBytes; offset: Integer; count: Integer): Integer;
+begin
+  inherited;
+  Result := 0;
+  if FStream <> nil then
+  begin
+    if count > 0 then
+    begin
+      FStream.Read( @buffer[offset], count, @Result);
+    end;
+  end;
+end;
+
+function TThriftStreamAdapterCOM.ToArray: TBytes;
+var
+  statstg: TStatStg;
+  len : Integer;
+  NewPos : Int64;
+  cbRead : Integer;
+begin
+  FillChar( statstg, SizeOf( statstg), 0);
+  len := 0;
+  if IsOpen then
+  begin
+    if Succeeded( FStream.Stat( statstg, STATFLAG_NONAME )) then
+    begin
+      len := statstg.cbSize;
+    end;
+  end;
+
+  SetLength( Result, len );
+
+  if len > 0 then
+  begin
+    if Succeeded( FStream.Seek( 0, STREAM_SEEK_SET, NewPos) ) then
+    begin
+      FStream.Read( @Result[0], len, @cbRead);
+    end;
+  end;
+end;
+
+procedure TThriftStreamAdapterCOM.Write( const buffer: TBytes; offset: Integer; count: Integer);
+var
+  nWritten : Integer;
+begin
+  inherited;
+  if IsOpen then
+  begin
+    if count > 0 then
+    begin
+      FStream.Write( @buffer[0], count, @nWritten);
+    end;
+  end;
+end;
+
+{ TThriftStreamImpl }
+
+procedure TThriftStreamImpl.CheckSizeAndOffset(const buffer: TBytes; offset,
+  count: Integer);
+var
+  len : Integer;
+begin
+  if count > 0 then
+  begin
+    len := Length( buffer );
+    if (offset < 0) or ( offset >= len) then
+    begin
+      raise ERangeError.Create( SBitsIndexError );
+    end;
+    if count > len then
+    begin
+      raise ERangeError.Create( SBitsIndexError );
+    end;
+  end;
+end;
+
+function TThriftStreamImpl.Read(var buffer: TBytes; offset,
+  count: Integer): Integer;
+begin
+  Result := 0;
+  CheckSizeAndOffset( buffer, offset, count );
+end;
+
+procedure TThriftStreamImpl.Write(const buffer: TBytes; offset, count: Integer);
+begin
+  CheckSizeAndOffset( buffer, offset, count );
+end;
+
+{ TThriftStreamAdapterDelphi }
+
+procedure TThriftStreamAdapterDelphi.Close;
+begin
+  FStream.Free;
+  FStream := nil;
+  FOwnsStream := False;
+end;
+
+constructor TThriftStreamAdapterDelphi.Create(AStream: TStream; AOwnsStream: Boolean);
+begin
+  FStream := AStream;
+  FOwnsStream := AOwnsStream;
+end;
+
+destructor TThriftStreamAdapterDelphi.Destroy;
+begin
+  if FOwnsStream then
+  begin
+    FStream.Free;
+  end;
+  inherited;
+end;
+
+procedure TThriftStreamAdapterDelphi.Flush;
+begin
+
+end;
+
+function TThriftStreamAdapterDelphi.IsOpen: Boolean;
+begin
+  Result := FStream <> nil;
+end;
+
+procedure TThriftStreamAdapterDelphi.Open;
+begin
+
+end;
+
+function TThriftStreamAdapterDelphi.Read(var buffer: TBytes; offset,
+  count: Integer): Integer;
+begin
+  inherited;
+  Result := 0;
+  if count > 0 then
+  begin
+    Result := FStream.Read( Pointer(@buffer[offset])^, count)
+  end;
+end;
+
+function TThriftStreamAdapterDelphi.ToArray: TBytes;
+var
+  OrgPos : Integer;
+  len : Integer;
+begin
+  len := 0;
+  if FStream <> nil then
+  begin
+    len := FStream.Size;
+  end;
+
+  SetLength( Result, len );
+
+  if len > 0 then
+  begin
+    OrgPos := FStream.Position;
+    try
+      FStream.Position := 0;
+      FStream.ReadBuffer( Pointer(@Result[0])^, len );
+    finally
+      FStream.Position := OrgPos;
+    end;
+  end
+end;
+
+procedure TThriftStreamAdapterDelphi.Write(const buffer: TBytes; offset,
+  count: Integer);
+begin
+  inherited;
+  if count > 0 then
+  begin
+    FStream.Write( Pointer(@buffer[offset])^, count)
+  end;
+end;
+
+end.