patterncppModerate
Use of nested namespaces in 3D graphics engine library
Viewed 0 times
enginenamespacesgraphicsnestedlibraryuse
Problem
I know that nested namespaces are used in C++ very rarely. But I think it's a nice solution to exclude types from global scope and sometimes it helps to write programs faster when we use things like "IntelliSense" or search for documentation (maybe it's similar for C# developers).
I've tried to organize my simple 3D graphics engine library in this way. Here is its main header file:
```
#ifndef RAZOR_H
#define RAZOR_H
#pragma warning (disable: 4482)
#include
#include
#include
#include
#include
#include
#include
#include
#include "Externals\wglext.h"
#include "Externals\gl3.h"
namespace Razor
{
namespace Framework
{
#include "DisplayOrientation.h"
#include "ButtonState.h"
#include "GameTime.h"
#include "Rect.h"
#include "Point.h"
#include "Color.h"
#include "Vector2.h"
#include "Vector3.h"
#include "Vector4.h"
#include "Matrix.h"
#include "GameWindow.h"
#include "Game.h"
namespace IO
{
#include "File.h"
}
namespace Input
{
#include "ButtonState.h"
#include "MouseState.h"
#include "Keyboard.h"
#include "Mouse.h"
#include "TouchPanel.h"
}
namespace Graphics
{
#include "PrimitiveType.h"
#include "VertexElementFormat.h"
#include "VertexElementUsage.h"
#include "VertexElement.h"
#include "VertexDeclaration.h"
#include "OpenGLExtensions.h"
#include "DepthFormat.h"
#include "BufferUsage.h"
#include "PresentationParameters.h"
#include "RenderTarget.h"
#include "OpenGLVersion.h"
#include "Viewport.h"
#include "Shader.h"
#include "VertexShader.h"
#include "FragmentShader.h"
#include "GeometryShader.h"
#include "Shader
I've tried to organize my simple 3D graphics engine library in this way. Here is its main header file:
```
#ifndef RAZOR_H
#define RAZOR_H
#pragma warning (disable: 4482)
#include
#include
#include
#include
#include
#include
#include
#include
#include "Externals\wglext.h"
#include "Externals\gl3.h"
namespace Razor
{
namespace Framework
{
#include "DisplayOrientation.h"
#include "ButtonState.h"
#include "GameTime.h"
#include "Rect.h"
#include "Point.h"
#include "Color.h"
#include "Vector2.h"
#include "Vector3.h"
#include "Vector4.h"
#include "Matrix.h"
#include "GameWindow.h"
#include "Game.h"
namespace IO
{
#include "File.h"
}
namespace Input
{
#include "ButtonState.h"
#include "MouseState.h"
#include "Keyboard.h"
#include "Mouse.h"
#include "TouchPanel.h"
}
namespace Graphics
{
#include "PrimitiveType.h"
#include "VertexElementFormat.h"
#include "VertexElementUsage.h"
#include "VertexElement.h"
#include "VertexDeclaration.h"
#include "OpenGLExtensions.h"
#include "DepthFormat.h"
#include "BufferUsage.h"
#include "PresentationParameters.h"
#include "RenderTarget.h"
#include "OpenGLVersion.h"
#include "Viewport.h"
#include "Shader.h"
#include "VertexShader.h"
#include "FragmentShader.h"
#include "GeometryShader.h"
#include "Shader
Solution
I don't think there is anything inherently wrong with nested namespaces.
I also disagree that it is rare. It is just not exposed directly.
But like all features they can be abused so use judiciously. But saying that I see nothing wrong with your namespace hierarchy. BUT the way you are implementing it is a very bad idea.
If I include the file "File.h" I expect everything included to be declared in the correct namespace. But the way you define it, it is now possible to include it in a way that puts the classes in the wrong namespace.
Each header file should be designed so that it can not be used incorrectly.
I also disagree that it is rare. It is just not exposed directly.
std::tr1
CompanyName::ProductName
CompanyName::ProductName::DetailsBut like all features they can be abused so use judiciously. But saying that I see nothing wrong with your namespace hierarchy. BUT the way you are implementing it is a very bad idea.
If I include the file "File.h" I expect everything included to be declared in the correct namespace. But the way you define it, it is now possible to include it in a way that puts the classes in the wrong namespace.
Each header file should be designed so that it can not be used incorrectly.
Code Snippets
std::tr1
CompanyName::ProductName
CompanyName::ProductName::DetailsContext
StackExchange Code Review Q#4132, answer score: 15
Revisions (0)
No revisions yet.