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
26 template<
int N>
class RscStructReader;
27 template<
int N>
class RscStructWriter;
28 template<
class T,
bool IsClass,
bool IsSerializable>
class RscValueAdapter;
36 template<
int MaxStringSize = 0>
44 template<
class T,
bool IsClass,
bool IsSerializable>
friend class RscValueAdapter;
47 using ReadElementFunction = std::function<RscType(RscType expectedType, byte* pValue)>;
48 using WriteElementFunction = std::function<void(RscType valueType, const byte* pValue)>;
98 RscVariant(
size_t arraySize,
RscType arrayElementType,
size_t dimensions = 1,
size_t fieldCount = 0);
109 RscVariant(
size_t arraySize,
RscType arrayElementType, ReadElementFunction* pFunction,
size_t dimensions = 1,
size_t fieldCount = 0);
120 RscVariant(
size_t arraySize,
RscType arrayElementType, WriteElementFunction* pFunction,
size_t dimensions = 1,
size_t fieldCount = 0);
125 RscVariant(
size_t fieldCount, ReadElementFunction* pFunction);
161 RscVariant& operator=(
const T& value);
162 RscVariant& operator=(
const String& value);
163 RscVariant& operator=(
const char* value);
168 RscVariant& operator=(
const RscVariant& value) =
default;
169 RscVariant& operator=(RscVariant&&) =
default;
170 bool operator==(
const RscVariant& value)
const;
171 bool operator!=(
const RscVariant& value)
const;
251 void CopyTo(T& value)
const;
267 void InitComplexTypeInfo(
void);
268 bool ContainsTypeInformation(
void)
const;
269 size_t GetSize(
void)
const;
272 struct ComplexTypeInfo
279 RemotingReader* pReader =
nullptr;
280 mutable RemotingWriter* pWriter =
nullptr;
281 ReadElementFunction* pReadElementFunction =
nullptr;
282 mutable WriteElementFunction* pWriteElementFunction =
nullptr;
286 static const size_t maxPrimitiveSize = 8;
287 #ifdef ARP_SYSTEM_RSC_SUPPORT_VERSION_GUID_AS_OBJECT 288 static const size_t minBufferSize = std::max(maxPrimitiveSize, std::max(
sizeof(
RscGuid),
sizeof(
RscVersion)));
290 static const size_t minBufferSize = maxPrimitiveSize;
291 #endif // ARP_SYSTEM_RSC_SUPPORT_VERSION_GUID_AS_OBJECT 293 static const size_t bufferSize = minBufferSize < (size_t)MaxStringSize ? (
size_t)MaxStringSize : minBufferSize;
298 ComplexTypeInfo typeInfo;
299 byte buffer[bufferSize];
316 template<
int MaxStringSize>
322 template<
int MaxStringSize>
326 this->operator=(value);
329 template<
int MaxStringSize>
333 this->operator=(value);
336 template<
int MaxStringSize>
339 this->operator=(value);
342 template<
int MaxStringSize>
346 this->operator=(value);
349 template<
int MaxStringSize>
353 this->operator=(value);
356 template<
int MaxStringSize>
360 if(arrayElementType == RscType::Struct && fieldCount == 0)
362 throw ArgumentException::Create(
"fieldCount", fieldCount,
"fieldCount has to be set for struct element types");
364 this->GetArrayInformation() =
RscArrayInformation(arraySize, arrayElementType, dimensions, fieldCount);
365 this->typeInfo.pReader =
nullptr;
366 this->typeInfo.pWriter =
nullptr;
367 this->typeInfo.pReadElementFunction =
nullptr;
368 this->typeInfo.pWriteElementFunction =
nullptr;
371 template<
int MaxStringSize>
375 if(arrayElementType == RscType::Struct && fieldCount == 0)
377 throw ArgumentException::Create(
"fieldCount", fieldCount,
"fieldCount has to be set for struct element types");
379 this->GetArrayInformation() =
RscArrayInformation(arraySize, arrayElementType, dimensions, fieldCount);
380 this->typeInfo.pReader =
nullptr;
381 this->typeInfo.pWriter =
nullptr;
382 this->typeInfo.pReadElementFunction = pFunction;
383 this->typeInfo.pWriteElementFunction =
nullptr;
386 template<
int MaxStringSize>
390 if(arrayElementType == RscType::Struct && fieldCount == 0)
392 throw ArgumentException::Create(
"fieldCount", fieldCount,
"fieldCount has to be set for struct element types");
394 this->GetArrayInformation() =
RscArrayInformation(arraySize, arrayElementType, dimensions, fieldCount);
395 this->typeInfo.pReader =
nullptr;
396 this->typeInfo.pWriter =
nullptr;
397 this->typeInfo.pReadElementFunction =
nullptr;
398 this->typeInfo.pWriteElementFunction = pFunction;
401 template<
int MaxStringSize>
405 this->InitComplexTypeInfo();
407 this->typeInfo.pReadElementFunction = pFunction;
410 template<
int MaxStringSize>
416 template<
int MaxStringSize>
421 case RscType::IecTime:
422 return RscType::Int32;
423 case RscType::IecTime64:
424 case RscType::IecDate64:
425 case RscType::IecDateTime64:
426 case RscType::IecTimeOfDay64:
427 return RscType::Int64;
434 template<
int MaxStringSize>
437 return this->GetArrayInformation().ElementType;
440 template<
int MaxStringSize>
443 if(this->GetType() != RscType::Array)
447 return this->typeInfo.arrayInformation;
450 template<
int MaxStringSize>
453 if(this->GetType() != RscType::Array)
457 return this->typeInfo.arrayInformation;
460 template<
int MaxStringSize>
463 if(this->GetType() != RscType::Struct)
467 return this->typeInfo.structInformation;
470 template<
int MaxStringSize>
473 if(this->GetType() != RscType::Struct)
477 return this->typeInfo.structInformation;
480 template<
int MaxStringSize>
483 return this->GetArrayInformation().Dimensions;
486 template<
int MaxStringSize>
489 return this->GetStructInformation().FieldCount;
492 template<
int MaxStringSize>
498 template<
int MaxStringSize>
504 template<
int MaxStringSize>
507 this->type = rscType;
510 template<
int MaxStringSize>
513 return (this->type == RscType::Struct || this->type == RscType::Array);
516 template<
int MaxStringSize>
520 this->type = GetRscType<T>();
521 if(this->type == RscType::None)
525 #ifndef ARP_SYSTEM_RSC_SUPPORT_VERSION_GUID_AS_OBJECT 530 #endif // ARP_SYSTEM_RSC_SUPPORT_VERSION_GUID_AS_OBJECT 531 *
reinterpret_cast<T*
>(this->buffer) = value;
535 template<
int MaxStringSize>
539 result.InitComplexTypeInfo();
540 result.GetStructInformation().FieldCount = fieldCount;
544 template<
int MaxStringSize>
551 template<
int MaxStringSize>
555 result.typeInfo.pWriter = &writer;
559 template<
int MaxStringSize>
563 result.typeInfo.pWriter = &writer;
567 template<
int MaxStringSize>
570 size_t len = value.
Length();
571 this->Assign(value.
CStr(), len);
575 template<
int MaxStringSize>
578 size_t len = strlen(value);
579 this->Assign(value, len);
583 template<
int MaxStringSize>
587 this->Assign(value.
CStr(), strlen(value.
CStr()), RscType::Utf8String);
591 template<
int MaxStringSize>
595 this->Assign(value.
CStr(), strlen(value.
CStr()), RscType::SecureString);
599 template<
int MaxStringSize>
602 if (len >= MaxStringSize)
604 throw ArgumentException(
"Cannot copy string: argument string length {0} exceeds MaxStringSize {1}", len, MaxStringSize);
606 if (typeArg != RscType::Utf8String && typeArg != RscType::SecureString)
610 memcpy(this->buffer, value, len);
611 this->buffer[len] =
'\0';
612 this->type = typeArg;
615 template<
int MaxStringSize>
618 if (this->GetType() != value.
GetType())
623 if (this->GetSize() != value.GetSize())
628 if (memcmp(this->buffer, value.buffer, this->GetSize()) != 0)
633 if (this->GetType() == RscType::Utf8String)
635 if (strcmp((
char*)this->buffer, (
char*)value.buffer) != 0)
644 template<
int MaxStringSize>
647 return !(*
this == value);
650 template<
int MaxStringSize>
654 this->typeInfo.pReader =
nullptr;
655 this->typeInfo.pWriter =
nullptr;
656 this->typeInfo.pReadElementFunction =
nullptr;
657 this->typeInfo.pWriteElementFunction =
nullptr;
660 template<
int MaxStringSize>
664 RscType argType = GetRscType<T>();
665 if(this->GetValueType() != argType)
667 throw InvalidCastException(
"Cannot copy value to argument: RscVariant contains data type {0} but arg is of type {1}", this->type, argType);
669 value = *
reinterpret_cast<const T*
>(this->buffer);
672 template<
int MaxStringSize>
675 if (this->type != RscType::Utf8String && this->type != RscType::SecureString && this->type != RscType::AnsiString)
679 return reinterpret_cast<const char*
>(this->buffer);
682 template<
int MaxStringSize>
685 if(this->type != RscType::Array && this->type != RscType::Struct)
689 this->typeInfo.pWriteElementFunction = pFunction;
692 template<
int MaxStringSize>
697 this->CopyTo(result);
701 template<
int MaxStringSize>
704 const byte* pValue = this->GetDataAddress();
708 case RscType::SecureString:
709 case RscType::AnsiString:
710 case RscType::Utf8String:
711 return reinterpret_cast<const char*
>(pValue);
712 case RscType::DateTime:
713 return reinterpret_cast<const DateTime*
>(pValue)->ToIso8601String();
716 std::ostringstream oss;
717 oss << std::boolalpha << *reinterpret_cast<const bool*>(pValue);
725 return std::to_string(*reinterpret_cast<const char16*>(pValue));
727 return std::to_string(*reinterpret_cast<const int16*>(pValue));
729 case RscType::IecTime:
730 return std::to_string(*reinterpret_cast<const int32*>(pValue));
732 return std::to_string(*reinterpret_cast<const int8*>(pValue));
734 case RscType::IecTime64:
735 case RscType::IecDate64:
736 case RscType::IecDateTime64:
737 case RscType::IecTimeOfDay64:
738 return std::to_string(*reinterpret_cast<const int64*>(pValue));
741 return String::Empty;
742 case RscType::Real32:
743 return std::to_string(*reinterpret_cast<const float*>(pValue));
744 case RscType::Real64:
745 return std::to_string(*reinterpret_cast<const double*>(pValue));
746 case RscType::Uint16:
747 return std::to_string(*reinterpret_cast<const uint16*>(pValue));
748 case RscType::Uint32:
749 return std::to_string(*reinterpret_cast<const uint32*>(pValue));
750 case RscType::Uint64:
751 return std::to_string(*reinterpret_cast<const uint64*>(pValue));
753 return std::to_string(*reinterpret_cast<const uint8*>(pValue));
755 return reinterpret_cast<const RscVersion*
>(pValue)->ToString();
757 return reinterpret_cast<const RscGuid*
>(pValue)->ToString();
759 return String::Empty;
766 template<
int MaxStringSize>
769 switch (this->GetValueType())
781 case RscType::Uint16:
785 case RscType::Uint32:
786 case RscType::Real32:
787 case RscType::IecTime:
791 case RscType::Uint64:
792 case RscType::Real64:
793 case RscType::DateTime:
794 case RscType::IecTime64:
795 case RscType::IecDate64:
796 case RscType::IecDateTime64:
797 case RscType::IecTimeOfDay64:
800 case RscType::Utf8String:
801 return strlen((
char*)this->buffer);
808 case RscType::Struct:
813 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
const char * GetChars(void) const
Gets pointer to internal string buffer. Operation only valid for Utf8Strings.
Definition: RscVariant.hxx:673
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:1763
This exception is used when a method is not supported.
Definition: NotSupportedException.hpp:14
String ToString(void) const
Converts this instance to string if the variant type has a reasonable string representation.
Definition: RscVariant.hxx:702
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
RscType GetArrayElementType(void) const
Gets the RscType of the array elements if RscVariant contains array information.
Definition: RscVariant.hxx:435
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
void SetWriteElementFunction(WriteElementFunction *pFunction) const
Sets callback for write function to write a single array element. This could only be used for Variant...
Definition: RscVariant.hxx:683
const CharType * CStr() const
Gets the character data of this string.
Definition: BasicString.hxx:1482
RscType
Datatypes supported by Rsc. Values are identical with CommonRemoting::RemotingMarshalType. Only supported types of RemotingMarshalType are included.
Definition: RscType.hpp:35
void CopyTo(T &value) const
Copies internal data to memory referenced by value. Only for primtive types. Read Strings with GetCha...
Definition: RscVariant.hxx:662
Rsc container class for primitive data type, strings or information about arrays or structs...
Definition: RscVariant.hxx:37
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
const byte * GetDataAddress(void) const
Gets a raw pointer to internal data buffer. To read data prefer CopyTo and to write prefer assignment...
Definition: RscVariant.hxx:493
size_type Length() const
Returns the number of char elements in this string.
Definition: BasicString.hxx:1037
void SetType(RscType rscType)
Forces the internal RscType to be set to another RscType. This Method does no conversion or validatio...
Definition: RscVariant.hxx:505
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:27
Root namespace for the PLCnext API
RscType GetType(void) const
Gets the RscType of the contained element
Definition: RscVariant.hxx:411
T GetValue(void) const
Converts this value to the given type T.
Definition: RscVariant.hxx:694
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:1727
RscType GetValueType(void) const
Gets the value type as RscType of the contained element
Definition: RscVariant.hxx:417
size_t GetFieldCount(void) const
Returns field count, if RscVariant contains struct information
Definition: RscVariant.hxx:487
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
static RscVariant< MaxStringSize > CreateArrayVariant(size_t arraySize, RscType elementType, size_t dimensions=1, size_t fieldCount=0)
Creates a new RscVariant initialized with RscArrayInformation
Definition: RscVariant.hxx:545
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
size_t GetArrayDimensions(void) const
Gets the count of array dimensions (1 for simple array, 2 for array of array etc.). This method is only valid for RscVariants with array information.
Definition: RscVariant.hxx:481
static RscVariant< MaxStringSize > CreateStructVariant(size_t fieldCount)
Creates a new RscVariant initialized with RscStructInformation
Definition: RscVariant.hxx:536
RscVariant(RscType type=RscType::None)
Creates an empty instance of RscVariant
Definition: RscVariant.hxx:317
unsigned char byte
The Arp character type.
Definition: PrimitiveTypes.hpp:23