#include <windows.h>
#include <stdio.h>
#include "..\Contextualize.h"
void fun1( bool boolIsApplication, bool boolIsDenominator )
{
// Can easily make out of context assignments.
boolIsApplication = boolIsDenominator;
boolIsDenominator = boolIsApplication;
}
void fun2( contextualize< Bool, sX::Application > boolIsApplication,
contextualize< Bool, sX::Denominator > boolIsDenominator )
{
// Cannot make out of context assignments.
//boolIsApplication = boolIsDenominator;
//boolIsDenominator = boolIsApplication;
}
class MyClass
{
public:
bool fun_bool( bool bBoolean )
{
return( bBoolean ^ bBoolean );
}
bool fun_bool( contextualize< Bool, sX::Application > bBoolean )
{
return( bBoolean ^ bBoolean );
}
bool fun_bool( contextualize< Bool, sX::Denominator > bBoolean )
{
return( bBoolean ^ bBoolean );
}
bool operator==( const bool& bBoolean ) const
{
return( bBoolean );
}
bool operator==( const contextualize< Bool, sX::Application >& bBoolean ) const
{
return( bBoolean );
}
bool operator==( const contextualize< Bool, sX::Denominator >& bBoolean ) const
{
return( bBoolean );
}
};
struct FileAttributes
{
unsigned __int64 uiFileSize;
DWORD dwAttributes;
contextualize< FILETIME as sX::CreationTime > ftCreationTime;
contextualize< FILETIME as sX::LastAccessTime > ftLastAccessTime;
contextualize< FILETIME as sX::LastWriteTime > ftLastWriteTime;
};
void main()
{
// ASSIGNMENT OPERATOR - The code below demonstrates how the Object Contextualization Model
// helps to eliminate incorrect assignment errors.
// Create a Boolean variable contextualized with context of Application, e.g. as opposed to library.
contextualize< Bool, sX::Application > boolIsApplication( contextualize< Bool, sX::Application >::Init( true ) );
// Create a Boolean object contextualized with context of Denominator, e.g. as opposed to nominator.
contextualize< Bool, sX::Denominator > boolIsDenominator( contextualize< Bool, sX::Denominator >::Init( false ) );
// Notice that the selected contexts are chosen to be of an incompatible nature.
// Bad assignment - variables from different contexts cannot be assigned except through explicit conversion.
//boolIsApplication = boolIsDenominator;
// Bad assignment - non contextualized data cannot be
// assigned to contextualized without an explicit conversion.
//boolIsApplication = true;
// OK assignment.
boolIsApplication = contextualize< Bool, sX::Application >::Initialize( true );
// Create a second Boolean contextualized as "Application".
contextualize< Bool, sX::Application > boolIsApplication2( contextualize< Bool, sX::Application >::Init( false ) );
// OK assignment – variables from the same type and context can be assigned immediately.
boolIsApplication2 = boolIsApplication;
// Invalid context assignment cannot be processed.
// boolIsApplication = boolIsDenominator;
// Example using data members.
// Create two objects and initialize them with zeroes.
FileAttributes faFileA = { 0 }, faFileB = { 0 };
// ... some code to initialize the "faFileA" and "faFileB" objects.
// OK call.
faFileA.ftCreationTime = faFileB.ftCreationTime;
// This line will generate an error.
// faFileA.ftLastWriteTime = faFileB.ftLastAccessTime;
// OK call.
faFileA.ftLastWriteTime = contextualize< FILETIME as sX::LastWriteTime >::Init( faFileB.ftLastAccessTime );
// Context-specialize the object itself, which has contextualized members.
contextualize< FileAttributes as contextualize< sX::Video by sX::Description > > faOfFileWithVideoDescription;
//***************************************************************************************************
// FUNCTION CALL - The code below demonstrates how the Object Contextualization Model
// helps to eliminate incorrect function call errors.
// OK - this is a correct call.
fun1( boolIsApplication, boolIsDenominator );
// Bad call - the two variables are swapped by "mistake".
// Because the function parameters are non-contextualized variables the compilation will not generate
// any errors. The discovery of the error is not assisted and will only happen by careful observation.
fun1( boolIsDenominator, boolIsApplication );
// OK - this is a correct call.
fun2( boolIsApplication, boolIsDenominator );
// Bad call - the two variables are swapped by "mistake".
// Because the function parameters are contextualized the compiler will catch the
// error and not compile until the error is corrected or explicit conversion is made.
//fun2( boolIsDenominator, boolIsApplication );
// Bad call - non contextualized objects cannot be passed
// where contextualized are expected, except after explicit conversion.
//fun2( true, false );
// OK - this is a correct call.
fun2( contextualize< Bool, sX::Application >::Init( true ), contextualize< Bool, sX::Denominator >::Init( false ) );
//***************************************************************************************************
// Operator - The code below demonstrates how the Object Contextualization Model
// enhances function and operator overloading.
MyClass myClass;
bool b( false );
// The respective overload function is called depending on the context of the supplied parameter.
b = myClass.fun_bool( true );
b = myClass.fun_bool( boolIsApplication );
b = myClass.fun_bool( boolIsDenominator );
// The respective operator overload is called depending on the context of the supplied parameter.
b = myClass == true;
b = myClass == boolIsApplication;
b = myClass == boolIsDenominator;
}
|