3

The multiplatform engine we're developing for our game uses the EASTL to replace the STL, because of memory, performance and portability reasons. The EASTL can be found here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html

Because Visual Studio is really good for debugging the STL we're not using the EASTL on Windows while the engine is in development. Also some STL containers as std::queue, std::deque, ... aren't available in the EASTL. Because it doesn't matter we're using the eastl::string on Windows as well, because it's fine for debugging.

What we're doing until now is

#if (GIN_USE_EASTL)
    #define ginstl eastl
#else
    #define ginstl std
#endif

and GIN_USE_EASTL is defined as 0 on windows. The engine is called Ginkgo, thats where the GIN comes from.
We're using lists, vectors, ... like this

ginstl::list myList;
ginstl::vector myVector;

which works quite fine. But when we use a string or a queue, because of reasons I explained above, we have to write

eastl::string myString;
std::queue myQueue;

which is the reason why i'm posting here, because this is not optimal! What I want to do is use the ginstl:: macro for everything in the engine, without thinking about what implementation to use. So my approach on the problem was this:

#if (GIN_USE_EASTL)
    #define ginstl::list eastl::list
    #define ginstl::vector eastl::vector
    #define ginstl::string eastl::string
    #define ginstl::queue std::queue
#else
    #define ginstl::list std::list
    #define ginstl::vector std::vector
    #define ginstl::string eastl::string
    #define ginstl::queue std::queue
#endif

But unfortunalty that's not working, because the :: is not allowed in the #define macro. Is there any way to achieve the same thing (be able to specify which implementation I want to use for a specific datastructure) somehow different? I could go for

#define ginstl_list eastl::list

but I would prefer the structure from above! Is there a way (except writing a custom pre-processor) around the problem?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
v01pe
  • 1,096
  • 2
  • 11
  • 19
  • 1
    +1 for developing a cross-platform game :) – Niklas B. Jan 06 '12 at 18:52
  • Do you actually **need** the EASTL (yet)? Why not use the default STL implementation until you know that it's actually insufficient for your needs? Premature optimization and all that :) – jalf Jan 06 '12 at 19:02
  • actually no, we don't need it ATM, but for not using it now, the benefit of using ginstl:: would even be bigger, because we can just swap the implementation at the end. – v01pe Jan 06 '12 at 19:08
  • And I forgot, we actually do use some eastl specific implementations of the string class (.toUpper(), .toLower(), ...) – v01pe Jan 06 '12 at 20:04

3 Answers3

6

What about using namespace:

namespace ginstl
{
#if (GIN_USE_EASTL)
    using eastl::list
    using eastl::vector
    using eastl::string
    using std::queue
#else
    using std::list
    using std::vector
    using eastl::string
    using std::queue
#endif
}
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • that sounds promising! will try it tomorrow (beer-o-clock)! – v01pe Jan 06 '12 at 19:37
  • Thanks for the helping lead! Could also have done it this way, but @Krizz's way is the more comfortable one, because using a different implementation then the default one if rare. And you also see at a glace where the exceptions are! – v01pe Jan 07 '12 at 00:34
5

Developing further on the Mark B's way (no need to list all types used):

namespace ginstl
{
#if (GIN_USE_EASTL)
    using namespace eastl;
#else
    using namespace std;
#endif
    using eastl::string
    using std::queue
}
Krizz
  • 11,362
  • 1
  • 30
  • 43
  • Awesome! Exactly what I needed, couldn't wait to try it, and it works! Just make sure to use it in a C++ environment (won't compile in C context) – v01pe Jan 07 '12 at 00:31
  • Of course, there is no such a thing as namespace in C. Glad to hear that it helped you. – Krizz Jan 07 '12 at 10:22
3

If you use namespace aliases instead of macros:

#if (GIN_USE_EASTL)
    namespace ginstl = eastl;
#else
    namespace ginstl = std;
#endif

You can then do this:

using namespace ginstl;
using std::queue;
using std::deque;

list<int> x; // from eastl or std, depending on GIN_USE_EASTL
queue<int> y; // always from std
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • I don't want to use the using lines in every file I'm using one of the structures, but thanks for the equals syntax… didn't know that existed – v01pe Jan 06 '12 at 20:01