Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6884445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:30:34+00:00 2026-05-27T05:30:34+00:00

I have this atomic optimistic initializer class: type Atomic<T: IInterface> = class type TFactory

  • 0

I have this atomic optimistic initializer class:

type
  Atomic<T: IInterface> = class
    type TFactory = reference to function: T;
    class function Initialize(var storage: T; factory: TFactory): T;
  end;

class function Atomic<T>.Initialize(var storage: T; factory: TFactory): T;
var
  tmpIntf: T;
begin
  if not assigned(storage) then begin
    tmpIntf := factory();
    if InterlockedCompareExchangePointer(PPointer(@storage)^, PPointer(@tmpIntf)^, nil) = nil then
      PPointer(@tmpIntf)^ := nil;
  end;
  Result := storage;
end;

Now I would like to implement the same pattern for objects.

type
  Atomic<T: class> = class
    type TFactory = reference to function: T;
    class function Initialize(var storage: T; factory: TFactory): T;
  end;

class function Atomic<T>.Initialize(var storage: T; factory: TFactory): T;
var
  tmpIntf: T;
begin
  if not assigned(storage) then begin
    tmpIntf := factory();
    if InterlockedCompareExchangePointer(PPointer(@storage)^, PPointer(@tmpIntf)^, nil) = nil then
      tmpIntf.Free;
  end;
  Result := storage;
end;

I can do those two in two separate classes, but I would really like to put both initializers under the same umbrella. IOW, I would ideally like to use this as

var
  o: TObject;
  i: IInterface;

Atomic<TObject>.Initialize(o, CreateObject);
Atomic<IInterface>.Initialize(i, CreateInterface);

I can’t find any good solution for this. The only idea I got is to declare class as Atomic<T> (without constraints) and then somehow (don’t yet know how) check the RTTI of T in runtime and proceed accordingly.

I don’t like this idea much and I’m looking for a better approach.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:30:34+00:00Added an answer on May 27, 2026 at 5:30 am

    It seems you cannot specify constraints of the type “class or interface”. Therefore the easiest solution seems to be to drop the constraint (you can enforce it at runtime, using RTTI).

    For the RTTI approach, you can use the TypeInfo function:

    uses
      ..., TypInfo;    
    
    class function Atomic<T>.Initialize(var storage: T; factory: TFactory): T;
    var
      tmpT: T;
    begin
      if not assigned(PPointer(@storage)^) then begin
        tmpT := factory();
        if InterlockedCompareExchangePointer(PPointer(@storage)^, PPointer(@tmpT)^, nil) = nil then begin
          case PTypeInfo(TypeInfo(T))^.Kind of
            tkInterface:
              PPointer(@tmpT)^ := nil;
            tkClass:
              TObject(tmpT).Free;
            else
              raise Exception.Create('Atomic<T>.Initialize: Unsupported type');
          end;
        end;
      end;
      Result := storage;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a type class Atomic , which defines functions for converting certain types
I have this : class Stress def initialize(user, pass) @user = user @pass =
I have unconstrained generic type Atomic which implements an initializer (details in my previous
If I have this table: +------+-------+---------------+--------+-----------------+------------+-----------+----------------+------+------+--------+------------+------------+ | type | class | username | userid
If you have this xml <AssetComponent name=AssetComponent4881 width=300> <Asset id=5 type=1 /> </AssetComponent> <AssetComponent
This maybe a related question: Java assignment issues - Is this atomic? I have
I have a class called Atomic which is basically an _Atomic_word plus methods that
I have a method with a single atomic operation, like this one int value;
So I have this class: @interface AmountViewController : UITableViewController <UITableViewDelegate, UITextFieldDelegate>{ NSManagedObjectContext *managedObjectContext; NSManagedObject
I have a linux multithread application in C++. In this application in class App

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.