Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two or more C++ namespaces into one

First of all, sorry for my English.

Ok, I'm working a program that performs a specific process. This process needs some classes and functions to be defined. All of them must be organized in blocks to access them.

My first idea was to work with namespaces (C++), to get something like this:

namespace LoadSystem
{
   namespace ParseBlock1
   {
      class ClassA {...}
      class ClassB {...}
      class ClassC {...}
   }
   namespace ParseBlock2
   {
      class ClassA {...}
      class ClassB {...}
      class ClassC {...}
   }
}

So, I was reading to get the best idea if this is good or not. I already have read that I must not use lots of nested namespaces, so, for my purpose, the minimum levels are two, as shown above.

My objective is to be able to add more and more ParseBlocks into the LoadSystem namespace. It will be stored in a single .h file, so, there will be only interfaces of the classes. Since there can be lots of classes per block, I want to split the definition of each block into other .h files to keep the main .h file small as I can.

So, I ended with an idea of defining a file block1.h and block2.h, each one with a structure like this:

namespace LoadSystem
{
   namespace ParseBlock1
   {
      class ClassA {...}
      class ClassB {...}
      class ClassC {...}
   }
}

and

namespace LoadSystem
{
   namespace ParseBlock2
   {
      class ClassA {...}
      class ClassB {...}
      class ClassC {...}
   }
}

And import them in the load_system.h file. So, every time I need to add another block, I write the needed files, and finally, I just import the new blockX.h into the main load_system.h.

Then, I must be able to access both blocks from the same namespace using LoadSystem::ParseBlock1::Class1 or LoadSystem::ParseBlock2::Class1.

I have tested this with simple integer values and it works. The namespaces combine and I can access the values without any warning (I used gcc -Wall -Werror -Wextra -pedantic).

So, is this combination of namespaces is correct or not. Maybe it works, but I maybe should not use it, I don't know.

Also, I want to know if this process, of importing a "master" header file (which imports other header files) is also correct or not (I'm using the needed #ifndef, #define and #endif macros to guard from multiple imports), I'm using something like this:

# ifndef LOAD_SYSTEM_H_
# define LOAD_SYSTEM_H_

# include "block1/block1.h"
# include "block2/block2.h"

# endif

So, please help me to know if this is correct or not.

like image 585
Moises Chavez Avatar asked Nov 04 '22 07:11

Moises Chavez


1 Answers

You can always extend an existing namespace, so that part is OK.

And that part is the only that has a simple technical answer.

Regarding "master header file", it's more subjective, a matter of personal preference. I prefer that the included headers can be included on their own without any prerequisites (like including other stuff before them). If so, then all's OK for me, but if not, then users of your code will in practice have to include the big master header file in order to get any little smaller header, and that can affect build times negatively (and if they don't, but themselves include the prerequisites, then they have brittle code that can stop working when you update a header).

like image 86
Cheers and hth. - Alf Avatar answered Nov 13 '22 05:11

Cheers and hth. - Alf