Recommended C ++ Library Design

I am developing a C ++ library providing various functions related to image / signal processing and other things. Its basically a development library for future reference by developers. I want it to be as user-friendly and easy to use as possible. I have 3 different models:

Model 1:

One large namespace containing all the functionality of the library. for example the C ++ standard library is implemented internally namespace std

. Or OpenCV is implemented internally namespace cv

.

namespace library
{
    //all classes, variables, functions, datatypes are present inside this namespace
}

      

Model 2:

Parent namespace, further subdivided into child namespaces according to functionality. for example, the parent of the .NET Framework namespace System

contains namespace Collections

, namespace Windows

etc.

namespace library
{
    //datatypes go here

    namespace group1
    {
       //functions related to group 1
    }

    namespace group2
    {
       //functions related to group 2
    }

    .
    .
    .
}

      

Model 3:

Almost the same as Model 2, but contains functions as static

class members instead of namespaces.

namespace library
{
   //datatypes go here

   class group1
   {
     public:
        static function1();
        static function2();
   }

   class group2
   {
     public:
        static function1();
        static function2();
   }
}  

      

I need a recommendation, which of these design models is better? Is there any other better approach? I currently like the second model.

+3


source to share


1 answer


If functions can be used independently (it seems like they are by looking at your suggestions) and multiple functions can be grouped semantically, I would absolutely choose the second option.


Option 1: putting everything in one namespace

will add too many different things grouped into one group.

Option 2: I find it more intuitive than others. Since the library brings together several very different things, it seems like a good idea to group everything in groups and all of those groups to be part of a larger one namespace

with the name of the library.

Option 3: These options are almost the same as Option 2, but using class

instead namespace

seems bad to me. The C ++ standard gives you namespace

for this, use them.




But it really depends on the library you are going to write. Honestly, choosing one of these options and then switching to the other won't be a big deal. So you can start using one of these options, and if things get ugly and you see some of the other options being better choices, just do it.


Example: I have my own library that implements wrappers for sockets, streams, IPC communication, etc. I have chosen exactly what you suggest in Option 2.

+5


source







All Articles