X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2Fengine%2FAny.cxx;h=cda84b110d7e5bea36988cc02db4039941bdcf9d;hb=1894c52d0838df8676e770bef061fc23ca436452;hp=a1ec6e5593c94fb77f91659fae92950569366502;hpb=802458daad8b198beabbb058dc87437bdc63b1a3;p=modules%2Fyacs.git diff --git a/src/engine/Any.cxx b/src/engine/Any.cxx index a1ec6e559..cda84b110 100644 --- a/src/engine/Any.cxx +++ b/src/engine/Any.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2006-2015 CEA/DEN, EDF R&D +// Copyright (C) 2006-2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -22,6 +22,13 @@ #include "TypeCode.hxx" #include "InvalidExtractionException.hxx" +#include +#include +#include +#include +#include + +#include #include #include @@ -31,12 +38,160 @@ using namespace std; // forbidden value int=-269488145 double=-1.54947e+231 bool=239 const char SeqAlloc::DFT_CHAR_VAR=-17;//0xEF -StringOnHeap::StringOnHeap(const char *val):_dealloc(0),_str(strdup(val)) +constexpr unsigned NB_BITS = 6; + +constexpr unsigned char TAB[64]={46, 61, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}; + +unsigned char BitAtPosSimple(char val, std::size_t bitPos) +{ + return (val >> bitPos) & 0x1; +} + +unsigned char BitAtPos(char pt0, char pt1, std::size_t bitPos) +{ + if(bitPos<8) + return BitAtPosSimple(pt0,bitPos); + else + return BitAtPosSimple(pt1,bitPos-8); +} + +unsigned char ChunkInternal(char pt0, char pt1, std::size_t startBitIdInByte) +{ + unsigned char ret(0); + for(unsigned i = 0; i>= 1; + return ret; +} + +unsigned char ChunkAtPos(const char *pt, std::size_t len, std::size_t posChunk) { + std::size_t startByte((posChunk*NB_BITS)/8); + std::size_t startBitIdInByte((posChunk*NB_BITS)%8); + char pt1(startByte!=len-1?pt[startByte+1]:pt[startByte]); + return ChunkInternal(pt[startByte],pt1,startBitIdInByte); } -StringOnHeap::StringOnHeap(const std::string& val):_dealloc(0),_str(strdup(val.c_str())) +std::size_t OnOff(std::size_t i) { + if(i!=0) + return 1; + return 0; +} + +std::string YACS::ENGINE::ToBase64(const std::string& bytes) +{//64 == 2**6 + const char *bytesPt(bytes.c_str()); + std::size_t input_len(bytes.size()); + std::size_t input_len_bit(input_len*8); + std::size_t nb_chunks( input_len_bit/NB_BITS + OnOff((NB_BITS - input_len_bit%NB_BITS)%NB_BITS) ); + std::string ret(nb_chunks,'\0'); + for(std::size_t i=0;i> (5-bitPos) ) & 0x1; +} + +char BitAtPosOnChunk(char pt0, char pt1, std::size_t bitPos) +{ + if(bitPos<6) + return BitAtPosSimple2(pt0,bitPos); + else + return BitAtPosSimple2(pt1,bitPos-6); +} + +unsigned char CheckEntry(char c) +{ + if( ((unsigned) c) < MAX_VAL_TAB2 ) + { + unsigned char ret(TAB2[(unsigned char)c]); + if(ret != NOT_OK_VAL) + return ret; + throw YACS::Exception("Invalid character found !"); + } + throw YACS::Exception("Invalid character found !"); +} + +char ByteInternal(char c0, char c1, std::size_t startBitIdInByte) +{ + unsigned char ret(0); + char ct0(CheckEntry(c0)),ct1(CheckEntry(c1)); + for(int i = 7; i>=0; --i) + { + ret |= BitAtPosOnChunk(ct0,ct1,startBitIdInByte+i); + if(i!=0) + ret <<= 1; + } + return ret; +} + +char ByteAtPos(const char *chunckPt, std::size_t bytePos) +{ + std::size_t startChunk((bytePos*8)/NB_BITS); + std::size_t startBitId((bytePos*8)%NB_BITS); + return ByteInternal(chunckPt[startChunk],chunckPt[startChunk+1],startBitId); +} + +std::string YACS::ENGINE::FromBase64(const std::string& bytes) +{ + std::size_t nb_chunks(bytes.size()); + const char *chunckPt(bytes.c_str()); + std::size_t nb_bytes_output((nb_chunks*NB_BITS)/8); + std::string ret(nb_bytes_output,'\0'); + for(std::size_t i = 0; iisA(Runtime::_tc_string)) + if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref) { StringOnHeap *cpy=(other._value._s)->deepCopy(); memcpy(&_value._s,&cpy,_type->getSizeInByteOfAnyReprInSeq()); @@ -144,7 +307,7 @@ AtomAny::AtomAny(char *val, Deallocator deAlloc):Any(Runtime::_tc_string) AtomAny::AtomAny(char *data, TypeCode* type):Any(type) { - if(type->isA(Runtime::_tc_string)) + if(type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref) { void **tmp=(void **)data; StringOnHeap *cpy=((StringOnHeap *)(*tmp))->deepCopy(); @@ -168,12 +331,17 @@ AtomAny *AtomAny::New(char *val,Deallocator dealloc) return new AtomAny(val,dealloc); } -AnyPtr AtomAny::operator[](int i) const throw(YACS::Exception) +AtomAny *AtomAny::New(const std::string& val, TypeCode *type) +{ + return new AtomAny(val,type); +} + +AnyPtr AtomAny::operator[](int i) const { throw InvalidExtractionException(_type->kind(),Sequence); } -AnyPtr AtomAny::operator[](const char *key) const throw(YACS::Exception) +AnyPtr AtomAny::operator[](const char *key) const { throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical."); } @@ -189,13 +357,13 @@ bool AtomAny::operator ==(const Any& other) const return _value._i==otherC._value._i; else if(_type->isA(Runtime::_tc_bool)) return _value._b==otherC._value._b; - else if(_type->isA(Runtime::_tc_string)) + else if(_type->isA(Runtime::_tc_string) || _type->kind()==Objref) return (*_value._s)==*(otherC._value._s); else return false; } -int AtomAny::getIntValue() const throw(YACS::Exception) +int AtomAny::getIntValue() const { if(_type->isA(Runtime::_tc_int)) return _value._i; @@ -203,7 +371,7 @@ int AtomAny::getIntValue() const throw(YACS::Exception) throw Exception("Value is not an int"); } -bool AtomAny::getBoolValue() const throw(YACS::Exception) +bool AtomAny::getBoolValue() const { if(_type->isA(Runtime::_tc_bool)) return _value._b; @@ -211,7 +379,7 @@ bool AtomAny::getBoolValue() const throw(YACS::Exception) throw Exception("Value is not a bool"); } -double AtomAny::getDoubleValue() const throw(YACS::Exception) +double AtomAny::getDoubleValue() const { if(_type->isA(Runtime::_tc_double)) return _value._d; @@ -219,10 +387,27 @@ double AtomAny::getDoubleValue() const throw(YACS::Exception) throw Exception("Value is not a double"); } -std::string AtomAny::getStringValue() const throw(YACS::Exception) +std::string AtomAny::getStringValue() const { - if(_type->isA(Runtime::_tc_string)) - return string(_value._s->cStr()); + if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref) + { + std::size_t sz(_value._s->size()); + if(sz==0) + return string(_value._s->cStr()); + else + return string(_value._s->cStr(),sz); + } + else + throw Exception("Value is not a string"); +} + +const char *AtomAny::getBytesValue(std::size_t& len) const +{ + if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref) + { + len=_value._s->size(); + return _value._s->cStr(); + } else throw Exception("Value is not a string"); } @@ -236,7 +421,7 @@ std::string AtomAny::getStringValue() const throw(YACS::Exception) */ void AtomAny::putMyReprAtPlace(char *data) const { - if(_type->isA(Runtime::_tc_string)) + if(_type->isA(Runtime::_tc_string) || _type->kind()==YACS::ENGINE::Objref) { StringOnHeap *tmp=_value._s->deepCopy(); memcpy(data,&tmp,_type->getSizeInByteOfAnyReprInSeq()); @@ -262,7 +447,7 @@ void AtomAny::putMyReprAtPlace(char *data) const */ void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy) { - if(type->isA(Runtime::_tc_string)) + if(type->isA(Runtime::_tc_string) || type->kind()==YACS::ENGINE::Objref) { void **tmp1=(void **)src; StringOnHeap *tmp=((const StringOnHeap *)(*tmp1))->deepCopy(); @@ -283,7 +468,7 @@ void AtomAny::putReprAtPlace(char *data, const char *src, const TypeCode *type, void AtomAny::destroyReprAtPlace(char *data, const TypeCode *type) { DynType typ=type->kind(); - if(typ==String) + if(typ==String || typ==Objref) { if(!Any::IsNull(data)) { @@ -308,7 +493,7 @@ bool AtomAny::takeInChargeStorageOf(TypeCode *type) AtomAny::~AtomAny() { - if(_type->kind() == String) + if(_type->kind() == String || _type->kind()==Objref) delete _value._s; } @@ -322,33 +507,33 @@ ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type) _type->decrRef(); } -AnyPtr ComposedAny::operator[](const char *key) const throw(YACS::Exception) +AnyPtr ComposedAny::operator[](const char *key) const { throw Exception("AtomAny::operator[] : try to get a part of a partitionned data not localizable by a string."); } -void ComposedAny::checkTypeOf(const Any *elem) const throw(YACS::Exception) +void ComposedAny::checkTypeOf(const Any *elem) const { if(!elem->getType()->isA(_type->contentType())) throw Exception("ComposedAny::checkTypeOf : invalid type."); } -int ComposedAny::getIntValue() const throw(YACS::Exception) +int ComposedAny::getIntValue() const { throw InvalidExtractionException(_type->kind(),Runtime::_tc_int->kind()); } -bool ComposedAny::getBoolValue() const throw(YACS::Exception) +bool ComposedAny::getBoolValue() const { throw InvalidExtractionException(_type->kind(),Runtime::_tc_bool->kind()); } -double ComposedAny::getDoubleValue() const throw(YACS::Exception) +double ComposedAny::getDoubleValue() const { throw InvalidExtractionException(_type->kind(),Runtime::_tc_double->kind()); } -std::string ComposedAny::getStringValue() const throw(YACS::Exception) +std::string ComposedAny::getStringValue() const { throw InvalidExtractionException(_type->kind(),Runtime::_tc_string->kind()); } @@ -508,14 +693,14 @@ bool SequenceAny::operator ==(const Any& other) const return true; } -void SequenceAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception) +void SequenceAny::setEltAtRank(int i, const Any *elem) { checkTypeOf(elem); _alloc.destroy(_alloc._start+i*_alloc._sizeOf1Elm,_type->contentType()); _alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem); } -AnyPtr SequenceAny::operator[](int i) const throw(YACS::Exception) +AnyPtr SequenceAny::operator[](int i) const { return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm); } @@ -584,23 +769,11 @@ SequenceAny *SequenceAny::removeUnsetItemsFromThis() const SequenceAny *SequenceAny::New(const TypeCode *typeOfContent) { - if(typeOfContent->kind() == Objref) - { - //In case of Objref, use a sequence of string - return new SequenceAny(Runtime::_tc_string); - } - else return new SequenceAny(typeOfContent); } SequenceAny *SequenceAny::New(const TypeCode *typeOfContent, unsigned lgth) { - if(typeOfContent->kind() == Objref) - { - //In case of Objref, use a sequence of string - return new SequenceAny(Runtime::_tc_string,lgth); - } - else return new SequenceAny(typeOfContent,lgth); } @@ -799,7 +972,7 @@ ArrayAny::ArrayAny(const std::vector& val):ComposedAny(new TypeCode } } -void ArrayAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception) +void ArrayAny::setEltAtRank(int i, const Any *elem) { checkTypeOf(elem); const TypeCode *subType=_type->contentType(); @@ -818,7 +991,7 @@ bool ArrayAny::operator ==(const Any& other) const return true; } -AnyPtr ArrayAny::operator[](int i) const throw(YACS::Exception) +AnyPtr ArrayAny::operator[](int i) const { const TypeCode *subType=_type->contentType(); unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq(); @@ -896,13 +1069,13 @@ bool StructAny::operator ==(const Any& other) const return true; } -AnyPtr StructAny::operator[](int i) const throw(YACS::Exception) +AnyPtr StructAny::operator[](int i) const { const char what[]="StructAny::operator[](int i) : Struct key are strings not integers."; throw Exception(what); } -AnyPtr StructAny::operator[](const char *key) const throw(YACS::Exception) +AnyPtr StructAny::operator[](const char *key) const { const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type; char *whereToGet=_data; @@ -920,13 +1093,13 @@ AnyPtr StructAny::operator[](const char *key) const throw(YACS::Exception) return (*iter).second->getOrBuildAnyFromZippedData(whereToGet); } -void StructAny::setEltAtRank(int i, const Any *elem) throw(YACS::Exception) +void StructAny::setEltAtRank(int i, const Any *elem) { const char what[]="Struct key are strings not integers."; throw Exception(what); } -void StructAny::setEltAtRank(const char *key, const Any *elem) throw(YACS::Exception) +void StructAny::setEltAtRank(const char *key, const Any *elem) { const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type; unsigned offset;