X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2Fengine%2FAny.cxx;h=cda84b110d7e5bea36988cc02db4039941bdcf9d;hb=1894c52d0838df8676e770bef061fc23ca436452;hp=af845e2ce6b49b1db847704552583193b503d68d;hpb=313a04631c63078d01c2f643a53500ec549d034e;p=modules%2Fyacs.git diff --git a/src/engine/Any.cxx b/src/engine/Any.cxx index af845e2ce..cda84b110 100644 --- a/src/engine/Any.cxx +++ b/src/engine/Any.cxx @@ -1,29 +1,207 @@ +// 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 +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + #include "Any.hxx" #include "Runtime.hxx" #include "TypeCode.hxx" #include "InvalidExtractionException.hxx" +#include +#include +#include +#include +#include + +#include #include +#include using namespace YACS::ENGINE; using namespace std; -StringOnHeap::StringOnHeap(const char *val):_dealloc(0),_str(strdup(val)) +// forbidden value int=-269488145 double=-1.54947e+231 bool=239 +const char SeqAlloc::DFT_CHAR_VAR=-17;//0xEF + +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); +} + +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; idecrRef(); } +bool Any::IsNull(char *data) +{ + if(!data) + return true; + bool isNull(true); + 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()); @@ -111,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(); @@ -135,12 +331,17 @@ AtomAny *AtomAny::New(char *val,Deallocator dealloc) return new AtomAny(val,dealloc); } -AnyPtr AtomAny::operator[](int i) const throw(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(Exception) +AnyPtr AtomAny::operator[](const char *key) const { throw Exception("AtomAny::operator[] : try to get a part of a partitionned data whereas atomical."); } @@ -156,42 +357,59 @@ 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(Exception) +int AtomAny::getIntValue() const { if(_type->isA(Runtime::_tc_int)) return _value._i; else - throw Exception("Value is not an Int"); + throw Exception("Value is not an int"); } -bool AtomAny::getBoolValue() const throw(Exception) +bool AtomAny::getBoolValue() const { if(_type->isA(Runtime::_tc_bool)) return _value._b; else - throw Exception("Value is not a Bool"); + throw Exception("Value is not a bool"); } -double AtomAny::getDoubleValue() const throw(Exception) +double AtomAny::getDoubleValue() const { if(_type->isA(Runtime::_tc_double)) return _value._d; else - throw Exception("Value is not a Double"); + throw Exception("Value is not a double"); } -std::string AtomAny::getStringValue() const throw(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"); + 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"); } /*! @@ -203,7 +421,7 @@ std::string AtomAny::getStringValue() const throw(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()); @@ -223,10 +441,13 @@ void AtomAny::putMyReprAtPlace(char *data) const * For memory space minimal use, not all of '*this' is pushed at data location. * 'deepCpy' param is not used here because by definition of AtomAny deep copy is performed. * \param data : already allocated memory zone where to put compressed content of 'this' + * \param src : + * \param type : + * \param deepCpy : */ 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(); @@ -247,10 +468,13 @@ 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) { - void **tmp=(void **)data; - delete ((StringOnHeap *)(*tmp)); + if(!Any::IsNull(data)) + { + void **tmp=(void **)data; + delete ((StringOnHeap *)(*tmp)); + } } } @@ -269,7 +493,7 @@ bool AtomAny::takeInChargeStorageOf(TypeCode *type) AtomAny::~AtomAny() { - if(_type->isA(Runtime::_tc_string)) + if(_type->kind() == String || _type->kind()==Objref) delete _value._s; } @@ -283,33 +507,33 @@ ComposedAny::ComposedAny(TypeCode* type, bool isNew):Any(type) _type->decrRef(); } -AnyPtr ComposedAny::operator[](const char *key) const throw(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(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(Exception) +int ComposedAny::getIntValue() const { throw InvalidExtractionException(_type->kind(),Runtime::_tc_int->kind()); } -bool ComposedAny::getBoolValue() const throw(Exception) +bool ComposedAny::getBoolValue() const { throw InvalidExtractionException(_type->kind(),Runtime::_tc_bool->kind()); } -double ComposedAny::getDoubleValue() const throw(Exception) +double ComposedAny::getDoubleValue() const { throw InvalidExtractionException(_type->kind(),Runtime::_tc_double->kind()); } -std::string ComposedAny::getStringValue() const throw(Exception) +std::string ComposedAny::getStringValue() const { throw InvalidExtractionException(_type->kind(),Runtime::_tc_string->kind()); } @@ -359,7 +583,7 @@ void SeqAlloc::initCoarseMemory(char *mem, unsigned int size, Deallocator deallo memcpy(_start,mem,sizeInByte); else { - for(unsigned int i=0;i SeqAlloc::getSetItems() const +{ + std::vector ret; + unsigned int sz(size()); + for(unsigned int i=0;icontentType()); _alloc.construct(_alloc._start+i*_alloc._sizeOf1Elm,elem); } -AnyPtr SequenceAny::operator[](int i) const throw(Exception) +AnyPtr SequenceAny::operator[](int i) const { return _type->contentType()->getOrBuildAnyFromZippedData(_alloc._start+i*_alloc._sizeOf1Elm); } @@ -497,7 +737,7 @@ void SequenceAny::putReprAtPlace(char *data, const char *src, const TypeCode *ty void SequenceAny::destroyReprAtPlace(char *data, const TypeCode *type) { void **tmp=(void **) data; - if(*tmp) + if(!Any::IsNull(data)) ((SequenceAny *)(*tmp))->decrRef(); //((SequenceAny *)data)->~SequenceAny(); } @@ -514,25 +754,26 @@ Any *SequenceAny::clone() const return new SequenceAny(*this); } -SequenceAny *SequenceAny::New(const TypeCode *typeOfContent) +SequenceAny *SequenceAny::removeUnsetItemsFromThis() const { - if(typeOfContent->kind() == Objref) + std::vector its(getSetItems()); + std::size_t sz(its.size()); + SequenceAny *ret(SequenceAny::New(getType()->contentType(),sz)); + for(std::size_t i=0;isetEltAtRank(i,obj); } - else + return ret; +} + +SequenceAny *SequenceAny::New(const TypeCode *typeOfContent) +{ 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); } @@ -661,7 +902,7 @@ ArrayAny::ArrayAny(const TypeCode *typeOfContent, unsigned int lgth):ComposedAny { _data=new char[_type->getSizeInByteOfAnyReprInSeq()]; for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++) - _data[i]='\0'; + _data[i]=SeqAlloc::DFT_CHAR_VAR; } ArrayAny::ArrayAny(char *data, TypeCodeArray * type):ComposedAny(type,false),_data(0) @@ -731,7 +972,7 @@ ArrayAny::ArrayAny(const std::vector& val):ComposedAny(new TypeCode } } -void ArrayAny::setEltAtRank(int i, const Any *elem) throw(Exception) +void ArrayAny::setEltAtRank(int i, const Any *elem) { checkTypeOf(elem); const TypeCode *subType=_type->contentType(); @@ -750,7 +991,7 @@ bool ArrayAny::operator ==(const Any& other) const return true; } -AnyPtr ArrayAny::operator[](int i) const throw(Exception) +AnyPtr ArrayAny::operator[](int i) const { const TypeCode *subType=_type->contentType(); unsigned sizePerContent=subType->getSizeInByteOfAnyReprInSeq(); @@ -828,13 +1069,13 @@ bool StructAny::operator ==(const Any& other) const return true; } -AnyPtr StructAny::operator[](int i) const throw(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(Exception) +AnyPtr StructAny::operator[](const char *key) const { const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type; char *whereToGet=_data; @@ -852,13 +1093,13 @@ AnyPtr StructAny::operator[](const char *key) const throw(Exception) return (*iter).second->getOrBuildAnyFromZippedData(whereToGet); } -void StructAny::setEltAtRank(int i, const Any *elem) throw(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(Exception) +void StructAny::setEltAtRank(const char *key, const Any *elem) { const TypeCodeStruct *typeC=(const TypeCodeStruct *)_type; unsigned offset; @@ -929,7 +1170,7 @@ StructAny::StructAny(TypeCodeStruct *type):ComposedAny(type,false) { _data=new char[_type->getSizeInByteOfAnyReprInSeq()]; for(unsigned int i=0;i<_type->getSizeInByteOfAnyReprInSeq();i++) - _data[i]='\0'; + _data[i]=SeqAlloc::DFT_CHAR_VAR; } StructAny::StructAny(const StructAny& other):ComposedAny(other)