Commit ceffd236 authored by Jonas Stienen's avatar Jonas Stienen
Browse files

Refactoring VA utils

parent b52ce7a0
/*
*
* VVV VVV A
* VVV VVV AAA Virtual Acoustics
* VVV VVV AAA Real-time auralisation for virtual reality
* VVV VVV AAA
* VVVVVV AAA (c) Copyright Institut fr Technische Akustik (ITA)
* VVVV AAA RWTH Aachen (http://www.akustik.rwth-aachen.de)
*
* ---------------------------------------------------------------------------------
*
* File: VAUtils.cpp
*
* Purpose: Utility functions
*
* Author(s): Frank Wefers (Frank.Wefers@akustik.rwth-aachen.de)
* Jonas Stienen (jst@akustik.rwth-aachen.de)
*
* ---------------------------------------------------------------------------------
*/
// $Id: VAUtils.cpp 4152 2015-08-03 16:48:24Z fwefers $
#include "VAUtils.h"
#include <VABaseDefinitions.h>
......@@ -41,6 +18,7 @@
#include <windows.h>
#endif
void VASleep( int iMilliseconds )
{
VistaTimeUtils::Sleep( iMilliseconds );
......@@ -307,5 +285,3 @@ void SetCoreEventParams(CVACoreEvent& oEvent, const CVAMotionState* pMotionState
oEvent.vVelocity = pMotionState->GetVelocity();
oEvent.oOrientation = pMotionState->GetOrientationYPR();
}
const CVAStruct CVAConfigInterpreter::g_oEmptyStruct;
......@@ -10,10 +10,11 @@
#include <VistaBase/VistaExceptionBase.h>
#include <assert.h>
#include <cassert>
#include <string>
#include <map>
static const CVAStruct g_oEmptyStruct;
class CVAMotionState;
//! Sleep function
......@@ -88,13 +89,16 @@ private:
class CVAConfigInterpreter
{
public:
inline CVAConfigInterpreter( const CVAStruct& oStruct ) : m_oStruct( oStruct ) {};
inline CVAConfigInterpreter( const CVAStruct& oStruct )
: m_oStruct( oStruct )
{
};
// Returns the prefix (string) for error outputs
inline std::string GetErrorPrefix() const
{
return m_sErrorPrefix;
}
};
// Sets a prefix (string) for error outputs
// Helpful for meaningful error messages...
......@@ -105,31 +109,31 @@ public:
m_sErrorPrefix = "";
else
m_sErrorPrefix = sPrefix + ": ";
}
};
// Hook: Error handler
// Customize to your needs
inline virtual void Error( const std::string& sErrorMessage ) const
{
VA_EXCEPT1( m_sErrorPrefix + sErrorMessage );
}
};
// --= Error messages =--
inline void ErrorMissKey( const std::string& sKey ) const
{
Error( std::string( "Missing a key \"" ) + sKey + std::string( "\"" ) );
}
};
inline void ErrorWrongType( const std::string& sKey, const std::string& sType ) const
{
Error( std::string( "Key \"" ) + sKey + std::string( "\" must be " ) + sType );
}
};
inline void ErrorEmptyKey( const std::string& sKey ) const
{
Error( std::string( "Key \"" ) + sKey + std::string( "\" must not be empty" ) );
}
};
// --= Sub groups =--
......@@ -142,18 +146,21 @@ public:
if( !oKey.IsStruct() ) ErrorWrongType( sStructName, "a struct" );
return oKey;
}
};
// Semantic: if HasKey(s) then check IsStruct(s)
// Info: If no key exists, an empty struct is returned
inline const CVAStruct& OptStruct( const std::string& sStructName ) const
{
if( m_oStruct.HasKey( sStructName ) ) {
if( m_oStruct.HasKey( sStructName ) )
{
const CVAStructValue& oValue = m_oStruct[ sStructName ];
if( !oValue.IsStruct() ) ErrorWrongType( sStructName, "a struct" );
if( !oValue.IsStruct() )
ErrorWrongType( sStructName, "a struct" );
return oValue;
}
else {
else
{
// Group does not exist => return empty struct
return g_oEmptyStruct;
}
......@@ -216,7 +223,7 @@ public:
ErrorWrongType( sKey, "boolean" );
return false;
}
};
// --= Strings =--
......@@ -233,7 +240,7 @@ public:
}
ErrorWrongType( sKey, "a string" );
}
};
// Gets an Optional string value. A default value set if it does not exist
// Return value: Key exists?
......@@ -253,14 +260,14 @@ public:
ErrorWrongType( sKey, "a string" );
return false;
}
};
// Like ReqString(...), but checks that the string is non-empty
inline void ReqNonEmptyString( const std::string& sKey, std::string& sValue ) const
{
ReqString( sKey, sValue );
if( sValue.empty() ) ErrorEmptyKey( sKey );
}
};
// Like OptString(...), but checks that the string is non-empty
inline bool OptNonEmptyString( const std::string& sKey, std::string& sValue ) const
......@@ -269,7 +276,7 @@ public:
if( sValue.empty() )
ErrorEmptyKey( sKey );
return bExist;
}
};
// --= Integers =--
......@@ -334,7 +341,7 @@ public:
ErrorWrongType( sKey, "an integer number" );
return false;
}
};
// --= Numbers (floating point) =--
......@@ -362,7 +369,7 @@ public:
}
ErrorWrongType( sKey, "a number" );
}
};
inline bool OptNumber( const std::string& sKey, double& dValue, double dDefault, const CVALiterals<double>* pLiterals = nullptr ) const
{
......@@ -393,7 +400,7 @@ public:
ErrorWrongType( sKey, "a number" );
return false;
}
};
// --= Strings =--
......@@ -420,7 +427,7 @@ public:
}
ErrorWrongType( sKey, "a string" );
}
};
// Gets a mandatory list of strings value. An exception is thrown if it does not exist
inline void ReqStringListRegex( const std::string& sKey, std::vector< std::string >& vList, const std::string& sRegex ) const
......@@ -445,10 +452,9 @@ public:
}
ErrorWrongType( sKey, "a string" );
}
};
private:
static const CVAStruct g_oEmptyStruct;
const CVAStruct& m_oStruct;
std::string m_sErrorPrefix;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment