Skip to content

Commit 185b257

Browse files
authored
Merge branch 'develop' into gangatp/fix_examples
2 parents 377b24e + 77282f4 commit 185b257

File tree

8 files changed

+697
-45
lines changed

8 files changed

+697
-45
lines changed

Build/build.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set startingDir="%CD%"
33
set basepath="%~dp0"
44

55
cd %basepath%\..\Source
6-
set Sources=actutils.go automaticcomponenttoolkit.go buildbindingccpp.go buildbindingccppdocumentation.go buildbindingcsharp.go buildbindinggo.go buildbindingnode.go buildbindingpascal.go buildbindingpython.go buildimplementationcpp.go buildbindingjava.go buildimplementationpascal.go componentdefinition.go componentdiff.go languagewriter.go languagec.go languagecpp.go languagepascal.go
6+
set Sources=actutils.go automaticcomponenttoolkit.go buildbindingccpp.go buildbindingccppdocumentation.go buildbindingcsharp.go buildbindinggo.go buildbindingnode.go buildbindingpascal.go buildbindingpython.go buildbindingwasm.go buildbindingjava.go buildimplementationcpp.go buildimplementationpascal.go componentdefinition.go componentdiff.go languagewriter.go languagec.go languagecpp.go languagepascal.go
77

88
set GOOS=windows
99
set GOARCH=amd64

Examples/RTTI/RTTI_component/Bindings/CSharp/RTTI.cs

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,126 @@ use of RTTI
1919

2020
namespace RTTI {
2121

22+
/// <summary>
23+
/// Exception class for RTTI errors
24+
/// </summary>
25+
public class RTTIException : Exception
26+
{
27+
private readonly int _errorCode;
28+
private readonly string _errorMessage;
29+
30+
/// <summary>
31+
/// Initializes a new instance of the RTTIException class
32+
/// </summary>
33+
/// <param name="errorCode">The error code</param>
34+
/// <param name="errorMessage">The error message</param>
35+
public RTTIException(int errorCode, string errorMessage = "") : base(FormatMessage(errorCode, errorMessage))
36+
{
37+
_errorCode = errorCode;
38+
_errorMessage = errorMessage;
39+
}
40+
41+
/// <summary>
42+
/// Gets the error code
43+
/// </summary>
44+
public int ErrorCode => _errorCode;
45+
46+
/// <summary>
47+
/// Gets the custom error message
48+
/// </summary>
49+
public string ErrorMessage => _errorMessage;
50+
51+
/// <summary>
52+
/// Gets the error name (constant name)
53+
/// </summary>
54+
public string ErrorName
55+
{
56+
get
57+
{
58+
switch (_errorCode)
59+
{
60+
case 0: return "SUCCESS";
61+
case 1: return "NOTIMPLEMENTED";
62+
case 2: return "INVALIDPARAM";
63+
case 3: return "INVALIDCAST";
64+
case 4: return "BUFFERTOOSMALL";
65+
case 5: return "GENERICEXCEPTION";
66+
case 6: return "COULDNOTLOADLIBRARY";
67+
case 7: return "COULDNOTFINDLIBRARYEXPORT";
68+
case 8: return "INCOMPATIBLEBINARYVERSION";
69+
default: return "UNKNOWN";
70+
}
71+
}
72+
}
73+
74+
/// <summary>
75+
/// Gets the error description (human-readable)
76+
/// </summary>
77+
public string ErrorDescription
78+
{
79+
get
80+
{
81+
switch (_errorCode)
82+
{
83+
case 0: return "success";
84+
case 1: return "functionality not implemented";
85+
case 2: return "an invalid parameter was passed";
86+
case 3: return "a type cast failed";
87+
case 4: return "a provided buffer is too small";
88+
case 5: return "a generic exception occurred";
89+
case 6: return "the library could not be loaded";
90+
case 7: return "a required exported symbol could not be found in the library";
91+
case 8: return "the version of the binary interface does not match the bindings interface";
92+
default: return "unknown error";
93+
}
94+
}
95+
}
96+
97+
private static string FormatMessage(int errorCode, string errorMessage)
98+
{
99+
string errorName = GetErrorName(errorCode);
100+
string errorDesc = GetErrorDescription(errorCode);
101+
if (!string.IsNullOrEmpty(errorMessage))
102+
return $"RTTIException {errorName} ({errorCode}): {errorDesc} - {errorMessage}";
103+
else
104+
return $"RTTIException {errorName} ({errorCode}): {errorDesc}";
105+
}
106+
107+
private static string GetErrorName(int errorCode)
108+
{
109+
switch (errorCode)
110+
{
111+
case 0: return "SUCCESS";
112+
case 1: return "NOTIMPLEMENTED";
113+
case 2: return "INVALIDPARAM";
114+
case 3: return "INVALIDCAST";
115+
case 4: return "BUFFERTOOSMALL";
116+
case 5: return "GENERICEXCEPTION";
117+
case 6: return "COULDNOTLOADLIBRARY";
118+
case 7: return "COULDNOTFINDLIBRARYEXPORT";
119+
case 8: return "INCOMPATIBLEBINARYVERSION";
120+
default: return "UNKNOWN";
121+
}
122+
}
123+
124+
private static string GetErrorDescription(int errorCode)
125+
{
126+
switch (errorCode)
127+
{
128+
case 0: return "success";
129+
case 1: return "functionality not implemented";
130+
case 2: return "an invalid parameter was passed";
131+
case 3: return "a type cast failed";
132+
case 4: return "a provided buffer is too small";
133+
case 5: return "a generic exception occurred";
134+
case 6: return "the library could not be loaded";
135+
case 7: return "a required exported symbol could not be found in the library";
136+
case 8: return "the version of the binary interface does not match the bindings interface";
137+
default: return "unknown error";
138+
}
139+
}
140+
}
141+
22142

23143
namespace Internal {
24144

@@ -89,7 +209,7 @@ public static void ThrowError(IntPtr Handle, Int32 errorCode)
89209
}
90210
}
91211

92-
throw new Exception(sMessage + "(# " + errorCode + ")");
212+
throw new RTTIException(errorCode, sMessage);
93213
}
94214

95215
/**
@@ -310,7 +430,7 @@ public CAnimalIterator Iterator ()
310430

311431
}
312432

313-
class Wrapper
433+
public class Wrapper
314434
{
315435
private static void CheckError (Int32 errorCode)
316436
{

Examples/RTTI/RTTI_component/Bindings/Python/RTTI.py

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,80 @@ def __str__(self):
3232
if self._message:
3333
return 'RTTIException ' + str(self._code) + ': '+ str(self._message)
3434
return 'RTTIException ' + str(self._code)
35+
36+
def get_error_code(self):
37+
"""Returns the error code"""
38+
return self._code
39+
40+
def get_error_message(self):
41+
"""Returns the custom error message"""
42+
return self._message
43+
44+
def get_error_name(self):
45+
"""Returns the error name (constant name)"""
46+
if self._code == ErrorCodes.SUCCESS:
47+
return 'SUCCESS'
48+
elif self._code == ErrorCodes.NOTIMPLEMENTED:
49+
return 'NOTIMPLEMENTED'
50+
elif self._code == ErrorCodes.INVALIDPARAM:
51+
return 'INVALIDPARAM'
52+
elif self._code == ErrorCodes.INVALIDCAST:
53+
return 'INVALIDCAST'
54+
elif self._code == ErrorCodes.BUFFERTOOSMALL:
55+
return 'BUFFERTOOSMALL'
56+
elif self._code == ErrorCodes.GENERICEXCEPTION:
57+
return 'GENERICEXCEPTION'
58+
elif self._code == ErrorCodes.COULDNOTLOADLIBRARY:
59+
return 'COULDNOTLOADLIBRARY'
60+
elif self._code == ErrorCodes.COULDNOTFINDLIBRARYEXPORT:
61+
return 'COULDNOTFINDLIBRARYEXPORT'
62+
elif self._code == ErrorCodes.INCOMPATIBLEBINARYVERSION:
63+
return 'INCOMPATIBLEBINARYVERSION'
64+
else:
65+
return 'UNKNOWN'
66+
67+
def get_error_description(self):
68+
"""Returns the error description (human-readable)"""
69+
if self._code == ErrorCodes.SUCCESS:
70+
return 'success'
71+
elif self._code == ErrorCodes.NOTIMPLEMENTED:
72+
return 'functionality not implemented'
73+
elif self._code == ErrorCodes.INVALIDPARAM:
74+
return 'an invalid parameter was passed'
75+
elif self._code == ErrorCodes.INVALIDCAST:
76+
return 'a type cast failed'
77+
elif self._code == ErrorCodes.BUFFERTOOSMALL:
78+
return 'a provided buffer is too small'
79+
elif self._code == ErrorCodes.GENERICEXCEPTION:
80+
return 'a generic exception occurred'
81+
elif self._code == ErrorCodes.COULDNOTLOADLIBRARY:
82+
return 'the library could not be loaded'
83+
elif self._code == ErrorCodes.COULDNOTFINDLIBRARYEXPORT:
84+
return 'a required exported symbol could not be found in the library'
85+
elif self._code == ErrorCodes.INCOMPATIBLEBINARYVERSION:
86+
return 'the version of the binary interface does not match the bindings interface'
87+
else:
88+
return 'unknown error'
89+
90+
@property
91+
def error_code(self):
92+
"""Property to access error code"""
93+
return self._code
94+
95+
@property
96+
def error_message(self):
97+
"""Property to access custom error message"""
98+
return self._message
99+
100+
@property
101+
def error_name(self):
102+
"""Property to access error name"""
103+
return self.get_error_name()
104+
105+
@property
106+
def error_description(self):
107+
"""Property to access error description"""
108+
return self.get_error_description()
35109

36110
'''Definition of binding API version
37111
'''
@@ -260,23 +334,23 @@ def checkError(self, instance, errorCode):
260334
message,_ = self.GetLastError(instance)
261335
raise ERTTIException(errorCode, message)
262336

263-
def GetVersion(self):
264-
pMajor = ctypes.c_uint32()
265-
pMinor = ctypes.c_uint32()
266-
pMicro = ctypes.c_uint32()
337+
def GetVersion(self, Major = None, Minor = None, Micro = None):
338+
pMajor = ctypes.c_uint32(Major if Major is not None else 0)
339+
pMinor = ctypes.c_uint32(Minor if Minor is not None else 0)
340+
pMicro = ctypes.c_uint32(Micro if Micro is not None else 0)
267341
self.checkError(None, self.lib.rtti_getversion(pMajor, pMinor, pMicro))
268342

269343
return pMajor.value, pMinor.value, pMicro.value
270344

271-
def GetLastError(self, InstanceObject):
345+
def GetLastError(self, InstanceObject, ErrorMessage = None):
272346
InstanceHandle = None
273347
if InstanceObject:
274348
InstanceHandle = InstanceObject._handle
275349
else:
276350
raise ERTTIException(ErrorCodes.INVALIDPARAM, 'Invalid return/output value')
277-
nErrorMessageBufferSize = ctypes.c_uint64(0)
351+
nErrorMessageBufferSize = ctypes.c_uint64(len(ErrorMessage) if ErrorMessage else 0)
278352
nErrorMessageNeededChars = ctypes.c_uint64(0)
279-
pErrorMessageBuffer = ctypes.c_char_p(None)
353+
pErrorMessageBuffer = ctypes.c_char_p(str.encode(ErrorMessage) if ErrorMessage else None)
280354
pHasError = ctypes.c_bool()
281355
self.checkError(None, self.lib.rtti_getlasterror(InstanceHandle, nErrorMessageBufferSize, nErrorMessageNeededChars, pErrorMessageBuffer, pHasError))
282356
nErrorMessageBufferSize = ctypes.c_uint64(nErrorMessageNeededChars.value)
@@ -469,23 +543,27 @@ def GetNextAnimal(self):
469543

470544
return AnimalObject
471545

472-
def GetNextOptinalAnimal(self):
546+
def GetNextOptinalAnimal(self, AnimalObject = None):
473547
AnimalHandle = ctypes.c_void_p()
548+
if AnimalObject is not None:
549+
AnimalHandle = ctypes.c_void_p(AnimalObject._handle)
474550
pError = ctypes.c_bool()
475551
self._wrapper.checkError(self, self._wrapper.lib.rtti_animaliterator_getnextoptinalanimal(self._handle, AnimalHandle, pError))
476-
if AnimalHandle:
477-
AnimalObject = self._wrapper._polymorphicFactory(AnimalHandle)
552+
if AnimalHandle.value:
553+
AnimalObject = self._wrapper._polymorphicFactory(AnimalHandle.value)
478554
else:
479555
AnimalObject = None
480556

481557
return AnimalObject, pError.value
482558

483-
def GetNextMandatoryAnimal(self):
559+
def GetNextMandatoryAnimal(self, AnimalObject = None):
484560
AnimalHandle = ctypes.c_void_p()
561+
if AnimalObject is not None:
562+
AnimalHandle = ctypes.c_void_p(AnimalObject._handle)
485563
pError = ctypes.c_bool()
486564
self._wrapper.checkError(self, self._wrapper.lib.rtti_animaliterator_getnextmandatoryanimal(self._handle, AnimalHandle, pError))
487-
if AnimalHandle:
488-
AnimalObject = self._wrapper._polymorphicFactory(AnimalHandle)
565+
if AnimalHandle.value:
566+
AnimalObject = self._wrapper._polymorphicFactory(AnimalHandle.value)
489567
else:
490568
AnimalObject = None
491569

Examples/RTTI/RTTI_component/Examples/CSharp/RTTI_Example.cs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,68 @@ namespace RTTI_Example
1919
{
2020
class RTTI_Example
2121
{
22+
static void TestExceptionMethods()
23+
{
24+
Console.WriteLine("Testing RTTI Exception Methods...");
25+
26+
// Test by calling a method with invalid parameters to trigger an exception
27+
try
28+
{
29+
// This should throw an ERTTIException with INVALIDPARAM error
30+
// Manually throw an exception to test the exception properties
31+
throw new RTTI.ERTTIException(2, "rtti exception");
32+
}
33+
catch (RTTI.ERTTIException ex)
34+
{
35+
Console.WriteLine("+ Successfully caught ERTTIException");
36+
37+
// Test the new exception properties
38+
int errorCode = ex.ErrorCode;
39+
string errorMessage = ex.ErrorMessage;
40+
string errorName = ex.ErrorName;
41+
string errorDescription = ex.ErrorDescription;
42+
string baseMessage = ex.Message; // Base exception message from Exception class
43+
44+
Console.WriteLine($" Error Code: {errorCode}");
45+
Console.WriteLine($" Error Message (custom): '{errorMessage}'");
46+
Console.WriteLine($" Error Name: {errorName}");
47+
Console.WriteLine($" Error Description: {errorDescription}");
48+
Console.WriteLine($" Base Message: '{baseMessage}'");
49+
50+
// Verify error details - expecting INVALIDPARAM (code 2)
51+
if (errorCode != 2)
52+
throw new Exception($"Expected error code 2 (INVALIDPARAM), got {errorCode}");
53+
if (errorName != "INVALIDPARAM")
54+
throw new Exception($"Expected 'INVALIDPARAM', got '{errorName}'");
55+
if (errorDescription != "an invalid parameter was passed")
56+
throw new Exception($"Expected invalid parameter error description, got '{errorDescription}'");
57+
58+
// Test string representation
59+
string exceptionStr = ex.ToString();
60+
if (!exceptionStr.Contains("ERTTIException"))
61+
throw new Exception($"String representation should contain 'ERTTIException', got '{exceptionStr}'");
62+
if (!exceptionStr.Contains(errorCode.ToString()))
63+
throw new Exception($"String representation should contain error code, got '{exceptionStr}'");
64+
65+
Console.WriteLine("+ All exception property tests passed!");
66+
}
67+
}
68+
2269
static void Main()
2370
{
71+
Console.WriteLine("RTTI C# Example with Exception Testing");
72+
Console.WriteLine("==================================================");
73+
Console.WriteLine();
74+
75+
// Test exception methods first
76+
Console.WriteLine("Testing Exception Methods:");
77+
Console.WriteLine("------------------------------");
78+
TestExceptionMethods();
79+
Console.WriteLine();
80+
81+
// Then run the main example
82+
Console.WriteLine("Running Main RTTI Example:");
83+
Console.WriteLine("------------------------------");
2484
try
2585
{
2686
UInt32 nMajor, nMinor, nMicro;
@@ -96,9 +156,21 @@ static void Main()
96156
if (!(Animal.GetHandle() != IntPtr.Zero)) throw new Exception("Wrong data");
97157
if (!(Animal.Name().Equals("Gary Giraffe"))) throw new Exception("Wrong data");
98158
if (!(Animal is RTTI.CGiraffe)) throw new Exception("Wrong data");
99-
159+
100160
Animal = Iterator.GetNextAnimal();
101161
if (!(Animal.GetHandle() == IntPtr.Zero)) throw new Exception("Wrong data");
162+
163+
Console.WriteLine("+ Main example completed successfully!");
164+
}
165+
catch (RTTI.ERTTIException ex)
166+
{
167+
Console.WriteLine("RTTI Exception occurred:");
168+
Console.WriteLine($" Error Code: {ex.ErrorCode}");
169+
Console.WriteLine($" Error Message: '{ex.ErrorMessage}'");
170+
Console.WriteLine($" Error Name: {ex.ErrorName}");
171+
Console.WriteLine($" Error Description: {ex.ErrorDescription}");
172+
Console.WriteLine($" Full Exception: {ex}");
173+
System.Environment.Exit(1);
102174
}
103175
catch (Exception e)
104176
{

0 commit comments

Comments
 (0)