a C++ library of mine.

This is a discussion on a C++ library of mine. within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Accessors are not horrible if they are relevant to the problem domain. get/set is horrible...I avoided this issue by using ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Accessors are not horrible if they are relevant to the problem domain.

    get/set is horrible...I avoided this issue by using function overloading.

    form.caption(str); //set the caption of form
    str = form.caption(); //get the caption of from;
    How is that not an accessor, anyway? I mean yeah, you didn't name the pair get and set, but a rose by any other name ...

  2. #17
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Quote Originally Posted by citizen View Post
    Accessors are not horrible if they are relevant to the problem domain.
    How is that not an accessor, anyway? I mean yeah, you didn't name the pair get and set, but a rose by any other name ...
    I can accept

    form.caption(str); //set
    str = form.caption(); //get

    the name caption is more flexibler than get_caption/set_catpion, it's easy and harmless.

    a property has to face an important issue. e.g.
    Code:
    template<typename T>
    struct property
    {
            operator T()const { return T(); }
            
    };
    assume a form has a caption property object by type of property<std::string>
    Code:
    std::string str;
    str = form.property; //OK,  operator T() works
    str == form.property; //!!
    Can't deduce the template parameter of bool operator==(const std::basic_string<C, T, A>&, const std::basic_string<C, T, A>&) with form.property.

    fix the issue by overloading the operator==, but this solution is not good enough in my opinion, too many operators need to be overloaded.
    Last edited by jinhao; 09-10-2008 at 10:24 AM.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    No, you're missing the point. The pair of functions

    std::string &form::caption() const
    void form::caption(std::str &)

    is as much an accessor as get_caption and set_caption would be, so as a matter of fact you did not avoid the concept of accessors at all.

    And the problem that you have with implementing a property in C++ is that it must be read only at times as well as capable of changing state. The most straightforward way seems to be through a reference, but that doesn't hide your data very well.

  4. #19
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    I just think about whether the property is necessary in a C++ class. the form::caption is not only an assignment operation, but also setting the caption of a window. Actually, a generic property template is not cheap for some situations.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  5. #20
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Well I'm glad we agree

  6. #21
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Code:
    form.SetCaption(str);
    str = form.GetCaption();
    I honestly don't see the problem with this. It's so beautifully verbose. I realize you already reached a consensus. So never mind me.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    I have a problem with it mostly due to two [four] reasons:
    1) Having to write two functions to do thing (basically).
    2) Having two functions littering the definition instead of one.
    And the [optional] 3rd) It's just ugly to have to type Get/Set.
    And the [optional] 4th) It just feels more natural to be able to use operator = to assign and nothing to get since it feels like it's a property of the object - not some function that changes the state of the object.

    1 and 2 can be solved using a generic template, alternatively two functions for advanced functionality.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Disrupting the universe Mad_guy's Avatar
    Join Date
    Jun 2005
    Posts
    258
    Is there a reason you have your own custom license for the project instead of just releasing it under the GPL or BSD license (or even public domain)?
    operating systems: mac os 10.6, debian 5.0, windows 7
    editor: back to emacs because it's more awesomer!!
    version control: git

    website: http://0xff.ath.cx/~as/

  9. #24
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Quote Originally Posted by Elysia View Post
    The function pointer can be passed via the template declaration instead to save some bytes...
    4 bytes overhead is not much. It's typically what a smart pointer takes (or less).
    I find it acceptable between functionality and overhead/size.
    I have implemented a generic property template before. like this

    Code:
    template<typename T, typename Class>
    class property
    {
        typedef property self_type;
    public:
        property(Class& obj, void (Class::* setter)(const T&), const T& (Class::*getter)() const)
            :object_(obj), setter_(setter), getter_(getter)
        {}
    
        operator T() const
        {
             return object_.getter_();
        }
    
        self_type& operator=(const T&t)
        {
             object_.setter_(t);
             return *this;
        }
        
    private:
        Class& object_;
        void (Class::* setter_)(const T&);
        const T& (Class::*getter_)() const;
    };
    the length setter_ and getter_ is depend on compiler and Class. one property object takes 20 bytes, a widget has 10 property objects, a program has 20 widgets, so the program needs more 4000 bytes. it's not cheap. -_-!
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  10. #25
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Quote Originally Posted by Mad_guy View Post
    Is there a reason you have your own custom license for the project instead of just releasing it under the GPL or BSD license (or even public domain)?
    I don't like GPL.. -_-! releasing with my own license. refer to http://stdex.sourceforge.net/LICENSE_1_0.txt
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    I don't like GPL.. -_-! releasing with my own license. refer to http://stdex.sourceforge.net/LICENSE_1_0.txt
    While your license appears to comply with the Open Source Definition, it is not OSI approved, and is similiar to the BSD license and perhaps the MIT/Expat license, so why not use an existing OSI approved permissive license?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    The reason is that i've not read BSD/MIT license -_-!
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  13. #28
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    That isn't an excuse. While nothing may come of it in the end, your licensing terms should be protected by a private entity in the event of your site's death and copies of the license are no longer available there, and especially if a copy of the license need not be distributed as part of the terms.

    Otherwise it would just be your word, which makes your code public domain ex post facto.

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Quote Originally Posted by jinhao View Post
    I have implemented a generic property template before. like this

    Code:
    template<typename T, typename Class>
    class property
    {
        typedef property self_type;
    public:
        property(Class& obj, void (Class::* setter)(const T&), const T& (Class::*getter)() const)
            :object_(obj), setter_(setter), getter_(getter)
        {}
    
        operator T() const
        {
             return object_.getter_();
        }
    
        self_type& operator=(const T&t)
        {
             object_.setter_(t);
             return *this;
        }
        
    private:
        Class& object_;
        void (Class::* setter_)(const T&);
        const T& (Class::*getter_)() const;
    };
    the length setter_ and getter_ is depend on compiler and Class. one property object takes 20 bytes, a widget has 10 property objects, a program has 20 widgets, so the program needs more 4000 bytes. it's not cheap. -_-!
    This can be changed. Let me demonstrate.
    Code:
    template<typename T, typename Class, void (Class::* setter_)(const T&), const T& (Class::*getter_)() const>
    class property
    {
        typedef property self_type;
    public:
        property(Class& obj)
            :object_(obj)
        {}
    
        operator T() const
        {
             return object_.getter_();
        }
    
        self_type& operator=(const T&t)
        {
             object_.setter_(t);
             return *this;
        }
        
    private:
        Class& object_;
    };
    Given, I did not compile and test this, but try it - it should work.
    That leaves just 4 bytes overhead.
    And mate - 4KB of memory is nothing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Quote Originally Posted by Elysia View Post
    This can be changed. Let me demonstrate.
    Code:
    template<typename T, typename Class, void (Class::* setter_)(const T&), const T& (Class::*getter_)() const>
    class property
    {
        typedef property self_type;
    public:
        property(Class& obj)
            :object_(obj)
        {}
    
        operator T() const
        {
             return object_.getter_();
        }
    
        self_type& operator=(const T&t)
        {
             object_.setter_(t);
             return *this;
        }
        
    private:
        Class& object_;
    };
    Given, I did not compile and test this, but try it - it should work.
    That leaves just 4 bytes overhead.
    And mate - 4KB of memory is nothing.
    this version is too restrictive. the property just can set/get one thing. if it gets/sets caption and how it gets/sets size/position and so on...
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  2. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  3. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. better c string functions
    By samps005 in forum C Programming
    Replies: 8
    Last Post: 11-04-2003, 12:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21