Common

From Nocturnal Initiative

Jump to: navigation, search

Contents

Assert.h

A lightweight and continuable assert and break mechanism.

if (isOk)
{
  ...
}
else
{
  NOC_BREAK(); // execution will be interrupted here
}
 
...
 
NOC_ASSERT( i == 1 ); // execution will be interrupted here if i != 1
 
...
 
NOC_COMPILE_ASSERT( sizeof(int) == 4 ); // compilation will be interrupted here if sizeof(int) != 4

Config.h

This configuration header is the pinch point for customizing site-specific information such as special computer paths, email addresses, and environment variable prefixes.

Exception.h

Nocturnal::Exception is a base class for polymorphic exception classes used in all of the Nocturnal code.

An exception of this type is thrown only in unexpected error conditions, and are typically only caught in UI applications that need safe guarding against heinous error conditions. Library code should do appropriate validation before making a call that may throw an exception, as opposed to catching, processing, and/or throwing its own type of exception object. A good example of this is opening a file. Library code should check for file existence and conditionally handle the lack of existence if it wishes to process that condition. If the file must exist for the application to execute normally, it may omit this step expecting the library call to throw a missing file exception (if that library was expecting it to open the specified file for read).

Insomniac does not employ the C++ exception heavy 'Programming by Exception' methodology. Keep in mind: A Nocturnal::Exception is an ERROR condition.

Currently this Exception class subclasses std::exception for usage and interface convenience only.

if (!FileSystem::Exists( filePath ))
{
  throw Nocturnal::Exception( "File '%s' does not exist!", filePath.c_str() );
}

Types.h

Sized typedefs for integral and floating point types, and some stl containers.

i8/u8, i16/u16, i32/u32, and i64/u64 are sized signed and unsigned integer types.

f32/f64 are sized floating point types.

PointerSizedInt and PointerSizedUInt are signed and unsigned integer types the same size as pointers for the current build configuration.

V_u32 integers;
 
integers.push_back( (u32) 5 );

Automation

Event.h

A C++ delegate/event system.

This chunk of code has a long history at Insomniac and has been crucial to engineering our most ambitious applications. This code uses standard C++ to implement the .NET event and delegate functionality. It is type-safe and supports parameters and return value functions (or void functions). The Signature template is instantiated to specify a function signature (or prototype), and its internal classes Delegate and Event work as substitutes for the event and delegate keywords.

This code currently uses the heap for tracking delegate objects in events, and external allocator support should be added to this code to make it more console friendly.

struct Args {};
 
void Function( Args args ) {}
 
class Class
{
  void Method( Args args ) {}
};
 
// this explicitly instantiates all the classes required for a signature
typedef Nocturnal::Signature<void, Args> ExampleSignature;
 
ExampleSignature::Event g_Event;
Class                   g_Instance;
 
// add some handlers, static and instance functions 
g_Event.Add( &Function  );
g_Event.Add( &g_Instance, &Class::Method ) );
 
// raise the event (will cause the above handlers to execute synchronously)
g_Event.Raise( Args () );
 
// remove the handlers
g_Event.Remove( &Function  );
g_Event.Remove( &g_Instance, & Class::Method ) );

Property.h

Within a lot of our code we define a 'Property' to be a get/set function that manages a private piece of data that may or may not exactly match the type of the get/set function return value and parameter. In this case we use this Property object to handle unifying the function call to the get/set methods to a single prototype to simplify code that interfaces with the property.

struct Args {};
 
Args g_Args;
 
const Args& GetArgs()
{
  return g_Args;
}
 
void SetArgs(const Args& args)
{
  g_Args = args;
}
 
class Class
{
private:
  Args m_Args;
 
public:
  const Args& GetArgs()
  {
    return g_Args;
  }
 
  void SetArgs( const Args& args )
  {
    m_Args = args;
  }
};
 
Property<Args> staticProperty = new StaticProperty<Args>( &GetArgs, &SetArgs );
 
staticProperty->Set( Args () );
 
Property<Args> memberProperty = new MemberProperty<Class, Args>( &Class::GetArgs, &Class::SetArgs );
 
memberProperty->Set( staticProperty->Get() );

Memory/ArrayPtr.h

A C++ pointer class that properly handles array deletion (delete[]).

{
 
  ArrayPtr<int> integers = new int[ 50 ];
 
} // integers will be deleted when it goes out of scope here

Memory

HybridPtr.h

HybribPtr.h contains a C++ pointer class that stores the const-ness of the data pointed to at runtime.

This sounds like a really bizzare thing to do, but in a lot of situations decorating classes with the const keyword causes the need for duplicate code to be written. This pointer fixes that situation for objects with pointer members that wish to be const some of the time, and non-const at other times. It is a run-time check only so its actual usefulness is debatable.

class Class
{
public:
  void Mutate() {}
};
 
Class       mutableInstance;
const Class constantInstance;
 
HybridPtr<Class> ptr = &mutableInstance;
 
ptr->Mutate(); // this will succeed
 
ptr = &constantInstance;
 
ptr->Mutate(); // this will assert

SmartPtr.h

SmartPtr.h contains a C++ reference counting interface and smart pointer class.

Reference counted smart pointer classes are a dime a dozen around the internet, and quite a few people have settled on boost::instrusive_ptr for this sort of functionality. We use our own pointer class because it is used extremely pervasively, its very simple, and has no header include dependencies into boost (which would require lots of projects to potentially have their include and lib paths updated).

If the community is behind ditching SmartPtr in favor of intrusive_ptr, or if intrusive_ptr is adopted into C++ 0x, then we should consider removing this code as it reduntant.

class Class : public RefCountBase<Class> {};
 
{
 
  SmartPtr<Class> instance = new Class ();
 
} // instance will be deleted when it goes out of scope here
Personal tools
Navigation
projects