]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliSimpleValue.cxx
Record changes.
[u/mrichter/AliRoot.git] / STEER / AliSimpleValue.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17 $Log$
18 Revision 1.2  2006/03/07 07:52:34  hristov
19 New version (B.Yordanov)
20
21 Revision 1.2  2005/11/17 14:43:23  byordano
22 import to local CVS
23
24 Revision 1.1.1.1  2005/10/28 07:33:58  hristov
25 Initial import as subdirectory in AliRoot
26
27 Revision 1.1.1.1  2005/09/12 22:11:40  byordano
28 SHUTTLE package
29
30 Revision 1.2  2005/08/30 10:53:23  byordano
31 some more descriptions added
32
33 */
34
35 //
36 // This class is a simple wrapper of
37 // all primitive types used in PVSS SCADA system.
38 //
39
40
41 #include "AliSimpleValue.h"
42
43 #include "AliLog.h"
44 #include <TClass.h>
45
46 TObject* AliSimpleValue::BoolHolder::Clone(const char* /*name*/) const {
47         return new BoolHolder(fValue);
48 }
49
50 Bool_t AliSimpleValue::BoolHolder::IsEqual(const TObject* obj) const {
51         
52         if (this == obj) {
53                 return kTRUE;
54         }
55
56         if (BoolHolder::Class() != obj->IsA()) {
57                 return kFALSE;
58         }
59
60         return fValue == ((const BoolHolder*) obj)->fValue;
61 }
62
63 TObject* AliSimpleValue::ByteHolder::Clone(const char* /*name*/) const {
64         return new ByteHolder(fValue);
65 }
66
67 Bool_t AliSimpleValue::ByteHolder::IsEqual(const TObject* obj) const {
68
69         if (this == obj) {
70                 return kTRUE;
71         }
72
73         if (ByteHolder::Class() != obj->IsA()) {
74                 return kFALSE;
75         }
76
77         return fValue == ((const ByteHolder*) obj)->fValue;
78 }
79
80 TObject* AliSimpleValue::IntHolder::Clone(const char* /*name*/) const {
81         return new IntHolder(fValue);
82 }
83
84 Bool_t AliSimpleValue::IntHolder::IsEqual(const TObject* obj) const {
85
86         if (this == obj) {
87                 return kTRUE;
88         }
89
90         if (IntHolder::Class() != obj->IsA()) {
91                 return kFALSE;
92         }
93
94         return fValue == ((const IntHolder*) obj)->fValue;
95 }
96
97 TObject* AliSimpleValue::UIntHolder::Clone(const char* /*name*/) const {
98         return new UIntHolder(fValue);
99 }
100
101 Bool_t AliSimpleValue::UIntHolder::IsEqual(const TObject* obj) const {
102
103         if (this == obj) {
104                 return kTRUE;
105         }
106
107         if (UIntHolder::Class() != obj->IsA()) {
108                 return kFALSE;
109         }
110
111         return fValue == ((const UIntHolder*) obj)->fValue;
112 }
113
114 TObject* AliSimpleValue::FloatHolder::Clone(const char* /*name*/) const {
115         return new FloatHolder(fValue);
116 }
117
118 Bool_t AliSimpleValue::FloatHolder::IsEqual(const TObject* obj) const {
119
120         if (this == obj) {
121                 return kTRUE;
122         }
123
124         if (FloatHolder::Class() != obj->IsA()) {
125                 return kFALSE;
126         }
127
128         return fValue == ((const FloatHolder*) obj)->fValue;
129 }
130
131 TObject* AliSimpleValue::DynBoolHolder::Clone(const char* /*name*/) const {
132         return new DynBoolHolder(fSize, fValues);
133 }
134
135 Bool_t AliSimpleValue::DynBoolHolder::IsEqual(const TObject* obj) const {
136
137         if (this == obj) {
138                 return kTRUE;
139         }
140
141         if (DynBoolHolder::Class() != obj->IsA()) {
142                 return kFALSE;
143         }
144
145         const DynBoolHolder* other = ((const DynBoolHolder*) obj);
146         
147         if (fSize != other->fSize) {
148                 return kFALSE;
149         }
150
151         return !memcmp(fValues, other->fValues, fSize * sizeof(Bool_t));
152 }
153
154 TObject* AliSimpleValue::DynByteHolder::Clone(const char* /*name*/) const {
155         return new DynByteHolder(fSize, fValues);
156 }
157
158 Bool_t AliSimpleValue::DynByteHolder::IsEqual(const TObject* obj) const {
159
160         if (this == obj) {
161                 return kTRUE;
162         }
163
164         if (DynByteHolder::Class() != obj->IsA()) {
165                 return kFALSE;
166         }
167
168         const DynByteHolder* other = ((const DynByteHolder*) obj);
169
170         if (fSize != other->fSize) {
171                 return kFALSE;
172         }
173
174         return !memcmp(fValues, other->fValues, fSize * sizeof(Char_t));
175 }
176
177 TObject* AliSimpleValue::DynIntHolder::Clone(const char* /*name*/) const {
178         return new DynIntHolder(fSize, fValues);
179 }
180
181 Bool_t AliSimpleValue::DynIntHolder::IsEqual(const TObject* obj) const {
182
183         if (this == obj) {
184                 return kTRUE;
185         }
186
187         if (DynIntHolder::Class() != obj->IsA()) {
188                 return kFALSE;
189         }
190
191         const DynIntHolder* other = ((const DynIntHolder*) obj);
192
193         if (fSize != other->fSize) {
194                 return kFALSE;
195         }
196
197         return !memcmp(fValues, other->fValues, fSize * sizeof(Int_t));
198 }
199
200 TObject* AliSimpleValue::DynUIntHolder::Clone(const char* /*name*/) const {
201         return new DynUIntHolder(fSize, fValues);
202 }
203
204 Bool_t AliSimpleValue::DynUIntHolder::IsEqual(const TObject* obj) const {
205
206         if (this == obj) {
207                 return kTRUE;
208         }
209
210         if (DynUIntHolder::Class() != obj->IsA()) {
211                 return kFALSE;
212         }
213
214         const DynUIntHolder* other = ((const DynUIntHolder*) obj);
215
216         if (fSize != other->fSize) {
217                 return kFALSE;
218         }
219
220         return !memcmp(fValues, other->fValues, fSize * sizeof(UInt_t));
221 }
222
223 TObject* AliSimpleValue::DynFloatHolder::Clone(const char* /*name*/) const {
224         return new DynFloatHolder(fSize, fValues);
225 }
226
227 Bool_t AliSimpleValue::DynFloatHolder::IsEqual(const TObject* obj) const {
228
229         if (this == obj) {
230                 return kTRUE;
231         }
232
233         if (DynFloatHolder::Class() != obj->IsA()) {
234                 return kFALSE;
235         }
236
237         const DynFloatHolder* other = ((const DynFloatHolder*) obj);
238
239         if (fSize != other->fSize) {
240                 return kFALSE;
241         }
242
243         return !memcmp(fValues, other->fValues, fSize * sizeof(Float_t));
244 }
245
246 ClassImp(AliSimpleValue)
247
248 AliSimpleValue::AliSimpleValue():
249         fHolder(NULL), fType(kInvalid)
250 {
251
252 }
253
254 AliSimpleValue::AliSimpleValue(const AliSimpleValue& other):
255         TObject(other), fHolder(NULL), fType(other.fType)
256 {
257         if (other.fHolder) {
258                 fHolder = other.fHolder->Clone();
259         }
260 }
261
262 AliSimpleValue::AliSimpleValue(AliSimpleValue::Type type, Int_t size):
263         fHolder(NULL), fType(type)
264 {
265         
266         switch (type) {
267                 case kBool:
268                         fHolder = new BoolHolder();
269                         break;
270                 case kByte:
271                         fHolder = new ByteHolder();
272                         break;
273                 case kInt:
274                         fHolder = new IntHolder();
275                         break;
276                 case kUInt:
277                         fHolder = new UIntHolder();
278                         break;
279                 case kFloat:
280                         fHolder = new FloatHolder();
281                         break;
282                 case kDynBool:
283                         fHolder = new DynBoolHolder(size);
284                         break;
285                 case kDynByte:
286                         fHolder = new DynByteHolder(size);
287                         break;
288                 case kDynInt:
289                         fHolder = new DynIntHolder(size);
290                         break;
291                 case kDynUInt:
292                         fHolder = new DynUIntHolder(size);
293                         break;
294                 case kDynFloat:
295                         fHolder = new DynFloatHolder(size);
296                         break;
297                 default:        
298                         break;
299         }
300 }
301
302 AliSimpleValue::AliSimpleValue(Bool_t val) {
303
304         fType = kBool;
305         fHolder = new BoolHolder(val);
306 }
307
308 AliSimpleValue::AliSimpleValue(Char_t val) {
309         
310         fType = kByte;
311         fHolder = new ByteHolder(val);
312 }
313
314 AliSimpleValue::AliSimpleValue(Int_t val) {
315         
316         fType = kInt;
317         fHolder = new IntHolder(val);
318 }
319
320 AliSimpleValue::AliSimpleValue(UInt_t val) {
321         
322         fType = kUInt;
323         fHolder = new UIntHolder(val);
324 }
325
326 AliSimpleValue::AliSimpleValue(Float_t val) {
327
328         fType = kFloat;
329         fHolder = new FloatHolder(val);
330 }
331
332 AliSimpleValue::AliSimpleValue(Int_t size, const Bool_t* buf) {
333         
334         fType = kDynBool;
335         fHolder = new DynBoolHolder(size, buf);
336 }
337
338 AliSimpleValue::AliSimpleValue(Int_t size, const Char_t* buf) {
339
340         fType = kDynByte;
341         fHolder = new DynByteHolder(size, buf);
342 }
343
344 AliSimpleValue::AliSimpleValue(Int_t size, const Int_t* buf) {
345
346         fType = kDynInt;
347         fHolder = new DynIntHolder(size, buf);
348 }
349
350 AliSimpleValue::AliSimpleValue(Int_t size, const UInt_t* buf) {
351
352         fType = kDynUInt;
353         fHolder = new DynUIntHolder(size, buf);
354 }
355
356 AliSimpleValue::AliSimpleValue(Int_t size, const Float_t* buf) {
357
358         fType = kDynFloat;
359         fHolder = new DynFloatHolder(size, buf);
360 }
361
362 AliSimpleValue::~AliSimpleValue() {
363         
364         if (fHolder) {
365                 delete fHolder;
366         }
367 }
368
369 AliSimpleValue& AliSimpleValue::operator=(const AliSimpleValue& other) {
370         
371         if (fHolder) {
372                 delete fHolder;
373         }
374
375         fType = other.fType;
376         
377         if (other.fHolder) {
378                 fHolder = other.fHolder->Clone();
379         } else {
380                 fHolder = NULL;
381         }
382
383         return *this;
384 }
385
386 Bool_t AliSimpleValue::operator==(const AliSimpleValue& other) const {
387
388         if (fType != other.fType) {
389                 return kFALSE;
390         }
391
392         if (!(fHolder && other.fHolder)) {
393                 return kFALSE;
394         }
395
396         return fHolder->IsEqual(other.fHolder);
397 }
398
399 void AliSimpleValue::SetBool(Bool_t val) {
400
401         if (!TypeOk(kBool)) {
402                 return;
403         }
404
405         ((BoolHolder*) fHolder)->fValue = val;
406 }
407
408 void AliSimpleValue::SetByte(Char_t val) {
409
410         if (!TypeOk(kByte)) {
411                 return;
412         }
413
414         ((ByteHolder*) fHolder)->fValue = val;
415 }
416
417 void AliSimpleValue::SetInt(Int_t val) {
418
419         if (!TypeOk(kInt)) {
420                 return;
421         }
422
423         ((IntHolder*) fHolder)->fValue = val;
424 }
425
426 void AliSimpleValue::SetUInt(UInt_t val) {
427
428         if (!TypeOk(kUInt)) {
429                 return;
430         }
431         
432         ((UIntHolder*) fHolder)->fValue = val;
433 }
434
435 void AliSimpleValue::SetFloat(Float_t val) {
436
437         if (!TypeOk(kFloat)) {
438                 return;
439         }
440
441         ((FloatHolder*) fHolder)->fValue = val;
442 }
443
444 Bool_t AliSimpleValue::GetBool() const {
445
446         if (!TypeOk(kBool)) {
447                 return kFALSE;
448         }       
449
450         return ((BoolHolder*) fHolder)->fValue;
451 }
452
453 Char_t AliSimpleValue::GetByte() const {
454
455         if (!TypeOk(kByte)) {
456                 return 0;
457         }
458         
459         return ((ByteHolder*) fHolder)->fValue;
460 }
461
462 Int_t AliSimpleValue::GetInt() const {
463
464         if (!TypeOk(kInt)) {
465                 return 0;
466         }
467         return ((IntHolder*) fHolder)->fValue;
468 }
469
470 UInt_t AliSimpleValue::GetUInt() const {
471
472         if (!TypeOk(kUInt)) {
473                 return 0;
474         }
475
476         return ((UIntHolder*) fHolder)->fValue;
477 }
478
479 Float_t AliSimpleValue::GetFloat() const {
480
481         if (!TypeOk(kFloat)) {
482                 return 0;
483         }
484
485         return ((FloatHolder*) fHolder)->fValue;
486 }
487
488 void AliSimpleValue::SetDynBool(Int_t n, Bool_t val) {
489         
490         if (!TypeOk(kDynBool)) {
491                 return;
492         }
493
494         if (!BoundsOk(n)) {
495                 return;
496         }
497
498         ((DynBoolHolder*) fHolder)->fValues[n] = val;
499 }
500
501 void AliSimpleValue::SetDynByte(Int_t n, Char_t val) {
502
503         if (!TypeOk(kDynByte)) {
504                 return;
505         }
506
507         if (!BoundsOk(n)) {
508                 return;
509         }
510
511         ((DynByteHolder*) fHolder)->fValues[n] = val;
512 }
513
514 void AliSimpleValue::SetDynInt(Int_t n, Int_t val) {
515
516         if (!TypeOk(kDynInt)) {
517                 return;
518         }
519
520         if (!BoundsOk(n)) {
521                 return;
522         }
523
524         ((DynIntHolder*) fHolder)->fValues[n] = val;
525 }
526
527 void AliSimpleValue::SetDynUInt(Int_t n, UInt_t val) {
528
529         if (!TypeOk(kDynUInt)) {
530                 return;
531         }
532
533         if (!BoundsOk(n)) {
534                 return;
535         }
536
537         ((DynUIntHolder*) fHolder)->fValues[n] = val;
538 }
539
540 void AliSimpleValue::SetDynFloat(Int_t n, Float_t val) {
541
542         if (!TypeOk(kDynFloat)) {
543                 return;
544         }
545
546         if (!BoundsOk(n)) {
547                 return;
548         }
549
550         ((DynFloatHolder*) fHolder)->fValues[n] = val;
551 }
552
553 Bool_t AliSimpleValue::GetDynBool(Int_t n) const {
554
555         if (!TypeOk(kDynBool)) {
556                 return kFALSE;
557         }
558
559         if (!BoundsOk(n)) {
560                 return kFALSE;
561         }
562
563         return ((DynBoolHolder*) fHolder)->fValues[n];
564 }
565
566 Char_t AliSimpleValue::GetDynByte(Int_t n) const {
567
568         if (!TypeOk(kDynByte)) {
569                 return 0;
570         }
571
572         if (!BoundsOk(n)) {
573                 return 0;
574         }
575
576         return ((DynByteHolder*) fHolder)->fValues[n];
577 }
578
579 Int_t AliSimpleValue::GetDynInt(Int_t n) const {
580
581         if (!TypeOk(kDynInt)) {
582                 return 0;
583         }
584
585         if (!BoundsOk(n)) {
586                 return 0;
587         }
588
589         return ((DynIntHolder*) fHolder)->fValues[n];
590 }
591
592 UInt_t AliSimpleValue::GetDynUInt(Int_t n) const {
593
594         if (!TypeOk(kDynUInt)) {
595                 return 0;
596         }
597
598         if (!BoundsOk(n)) {
599                 return 0;
600         }
601
602         return ((DynUIntHolder*) fHolder)->fValues[n];
603 }
604
605 Float_t AliSimpleValue::GetDynFloat(Int_t n) const {
606
607         if (!TypeOk(kDynFloat)) {
608                 return 0;
609         }
610
611         if (!BoundsOk(n)) {
612                 return 0;
613         }
614
615         return ((DynFloatHolder*) fHolder)->fValues[n];
616 }
617
618 Bool_t AliSimpleValue::TypeOk(AliSimpleValue::Type type) const {
619
620         if (fType != type) {
621                 AliError(Form("SimpleValue type is not %s!", 
622                         GetTypeString(type)));
623                 return kFALSE;
624         }
625
626         return kTRUE;
627 }
628
629 Bool_t AliSimpleValue::BoundsOk(Int_t n) const {
630
631         switch (fType) {
632                 case kDynBool:
633                 case kDynByte:
634                 case kDynInt:
635                 case kDynUInt:
636                 case kDynFloat: {
637                         Int_t size = ((DynHolder*) fHolder)->fSize;
638                         if (n < 0 || n >= size) {
639                                 AliError(Form("Index %d out of bounds!", n));
640                                 return kFALSE;
641                         }
642                         return kTRUE;
643                 }
644                 case kBool:
645                 case kByte:
646                 case kInt:
647                 case kUInt:
648                 case kFloat:
649                         AliError(Form("SimpleValue type %s is not dynamic!",
650                                 GetTypeString(fType)));
651                         return kFALSE;
652                 default:
653                         AliError("Invalid or unknown type!");
654                         return kFALSE;
655         }
656 }
657
658 Int_t AliSimpleValue::GetDynamicSize() const {
659         //
660         // returns the size of dynamic type or 0 in case of 
661         // none dynamic type.
662         //
663
664         if (!fHolder) {
665                 return 0;
666         }
667
668         if (!fHolder->IsA()->InheritsFrom(DynHolder::Class())) {
669                 return 0;
670         }
671
672         return ((DynHolder*) fHolder)->fSize;
673 }
674
675 TString AliSimpleValue::ToString() const {
676         
677         TString result;
678         
679         result += "Type: ";
680         result += GetTypeString(fType);
681         
682         result += ", Value: ";
683         switch (fType) {
684                 case kBool:
685                         result += GetBool();
686                         break;
687                 case kByte:
688                         result += (Int_t) GetByte();
689                         break;
690                 case kInt:
691                         result += GetInt();
692                         break;
693                 case kUInt:
694                         result += GetUInt();
695                         break;
696                 case kFloat:
697                         result += GetFloat();
698                         break;
699                 case kDynBool: {
700                                 result += "[";
701                                 Int_t size = GetDynamicSize();
702                                 for (Int_t k = 0; k < size; k ++) {
703                                         result += GetDynBool(k);
704                                         if (k + 1 < size) {
705                                                 result += ", ";
706                                         }
707                                 }
708                                 result += "]";
709                         }
710                         break;
711                 case kDynByte: {
712                                 result += "[";
713                                 Int_t size = GetDynamicSize();
714                                 for (Int_t k = 0; k < size; k ++) {
715                                         result += GetDynByte(k);
716                                         if (k + 1 < size) {
717                                                 result += ", ";
718                                         }
719                                 }
720                                 result += "]";
721                         }
722                         break;
723                 case kDynInt: {
724                                 result += "[";
725                                 Int_t size = GetDynamicSize();
726                                 for (Int_t k = 0; k < size; k ++) {
727                                         result += GetDynInt(k);
728                                         if (k + 1 < size) {
729                                                 result += ", ";
730                                         }
731                                 }
732                                 result += "]";
733                         }
734                         break;
735                 case kDynUInt: {
736                                 result += "[";
737                                 Int_t size = GetDynamicSize();
738                                 for (Int_t k = 0; k < size; k ++) {
739                                         result += GetDynUInt(k);
740                                         if (k + 1 < size) {
741                                                 result += ", ";
742                                         }
743                                 }
744                                 result += "]";
745                         }
746                         break;
747                 case kDynFloat: {
748                                 result += "[";
749                                 Int_t size = GetDynamicSize();
750                                 for (Int_t k = 0; k < size; k ++) {
751                                         result += GetDynFloat(k);
752                                         if (k + 1 < size) {
753                                                 result += ", ";
754                                         }
755                                 }
756                                 result += "]";
757                         }
758                         break;
759                 default:
760                         result += "Unknown";            
761         }       
762
763         return result;
764 }
765
766 Bool_t AliSimpleValue::IsDynamic(AliSimpleValue::Type type) {
767
768          switch (type) {
769                 case kDynBool:
770                 case kDynByte:
771                 case kDynInt:
772                 case kDynUInt:
773                 case kDynFloat:
774                         return kTRUE;
775                 default:
776                         return kFALSE; 
777         }
778 }
779
780 Int_t AliSimpleValue::GetSize() const {
781         //
782         // return the number of bytes used by this value.
783         // In case of dynamic type it returns dynamic size multiplied
784         // by the size of corresponding primitive type.
785         //
786         
787         return IsDynamic(fType) ? 
788                 GetDynamicSize() * AliSimpleValue::GetPrimitiveSize(fType): 
789                 AliSimpleValue::GetPrimitiveSize(fType);
790
791
792 Int_t AliSimpleValue::GetPrimitiveSize(AliSimpleValue::Type type) {
793         //
794         // returns the number of bytes used by particular primitive type
795         // or by the corresponding primitive type in case of dynamic type.
796         //
797
798
799         switch (type) {
800
801                 case kBool: 
802                 case kDynBool: return sizeof(Bool_t);
803                 case kByte: 
804                 case kDynByte: return sizeof(Char_t);
805                 case kInt: 
806                 case kDynInt: return sizeof(Int_t);
807                 case kUInt: 
808                 case kDynUInt: return sizeof(UInt_t);
809                 case kFloat: 
810                 case kDynFloat: return sizeof(Float_t);
811                 default:
812                         return 0;
813         }
814
815
816 const char* AliSimpleValue::GetTypeString(AliSimpleValue::Type type) {
817
818         switch (type) {
819                 case kBool: return "Bool";
820                 case kByte: return "Byte";
821                 case kInt: return "Int";
822                 case kUInt: return "UInt";
823                 case kFloat: return "Float";
824                 case kDynBool: return "DynBool";
825                 case kDynByte: return "DynByte";
826                 case kDynInt: return "DynInt";
827                 case kDynUInt: return "DynUInt";
828                 case kDynFloat: return "DynFloat";
829                 default:
830                         return "Unknown";
831         }
832 }