Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

charstr.h

Go to the documentation of this file.
00001 //charstr.h - static and dynamically allocated strings
00002 
00003 #ifndef __CHARSTR_H__
00004 #define __CHARSTR_H__
00005 
00006 #include "defs.h"
00007 
00008 #define STR_STATIC  's'         // constants for type argument of charstr constuctor
00009 #define STR_DYNAMIC 'd'
00010 
00011 //This class handles automatic allocation of dynamic strings and keeps track of which strings are statick
00012 //it is used in all string arguments of XMLData/XMLAttrib methods and their return values
00013 class COM_API CharStr {
00014 
00015 private:
00016 
00017     int store_counter;  //how many times was this dynamic string stored somewhere
00018                         //or -1 if this string is static
00019 
00020 public:
00021 
00022     const char *str;
00023 
00024     //this is a special constant which one can use to indicate error (return) value
00025     //instead of returning NULL, which might cause applications to crash when they
00026     //acess the values without checking it
00027     static CharStr *Error;
00028 
00029     //construct static or dynamic string, use type 'd' (or STR_DYNAMIC) for dynamic strings,
00030     //or 's' (or STR_STATIC) for static strings. Static strings are stored only by its pointers;
00031     //for dynamic strings new memory is allocated and they are copied. Each time you store them
00032     //somewhere in the memory, call stored() method. Each time you release them, call released()
00033     //method. Then you don't have to care about how, when, and where they are allocated/dealllocated,
00034     //i.e. they will be automatically deallocated when release() is called same number of times
00035     //as stored() has been called - i.e. when they are not stored anywhere anymore.
00036     //Static string should be used only as a global variable and defined as:
00037     //  CharStr  xs_Message("Message", 's');
00038     //While dynamic string should be allocated with new operator, typically when passing as an argument,
00039     //such as:
00040     //  setTag(new CharStr("JobID"));
00041     CharStr(const char *s, char type = STR_DYNAMIC);
00042 
00043     //construct a dynamic string by copying len characters from string s. Use this to
00044     //allocate a substring copy of s that is not terminated by '\0'.
00045     CharStr(const char *s, int len);
00046 
00047     //for convenience, this constructor converts the number into a string and allocates
00048     //dynamic string
00049     CharStr(double d);
00050 
00051     //destructor - not needed, only checks whether the object was released before
00052     ~CharStr();
00053 
00054     //should be called each time this object is stored in some variable
00055     void stored();
00056 
00057     //should be called each time this object is released from a variable where it was previously stored
00058     void released();
00059 
00060     //this is a shortcut for creating new CharStr
00061     static CharStr *create(const char *s)
00062     {
00063         if (!s) return CharStr::Error;
00064         else return new CharStr(s);
00065     }
00066 
00067     //performs initialization of static constants
00068     static int init_static();
00069 
00070 private:
00071 
00072     //default constructor not allowed
00073     CharStr() {};
00074 };
00075 
00076 #endif

Generated on Mon Nov 25 12:46:28 2002 for qadpz by doxygen1.2.18