# InterprocessConnection and reading/writing a MemoryBlock

**URL:** <https://forum.juce.com/t/interprocessconnection-and-reading-writing-a-memoryblock/36985>\
**Category:** General JUCE discussion\
**Created:** [January 6, 2020, 1:40pm UTC](https://forum.juce.com/t/interprocessconnection-and-reading-writing-a-memoryblock/36985 "2020-01-06T13:40:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![StefanS](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@StefanS](https://forum.juce.com/u/StefanS)\
**Post date:** [January 6, 2020, 1:40pm UTC](https://forum.juce.com/t/interprocessconnection-and-reading-writing-a-memoryblock/36985/1 "2020-01-06T13:40:26Z")

</div>

Hi all,

I’m writing an InterprocessConnection Plug-In/App and am a bit struggling with the MemoryBlock handling. So far I have this code for primitive types like bool, int, float, etc

```
// sending process: preparing the memory block for sending
int someInt = 46;
MemoryBlock mBlock;
MemoryOutputStream mos(mBlock, false);
mos.writeInt(someInt);
interprocessSender.sendMessage(mBlock);

// receiving process: callback for reading the received memory block
void messageReceived (const MemoryBlock& message) 
{
MemoryInputStream mis(message, false);
int result = mis.readInt();
}

```

This is working so far. I also read (and understand) the InterprocessConnection example code for sending and receiving Strings. But I wonder how I can transmit other types like custom structs. Will I have to read the memory block byte per byte and writing my own code to reconstruct it? Or is there an easier and safer way to do this?

Thanks  
Stefan

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [January 6, 2020, 1:54pm UTC](https://forum.juce.com/t/interprocessconnection-and-reading-writing-a-memoryblock/36985/2 "2020-01-06T13:54:53Z")

</div>

> [@StefanS](#):
>
> Will I have to read the memory block byte per byte and writing my own code to reconstruct it? Or is there an easier and safer way to do this?

Since C++ has no reflection (you can’t query what member variables classes have), you will have to do it manually. If your class is trivial to construct and destroy (including all its member variables), you may be able to get away by doing a byte level copy into and back from the MemoryBlock.

Example :

```auto
class Baz
{
public:
	double x = 0.0;
	double y = 1.0;
	int z = 3;
};

MemoryBlock g_block; 

void test_serialization()
{
	{
		Baz ob;
		ob.x = 42.777;
		g_block = MemoryBlock{ (void*)&ob,sizeof(Baz) };
	}
	{
		Baz ob;
		memcpy(&ob, g_block.getData(), sizeof(Baz));
		std::cout << ob.x << " " << ob.y << " " << ob.z << "\n";
	}
}

```

This isn’t very flexible and safe, though. All the member variables of the class that is serialized this way need to be trivial types like ints and doubles.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [January 6, 2020, 2:29pm UTC](https://forum.juce.com/t/interprocessconnection-and-reading-writing-a-memoryblock/36985/3 "2020-01-06T14:29:28Z")

</div>

With a bit of templating, you can use the same function to read/write to a stream, which makes round-tripping a lot more robust (there’s no danger that you edit the ‘write’ function and forget to update the ‘read’ function). This is also a lot safer than just writing the raw bits that make up the object into a stream.

```auto
// Wrappers around the juce streams...
struct OutputArchive final {
  OutputArchive(OutputStream& stream) : stream_{stream} {}

  void operator()(bool x) { stream_.writeBool(x); }
  void operator()(int x) { stream_.writeInt(x); }
  void operator()(float x) { stream_.writeFloat(x); }
  void operator()(const std::string& x) { stream_.writeString(x); }

  OutputStream& stream_;
};

struct InputArchive final {
  InputArchive(InputStream& stream) : stream_{stream} {}

  void operator()(bool& x) { x = stream_.readBool(); }
  void operator()(int& x) { x = stream_.readInt(); }
  void operator()(float& x) { x = stream_.readFloat(); }
  void operator()(std::string& x) { x = stream_.readString(); }

  InputStream& stream_;
};

struct MyComplexStruct final {
  bool bool_;
  int int_;
  float float_;
  std::string string_;

  // This function is used to both read and write this object!
  template <typename Archive>
  void serialize(Archive& archive) {
    archive(string_);
    archive(bool_);
    archive(int_);
    archive(float_);
  }
};

int main() {
  MyComplexStruct str{true, 543, 0.12, "hello world"};

  MemoryOutputStream outputStream;
  OutputArchive outputArchive{outputStream};
  // When we call serialize with an OutputArchive, the object writes its
  // contents into an output stream
  str.serialize(outputArchive);

  MemoryInputStream inputStream{outputStream.getData(),
                                outputStream.getDataSize(), false};
  InputArchive inputArchive{inputStream};

  MyComplexStruct copy;
  // When we call serialize with an InputArchive, the object updates its data
  // members using the next items in the input stream
  copy.serialize(inputArchive);

  std::cout << copy.bool_ << '\n';
  std::cout << copy.int_ << '\n';
  std::cout << copy.float_ << '\n';
  std::cout << copy.string_ << '\n';
}

```

---

<div class="post-metadata">

**Author:** ![StefanS](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@StefanS](https://forum.juce.com/u/StefanS)\
**Post date:** [January 6, 2020, 3:03pm UTC](https://forum.juce.com/t/interprocessconnection-and-reading-writing-a-memoryblock/36985/4 "2020-01-06T15:03:22Z")

</div>

Great! Thank you Xenakios and reuk!

Both solutions are working in my scenario, but I think I will prefer reuks way using the stream wrapper approach. Seems to me a bit more robust.

@reuk I had to replace std::string with String to get the code compiling. Seems that

> bool OutputStream::writeString(const String& text)

and

> String InputStream::readStream( )

don’t work with std::string.

Thanks again for the quick replies, you saved my day!  
Stefan

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [January 6, 2020, 3:05pm UTC](https://forum.juce.com/t/interprocessconnection-and-reading-writing-a-memoryblock/36985/5 "2020-01-06T15:05:25Z")

</div>

Oops, sorry about that. I tested with std types on [godbolt.org](http://godbolt.org), but I must have forgotten to switch that parameter to the juce eqivalent
