AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi Methoden-Parameter soll Referenz, aber kein nil sein können
Thema durchsuchen
Ansicht
Themen-Optionen

Methoden-Parameter soll Referenz, aber kein nil sein können

Ein Thema von Der schöne Günther · begonnen am 28. Nov 2013 · letzter Beitrag vom 29. Nov 2013
Antwort Antwort
Seite 3 von 3     123   
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.132 Beiträge
 
Delphi 12 Athens
 
#21

AW: Methoden-Parameter soll Referenz, aber kein nil sein können

  Alt 29. Nov 2013, 10:13
Zwar nicht im Compiler, aber zur Laufzeit sollte es gehen?
Delphi-Quellcode:
type
  TSimpleNonNullable<T> = record
    class operator Implicit(const Value: TSimpleNonNullable<T>): T; inline;
    class operator Implicit(const Value: T): TSimpleNonNullable<T>; inline;
    class procedure Check(const Value: T); inline; static;
  public type
    ENonNullable = class(Exception);
  private
    FValue: T;
  end;

{ TSimpleNonNullable<T> }

class operator TSimpleNonNullable<T>.Implicit(const Value: TSimpleNonNullable<T>): T;
begin
  Check(Value);
  Result := Value.FValue;
end;

class operator TSimpleNonNullable<T>.Implicit(const Value: T): TSimpleNonNullable<T>;
begin
  Check(Value);
  Result.FValue := Value;
end;

class procedure TSimpleNonNullable<T>.Check(const Value: T);
begin
  case SizeOf(Value) of
    4: if PInteger(@Value)^ <> 0 then Exit;
    8: if PInt64(@Value)^ <> 0 then Exit;
    else raise ENonNullable.CreateFmt('TNonNullable<T>: Type %s is not supported.', [GetTypeName(TypeInfo(T))]);
  end;
  raise ENonNullable.CreateFmt('TNonNullable<T>: Variable %s(%p) is null.', [GetTypeName(TypeInfo(T)), @Value]);
end;
Delphi-Quellcode:
uses
  TypInfo, Variants;

type
  TNonNullable<T> = record
    class operator Implicit(const Value: TNonNullable<T>): T; inline;
    class operator Implicit(const Value: T): TNonNullable<T>; inline;
    class procedure Check(const Value: T); static;
  public type
    ENonNullable = class(Exception);
  private
    FValue: T;
  end;

{ TNonNullable<T> }

class procedure TNonNullable<T>.Check(const Value: T);
begin
  case PTypeInfo(TypeInfo(T)).Kind of
    tkPointer, tkClass, tkClassRef, tkInterface, tkProcedure, tkString, tkUString, tkDynArray, tkWString:
      if PPointer(@Value)^ <> nil then
        Exit;
    tkInteger, tkChar, tkWChar, tkEnumeration, tkSet:
      case GetTypeData(TypeInfo(T)).OrdType of
        otSByte, otUByte:
          if PByte(@Value)^ <> 0 then
            Exit;
        otSWord, otUWord:
          if PWord(@Value)^ <> 0 then
            Exit;
        otSLong, otULong:
          if PLongWord(@Value)^ <> 0 then
            Exit;
        else
          raise ENonNullable.CreateFmt('TNonNullable<T>: Type %s is not supported.', [GetTypeName(TypeInfo(T))]);
      end;
    tkInt64:
      if PInt64(@Value)^ <> 0 then
        Exit;
    tkFloat:
      case GetTypeData(TypeInfo(T)).FloatType of
        ftSingle:
          if PSingle(@Value)^ <> 0 then
            Exit;
        ftDouble:
          if PDouble(@Value)^ <> 0 then
            Exit;
        ftExtended:
          if PExtended(@Value)^ <> 0 then
            Exit;
        ftCurr:
          if PCurrency(@Value)^ <> 0 then
            Exit;
        else
          raise ENonNullable.CreateFmt('TNonNullable<T>: Type %s is not supported.', [GetTypeName(TypeInfo(T))]);
      end;
    tkMethod:
      if TMethod(Pointer(@Value)^).Code <> nil then
        Exit;
    tkVariant:
      if not VarIsNull(PVariant(@Value)^) and VarIsEmpty(PVariant(@Value)^) then
        Exit;
    else
      raise ENonNullable.CreateFmt('TNonNullable<T>: Type %s is not supported.', [GetTypeName(TypeInfo(T))]);
  end;

  raise ENonNullable.CreateFmt('TNonNullable<T>: Variable %s(%p) is null.', [GetTypeName(TypeInfo(T)), @Value]);
end;

class operator TNonNullable<T>.Implicit(const Value: TNonNullable<T>): T;
begin
  Check(Value.FValue);
  Result := Value.FValue;
end;

class operator TNonNullable<T>.Implicit(const Value: T): TNonNullable<T>;
begin
  Check(Value);
  Result.FValue := Value;
end;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#22

AW: Methoden-Parameter soll Referenz, aber kein nil sein können

  Alt 29. Nov 2013, 10:19
Parametrisierte, operator-überladene Records. Jetzt habe ich alles gesehen.
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.132 Beiträge
 
Delphi 12 Athens
 
#23

AW: Methoden-Parameter soll Referenz, aber kein nil sein können

  Alt 29. Nov 2013, 11:37
Parametrisierte, operator-überladene Records. Jetzt habe ich alles gesehen.
Ich hatte hier vor Jahren mal irgendwo eine Codezeile gepostet, die sah aus wie ein C++-Code, oder eher wie soein obfuscateter JavaScript-Code.
Aber im Grunde war es nur eine For-Step-Schleife, wie man sie aus QBasic kennt, welche aus anonymen Methoden bestand, vielleicht noch mit bissl Generics und alles "schön" als Einzeiler.
Delphi-Quellcode:
FOR X% = 0 TO 9 STEP 3 // 0 3 6 9

FOR X% = 3 TO 0 STEP -1 // 3 2 1 0
Aber leider finde ich den einfach nicht mehr
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu (29. Nov 2013 um 11:40 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 3 von 3     123   

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:41 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz