#Proxy Class

94 messages · Page 1 of 1 (latest)

spark valleyBOT
#

@finite gull has reached level 1. GG!

fossil halo
#

Its similar to the Adapter design pattern. The name really already explains most of it. It is a proxy class that handles access to another object/class and is usually used if there needs to be simplified interface that the proxy can offer or if the underlying resource requires a lot of tracking/manual handling etc. so that way the proxy class can take care of all the nasty stuff, check if it is used correctly and forward function calls etc. to the underlying class while offering a nicer to use interface.

finite gull
clear hatch
#

is this for school or something?

finite gull
clear hatch
finite gull
#

Moreover, it’s a subject we haven’t seen in class. They want us to learn it by informing ourselves.

clear hatch
#

A proxy class is a very loose term, it doesn't have a strict definition or strict implementation, it would depend on the context

finite gull
spark valleyBOT
#

@finite gull has reached level 2. GG!

finite gull
clear hatch
finite gull
#

An example or explanatory links if you have any. That would help me a lot.

finite gull
# clear hatch can you give more information?

I would like to put 2-byte values ​​into memory with an operator overload. The problem is that the function must return a modifiable variable, therefore an n-bit reference. One of the only solutions found is to create a proxy class.

clear hatch
#

I'd probably define a proxy class as a class that has the same interface as an other class but that wraps another class to provide a different access or behaviour

finite gull
clear hatch
#

you could just return a reference to std::int16_t

#

that's 2 bytes

finite gull
clear hatch
#

so what's the issue?

finite gull
# clear hatch so what's the issue?

I need to overload the [] operator to be able to do mem[100] = 1234; and have it properly store 2 bytes in little-endian format in a uint8_t array.

The problem is that I need to return a modifiable reference (uint16_t&), but since the data is in two separate bytes, I don't see how to do it properly.

clear hatch
#

what's the type of mem?

finite gull
clear hatch
#

no it's not

#

you can't use [] on uint8_t ^^

finite gull
clear hatch
#

it's just not an option

#

it wouldn't compile

finite gull
#

What operator overload if it does not use [] ?

clear hatch
#

what's the type of mem

finite gull
#

mem is a pointer to the array of uint8

clear hatch
#

alright

#

so std::uint8_t*

#

now that makes sense

#

you could just return (std::uint16_t&)mem[index]

finite gull
#

Yes, but the variable will not be modifiable?

clear hatch
#

why not?

finite gull
# clear hatch why not?

but after that I will get an error: the initial value of a non-constant reference must be an lvalue?

#

Operator overloading should allow reading and writing the byte in the array. This is the reason why it does not work.

#

My classmates advised me to use proxy, as it was much easier

clear hatch
finite gull
spark valleyBOT
#

@finite gull has reached level 3. GG!

clear hatch
#

it'd be like

std::uint16_t& operator[] (std::size_t index)
{
  return (std::uint16_t&)mem[index];
}
finite gull
clear hatch
finite gull
# clear hatch so what's the problem with this funciton?

I need to overload the [] operator so that I can do something like mem[100] = 1234;, and have it correctly store 2 bytes in little-endian format in a uint8_t array.

The issue is that I need to return a modifiable reference (uint16_t&), but since the data spans two separate bytes, I’m not sure how to do it cleanly.

clear hatch
finite gull
#

This function must have both read and write together.

clear hatch
#

yes

#

that's what it does

finite gull
clear hatch
#

(std::uint16_t&)

#

technically this would be a reinterpret_cast

finite gull
finite gull
fossil halo
# finite gull and how should it be implemented? do you have an example?

Its more of an Architecutural / Desing Pattern. It is not concerned with implementation too much. But I could give you an example, sure: c++ class SMTPClient { public: SMTPClient(std::string user, std::string password, std::string server); void sendEmail(std::string title, std::string message, std::string recipient); private: LowLevelSSLSocket soc; }; this would a spin for a more complex "Proxy-Class" but in the end it only functions as a proxy to the low level socket or another networking library. This probably isn't the cleanest example though.

clear hatch
fossil halo
#

but I will make sure to read up on it myself later again and possibly correct myself ^^

finite gull
finite gull
finite gull
fossil halo
#

i found this video: https://youtu.be/nKYUdgLfCO0 (didn't watch yet)

In this video we will learn about a Proxy Design Pattern In C++.

Few points about the proxy design patterns are:

  • A proxy class acts as a surrogate or placeholder for another object and controls access to it.
  • The primary purpose of a proxy class is to provide a level of indirection, allowing you to control access to the underlying object by ...
â–¶ Play video
clear hatch
fossil halo
finite gull
# fossil halo sorry I didn't get that question.

When reading and writing uint16_t values into a uint8_t array, is the usual method to use bitwise operations like masking with &, combining with |, and incrementing the index by 1 to access the second byte?

clear hatch
#

you can do it with bitshift and OR but in this case you don't need to

fossil halo
clear hatch
fossil halo
#

but I guess just reinterpret_cast ing it would be the preferred solution

#

or using a union

clear hatch
#

no an union is worst

#

and definitely UB

finite gull
#

So my solution would not be ideal ? for example : If my 16-bit variable is foo, then foo & 0xff gives me the lower 8 bits, and (foo & 0xff00) >> 8 gives me the upper 8 bits. To combine them again, I would use foo = foolow | (foohigh << 8)

fossil halo
# clear hatch and definitely UB

that might be, but very convenient to use. And since it doesn't have a type index it should in theory always work even if it is technically UB

clear hatch
finite gull
spark valleyBOT
#

@finite gull has reached level 4. GG!

clear hatch
fossil halo
finite gull
fossil halo
#

But same applies to many other things we use daily like #pragma once it's never been in the standard, but still works on all major compilers

#

in theory a compiler could just ignore it

spark valleyBOT
#

@fossil halo has reached level 4. GG!

finite gull