• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Waterlimon

Wrapping numerical types in classes?

22 posts in this topic

Would it make any sense to wrap numerical primitive types into classes, so that information like amount of bits, signedness, max and min values and possibly mathematical functions would be easily accessible.

For example a template class for a 3D grid where you pass in the edge lenght. If it was wrapped, you could do something like pow(T.GetMaxValue(),3) so you know how large datatype you need to index the cells (this could be combined with some log and other fancy stuff to get the required byte count and use a template thing to get the best type for the situation)


Pros and cons i came up with:

Pros:
-Easily accessible information about the capabilities of the type
-Easily accessible methods for operating on it (The ide will list all the available methods i assume...)
-Possible to add debug code or such? (not that great benefit though?)
-Makes primitive types more OOP

Cons:
-There would be a lot of methods and code to add (to convert between types etc.)
-Possibly not optimized as well? (I would expect the compiler to make the same code as without wrapping though...)
-Possible issues with existing code?
0

Share this post


Link to post
Share on other sites
I thought max/min values were already globally defined... You would als have a seriously large amount of operators to overload.

I don't think it's an efficient way of going about doing it, mainly because it would make your classes too bulky. I mean, you would need another int to store MAX_INT, and another double to store MAX_DOUBLE. Much easier to just find the global defininition and go from there.
0

Share this post


Link to post
Share on other sites
[quote name='incertia' timestamp='1350930330' post='4992837']
I thought max/min values were already globally defined... You would als have a seriously large amount of operators to overload.

I don't think it's an efficient way of going about doing it, mainly because it would make your classes too bulky. I mean, you would need another int to store MAX_INT, and another double to store MAX_DOUBLE. Much easier to just find the global defininition and go from there.
[/quote]

The max and min values would of course be static, so there is zero overhead.

I know you can already find this data, but it would ve nicer to have it directly in the class like with everything else...

Would be nice if it was added to some later c++ standard (as the current ints etc are kind of legacy from c?) on top of the legacy stuff.

Maybe even make literals act like classes, so you could do something like

0x1234.BitCount()

would make c++ more consistent in my opinion. (if i go perfectionist i say the language must not have hard coded arithmetic because those clearly should be compiler intrinsics acting on byte arrays xP)
0

Share this post


Link to post
Share on other sites
Template programming can handle things like that rather easily.
[code]
template<unsigned int bits> struct bitcount {
static unsigned int const value = bitcount<(bits>>1)>::value + 1;
};
template<> struct bitcount<0> {
static unsigned int const value = 0;
};
[/code]
Now you can get the number of bits of a value by [i]int foo=bitcount<42>::value[/i].
2

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1350931731' post='4992851']
Template programming can handle things like that rather easily.
[code]
template<unsigned int bits> struct bitcount {
static unsigned int const value = bitcount<(bits>>1)>::value + 1;
};
template<> struct bitcount<0> {
static unsigned int const value = 0;
};
[/code]
Now you can get the number of bits of a value by [i]int foo=bitcount<42>::value[/i].
[/quote]

But that code looks nonstandard, i would feel a lot more comfortable using simple methods like for most other things.

And, i believe you wouldnt need to make too much code as you can reuse the same code for all integer types...

something like integer<bits> would be cool.
-1

Share this post


Link to post
Share on other sites
It is consistent with everything in the standard library that has to to with template programming. Look at, for example, the numeric limit class I mentioned earlier, as well as the type traits classes.
0

Share this post


Link to post
Share on other sites
I know this is existing functionality, it would just be nice to use a system where i can create lets say an integer by doing integer<32> and all the information i might need is accessible directly through it, no need to put it ib some other data structure.

Just like if you made a fixed size array class. You dont create a class for each array size and then create separate template magic to retrieve data about it...

0

Share this post


Link to post
Share on other sites
[quote name='Waterlimon' timestamp='1350934467' post='4992873']
Just like if you made a fixed size array class. You dont create a class for each array size and then create separate template magic to retrieve data about it...
[/quote]
You realize that actual computer hardware works with set numbers of bits at a time, right? The reason why your average PC C++ compiler has 8-bit, 16-bit, 32-bit and (usually) 64-bit integers is because those are the number of bits that your CPU works with most efficiently at a time. There's no 13-bit integer type because your standard x86-family processor doesn't have any assembly operations that work with 13-bit operands. If you created your hypothetical integer template, then implementing that template would require some hardcore special template magic in order to work anywhere near efficiently.
0

Share this post


Link to post
Share on other sites
You can get [url="http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic"]arbitrary-precision[/url] arithmetic libraries. One may some day be adopted into the standard library (propose it!). This sort of thing is not a part of the language proper because (1) C++ is a systems programming language, and arbitrary precision is not generally a system programming concept (although the fast_* and min_* integral types are system programming concepts) and (2) one of the fundamental design concepts of C++ is [i]pay only for what you use[/i] and adding such support would add increased cost to everyone for very common operations to support a rare and little-used function.

Making primitive types more OO is not a goal of C++ either. C++ is not an "OO language," it's a multi-paradigm language that has OO support.

There's no reason why you can't write a library to do what you propose. It's unlikely to have enough widespread applicability to end up in the standard, but I would have thought the same thing of special numeric functions yet there they are. The usual place for such specialized things is an external library, though.
0

Share this post


Link to post
Share on other sites
[quote name='Waterlimon' timestamp='1350931178' post='4992843']
but it would be nicer to have it directly in the class like with everything else
[/quote]This is the C#/Java way of thinking ([i]"corrupted OOP" IMO[/i]), where everything has to be in a class, not the C++ way of thinking.

The first way of thinking leads to #1 below, whereas #2 is more C++ style (in my experience).[code]//#1
class Foo
{
public: int Bar( int );
void UtilityHelper() { Bar(42); }
private: int m_Baz;
};
//#2
class Foo
{
public: int Bar( int );
private: int m_Baz;
};
void UtilityHelper(Foo& foo) { foo.Bar(42); }[/code]The reason is that in #1, the private details such as [font=courier new,courier,monospace]m_Baz[/font] have been unnecessarily exposed to the utility/helper function. This means that if you're investigating a bug involving [font=courier new,courier,monospace]m_Baz[/font], then you have to treat [font=courier new,courier,monospace]UtilityHelper[/font] as a suspect, increasing the amount of code to read/maintain.
In the 2nd style, the utility is known to only have access to the public interface, not the private details. Edited by Hodgman
1

Share this post


Link to post
Share on other sites
The number of bits passed down into the example integer<bits> would of course find the smallest integer type that has at least that many bits and is native to the processor.

Maybe utility functions werent a good idea to put there, but is there any major reasons why not to make a class like

integer<minimumBits> (or bytes?)

where it internally has a typedef for the primitive data type to use, and specialized operator etc. templates for each type? (if needed, probably a single template is fine for char, short and int...)

So basically just a class to make the different integer types a single class for the sole purpose of being able to pass in the amount of bits needed and get the best type for it.

C++ guarantees each of the primitive types to have some amount of bits, which probably was fine in C when the programmer had to copy the code anyways if lets say dimension or size changed, but now with templates the computer should do it, which in my opinion is easiest to achieve by making the integers template classes. This also makes them all carry their bit count with them, and doesnt require external template classes to lets say pick tge best integer type for a number of bits or to get the number of bits...
0

Share this post


Link to post
Share on other sites
Boost has a class to make integer type selection based on size; [i]boost::int_t<N>[/i]. It has three nested typedefs; [i]fast[/i], [i]least[/i] and [i]exact[/i], representing the fastest type with at least [i]N[/i] bits, the smallest type than can accommodate at least [i]N[/i] bits, and a type with exactly [i]N[/i] bits. The [i]exact[/i] typedef is only available if there actually is a type with that many bits though. Edited by Brother Bob
0

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1350973796' post='4993001']
[quote name='Waterlimon' timestamp='1350931178' post='4992843']
but it would be nicer to have it directly in the class like with everything else
[/quote]This is the C#/Java way of thinking ([i]"corrupted OOP" IMO[/i]), where everything has to be in a class, not the C++ way of thinking.

The first way of thinking leads to #1 below, whereas #2 is more C++ style (in my experience).[code]//#1
class Foo
{
public: int Bar( int );
static void UtilityHelper() { Bar(42); }
private: int m_Baz;
};
//#2
class Foo
{
public: int Bar( int );
private: int m_Baz;
};
void UtilityHelper(Foo&amp; foo) { foo.Bar(42); }[/code]The reason is that in #1, the private details such as [font=courier new,courier,monospace]m_Baz[/font] have been unnecessarily exposed to the utility/helper function. This means that if you're investigating a bug involving [font=courier new,courier,monospace]m_Baz[/font], then you have to treat [font=courier new,courier,monospace]UtilityHelper[/font] as a suspect, increasing the amount of code to read/maintain.
In the 2nd style, the utility is known to only have access to the public interface, not the private details.
[/quote]

Does #1 actually work; on what instance is Bar invoked?
0

Share this post


Link to post
Share on other sites
I think that as Brother Bob said std::numeric_limits should serve your purpose. However, I don't think wrapping numeric types in templates would be a bad idea otherwise. I'm doing it for atomic variables.
0

Share this post


Link to post
Share on other sites
[quote name='3DModelerMan' timestamp='1351173340' post='4993792']
I think that as Brother Bob said std::numeric_limits should serve your purpose. However, I don't think wrapping numeric types in templates would be a bad idea otherwise. I'm doing it for atomic variables.
[/quote]
The way [font=courier new,courier,monospace]std::atomic[/font] does?
0

Share this post


Link to post
Share on other sites
Yeah. Except std::atomic is C++ 11 and I'm not using C++ 11 features in my project because of portability.
0

Share this post


Link to post
Share on other sites
Don't forget [font=courier new,courier,monospace]std::atomic[/font] is a part of the [i]current[/i] C++ standard. If your compiler does not support the [i]current[/i] C++ standard, it's a non-compliant compiler. Why are you using a non-standard compiler?
0

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1351206043' post='4993957']
Why are you using a non-standard compiler?
[/quote]

All current C++ compilers are non-standards conformant, in fact you could go so far as saying "There never has been a fully standards conformant C++ compiler." As such your argument is moot. Edited by Washu
0

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1351206043' post='4993957']
Don't forget [font=courier new,courier,monospace]std::atomic[/font] is a part of the [i]current[/i] C++ standard. If your compiler does not support the [i]current[/i] C++ standard, it's a non-compliant compiler. Why are you using a non-standard compiler?
[/quote]Hardly any compilers have full C++11 compliance, as you probably know...
Most compilers are pretty good at C++03/98 compliance though, and a lot of non-PC platforms don't provide up-to-date toolchains...
0

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1351206043' post='4993957']
If your compiler does not support the [i]current[/i] C++ standard, it's a non-compliant compiler. Why are you using a non-standard compiler?
[/quote]

Do the xbox and ps3 compilers support C++11?
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0