minid.utils

A module holding a variety of utility functions used throughout MiniD. This module doesn't (and shouldn't) depend on the rest of the library in any way, and as such can't hold implementation-specific functionality. For that, look in the minid.misc module.

License:
Copyright (c) 2007 Jarrett Billingsley

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.

alias mdfloat ;
This alias defines what the MiniD 'float' type uses. By default and according to the MiniD spec, it's double, but if you don't need the extra precision and want to save some space/speed in the MiniD struct, you can alias it to float instead.

template isStringType (T)
Metafunction to see if a given type is one of char[], wchar[] or dchar[].

template isCharType (T)
Sees if a type is char, wchar, or dchar.

template isIntType (T)
Sees if a type is a signed or unsigned byte, short, int, or long.

template isFloatType (T)
Sees if a type is float, double, or real.

template isArrayType (T)
Sees if a type is an array.

template isAAType (T)
Sees if a type is an associative array.

template isPointerType (T)
Sees if a type is a pointer.

template realType (T)
Get to the bottom of any chain of typedefs! Returns the first non-typedef'ed type.

template hasUnions (T,uint Idx = 0)
Determine if a given aggregate type contains any unions, explicit or anonymous. Thanks to Frits van Bommel for the original code.

int Compare3 (T)(T a, T b);
Compare two values, a and b, using < and >. Returns -1 if a < b, 1 if a > b, and 0 otherwise.

int dcmp (dchar[] s1, dchar[] s2);
Compares dchar[] strings stupidly (just by character value, not lexicographically).

int idcmp (dchar[] s1, dchar[] s2);
Compares dchar[] strings stupidly, but case-insensitively.

bool isValidUniChar (dchar c);
See if a given Unicode character is valid.

template FOURCC (char[] name)
Make a FOURCC code out of a four-character string. This is I guess for little-endian platforms..

template MakeVersion (uint major,uint minor)
Make a version with the major number in the upper 16 bits and the minor in the lower 16 bits.

const uint MiniDVersion ;
The current version of MiniD. (this is kind of buried here)

template isInvalidSerializeType (T)
See if T is a type that can't be automatically serialized.

enum SerializeMethod ;
The different ways data can be serialized and deserialized.

template TypeSerializeMethod (T)
Given a type, determine how to serialize or deserialize a value of that type.

void Serialize (T)(IWriter s, T value);
Write out a value to a stream. This will automatically write out nested arrays and entire structures. Pointers, functions, delegates, interfaces, unions, and AAs can't be serialized.

For arrays, it will try to write the biggest chunks at a time possible. So if you write out an int[], or an S[] where S is a struct type marked as SerializeAsChunk, it will write out all the data in the array at once. Otherwise, it'll write out the array element-by-element.

For structs, the following methods are tried:
  1. If the struct has both "void serialize(IWriter)" and "static T deserialize(IReader)" methods, Serialize /Deserialize will call those.
  2. If the struct has a "const bool SerializeAsChunk = true" declaration in the struct, then it will serialize instances of the struct as chunks of memory.
  3. As a last resort, it will try to write out the struct member-by-member. If the struct has any unions (explicit or anonymous), the struct will not be able to be automatically serialized, and you will either have to make it chunk-serializable or provide custom serialization methods.


For classes, it will expect for there to be custom serialize/deserialize methods.

For all other types, it will just write them out. All other types are also considered chunk-serializable, so arrays of them will be serialized in one call.

If your struct or class declares custom serialize/deserialize methods, it must declare both or neither. These methods must always follow the form:

void serialize(IWriter);
static T deserialize(IReader);
where T is your custom type.

void Deserialize (T)(IReader s, out T dest);
The opposite of Serialize(). The same rules apply here as with Serialize().

class Profiler ;
A class used for profiling pieces of code. You initialize it with an output filename, and during execution of your program, you just create instances of this class with a certain name. Timings for each profile name are accumulated over the course of the program and the final output will show the name of the profile, how many times it was instanced, the total time in milliseconds, and the average time per instance in milliseconds.

struct List (T);
Just a simple list type for building up arrays a little more efficiently. I know, the runtime automatically over-allocates for arrays, but I don't like relying on implementation-specific features.

template NameOfFunc (alias f)
Gets the name of a function alias.

template QSort (alias Pred,List...)
Given a predicate template and a tuple, sorts the tuple. I'm not sure how quick it is, but it's probably fast enough for sorting most tuples, which hopefully won't be that long. The predicate template should take two parameters of the same type as the tuple's elements, and return <0 for A < B, 0 for A == B, and >0 for A > B (just like opCmp).

template ToUTF8 (char[] s)
template ToUTF8 (wchar[] s)
template ToUTF8 (dchar[] s)
template ToUTF16 (char[] s)
template ToUTF16 (wchar[] s)
template ToUTF16 (dchar[] s)
template ToUTF32 (char[] s)
template ToUTF32 (wchar[] s)
template ToUTF32 (dchar[] s)
Convert string literals between unicode encodings at compile time. I swear, this should be built into the compiler or something.

template Itoa (int i)
Convert an integer to a string at compile time.

Page was generated with on Sun Nov 18 11:04:10 2007