When using overloading in C++, how can I put the overloading function in the lhs?

1

I am a new learner of C++. I want to define a class 'ap_uint'. The goal is to conveniently implement the bit-wise operation. For example: I define an unsigned int value inside the class 'ap_uint'. I want to implement both read and write as below.

my_ap_uint(4, 2) = 7;                  // write operation, only set bits[4:2] to 3'b111
unsigne int data = my_ap_uint(4, 2);   // read operation, slice bits[4:2] and assign it to data.

Now, I can realize read operation, but I have no idea how to implement the write operation. I can define a function ap_uint::set(high_bit, low_bit, rhs), but it does not meet my requirements. I want to use 'my_ap_uint(4, 2) = 7;'

Thanks ahead!

#include "stdio.h"
#include "my_uint.h"

int main()
{
    unsigned int data;
    ap_uint my_ap_uint;

    // manually set bit[4:2] to 3'b111
    my_ap_uint.tmp = 0x0000001c;
    data = my_ap_uint(4, 2);
    printf("my_type1->tmp = %08x\n", data);

    // use function to set bit[4:1] to 4'b1111
    my_ap_uint.set(4, 1, 15);
    data = my_ap_uint(4, 1);
    printf("my_type1->tmp = %08x\n", data);

    // target to put my_ap_unit(4, 1) to the left hand side
    my_ap_uint(4, 1) = 0xff;
    data = my_ap_uint(4, 1);
    printf("my_type1->tmp = %08x\n", data);

    printf("All DONE\n");
    return 0;
}

my_unit.h

class ap_uint
{
  public:
    unsigned int tmp;

    // constructor
    ap_uint(){
        tmp = 0;
    }

    // slice the bit[b:a]
    unsigned int range(int b, int a){
        unsigned tmp1;
        unsigned tmp2;
        tmp1 = tmp >> a;
        tmp2 = (1 << (b-a+1))-1;
        return tmp1&tmp2;
    }

    // overloading () with range() function
    unsigned int operator() (int Hi, int Lo){
        return this->range(Hi, Lo);
    }

    // manually set bit[b:a] = rhs
    void set(int b, int a, unsigned int rhs){
        unsigned int hi;
        unsigned int mi;
        unsigned int lo;
        hi = (tmp >> (b+1)) << (b+1);
        lo = (tmp << (32-a)) >> (32-a);
        mi = rhs << a;
        tmp = hi | lo | mi;
    }

};
c++
overloading
asked on Stack Overflow Dec 24, 2020 by Yuanlong Xiao

2 Answers

1

Currently you are already calling the () operator on the left hand side of the = operator. This returns an unsigned int as you have it. You then try to set this unsigned int equal to 7 in your example which would not have any effect on your ap_uint object.

Try having another function that returns a different object that references the data inside your ap_uint object for that specific range, then overload the = operator for that new object.

answered on Stack Overflow Dec 24, 2020 by user14880435 • edited Dec 24, 2020 by shivlal kumavat
1

You might return proxy class:

class ap_uint
{

    struct Proxy
    {
        ap_uint* parent = nullptr;
        int hi;
        int lo;
        Proxy& operator =(unsigned u) {
         parent->set(hi, lo, u);
         return *this;
        }
    
        operator unsigned int () const {
            return parent->range(hi, lo);
        }
    };
public:
    Proxy operator() (int Hi, int Lo) {
        return {this, Hi, Lo};
    }

// ...
};

Demo

answered on Stack Overflow Dec 24, 2020 by Jarod42

User contributions licensed under CC BY-SA 3.0