I am trying to create a new type that will take a binary packed input (unsigned long shown below) and unpack it into another type (double shown below).
I can overload the operator= method to determine if input/RHS is binary (unsigned long) or already unpacked (all others), however I do not know if it is possible to overload such the output/LHS of operator= will be correct (without forcing the user to specify the member attribute).
1 2 3 4 5 6 7 8 9 10 11
struct s32_11_t {
s32_11_t(void) : val(0.0) {}
s32_11_t(double init_val) : val(init_val) {}
double val;
template <typename T> s32_11_t& operator=(T new_val) {this->val = static_cast<double>(new_val); return *this;}
template <> s32_11_t& operator=(unsignedlong new_bin) {/* TODO */ return *this;}
};
s32_11_t obj;
obj = (unsignedlong)0x123456; // unpack binary value
double obj_val = obj.val; // can anything be done so that ".val" can be left out???
Is this possible? Is there a better way to go about tackling this problem with data packing/unpacking build in somehow? It will have to be done for lots of data packed lots of ways, so I was planning on creating a type for each packed data type so that they can be handled easily in the code without worrying about packing/unpacking during usage!
You should probably write a copy constructor and a copy assignment constructor for this. (unrelated)
Anyways - using the assignment operator here really just seems to cloud what you are trying to do, pack() and unpack() methods respectively would probably be better. Something like
unpackSomething
unpackSomethingElse
But I'm not too sure what you are actually trying to achieve with this to be honest...
Thank you for the response. Basically, I will be pulling packed binary data off of a bus (lots of it) and would like to make using it in engineering values (ie. type double units feet) as transparent to the user as possible.
Then, after manipulation, when it is ready to go back on the bus I would like to make repacking it also as transparent as possible.
ie.
1 2 3 4 5 6 7
s32_11_t obj;
unsignedint binary_value(0x1234);
obj = binary_value; // pull off bus (packed binary)
obj = obj*2; // manipulate somehow (engineering)
double save_val = obj; // i would rather not have to use obj.val here b/c a lot of code will do this
binary_value = obj.bin(); // push back to bus (packed binary)
Do all kinds of data you are pulling allow the same kinds of operation on them (like, are all of them data types for which multiplication, addition etc make sense?).
Yes, all will probably be overloaded for all the standard math operators (+ - * / += -= *= /=).
They will all break down to engineering types of float, double, int, unsigned int, short, unsigned short, char, unsigned char, but will be pulled from packed bit fields of binary words (typed to unsigned long to differentiate binary vs. engineering data).
class Value
{
public:
virtual Value operator+...=0;
virtual Value operator*...=0;
virtualunsignedint pack();
};
class ValueFactory
{
public:
static CharValue unpackChar
static DoubleValue unpackDouble
};
Then you could deal with Value whenever you don't care so much about what's actually inside, and classes like CharValue if you do. Of course, if it's syntactic sugar you want this won't really help - for example for this double save_val = obj;
I think you could overload operatordouble() const; here, but I'm not sure.