7 #include "Arp/System/Commons/Exceptions/Exceptions.h" 8 #include "Arp/System/Rsc/Services/Rsc.h" 9 #include "Arp/System/Rsc/Services/RscGuid.hpp" 10 #include "Arp/System/Rsc/Services/RscVersion.hpp" 11 #include "Arp/System/Rsc/Services/RscString.hxx" 12 #include "Arp/System/Rsc/Services/SecureString.hxx" 18 namespace Arp {
namespace System {
namespace Rsc {
namespace Services
28 template<
int N>
class RscStructReader;
29 template<
int N>
class RscStructWriter;
30 template<
class T,
bool IsClass,
bool IsSerializable>
class RscValueAdapter;
38 template<
int MaxStringSize = 0>
46 template<
class T,
bool IsClass,
bool IsSerializable>
friend class RscValueAdapter;
49 using ReadElementFunction = std::function<RscType(RscType expectedType, byte* pValue)>;
50 using WriteElementFunction = std::function<void(RscType valueType, const byte* pValue)>;
100 RscVariant(
size_t arraySize,
RscType arrayElementType,
size_t dimensions = 1,
size_t fieldCount = 0);
111 RscVariant(
size_t arraySize,
RscType arrayElementType, ReadElementFunction* pFunction,
size_t dimensions = 1,
size_t fieldCount = 0);
122 RscVariant(
size_t arraySize,
RscType arrayElementType, WriteElementFunction* pFunction,
size_t dimensions = 1,
size_t fieldCount = 0);
127 RscVariant(
size_t fieldCount, ReadElementFunction* pFunction);
137 RscVariant(RscVariant&&) =
default;
159 static RscVariant<MaxStringSize> CreateArrayVariant(
size_t arraySize,
RscType elementType, RemotingWriter& writer,
size_t dimensions = 1,
size_t fieldCount = 0);
163 RscVariant& operator=(
const T& value);
164 RscVariant& operator=(
const String& value);
165 RscVariant& operator=(
const char* value);
170 RscVariant& operator=(
const RscVariant& value) =
default;
171 RscVariant& operator=(RscVariant&&) =
default;
172 bool operator==(
const RscVariant& value)
const;
173 bool operator!=(
const RscVariant& value)
const;
186 RscType GetValueType(
void)
const;
193 RscType GetArrayElementType(
void)
const;
200 size_t GetArrayDimensions(
void)
const;
207 size_t GetFieldCount(
void)
const;
213 const char* GetChars(
void)
const;
220 void SetWriteElementFunction(WriteElementFunction* pFunction)
const;
226 const byte* GetDataAddress(
void)
const;
232 byte* GetDataAddress(
void);
246 T GetValue(
void)
const;
253 void CopyTo(T& value)
const;
259 String ToString(
void)
const;
269 void InitComplexTypeInfo(
void);
270 bool ContainsTypeInformation(
void)
const;
271 size_t GetSize(
void)
const;
274 struct ComplexTypeInfo
281 RemotingReader* pReader =
nullptr;
282 mutable RemotingWriter* pWriter =
nullptr;
283 ReadElementFunction* pReadElementFunction =
nullptr;
284 mutable WriteElementFunction* pWriteElementFunction =
nullptr;
288 static const size_t maxPrimitiveSize = 8;
289 #ifdef ARP_SYSTEM_RSC_SUPPORT_VERSION_GUID_AS_OBJECT 290 static const size_t minBufferSize = std::max(maxPrimitiveSize, std::max(
sizeof(
RscGuid),
sizeof(
RscVersion)));
292 static const size_t minBufferSize = maxPrimitiveSize;
293 #endif // ARP_SYSTEM_RSC_SUPPORT_VERSION_GUID_AS_OBJECT 295 static const size_t bufferSize = minBufferSize < (size_t)MaxStringSize ? (
size_t)MaxStringSize : minBufferSize;
300 ComplexTypeInfo typeInfo;
301 byte buffer[bufferSize];
318 template<
int MaxStringSize>
324 template<
int MaxStringSize>
328 this->operator=(value);
331 template<
int MaxStringSize>
335 this->operator=(value);
338 template<
int MaxStringSize>
341 this->operator=(value);
344 template<
int MaxStringSize>
348 this->operator=(value);
351 template<
int MaxStringSize>
355 this->operator=(value);
358 template<
int MaxStringSize>
362 if(arrayElementType == RscType::Struct && fieldCount == 0)
364 throw ArgumentException::Create(
"fieldCount", fieldCount,
"fieldCount has to be set for struct element types");
366 this->GetArrayInformation() =
RscArrayInformation(arraySize, arrayElementType, dimensions, fieldCount);
367 this->typeInfo.pReader =
nullptr;
368 this->typeInfo.pWriter =
nullptr;
369 this->typeInfo.pReadElementFunction =
nullptr;
370 this->typeInfo.pWriteElementFunction =
nullptr;
373 template<
int MaxStringSize>
377 if(arrayElementType == RscType::Struct && fieldCount == 0)
379 throw ArgumentException::Create(
"fieldCount", fieldCount,
"fieldCount has to be set for struct element types");
381 this->GetArrayInformation() =
RscArrayInformation(arraySize, arrayElementType, dimensions, fieldCount);
382 this->typeInfo.pReader =
nullptr;
383 this->typeInfo.pWriter =
nullptr;
384 this->typeInfo.pReadElementFunction = pFunction;
385 this->typeInfo.pWriteElementFunction =
nullptr;
388 template<
int MaxStringSize>
392 if(arrayElementType == RscType::Struct && fieldCount == 0)
394 throw ArgumentException::Create(
"fieldCount", fieldCount,
"fieldCount has to be set for struct element types");
396 this->GetArrayInformation() =
RscArrayInformation(arraySize, arrayElementType, dimensions, fieldCount);
397 this->typeInfo.pReader =
nullptr;
398 this->typeInfo.pWriter =
nullptr;
399 this->typeInfo.pReadElementFunction =
nullptr;
400 this->typeInfo.pWriteElementFunction = pFunction;
403 template<
int MaxStringSize>
407 this->InitComplexTypeInfo();
409 this->typeInfo.pReadElementFunction = pFunction;
412 template<
int MaxStringSize>
418 template<
int MaxStringSize>
423 case RscType::IecTime:
424 return RscType::Int32;
425 case RscType::IecTime64:
426 case RscType::IecDate64:
427 case RscType::IecDateTime64:
428 case RscType::IecTimeOfDay64:
429 return RscType::Int64;
436 template<
int MaxStringSize>
439 return this->GetArrayInformation().ElementType;
442 template<
int MaxStringSize>
445 if(this->GetType() != RscType::Array)
449 return this->typeInfo.arrayInformation;
452 template<
int MaxStringSize>
455 if(this->GetType() != RscType::Array)
459 return this->typeInfo.arrayInformation;
462 template<
int MaxStringSize>
465 if(this->GetType() != RscType::Struct)
469 return this->typeInfo.structInformation;
472 template<
int MaxStringSize>
475 if(this->GetType() != RscType::Struct)
479 return this->typeInfo.structInformation;
482 template<
int MaxStringSize>
485 return this->GetArrayInformation().Dimensions;
488 template<
int MaxStringSize>
491 return this->GetStructInformation().FieldCount;
494 template<
int MaxStringSize>
500 template<
int MaxStringSize>
506 template<
int MaxStringSize>
509 this->type = rscType;
512 template<
int MaxStringSize>
515 return (this->type == RscType::Struct || this->type == RscType::Array);
518 template<
int MaxStringSize>
522 this->type = GetRscType<T>();
523 if(this->type == RscType::None)
527 #ifndef ARP_SYSTEM_RSC_SUPPORT_VERSION_GUID_AS_OBJECT 532 #endif // ARP_SYSTEM_RSC_SUPPORT_VERSION_GUID_AS_OBJECT 533 *
reinterpret_cast<T*
>(this->buffer) = value;
537 template<
int MaxStringSize>
541 result.InitComplexTypeInfo();
542 result.GetStructInformation().FieldCount = fieldCount;
546 template<
int MaxStringSize>
553 template<
int MaxStringSize>
557 result.typeInfo.pWriter = &writer;
561 template<
int MaxStringSize>
565 result.typeInfo.pWriter = &writer;
569 template<
int MaxStringSize>
572 size_t len = value.
Length();
573 this->Assign(value.
CStr(), len);
577 template<
int MaxStringSize>
580 size_t len = strlen(value);
581 this->Assign(value, len);
585 template<
int MaxStringSize>
589 this->Assign(value.
CStr(), strlen(value.
CStr()), RscType::Utf8String);
593 template<
int MaxStringSize>
597 this->Assign(value.
CStr(), strlen(value.
CStr()), RscType::SecureString);
601 template<
int MaxStringSize>
604 if (len >= MaxStringSize)
606 throw ArgumentException(
"Cannot copy string: argument string length {0} exceeds MaxStringSize {1}", len, MaxStringSize);
608 if (typeArg != RscType::Utf8String && typeArg != RscType::SecureString)
612 memcpy(this->buffer, value, len);
613 this->buffer[len] =
'\0';
614 this->type = typeArg;
617 template<
int MaxStringSize>
620 if (this->GetType() != value.
GetType())
625 if (this->GetSize() != value.GetSize())
630 if (memcmp(this->buffer, value.buffer, this->GetSize()) != 0)
635 if (this->GetType() == RscType::Utf8String)
637 if (strcmp((
char*)this->buffer, (
char*)value.buffer) != 0)
646 template<
int MaxStringSize>
649 return !(*
this == value);
652 template<
int MaxStringSize>
656 this->typeInfo.pReader =
nullptr;
657 this->typeInfo.pWriter =
nullptr;
658 this->typeInfo.pReadElementFunction =
nullptr;
659 this->typeInfo.pWriteElementFunction =
nullptr;
662 template<
int MaxStringSize>
666 RscType argType = GetRscType<T>();
667 if(this->GetValueType() != argType)
669 throw InvalidCastException(
"Cannot copy value to argument: RscVariant contains data type {0} but arg is of type {1}", this->type, argType);
671 value = *
reinterpret_cast<const T*
>(this->buffer);
674 template<
int MaxStringSize>
677 if (this->type != RscType::Utf8String && this->type != RscType::SecureString && this->type != RscType::AnsiString)
681 return reinterpret_cast<const char*
>(this->buffer);
684 template<
int MaxStringSize>
687 if(this->type != RscType::Array && this->type != RscType::Struct)
691 this->typeInfo.pWriteElementFunction = pFunction;
694 template<
int MaxStringSize>
699 this->CopyTo(result);
703 template<
int MaxStringSize>
706 const byte* pValue = this->GetDataAddress();
710 case RscType::SecureString:
711 case RscType::AnsiString:
712 case RscType::Utf8String:
713 return reinterpret_cast<const char*
>(pValue);
714 case RscType::DateTime:
715 return reinterpret_cast<const DateTime*
>(pValue)->ToIso8601String();
718 std::ostringstream oss;
719 oss << std::boolalpha << *reinterpret_cast<const bool*>(pValue);
727 return std::to_string(*reinterpret_cast<const char16*>(pValue));
729 return std::to_string(*reinterpret_cast<const int16*>(pValue));
731 case RscType::IecTime:
732 return std::to_string(*reinterpret_cast<const int32*>(pValue));
734 return std::to_string(*reinterpret_cast<const int8*>(pValue));
736 case RscType::IecTime64:
737 case RscType::IecDate64:
738 case RscType::IecDateTime64:
739 case RscType::IecTimeOfDay64:
740 return std::to_string(*reinterpret_cast<const int64*>(pValue));
743 return String::Empty;
744 case RscType::Real32:
745 return std::to_string(*reinterpret_cast<const float*>(pValue));
746 case RscType::Real64:
747 return std::to_string(*reinterpret_cast<const double*>(pValue));
748 case RscType::Uint16:
749 return std::to_string(*reinterpret_cast<const uint16*>(pValue));
750 case RscType::Uint32:
751 return std::to_string(*reinterpret_cast<const uint32*>(pValue));
752 case RscType::Uint64:
753 return std::to_string(*reinterpret_cast<const uint64*>(pValue));
755 return std::to_string(*reinterpret_cast<const uint8*>(pValue));
757 return reinterpret_cast<const RscVersion*
>(pValue)->ToString();
759 return reinterpret_cast<const RscGuid*
>(pValue)->ToString();
761 return String::Empty;
768 template<
int MaxStringSize>
771 switch (this->GetValueType())
783 case RscType::Uint16:
787 case RscType::Uint32:
788 case RscType::Real32:
789 case RscType::IecTime:
793 case RscType::Uint64:
794 case RscType::Real64:
795 case RscType::DateTime:
796 case RscType::IecTime64:
797 case RscType::IecDate64:
798 case RscType::IecDateTime64:
799 case RscType::IecTimeOfDay64:
802 case RscType::Utf8String:
803 return strlen((
char*)this->buffer);
810 case RscType::Struct:
815 case RscType::SecureString:
const char * CStr(void) const
Returns pointer to internal buffer.
Definition: RscString.hxx:109
Definition: RscGuid.hpp:17
Helper class to read a struct from an RscVariant. This class uses the struct information stored in Rs...
Definition: RscStructReader.hxx:24
Helper class to read an array of primtive types from an RscVariant. This class uses the array informa...
Definition: RscArrayWriter.hpp:21
bool operator!=(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string on inequality.
Definition: BasicString.hxx:1788
This exception is used when a method is not supported.
Definition: NotSupportedException.hpp:14
Arp::BasicVersion Version
The Arp Version class.
Definition: TypeSystem.h:29
This exception is used when a method is not implemented yet.
Definition: NotImplementedException.hpp:14
The class contains date and time informations.
Definition: DateTime.hpp:44
Helper class to read an array of primtive types from an RscVariant. This class uses the array informa...
Definition: RscArrayReader.hpp:22
This exception is used when an invalid cast occurs.
Definition: InvalidCastException.hpp:14
const CharType * CStr() const
Gets the character data of this string.
Definition: BasicString.hxx:1507
RscType
Datatypes supported by Rsc. Values are identical with CommonRemoting::RemotingMarshalType. Only supported types of RemotingMarshalType are included.
Definition: RscType.hpp:27
Rsc container class for primitive data type, strings or information about arrays or structs...
Definition: RscVariant.hxx:39
Object type handled by Rsc as RscVariant
Specifies a version with 4 version numbers and is marshalled to .NET type System.Version.
Definition: RscVersion.hpp:20
This exception is used when a method call is invalid for object's current state.
Definition: InvalidOperationException.hpp:14
size_type Length() const
Returns the number of char elements in this string.
Definition: BasicString.hxx:1062
This exception is used when an invalid argument occurs.
Definition: ArgumentException.hpp:14
Specialized version of RscString for security context. Not implemented in this version. Wraps only RscString
Definition: RscType.hpp:18
Root namespace for the PLCnext API
RscType GetType(void) const
Gets the RscType of the contained element
Definition: RscVariant.hxx:413
bool operator==(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string on equality.
Definition: BasicString.hxx:1752
System components used by the System, Device, Plc or Io domains.
Helper class to write a struct from an RscVariant. This class uses the struct information stored in R...
Definition: RscStructWriter.hxx:18
Contains a static string with string lentgh up to N characters. The string has to be null terminated...
Definition: RscString.hxx:18
This is a small immutable wrapper around the boost::uuids::uuid class and represents a universal uniq...
Definition: Uuid.hpp:18
Namespace for basic functions of the framework
Definition: Console.hpp:14
RscVariant(RscType type=RscType::None)
Creates an empty instance of RscVariant
Definition: RscVariant.hxx:319
unsigned char byte
The Arp character type.
Definition: PrimitiveTypes.hpp:23