Is there a Perl script to implement the functions of the / get class of a C ++ class?

I read the book Pragmatic Programmer Chapter 3 this morning about the basic tools that every programmer should have, and they mentioned Code Generation Tools. They mentioned one Perl script for C ++ programs, which helped automate the implementation of get / set () member functions for private data members.

Does anyone know about such a script and where to find it? I could not find the correct Google keywords to find it.

+3
source share
5 answers

, , ++. :

// Declare your class containing a few properties
class my_class {
public:
    property<int> x;
    property<string> y;
    ...
};

...

my_class obj;
cout << obj.x();          // Get
obj.y("Hello, world!");   // Set

:

// Utility template to choose the 2nd type if the 1st is void
template <typename T, typename U>
struct replace_void {
    typedef T type;
};

template <typename T>
struct replace_void<void, T> {
    typedef T type;
};

// Getter/setter template
template <typename T, typename D = void>
class property {
    typedef typename replace_void<D, property>::type derived_type;

    derived_type& derived() { return static_cast<derived_type&>(*this); }

public:
    property() {}   // May be safer to omit the default ctor
    explicit property(T const& v) : _v(v) {}
    property(property const& p) : _v(p._v) {}
    property& operator=(property const& p) { _v = p._v; return *this; }

    T operator()() const { return _v; }                 // Getter
    void operator()(T const& v) { derived().check(v); _v = v; }   // Setter

protected:
    // Default no-op check (derive to override)
    void check(T const& v) const { (void)v; //avoid unused variable warning}

private:
    T _v;
};

check() - , . :

class nonnegative_int : public property<int, nonnegative_int> {
public:
    // Have to redeclare all relevant ctors unfortunately :(
    nonnegative_int(int v) : property<int, nonnegative_int>(v) {}

    void check(int const& v) const {
        if (v < 0) {
            throw "Yikes! A negative integer!";
        }
    }
};

getter/setter, - !:)

check() a bool, , , . access(), .

EDIT:. - , (, property<int> x x()), , , . , getter/setter .

:. CRTP " ", check() , virtual.

+9

++ Get/Set style, .

, , ++ std::string. , , ( ):

private:
   int last, len;
   char * data;

, -/?

+3

script. , . , , . , , , , _. .

+2

, Perl C, ( ). , - , Perl script, :

struct My_struct {
    //set
    //get
    int x;

    int y;

    //get
    int z;
};

, script , "get" "set" (-) -, /. void set_x (int i), int get_x() int get_z() .

: s///. , , -, " /", , , -, .

, .

+2

Do you want the script to generate get / set functions for all your private members indiscriminately? This would not be a very useful script; you probably won’t find it on Google. If you want to somehow label your member variable and create automatically generated generated generated skeletons and / or setters, then the IDE macro seems more appropriate. Try Google for this.

+2
source

Source: https://habr.com/ru/post/1707606/


All Articles