How to implement "__property" in ANSI C++ ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    How to implement "__property" in ANSI C++ ?

    Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
    term to dump Borland (not only BCB).
    As a part of my attempt to dump long-loved BCB I'm trying to investigate how
    one can implement "__property " in ANSI C++.

    Would anyone have a solution hot to implement "__property " in ANSI C++ ie.
    how to make following code compile in non-BCB C++ compiler?

    class TMyClass
    {
    private:
    int FMyVariable;
    void __fastcall SetMyVariable(i nt Value) { FMyVariable = Value;}
    int __fastcall GetMyVariable() { return FMyVariable;}
    public:
    __property int MyVariable = {read=GetMyVari able, write=SetMyVari able};

    };
    TMyClass MyClass;

    void __fastcall TMyForm::Button 2Click(TObject *Sender)
    {
    MyClass.MyVaria ble = 1234;
    Caption = IntToStr(MyClas s.MyVariable);
    }


  • John Harrison

    #2
    Re: How to implement "__propert y" in ANSI C++ ?


    <David> wrote in message
    news:4100e2fd$0 $25461$afc38c87 @news.optusnet. com.au...[color=blue]
    > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
    > term to dump Borland (not only BCB).
    > As a part of my attempt to dump long-loved BCB I'm trying to investigate[/color]
    how[color=blue]
    > one can implement "__property " in ANSI C++.
    >
    > Would anyone have a solution hot to implement "__property " in ANSI C++ ie.
    > how to make following code compile in non-BCB C++ compiler?
    >
    > class TMyClass
    > {
    > private:
    > int FMyVariable;
    > void __fastcall SetMyVariable(i nt Value) { FMyVariable = Value;}
    > int __fastcall GetMyVariable() { return FMyVariable;}
    > public:
    > __property int MyVariable = {read=GetMyVari able, write=SetMyVari able};
    >
    > };
    > TMyClass MyClass;
    >
    > void __fastcall TMyForm::Button 2Click(TObject *Sender)
    > {
    > MyClass.MyVaria ble = 1234;
    > Caption = IntToStr(MyClas s.MyVariable);
    > }[/color]

    There is no way to make that code compile in standard C++. Are you asking
    how to rewrite it into standard C++? If so then try this

    class TMyClass
    {
    private:
    int FMyVariable;
    public:
    void SetMyVariable(i nt Value) { FMyVariable = Value;}
    int GetMyVariable() const { return FMyVariable;}
    };

    TMyClass MyClass;

    void TMyForm::Button 2Click(TObject *Sender)
    {
    MyClass.SetMyVa riable(1234);
    Caption = IntToStr(MyClas s.GetMyVariable ());
    }

    john


    Comment

    • Jacques Labuschagne

      #3
      Re: How to implement &quot;__propert y&quot; in ANSI C++ ?

      David wrote:
      [color=blue]
      > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
      > term to dump Borland (not only BCB).
      > As a part of my attempt to dump long-loved BCB I'm trying to investigate how
      > one can implement "__property " in ANSI C++.
      >
      > Would anyone have a solution hot to implement "__property " in ANSI C++ ie.
      > how to make following code compile in non-BCB C++ compiler?
      >
      > class TMyClass
      > {
      > private:
      > int FMyVariable;
      > void __fastcall SetMyVariable(i nt Value) { FMyVariable = Value;}
      > int __fastcall GetMyVariable() { return FMyVariable;}
      > public:
      > __property int MyVariable = {read=GetMyVari able, write=SetMyVari able};
      >
      > };
      > TMyClass MyClass;
      >
      > void __fastcall TMyForm::Button 2Click(TObject *Sender)
      > {
      > MyClass.MyVaria ble = 1234;
      > Caption = IntToStr(MyClas s.MyVariable);
      > }
      >
      >[/color]

      Macros? It would be pretty ugly, and you'd have to modify all the code.
      Probably not worth it.

      class C{
      public:
      DECLARE(int, MyVariable, GetMyVariable, SetMyVariable);
      };

      with
      #define DECLARE(type, name, get, set) \
      type name; \
      type get()const{ return name; } \
      type set(const type& t){ name = t; }

      The other option is a preprocessor that runs over your code converting
      the Borland syntax into real C++. This sounds easy.

      Jacques.

      Comment

      • roman ziak

        #4
        Re: How to implement &quot;__propert y&quot; in ANSI C++ ?

        <David> wrote in message
        news:4100e2fd$0 $25461$afc38c87 @news.optusnet. com.au...[color=blue]
        > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
        > term to dump Borland (not only BCB).
        > As a part of my attempt to dump long-loved BCB I'm trying to investigate[/color]
        how[color=blue]
        > one can implement "__property " in ANSI C++.
        >
        > Would anyone have a solution hot to implement "__property " in ANSI C++ ie.
        > how to make following code compile in non-BCB C++ compiler?[/color]

        There is no way other than get_Xyz and set_Xyz ... which involves rewriting
        the
        entire code.

        Since Borland did not have BCB for others, I assume you use MS platforms.
        Did you consider managed C++ ?


        Roman


        Comment

        • JoeRB

          #5
          Re: How to implement &quot;__propert y&quot; in ANSI C++ ?

          <David> wrote in message news:<4100e2fd$ 0$25461$afc38c8 7@news.optusnet .com.au>...[color=blue]
          > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
          > term to dump Borland (not only BCB).
          > As a part of my attempt to dump long-loved BCB I'm trying to investigate how
          > one can implement "__property " in ANSI C++.
          >
          > Would anyone have a solution hot to implement "__property " in ANSI C++ ie.
          > how to make following code compile in non-BCB C++ compiler?
          >
          > class TMyClass
          > {
          > private:
          > int FMyVariable;
          > void __fastcall SetMyVariable(i nt Value) { FMyVariable = Value;}
          > int __fastcall GetMyVariable() { return FMyVariable;}
          > public:
          > __property int MyVariable = {read=GetMyVari able, write=SetMyVari able};
          >
          > };
          > TMyClass MyClass;
          >
          > void __fastcall TMyForm::Button 2Click(TObject *Sender)
          > {
          > MyClass.MyVaria ble = 1234;
          > Caption = IntToStr(MyClas s.MyVariable);
          > }[/color]

          How about using templates? You could do something like this -

          template <typename T, typename C>
          class Property
          {
          typedef T (C::*Get)() const;
          typedef void (C::*Set)(T);
          Get GetFunc_;
          Set SetFunc_;
          C & Class_;
          public:
          Property(Get GetFunc, Set SetFunc, C &Class)
          : GetFunc_(GetFun c), SetFunc_(SetFun c), Class_(Class) {}

          operator T () const
          {
          return (Class_.*GetFun c_)();
          }

          Property<T, C>& operator=(T val)
          {
          (Class_.*SetFun c_)(val);
          return *this;
          }
          };

          class TMyClass
          {
          int FMyVariable;
          public:
          Property<int, TMyClass> MyVariable;
          TMyClass() : MyVariable(GetM yVariable, SetMyVariable, *this)
          {
          }
          int GetMyVariable() const
          {
          return FMyVariable;
          }
          void SetMyVariable(i nt Value)
          {
          FMyVariable = Value;
          }
          };

          Comment

          • Serge Paccalin

            #6
            Re: How to implement &quot;__propert y&quot; in ANSI C++ ?

            Le vendredi 23 juillet 2004 à 11:35, David a écrit dans comp.lang.c++ :
            [color=blue]
            > Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our
            > term to dump Borland (not only BCB).
            > As a part of my attempt to dump long-loved BCB I'm trying to investigate how
            > one can implement "__property " in ANSI C++.
            >
            > Would anyone have a solution hot to implement "__property " in ANSI C++ ie.
            > how to make following code compile in non-BCB C++ compiler?
            >
            > class TMyClass
            > {
            > private:
            > int FMyVariable;
            > void __fastcall SetMyVariable(i nt Value) { FMyVariable = Value;}
            > int __fastcall GetMyVariable() { return FMyVariable;}
            > public:
            > __property int MyVariable = {read=GetMyVari able, write=SetMyVari able};
            >
            > };
            > TMyClass MyClass;
            >
            > void __fastcall TMyForm::Button 2Click(TObject *Sender)
            > {
            > MyClass.MyVaria ble = 1234;
            > Caption = IntToStr(MyClas s.MyVariable);
            > }[/color]

            What about the following?

            class Int
            {
            private:
            int IntValue_;
            public:
            Int & operator=(int Value) { IntValue_ = Value; }
            operator int() const { return IntValue_; }
            };

            class TMyClass
            {
            public:
            Int MyVariable;

            };
            TMyClass MyClass;

            void __fastcall TMyForm::Button 2Click(TObject *Sender)
            {
            MyClass.MyVaria ble = 1234;
            Caption = IntToStr(MyClas s.MyVariable);
            }

            --
            ___________ 2004-07-26 08:39:13
            _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
            \ \_L_) Il faut donc que les hommes commencent
            -'(__) par n'être pas fanatiques pour mériter
            _/___(_) la tolérance. -- Voltaire, 1763

            Comment

            • Old Wolf

              #7
              Re: How to implement &quot;__propert y&quot; in ANSI C++ ?

              <David> wrote in message news:[color=blue]
              >
              > As a part of my attempt to dump long-loved BCB I'm trying to investigate how
              > one can implement "__property " in ANSI C++.[/color]

              The draft standard for C++/CLI includes 'property' with a different
              (and better) syntax. In fact CLI objects are a ripoff of VCL objects.
              You could wait around until either BCB 9 comes out, or some C++/CLI
              compilers come out.
              [color=blue]
              > Would anyone have a solution hot to implement "__property " in ANSI C++ ie.
              > how to make following code compile in non-BCB C++ compiler?
              >
              > class TMyClass
              > {
              > private:
              > int FMyVariable;
              > void __fastcall SetMyVariable(i nt Value) { FMyVariable = Value;}
              > int __fastcall GetMyVariable() { return FMyVariable;}
              > public:
              > __property int MyVariable = {read=GetMyVari able, write=SetMyVari able};
              >
              > };[/color]

              class MyClass
              {
              public:
              int MyVariable;
              };

              Comment

              • Kleidemos

                #8
                Re: How to implement &quot;__propert y&quot; in ANSI C++ ?

                You can try this mothod

                class Test
                {
                private:
                int _data;
                public:
                Test(int data ): _data(data){};
                ~Test(){};
                int& Data(){ return _data; };
                };

                You, now, can both read a value and write a value.
                Test t(10);
                int i = t.Data(); // i == 10
                t.Data() = 15; // i == 15
                i = t.Data();
                --
                Tnk

                Luca "Kleidemos" Francesca

                Un computer a un altro quando si incontrano:
                "Ciao, come ti boota oggi???"

                Comment

                Working...