Value.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_VALUE_H_
00014 #define _SUPPORT_VALUE_H_
00015 
00021 #include <support/SupportDefs.h>
00022 #include <support/TypeFuncs.h>
00023 #include <support/StaticValue.h>
00024 #include <support/IByteStream.h>
00025 #include <support/ITextStream.h>
00026 #include <support/IBinder.h>
00027 #include <support/ByteOrder.h>
00028 #include <support/TypeConstants.h>
00029 #include <support/KeyID.h>
00030 
00031 #include <string.h>
00032 #include <stdint.h>
00033 
00034 BNS(namespace palmos {)
00035 BNS(namespace support {)
00036 
00037 
00041 class SParcel;
00042 class SSharedBuffer;
00043 class SString;
00044 class BValueMap;
00045 class SValue;
00046 
00048 enum {
00050     B_NO_VALUE_FLATTEN          = 0x00000001,
00052     B_NO_VALUE_RECURSION        = 0x00000002
00053 };
00054 
00056 enum {
00058     B_EDIT_VALUE_DATA       = 0x00000001
00059 };
00060 
00062 enum {
00063     B_PRINT_VALUE_TYPES         = 0x00010000,   
00064     B_PRINT_CATALOG_CONTENTS    = 0x00020000,   
00065     B_PRINT_BINDER_INTERFACES   = 0x00040000,   
00066     B_PRINT_BINDER_CONTENTS     = 0x00080000    
00067 };
00068 
00069 /*----- Some useful constants -----------------------------*/
00070 
00071 const static_small_value B_UNDEFINED_VALUE  = { B_UNDEFINED_TYPE, "" };
00072 const static_small_value B_WILD_VALUE       = { B_PACK_SMALL_TYPE(B_WILD_TYPE, 0), "" };
00073 const static_small_value B_NULL_VALUE       = { B_PACK_SMALL_TYPE(B_NULL_TYPE, 0), "" };
00074 
00075 const static_bool_value  B_FALSE_BOOL       = { B_PACK_SMALL_TYPE(B_BOOL_TYPE, sizeof(int8_t)), false };
00076 const static_bool_value B_TRUE_BOOL         = { B_PACK_SMALL_TYPE(B_BOOL_TYPE, sizeof(int8_t)), true };
00077 
00078 B_CONST_INT32_VALUE(B_0_INT32, 0, );
00079 B_CONST_INT32_VALUE(B_1_INT32, 1, );
00080 B_CONST_INT32_VALUE(B_2_INT32, 2, );
00081 B_CONST_INT32_VALUE(B_3_INT32, 3, );
00082 B_CONST_INT32_VALUE(B_4_INT32, 4, );
00083 B_CONST_INT32_VALUE(B_5_INT32, 5, );
00084 B_CONST_INT32_VALUE(B_6_INT32, 6, );
00085 B_CONST_INT32_VALUE(B_7_INT32, 7, );
00086 B_CONST_INT32_VALUE(B_8_INT32, 8, );
00087 B_CONST_INT32_VALUE(B_9_INT32, 9, );
00088 B_CONST_INT32_VALUE(B_10_INT32, 10, );
00089 
00090 B_CONST_FLOAT_VALUE(B_0_0_FLOAT, 0.0f, );
00091 B_CONST_FLOAT_VALUE(B_0_1_FLOAT, 0.1f, );
00092 B_CONST_FLOAT_VALUE(B_0_2_FLOAT, 0.2f, );
00093 B_CONST_FLOAT_VALUE(B_0_25_FLOAT,0.25f, );
00094 B_CONST_FLOAT_VALUE(B_0_3_FLOAT, 0.3f, );
00095 B_CONST_FLOAT_VALUE(B_0_4_FLOAT, 0.4f, );
00096 B_CONST_FLOAT_VALUE(B_0_5_FLOAT, 0.5f, );
00097 B_CONST_FLOAT_VALUE(B_0_6_FLOAT, 0.6f, );
00098 B_CONST_FLOAT_VALUE(B_0_7_FLOAT, 0.7f, );
00099 B_CONST_FLOAT_VALUE(B_0_75_FLOAT,0.75f, );
00100 B_CONST_FLOAT_VALUE(B_0_8_FLOAT, 0.8f, );
00101 B_CONST_FLOAT_VALUE(B_0_9_FLOAT, 0.9f, );
00102 B_CONST_FLOAT_VALUE(B_1_0_FLOAT, 1.0f, );
00103 
00104 B_CONST_STRING_VALUE_LARGE(B_VALUE_VALUE, "value", );
00105 B_CONST_STRING_VALUE_SMALL(B_EMPTY_STRING, "", );
00106 
00107 // Those are here only for compatibility. Use the B_ constants instead.
00108 #define B_0_INT32       B_0_INT32
00109 #define B_1_INT32       B_1_INT32
00110 #define B_2_INT32       B_2_INT32
00111 #define B_3_INT32       B_3_INT32
00112 #define B_4_INT32       B_4_INT32
00113 #define B_5_INT32       B_5_INT32
00114 #define B_6_INT32       B_6_INT32
00115 #define B_7_INT32       B_7_INT32
00116 #define B_8_INT32       B_8_INT32
00117 #define B_9_INT32       B_9_INT32
00118 #define B_10_INT32      B_10_INT32
00119 #define B_0_0_FLOAT     B_0_0_FLOAT
00120 #define B_0_1_FLOAT     B_0_1_FLOAT
00121 #define B_0_2_FLOAT     B_0_2_FLOAT
00122 #define B_0_25_FLOAT    B_0_25_FLOAT
00123 #define B_0_3_FLOAT     B_0_3_FLOAT
00124 #define B_0_4_FLOAT     B_0_4_FLOAT
00125 #define B_0_5_FLOAT     B_0_5_FLOAT
00126 #define B_0_6_FLOAT     B_0_6_FLOAT
00127 #define B_0_7_FLOAT     B_0_7_FLOAT
00128 #define B_0_75_FLOAT    B_0_75_FLOAT
00129 #define B_0_8_FLOAT     B_0_8_FLOAT
00130 #define B_0_9_FLOAT     B_0_9_FLOAT
00131 #define B_1_0_FLOAT     B_1_0_FLOAT
00132 
00133 /*----- Create simple SValue -----------------------------*/
00134 
00136 
00144 template<class T>
00145 class SSimpleValue : public static_small_value
00146 {
00147 public:
00148     inline SSimpleValue(T v) {
00149         type = get_type(v);
00150         *reinterpret_cast<T*>(data) = v; 
00151     }
00152     inline SSimpleValue(int32_t tc, T v) {
00153         type = B_PACK_SMALL_TYPE(tc, sizeof(v));
00154         *reinterpret_cast<T*>(data) = v; 
00155     }
00156 private:
00157     int32_t get_type(int8_t)    { return B_PACK_SMALL_TYPE(B_INT8_TYPE,     sizeof(int8_t)); }
00158     int32_t get_type(int16_t)   { return B_PACK_SMALL_TYPE(B_INT16_TYPE,    sizeof(int16_t)); }
00159     int32_t get_type(int32_t)   { return B_PACK_SMALL_TYPE(B_INT32_TYPE,    sizeof(int32_t)); }
00160     int32_t get_type(uint8_t)   { return B_PACK_SMALL_TYPE(B_INT8_TYPE,     sizeof(int8_t)); }
00161     int32_t get_type(uint16_t)  { return B_PACK_SMALL_TYPE(B_INT16_TYPE,    sizeof(int16_t)); }
00162     int32_t get_type(uint32_t)  { return B_PACK_SMALL_TYPE(B_INT32_TYPE,    sizeof(int32_t)); }
00163     int32_t get_type(bool)      { return B_PACK_SMALL_TYPE(B_BOOL_TYPE,     sizeof(int8_t)); }
00164     int32_t get_type(float)     { return B_PACK_SMALL_TYPE(B_FLOAT_TYPE,    sizeof(float)); }
00165 };
00166 
00168 
00171 class SSimpleStatusValue : public static_small_value
00172 {
00173 public:
00174     inline SSimpleStatusValue(status_t v) {
00175         type = B_PACK_SMALL_TYPE(B_STATUS_TYPE, sizeof(status_t));
00176         *reinterpret_cast<status_t*>(data) = v; 
00177     }
00178 };
00179 
00180 // --------------------------------------------------------------------
00181 
00183 
00205 class SValue
00206 {
00207 public:
00210             inline          SValue();
00211             inline          SValue(const SValue& o);
00212                             
00214             inline          ~SValue();
00216 
00221             inline          SValue(const SValue& key, const SValue& value);
00222             inline          SValue(const SValue& key, const SValue& value, uint32_t flags);
00223             inline          SValue(const SValue& key, const SValue& value, uint32_t flags, size_t numMappings);
00225 
00231             inline          SValue(type_code type, const void* data, size_t len);
00232                             SValue(type_code type, const SSharedBuffer* buffer);
00233             inline explicit SValue(int32_t num);
00234             explicit        SValue(const char* str);
00235             explicit        SValue(const SString& str);
00236             inline explicit SValue(const static_small_string_value& str);
00237             inline explicit SValue(const static_large_string_value& str);
00238                             SValue(const sptr<IBinder>& binder);
00239                             SValue(const wptr<IBinder>& binder);
00240                             SValue(const sptr<SKeyID>& binder);
00242                             
00243     
00247     
00249     static inline const SValue& SValue::Undefined()     { return B_UNDEFINED_VALUE; }
00251     static inline const SValue& SValue::Wild()          { return B_WILD_VALUE; }
00253     static inline const SValue& SValue::Null()          { return B_NULL_VALUE; }
00254 
00256 
00262 
00263     /*
00264     // Unfortunately, this doesn't work because it can cause more than 2 userdef conversions which
00265     // is an error.
00266     static  inline SSimpleValue <int8_t>    Int8(int8_t v)      { return SSimpleValue<int8_t>(v); }
00267     static  inline SSimpleValue <int16_t>   Int16(int16_t v)    { return SSimpleValue<int16_t>(v); }
00268     static  inline SSimpleValue <int32_t>   Int32(int32_t v)    { return SSimpleValue<int32_t>(v); }
00269     static  inline SSimpleValue <bool>      Bool(bool v)        { return SSimpleValue<bool>(v); }
00270     static  inline SSimpleValue <float>     Float(float v)      { return SSimpleValue<float>(v); }
00271     */
00272 
00273     static   SValue         Int8(int8_t v);
00274     static   SValue         Int16(int16_t v);
00275     static  inline SValue   Int32(int32_t v)    { return SValue(v); }
00276     static   SValue         Bool(bool v);
00277     static   SValue         Float(float v);
00278 
00279     static  inline SValue   Binder(const sptr<IBinder>& value)      { return SValue(value); }
00280     static  inline SValue   WeakBinder(const wptr<IBinder>& value)  { return SValue(value); }
00281     static  inline SValue   String(const char *value)               { return SValue(value); }
00282     static  inline SValue   String(const SString& value)            { return SValue(value); }
00283     static  SValue          Int64(int64_t value);
00284     static  SValue          Time(nsecs_t value);
00285     static  SValue          Double(double value);
00286     static  SValue          Atom(const sptr<SAtom>& value);
00287     static  SValue          WeakAtom(const wptr<SAtom>& value);
00288     static  SValue          Status(status_t error);
00289     static  inline SValue   KeyID(const sptr<SKeyID>& value)        { return SValue(value); }
00290     
00291     static inline SValue    SSize(ssize_t value)                    { return SValue(int32_t(value)); }
00292     static inline SValue    Offset(off_t value)                     { return Int64(value); }
00293 
00295 
00299 
00301 
00308             status_t        StatusCheck() const;
00309 
00311 
00314             status_t        ErrorCheck() const;
00316 
00321             void            SetError(status_t error);
00322             
00323             SValue&         Assign(const SValue& o);
00324             SValue&         Assign(type_code type, const void* data, size_t len);
00325     inline  SValue&         operator=(const SValue& o)          { return Assign(o); }
00326 
00328     static  void            MoveBefore(SValue* to, SValue* from, size_t count = 1);
00330     static  void            MoveAfter(SValue* to, SValue* from, size_t count = 1);
00331 
00333             void            Swap(SValue& with);
00334             
00336             void            Undefine();
00338     inline  bool            IsDefined() const;
00339 
00341     inline  bool            IsWild() const;
00342             
00344     inline  bool            IsSpecified() const;
00345             
00347     inline  bool            IsSimple() const;
00348             
00350             bool            IsObject() const;
00351             
00352             status_t        CanByteSwap() const;
00353             status_t        ByteSwap(swap_action action);
00354             
00356 
00363     
00365             type_code       Type() const;
00367             const void*     Data() const;
00369             size_t          Length() const;
00371             const SSharedBuffer*    SharedBuffer() const;
00372             
00374             void            Pool();
00375 
00377             const void*     Data(type_code type, size_t length) const;
00378 
00380 
00389             const void*     Data(type_code type, size_t* inoutMinLength) const;
00390 
00392 
00395             status_t        SetType(type_code newType);
00396 
00398 
00410             void*           BeginEditBytes(type_code type, size_t length, uint32_t flags=0);
00412             /*  @param[in] final_length If >= zero, the value's data will be
00413                     resized to this amount, otherwise it is left unchanged.
00414             */
00415             status_t        EndEditBytes(ssize_t final_length=-1);
00416     
00418 
00422     
00424 
00441             SValue&         Join(const SValue& from, uint32_t flags = 0);
00443             const SValue    JoinCopy(const SValue& from, uint32_t flags = 0) const;
00444             
00446 
00448             SValue&         JoinItem(const SValue& key, const SValue& value, uint32_t flags=0);
00449             
00451 
00467             SValue&         Overlay(const SValue& from, uint32_t flags = 0);
00469             const SValue    OverlayCopy(const SValue& from, uint32_t flags = 0) const;
00470             
00472 
00487             SValue&         Inherit(const SValue& from, uint32_t flags = 0);
00489             const SValue    InheritCopy(const SValue& from, uint32_t flags = 0) const;
00490             
00492 
00507             SValue&         MapValues(const SValue& from, uint32_t flags = 0);
00509             const SValue    MapValuesCopy(const SValue& from, uint32_t flags = 0) const;
00510     
00512 
00517             SValue&         Remove(const SValue& from, uint32_t flags = 0);
00519             const SValue    RemoveCopy(const SValue& from, uint32_t flags = 0) const;
00521             status_t        RemoveItem( const SValue& key,
00522                                         const SValue& value = B_WILD_VALUE);
00523             
00525 
00529             SValue&         Retain(const SValue& from, uint32_t flags = 0);
00531             const SValue    RetainCopy(const SValue& from, uint32_t flags = 0) const;
00532             
00534 
00538             status_t        RenameItem( const SValue& old_key,
00539                                         const SValue& new_key);
00540             
00542             bool            HasItem(const SValue& key,
00543                                     const SValue& value = B_UNDEFINED_VALUE) const;
00544     
00546 
00549             void            GetKeyIndices(const SValue* keys, size_t* outIndices, size_t count);
00550 
00552 
00555             const SValue&   ReplaceValues(const SValue* values, const size_t* indices, size_t count);
00556             
00558 
00572             const SValue    ValueFor(const SValue& key, uint32_t flags) const;
00573 
00575             const SValue&           ValueFor(const SValue& key) const;
00577 
00578             const SValue&           ValueFor(const char* str) const;
00580 
00581             const SValue&           ValueFor(const SString& str) const;
00583 
00584             const SValue&           ValueFor(int32_t index) const;
00586             inline const SValue&    ValueFor(const static_small_string_value& str) const { return ValueFor(reinterpret_cast<const SValue&>(str)); }
00588             inline const SValue&    ValueFor(const static_large_string_value& str) const { return ValueFor(reinterpret_cast<const SValue&>(str)); }
00589 
00590     
00592             int32_t         CountItems() const;
00594 
00601             status_t        GetNextItem(void** cookie, SValue* out_key, SValue* out_value) const;
00602             
00604             SValue          Keys() const;
00605             
00607 
00615             SValue*         BeginEditItem(const SValue &key);
00617             /*  @param[in] item The item returned by BeginEditItem().
00618             */
00619             void            EndEditItem(SValue* item);
00620             
00622     inline  const SValue&   operator[](const SValue& key) const                     { return ValueFor(key); }
00624     inline  const SValue&   operator[](const char* key) const                       { return ValueFor(key); }
00626     inline  const SValue&   operator[](const SString& key) const                    { return ValueFor(key); }
00628     inline  const SValue&   operator[](int32_t index) const                         { return ValueFor(index); }
00630     inline  const SValue&   operator[](const static_small_string_value& key) const  { return ValueFor(key); }
00632     inline  const SValue&   operator[](const static_large_string_value& key) const  { return ValueFor(key); }
00633     
00635     inline  const SValue    operator+(const SValue& o) const                        { return JoinCopy(o); }
00637     inline  const SValue    operator-(const SValue& o) const                        { return RemoveCopy(o); }
00639     inline  const SValue    operator*(const SValue& o) const                        { return MapValuesCopy(o); }
00640 
00642     inline  SValue&         operator+=(const SValue& o)                             { return Join(o); }
00644     inline  SValue&         operator-=(const SValue& o)                             { return Remove(o); }
00646     inline  SValue&         operator*=(const SValue& o)                             { return MapValues(o); }
00647     
00649 
00653     
00654             ssize_t         ArchivedSize() const;
00655             ssize_t         Archive(SParcel& into) const;
00656             ssize_t         Archive(const sptr<IByteOutput>& into) const;
00657             ssize_t         Unarchive(SParcel& from);
00658             ssize_t         Unarchive(const sptr<IByteInput>& from);
00659             
00661 
00669     
00670             int32_t         Compare(const SValue& o) const;
00671             int32_t         Compare(const char* str) const;
00672             int32_t         Compare(const SString& str) const;
00673             int32_t         Compare(type_code type, const void* data, size_t length) const;
00674             
00675     inline  bool            operator==(const SValue& o) const                       { return Compare(o) == 0; }
00676     inline  bool            operator!=(const SValue& o) const                       { return Compare(o) != 0; }
00677     inline  bool            operator<(const SValue& o) const                        { return Compare(o) < 0; }
00678     inline  bool            operator<=(const SValue& o) const                       { return Compare(o) <= 0; }
00679     inline  bool            operator>=(const SValue& o) const                       { return Compare(o) >= 0; }
00680     inline  bool            operator>(const SValue& o) const                        { return Compare(o) > 0; }
00681     
00682     inline  bool            operator==(const char* str) const                       { return Compare(str) == 0; }
00683     inline  bool            operator!=(const char* str) const                       { return Compare(str) != 0; }
00684     inline  bool            operator<(const char* str) const                        { return Compare(str) < 0; }
00685     inline  bool            operator<=(const char* str) const                       { return Compare(str) <= 0; }
00686     inline  bool            operator>=(const char* str) const                       { return Compare(str) >= 0; }
00687     inline  bool            operator>(const char* str) const                        { return Compare(str) > 0; }
00688     
00689     inline  bool            operator==(const SString& str) const                    { return Compare(str) == 0; }
00690     inline  bool            operator!=(const SString& str) const                    { return Compare(str) != 0; }
00691     inline  bool            operator<(const SString& str) const                     { return Compare(str) < 0; }
00692     inline  bool            operator<=(const SString& str) const                    { return Compare(str) <= 0; }
00693     inline  bool            operator>=(const SString& str) const                    { return Compare(str) >= 0; }
00694     inline  bool            operator>(const SString& str) const                     { return Compare(str) > 0; }
00695     
00696     inline  bool            operator==(const static_small_string_value& str) const  { return Compare(reinterpret_cast<const SValue&>(str)) == 0; }
00697     inline  bool            operator!=(const static_small_string_value& str) const  { return Compare(reinterpret_cast<const SValue&>(str)) != 0; }
00698     inline  bool            operator<(const static_small_string_value& str) const   { return Compare(reinterpret_cast<const SValue&>(str)) < 0; }
00699     inline  bool            operator<=(const static_small_string_value& str) const  { return Compare(reinterpret_cast<const SValue&>(str)) <= 0; }
00700     inline  bool            operator>=(const static_small_string_value& str) const  { return Compare(reinterpret_cast<const SValue&>(str)) >= 0; }
00701     inline  bool            operator>(const static_small_string_value& str) const   { return Compare(reinterpret_cast<const SValue&>(str)) > 0; }
00702 
00703     inline  bool            operator==(const static_large_string_value& str) const  { return Compare(reinterpret_cast<const SValue&>(str)) == 0; }
00704     inline  bool            operator!=(const static_large_string_value& str) const  { return Compare(reinterpret_cast<const SValue&>(str)) != 0; }
00705     inline  bool            operator<(const static_large_string_value& str) const   { return Compare(reinterpret_cast<const SValue&>(str)) < 0; }
00706     inline  bool            operator<=(const static_large_string_value& str) const  { return Compare(reinterpret_cast<const SValue&>(str)) <= 0; }
00707     inline  bool            operator>=(const static_large_string_value& str) const  { return Compare(reinterpret_cast<const SValue&>(str)) >= 0; }
00708     inline  bool            operator>(const static_large_string_value& str) const   { return Compare(reinterpret_cast<const SValue&>(str)) > 0; }
00709 
00711 
00717             int32_t         LexicalCompare(const SValue& o) const;
00718     
00720 
00725     
00727 
00732             status_t        AsStatus(status_t* result = NULL) const;
00733             
00735 
00742             ssize_t         AsSSize(status_t* result = NULL) const;
00743             
00745 
00754             sptr<IBinder>   AsBinder(status_t* result = NULL) const;
00755             
00757 
00766             wptr<IBinder>   AsWeakBinder(status_t* result = NULL) const;
00767             
00769 
00781             SString         AsString(status_t* result = NULL) const;
00782             
00784 
00799             bool            AsBool(status_t* result = NULL) const;
00800             
00802 
00819             int32_t         AsInt32(status_t* result = NULL) const;
00820             
00822 
00834             int64_t         AsInt64(status_t* result = NULL) const;
00835             
00837 
00849             nsecs_t         AsTime(status_t* result = NULL) const;
00850             
00852 
00864             float           AsFloat(status_t* result = NULL) const;
00865             
00867 
00878             double          AsDouble(status_t* result = NULL) const;
00879             
00881 
00888             sptr<SAtom>     AsAtom(status_t* result = NULL) const;
00889             
00891 
00898             wptr<SAtom>     AsWeakAtom(status_t* result = NULL) const;
00899     
00901 
00904             sptr<SKeyID>    AsKeyID(status_t* result = NULL) const;
00905             
00907     inline  off_t           AsOffset(status_t* result = NULL) const { return (off_t)AsInt64(result); }
00908             
00910     inline  int32_t         AsInteger(status_t* result = NULL) const { return AsInt32(result); }
00911             
00913 
00918     
00919             status_t        GetBinder(sptr<IBinder> *obj) const;
00920             status_t        GetWeakBinder(wptr<IBinder> *obj) const;
00921             status_t        GetString(const char** a_string) const;
00922             status_t        GetString(SString* a_string) const;
00923             status_t        GetBool(bool* val) const;
00924             status_t        GetInt8(int8_t* val) const;
00925             status_t        GetInt16(int16_t* val) const;
00926             status_t        GetInt32(int32_t* val) const;
00927             status_t        GetInt64(int64_t* val) const;
00928             status_t        GetStatus(status_t* val) const;
00929             status_t        GetTime(nsecs_t* val) const;
00930             status_t        GetFloat(float* a_float) const;
00931             status_t        GetDouble(double* a_double) const;
00932             status_t        GetAtom(sptr<SAtom>* atom) const;
00933             status_t        GetWeakAtom(wptr<SAtom>* atom) const;
00934             status_t        GetKeyID(sptr<SKeyID> *keyid) const;
00935 
00937 
00941 
00942             status_t        PrintToStream(const sptr<ITextOutput>& io, uint32_t flags = 0) const;
00943             
00945 
00949     typedef status_t            (*print_func)(  const sptr<ITextOutput>& io,
00950                                                 const SValue& val,
00951                                                 uint32_t flags);
00952     
00954 
00957     static  status_t            RegisterPrintFunc(type_code type, print_func func);
00959     static  status_t            UnregisterPrintFunc(type_code type, print_func func=NULL);
00960 
00962 
00964     static  const SValue&   LookupConstantValue(int32_t index);
00965 
00966 private:
00967     
00968     friend  class           BValueMap;
00969     
00970             // These are technically public because some inline methods call them.
00971             void            InitAsCopy(const SValue& o);
00972             void            InitAsMap(const SValue& key, const SValue& value);
00973             void            InitAsMap(const SValue& key, const SValue& value, uint32_t flags);
00974             void            InitAsMap(const SValue& key, const SValue& value, uint32_t flags, size_t numMappings);
00975             void            InitAsRaw(type_code type, const void* data, size_t len);
00976             void            FreeData();
00977 
00978             void*           alloc_data(type_code type, size_t len);
00979             void            init_as_shared_buffer(type_code type, const SSharedBuffer* buffer);
00980             bool            is_defined() const;
00981             bool            is_wild() const;
00982             bool            is_null() const;
00983             bool            is_error() const;
00984             bool            is_final() const;   // wild or error
00985             bool            is_simple() const;  // !is_map()
00986             bool            is_map() const;
00987             bool            is_object() const;
00988             bool            is_specified() const;
00989             int32_t         compare(uint32_t type, const void* data, size_t length) const;
00990             status_t        remove_item_index(size_t index);
00991             status_t        find_item_index(size_t index,
00992                                             SValue* out_key, SValue* out_value) const;
00993             void            shrink_map(BValueMap* map);
00994     static  int32_t         compare_map(const SValue* key1, const SValue* value1,
00995                                         const SValue* key2, const SValue* value2);
00996             void*           edit_data(size_t len);
00997             status_t        copy_small_data(type_code type, void* buf, size_t len) const;
00998             status_t        copy_big_data(type_code type, void* buf, size_t len) const;
00999             ssize_t         unarchive_internal(SParcel& from, size_t amount);
01000             BValueMap**     edit_map();
01001             BValueMap**     make_map_without_sets();
01002             status_t        set_error(ssize_t code);
01003             status_t        type_conversion_error() const;
01004             bool            check_integrity() const;
01005             
01006             // --- THE STATE ---
01007             // It is no coincidence that the following structure is exactly
01008             // the same as a small_flat_data...  and in fact we count on that.
01009 
01010             uint32_t        m_type;
01011 
01012             union {
01013                 // out-of-line storage
01014                 const SSharedBuffer*    buffer;     // B_TYPE_LENGTH_BIG -- block of data
01015                 const BValueMap*        map;        // B_TYPE_LENGTH_MAP -- value mappings
01016 
01017                 // variations on inline storage
01018                 uint8_t     local[4];
01019                 char        string[4];
01020                 int32_t     integer;
01021                 uint32_t    uinteger;
01022                 void*       object;
01023             }               m_data;
01024 };
01025 
01026 /*----- Type and STL utilities --------------------------------------*/
01027 inline void     BMoveBefore(SValue* to, SValue* from, size_t count);
01028 inline void     BMoveAfter(SValue* to, SValue* from, size_t count);
01029 inline void     BSwap(SValue& v1, SValue& v2);
01030 inline int32_t  BCompare(const SValue& v1, const SValue& v2);
01031 inline void     swap(SValue& x, SValue& y);
01032 
01033 _IMPEXP_SUPPORT const sptr<ITextOutput>& operator<<(const sptr<ITextOutput>& io, const SValue& value);
01034 
01035 // -----------------------------------------------------------------
01065 
01066 // -----------------------------------------------------------------
01067 // Specializations for plain old data types
01068 // -----------------------------------------------------------------
01069 
01070 extern SSharedBuffer* BArrayAsValueHelper(void*& toPtr, size_t count, size_t elementSize, type_code typeCode);
01071 extern const void* BArrayConstructHelper(const SValue& value, size_t count, size_t elementSize, type_code typeCode);
01072 
01074 
01075 #define B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(TYPE, TYPECODE)                                   \
01076     inline SValue BArrayAsValue(const TYPE* from, size_t count)                                 \
01077     {                                                                                           \
01078         void* voidPtr = NULL;                                                                   \
01079         SSharedBuffer* buffer = BArrayAsValueHelper(voidPtr, count, sizeof(TYPE), TYPECODE);    \
01080         TYPE* toPtr = static_cast<TYPE*>(voidPtr);                                              \
01081         BCopy(toPtr, from, count);                                                              \
01082         SValue value(B_FIXED_ARRAY_TYPE, buffer);                                               \
01083         buffer->DecUsers();                                                                     \
01084         return value;                                                                           \
01085     }                                                                                           \
01086     inline status_t BArrayConstruct(TYPE* to, const SValue& value, size_t count)                            \
01087     {                                                                                                       \
01088         const TYPE* fromPtr = NULL;                                                                         \
01089         fromPtr = static_cast<const TYPE*>(BArrayConstructHelper(value, count, sizeof(TYPE), TYPECODE));    \
01090         if (fromPtr != NULL) {                                                                              \
01091             BCopy(to, fromPtr, count);                                                                      \
01092         }                                                                                                   \
01093         else {                                                                                              \
01094             return B_BAD_DATA;                                                                              \
01095         }                                                                                                   \
01096         return B_OK;                                                                                        \
01097     }                                                                                                       \
01098 
01099 // -----------------------------------------------------------------
01100 // Specializations for types derived from SFlattenable
01101 // -----------------------------------------------------------------
01102 
01103 #define B_IMPLEMENT_SFLATTENABLE_FLATTEN_FUNCS(TYPE)                                \
01104     inline SValue BArrayAsValue(const TYPE* from, size_t count)                     \
01105     {                                                                               \
01106         SValue result;                                                              \
01107         for (size_t i = 0; i < count; i++) {                                        \
01108             result.JoinItem(SSimpleValue<int32_t>(i), from->AsValue());             \
01109             from++;                                                                 \
01110         }                                                                           \
01111         return result;                                                              \
01112     }                                                                               \
01113     inline status_t BArrayConstruct(TYPE* to, const SValue& value, size_t count)    \
01114     {                                                                               \
01115         BConstruct(to, count);                                                      \
01116         for (size_t i = 0; i < count; i++) {                                        \
01117             to->SetFromValue(value[SSimpleValue<int32_t>(i)]);                      \
01118             to++;                                                                   \
01119         }                                                                           \
01120         return B_OK;                                                                \
01121     }                                                                               \
01122 
01123 // -----------------------------------------------------------------
01124 // Specializations for types implementing
01125 // SValue TYPE::AsValue() and TYPE::TYPE(const SValue&)
01126 // -----------------------------------------------------------------
01127 
01128 #define B_IMPLEMENT_FLATTEN_FUNCS(TYPE)                                             \
01129     inline SValue BArrayAsValue(const TYPE* from, size_t count)                     \
01130     {                                                                               \
01131         SValue result;                                                              \
01132         for (size_t i = 0; i < count; i++) {                                        \
01133             result.JoinItem(SSimpleValue<int32_t>(i), from->AsValue());             \
01134             from++;                                                                 \
01135         }                                                                           \
01136         return result;                                                              \
01137     }                                                                               \
01138     inline status_t BArrayConstruct(TYPE* to, const SValue& value, size_t count)    \
01139     {                                                                               \
01140         for (size_t i = 0; i < count; i++) {                                        \
01141         *to = TYPE(value[SSimpleValue<int32_t>(i)]);                                \
01142             to++;                                                                   \
01143         }                                                                           \
01144         return B_OK;                                                                \
01145     }                                                                               \
01146 
01147 // -----------------------------------------------------------------
01148 // Specializations for sptr<IInterface>
01149 // -----------------------------------------------------------------
01150 
01151 #define B_IMPLEMENT_IINTERFACE_FLATTEN_FUNCS(TYPE)                                          \
01152     inline SValue BArrayAsValue(const sptr<TYPE>* from, size_t count)                       \
01153     {                                                                                       \
01154         SValue result;                                                                      \
01155         for (size_t i = 0; i < count; i++) {                                                \
01156             result.JoinItem(SSimpleValue<int32_t>(i), SValue::Binder((*from)->AsBinder())); \
01157             from++;                                                                         \
01158         }                                                                                   \
01159         return result;                                                                      \
01160     }                                                                                       \
01161     inline status_t BArrayConstruct(sptr<TYPE>* to, const SValue& value, size_t count)      \
01162     {                                                                                       \
01163         BConstruct(to, count);                                                              \
01164         for (size_t i = 0; i < count; i++) {                                                \
01165             /* We know that only objects of TYPE are in sequence */                         \
01166             *to = TYPE::AsInterfaceNoInspect(value[SSimpleValue<int32_t>(i)]);              \
01167             to++;                                                                           \
01168         }                                                                                   \
01169         return B_OK;                                                                        \
01170     }                                                                                       \
01171 
01172 // -----------------------------------------------------------------
01173 // Specializations for sptr<IBinder>
01174 // -----------------------------------------------------------------
01175 
01176 inline SValue BArrayAsValue(const sptr<IBinder>* from, size_t count)
01177 {
01178     SValue result;
01179     for (size_t i = 0; i < count; i++) {
01180         result.JoinItem(SSimpleValue<int32_t>(i), SValue::Binder(*from));
01181         from++;
01182     }
01183     return result;
01184 }
01185 
01186 inline status_t BArrayConstruct(sptr<IBinder>* to, const SValue& value, size_t count)
01187 {
01188     BConstruct(to, count);
01189     for (size_t i = 0; i < count; i++) {
01190         *to = value[SSimpleValue<int32_t>(i)].AsBinder();
01191         to++;
01192     }
01193     return B_OK;
01194 }
01195 
01196 // -----------------------------------------------------------------
01197 // Specializations for marshalling SVector<SValue>
01198 // -----------------------------------------------------------------
01199 
01200 SValue BArrayAsValue(const SValue* from, size_t count);
01201 status_t BArrayConstruct(SValue* to, const SValue& value, size_t count);
01202 
01204 
01207 /*-------------------------------------------------------------*/
01208 /*---- No user serviceable parts after this -------------------*/
01209 
01210 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(bool, B_BOOL_TYPE)
01211 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(int8_t, B_INT8_TYPE)
01212 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(uint8_t, B_UINT8_TYPE)
01213 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(int16_t, B_INT16_TYPE)
01214 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(uint16_t, B_UINT16_TYPE)
01215 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(int32_t, B_INT32_TYPE)
01216 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(uint32_t, B_UINT32_TYPE)
01217 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(int64_t, B_INT64_TYPE)
01218 // nsecs_t specialization not needed...  it is handled by int64_t 
01219 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(uint64_t, B_UINT64_TYPE)
01220 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(float, B_FLOAT_TYPE)
01221 B_IMPLEMENT_SIMPLE_TYPE_FLATTEN_FUNCS(double, B_DOUBLE_TYPE)
01222 
01223 // Making this constructor inline is a nice performance optimization,
01224 // but it could potentially have binary compatibility issues -- it means
01225 // that we can never change the fact that an undefined SValue contains
01226 // a first integer with the constant below and second integer of garbage.
01227 inline SValue::SValue()
01228     : m_type(B_UNDEFINED_TYPE)
01229 {
01230 }
01231 
01232 inline SValue::SValue(const SValue& o)
01233 {
01234     //printf("*** Creating SValue %p from SValue\n", this);
01235     InitAsCopy(o);
01236 }
01237 
01238 inline SValue::SValue(const SValue& key, const SValue& value)
01239 {
01240     //printf("*** Creating SValue %p from SValue->SValue\n", this);
01241     InitAsMap(key, value);
01242 }
01243 
01244 inline SValue::SValue(const SValue& key, const SValue& value, uint32_t flags)
01245 {
01246     //printf("*** Creating SValue %p from SValue->SValue\n", this);
01247     InitAsMap(key, value, flags);
01248 }
01249 
01250 inline SValue::SValue(const SValue& key, const SValue& value, uint32_t flags, size_t numMappings)
01251 {
01252     //printf("*** Creating SValue %p from SValue->SValue\n", this);
01253     InitAsMap(key, value, flags, numMappings);
01254 }
01255 
01256 inline SValue::SValue(type_code type, const void* data, size_t len)
01257 {
01258     //printf("*** Creating SValue %p from type,data,len\n", this);
01259     InitAsRaw(type, data, len);
01260 }
01261 
01262 // Likewise for this constructor.
01263 inline SValue::SValue(int32_t num)
01264     : m_type(B_PACK_SMALL_TYPE(B_INT32_TYPE, sizeof(int32_t)))
01265 {
01266     m_data.integer = num;
01267 }
01268 
01269 inline SValue::SValue(const static_small_string_value& str)
01270 {
01271     InitAsCopy(static_cast<const SValue&>(str));
01272 }
01273 
01274 inline SValue::SValue(const static_large_string_value& str)
01275 {
01276     InitAsCopy(static_cast<const SValue&>(str));
01277 }
01278 
01279 inline SValue::~SValue()
01280 {
01281     //printf("*** Destroying SValue %p\n", this);
01282     FreeData();
01283 }
01284 
01285 inline void BMoveBefore(SValue* to, SValue* from, size_t count)
01286 {
01287     SValue::MoveBefore(to, from, count);
01288 }
01289 
01290 inline void BMoveAfter(SValue* to, SValue* from, size_t count)
01291 {
01292     SValue::MoveAfter(to, from, count);
01293 }
01294 
01295 inline void BSwap(SValue& v1, SValue& v2)
01296 {
01297     v1.Swap(v2);
01298 }
01299 
01300 inline int32_t BCompare(const SValue& v1, const SValue& v2)
01301 {
01302     return v1.Compare(v2);
01303 }
01304 
01305 inline void swap(SValue& x, SValue& y)
01306 {
01307     x.Swap(y);
01308 }
01309 
01310 inline bool SValue::IsDefined() const
01311 {
01312     return m_type != B_UNDEFINED_TYPE; //kUndefinedTypeCode
01313 }
01314 
01315 inline bool SValue::IsWild() const
01316 {
01317     return m_type == B_PACK_SMALL_TYPE(B_WILD_TYPE, 0); //kWildTypeCode;
01318 }
01319 
01320 inline bool SValue::IsSimple() const
01321 {
01322     return B_UNPACK_TYPE_LENGTH(m_type) != B_TYPE_LENGTH_MAP;
01323 }
01324 
01325 inline bool SValue::IsSpecified() const
01326 {
01327     return IsDefined() && !IsWild();
01328 }
01329 
01330 BNS(} })    // namespace palmos::support
01331 
01332 #endif  /* _SUPPORT_VALUE_H_ */