Creating wrappers

Started by
4 comments, last by PumpkinPieman 20 years, 5 months ago
Just wondering, when do you know when to make a wraper? I''m having confused idea''s of when to use one, and when not too.
Advertisement
quote:Original post by PumpkinPieman
Just wondering, when do you know when to make a wraper? I''m having confused idea''s of when to use one, and when not too.


"when do you know when to make a wraper?" - When you don''t want to type the whole WinMain code for example.
Well that''s just one example, and most people do that. Now say creating a wrapper for certain functions of the STL like multimap. I don''t want to deal with all that excessive typing, so I want to simplify it. But whilst I simplifiy it, I loose some general features that I like about the multimap. In that vase, and that you are using many classes that use it, should you or should you not create a wrapper for it?
A wrapper is a pretty general term but here I go...usually you write a wrapper layer to either simplify code or to isolate/abstract it.

If there''s a function that you use frequently that takes 10 arguments but you only change two of them in all of you calls then wrap it and default the other aguments. If you find yourself writing the same code over and over, look at it, find the patterns and wrap it in some functions or a class.

In the isolate/abstract case if there''s something you want to contain or want to be able to swap out later create an interface that wraps the functionality. The classic example in PC game programming is wrapping OpenGL and DirectX so your game can use either.



---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Sorry about the overlapped typing. As for you multimap example. Will it reduce errors, make the code easiler to read, etc? Then sure, wrap it. If you''re trying to simplify your common usage of multimap but don''t want to lose the flexibility write a proxy class or have your helper class have a member to get to the multimap for the exceptional cases.
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
Wrappers can be useful for certain libraries - DirectX is a good example as you don''t want to keep having to manually create/retrieve the DX interface and create your surfaces/palette every time you start a new project so writing a wrapper class that can accomplish this in a few function calls will make life easier

This topic is closed to new replies.

Advertisement