0

I have a BinaryMemoryReader class that has a function defined like this:

template <typename T>
inline void read(T **data) {
    *data = (T*)&stream[position];
    position += sizeof(T);
}

It handles most of the standard types from a stream in memory.

The custom complex types have read functions of their own, such as:

void AnotherClass::read(BinaryMemoryReader *const reader)
{
    reader->read<bool>(&num);
    reader->read<short>(&count);
}

What I'd like to is write the code that would, when I write

reader->read<AnotherClass>(&var);

would call the second function. It would mean that AnotherClasss read function would overload the BinaryMemoryReaders read function for that particular type.

It would allow me to write a much more cleaner code.

EDIT: The general idea is this:

  1. Have a central BinaryMemoryReader class.
  2. Have a generic read function within it to deal with the standard types.
  3. Have the syntax to call it be bmr->read<int>(&intVar);
  4. Have specialized read functions defined in their own classes.
  5. Most importantly, the syntax to call them would be bmr->read<customType>(&customTypeVar);

That way the specific read functions would be associated with their own classes, but would be able to be called from the BinaryMemoryReader.

12
  • Google "C++ template specialization". Commented Jul 9, 2017 at 9:51
  • 1
    Note that each of the additional reader functions are defined in their own classes, not in the BinaryMemoryReader. Commented Jul 9, 2017 at 9:52
  • 1
    Why not making a free template function, that takes a BinaryMemoryReader parameter and a T? This one could be easily specialized in any other class module. Commented Jul 9, 2017 at 9:56
  • It should be possible I think to use ADL select the best overload if you a read function in the global namespace, but I'm not sure how to achieve this in your case with pointers and such. Commented Jul 9, 2017 at 10:06
  • What do you mean by free? Where do I define it? Could I call with a reference to the BinaryMemoryReader class? Would there be a way to have the same call for the standard types and the custom types? Commented Jul 9, 2017 at 10:09

1 Answer 1

1

As I wrote in my comment I would use the following approach:

  1. Have a templated free function

    template <typename T>
    void read(BinaryMemoryReader& bmr, T& data) {
        bmr.read(&data);
    }
    
  2. Specialize that function for each of the types you want to handle

    class AnotherClass {
         template <typename T>
         friend void read(BinaryMemoryReader& bmr, T &data);
    
         bool num;
         short count;
     };
    
     template <>
     void read(BinaryMemoryReader& bmr, AnotherClass &data) {
         bmr.read<bool>(&(data.datanum));
         bmr.read<short>(&(data.count));
     };
    
  3. Call it like

     BinaryMemoryReader bmr;
     AnotherType at;
     read(bmr,at);
    

That technique is used for instance with the c++ standard I/O library and overloads provided for the

 std::ostream& operator<<(std::ostream&, const T&);
 std::istream& operator>>(std::istream&, T&);

functions.


Alternatively you could do that specialization for the BinaryMemoryReader::read() function. But BinaryMemoryReader must be friend of all class specializations.

Sign up to request clarification or add additional context in comments.

3 Comments

I dislike doing this in the global namespace. Is there on way to limit this to the BinaryMemoryReader class and the types that implement the specific readers? Even if it means writing a bit more of the boilerplate code?
@Karlovsky120 Sure, you should do that using a specific namespace. Specializing the BinaryMemoryReader::read() function would work as well IMO.
What I mean is that I dislike doing this outside of the classes. While keeping the code in the other class? I'm not sure how to use delegates, but could they be implemented here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.