Torque3D/Engine/source/console/propertyParsing.h
2025-04-10 08:55:42 +01:00

235 lines
8.5 KiB
C++

//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _PROPERTYPARSING_H_
#define _PROPERTYPARSING_H_
class ColorI;
class LinearColorF;
class Point2I;
class Point2F;
class Point3F;
class Point4F;
class Point3I;
class Point4I;
class RectI;
class RectF;
class Box3I;
class Box3F;
#ifndef USE_TEMPLATE_MATRIX
class MatrixF;
#else
template<typename DATA_TYPE, U32 rows, U32 cols> class Matrix;
typedef Matrix<F32, 4, 4> MatrixF;
#endif
class AngAxisF;
class QuatF;
class String;
class FileName;
class SimObject;
class SimObjectType;
template<class T> class Vector;
//-----------------------------------------------------------------------------
// String scan/print methods
//
// Macros in propertyImplementation.h point the ConcretePropertyDefinition scan/print delegates to these functions.
//
namespace PropertyInfo
{
// String
bool default_scan(const String &data, String & result);
bool default_print(String & result, const String & data);
// Bool
bool default_scan(const String &data, bool & result);
bool default_print(String & result, const bool & data);
// S32/F32/U32
bool default_scan(const String &data, F32 & result);
bool default_print(String & result, const F32 & data);
bool default_scan(const String &data, U32 & result);
bool default_print(String & result, const U32 & data);
bool default_scan(const String &data, S32 & result);
bool default_print(String & result, const S32 & data);
// Vector<S32/F32/U32>
bool default_scan(const String &data, Vector<F32> & result);
bool default_print(String & result, const Vector<F32> & data);
bool default_scan(const String &data, Vector<U32> & result);
bool default_print(String & result, const Vector<U32> & data);
bool default_scan(const String &data, Vector<S32> & result);
bool default_print(String & result, const Vector<S32> & data);
// Math Points
bool default_scan(const String &data, Point2F & result);
bool default_print(String & result, const Point2F & data);
bool default_scan(const String &data, Point2I & result);
bool default_print(String & result, const Point2I & data);
bool default_scan(const String &data, Point3F & result);
bool default_print(String & result, const Point3F & data);
bool default_scan(const String &data, Point3I & result);
bool default_print(String & result, const Point3I & data);
bool default_scan(const String &data, Point4F & result);
bool default_print(String & result, const Point4F & data);
bool default_scan(const String &data, Point4I & result);
bool default_print(String & result, const Point4I & data);
// Math Boxs & Rectangles
bool default_scan(const String &data, RectI & result);
bool default_print(String & result, const RectI & data);
bool default_scan(const String &data, RectF & result);
bool default_print(String & result, const RectF & data);
bool default_scan(const String &data, Box3I & result);
bool default_print(String & result, const Box3I & data);
bool default_scan(const String &data, Box3F & result);
bool default_print(String & result, const Box3F & data);
//-----------------------------------------------------------------------------
bool default_scan( const String &data, AngAxisF & result );
bool default_print( String & result, const AngAxisF & data );
bool default_scan( const String &data, QuatF & result );
bool default_print( String & result, const QuatF & data );
bool default_scan( const String &data, MatrixF & result );
bool default_print( String & result, const MatrixF & data );
// Colors
bool default_scan(const String &data, LinearColorF & result);
bool default_print(String & result, const LinearColorF & data);
bool default_scan(const String &data, ColorI & result);
bool default_print(String & result, const ColorI & data);
// filename handler
bool default_scan(const String &data, FileName & result);
bool default_print(String & result, const FileName & data);
// SimObjectType
bool default_scan(const String &data, SimObjectType & result);
bool default_print(String & result, SimObjectType const & data);
// SimObject
bool default_scan(const String &data, SimObject * & result);
bool default_print(String & result, SimObject * const & data);
template<class T>
inline bool typed_simobject_scan(const String &data, T * & result)
{
SimObject * obj;
result = default_scan(data,obj)? dynamic_cast<T*>(obj) : NULL;
return result;
}
template<class T>
inline bool typed_simobject_print(String & result, T * const & data)
{
return default_print(result,data);
}
bool hex_scan(const String & string, U32 & hex);
bool hex_print(String & string, const U32 & hex);
bool hex_scan(const String & string, S32 & hex);
bool hex_print(String & string, const S32 & hex);
bool hex_scan(const String & string, U16 & hex);
bool hex_print(String & string, const U16 & hex);
bool hex_scan(const String & string, S16 & hex);
bool hex_print(String & string, const S16 & hex);
bool hex_scan(const String & string, U8 & hex);
bool hex_print(String & string, const U8 & hex);
bool hex_scan(const String & string, S8 & hex);
bool hex_print(String & string, const S8 & hex);
bool default_print(String & result, SimObjectType * const & data);
template<typename T, size_t count>
char* FormatPropertyBuffer(const void* dataPtr, char* buffer, U32 bufSize)
{
const T* values = reinterpret_cast<const T*>(dataPtr);
char* ptr = buffer;
for (size_t i = 0; i < count; ++i)
{
S32 written = 0;
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, S32>)
written = dSprintf(ptr, bufSize - (ptr - buffer), "%d", values[i]);
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, F32>)
written = dSprintf(ptr, bufSize - (ptr - buffer), "%g", values[i]);
else
AssertFatal(sizeof(T) == 0, "Unsupported type in FormatProperty");
ptr += written;
if (i < count - 1)
*ptr++ = ' ';
}
return ptr; // return end of written string for chaining
}
template<typename T, size_t count>
const char* FormatProperty(const void* dataPtr)
{
static const U32 bufSize = 256;
char* buffer = Con::getReturnBuffer(bufSize);
FormatPropertyBuffer<T,count>(dataPtr, buffer, bufSize);
return buffer;
}
template<typename T, size_t count>
inline bool ParseProperty(char* str, T(&out)[count])
{
size_t index = 0;
char* tok = dStrtok(str, " \t");
while (tok && index < count)
{
if constexpr (std::is_same_v<T, int> || std::is_same_v<T, S32>)
{
out[index++] = mRound(dAtof(tok));
}
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, F32>)
{
out[index++] = dAtof(tok);
}
else
{
AssertFatal(sizeof(T) == 0, "Unsupported type in PropertyInfo::ParseProperty");
}
tok = dStrtok(nullptr, " \t");
}
return index == count;
}
}
// Default Scan/print definition
#define DEFINE_PROPERTY_DEFAULT_PRINT(dataType) bool PropertyInfo::default_print(String & resultString, dataType const & dataTyped)
#define DEFINE_PROPERTY_DEFAULT_SCAN(dataType) bool PropertyInfo::default_scan(const String &dataString, dataType & resultTyped)
#endif // _PROPERTYPARSING_H_