Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4680 | right-hear | 1 | /** \file lispstring.h |
2 | * Defining a string class. |
||
3 | */ |
||
4 | |||
5 | |||
6 | #ifndef __lispstring_h__ |
||
7 | #define __lispstring_h__ |
||
8 | |||
9 | #include "yacasbase.h" |
||
10 | #include "grower.h" |
||
11 | #include "refcount.h" |
||
12 | |||
13 | class LispStringSmartPtr; |
||
14 | |||
15 | |||
16 | /** \class LispString : zero-terminated byte-counted string. |
||
17 | * Also keeps a reference count for any one interested. |
||
18 | * LispString is derived from CArrayGrower, so the function |
||
19 | * Size returns the length of the buffer. Since the string |
||
20 | * is also zero-terminated (for better interfacing with the normal |
||
21 | * c functions), the string length is Size()-1. |
||
22 | * |
||
23 | * This class also allows the string to point to a buffer which is owned |
||
24 | * by another part of the system, in which case it cannot be resized. |
||
25 | * The array will then not be freed by this class. |
||
26 | */ |
||
27 | class LispString : public CArrayGrower |
||
28 | { |
||
29 | public: |
||
30 | // Constructors |
||
31 | // The constructors allow the caller to specify whether the storage is owned externally. |
||
32 | // Use the assignment operators to set the string after this. |
||
33 | inline LispString(LispBoolean aStringOwnedExternally=LispFalse); |
||
34 | inline LispString(LispString &aString, LispBoolean aStringOwnedExternally=LispFalse); |
||
35 | inline LispString(LispChar * aString, LispBoolean aStringOwnedExternally); |
||
36 | inline LispString(const LispChar * aString); |
||
37 | |||
38 | // Assignment |
||
39 | // This assignment abides by earlier functions setting the string as owned externally. |
||
40 | inline LispString& operator=(LispChar * aString); |
||
41 | |||
42 | // Assignments (with modifications). This string cannot be owned externally. |
||
43 | // Set string by taking part of another string. |
||
44 | void SetStringCounted(const LispChar * aString, LispInt aLength); |
||
45 | // Set string from other string, adding quotes around the string. |
||
46 | void SetStringUnStringified(const LispChar * aString); |
||
47 | // Set string from other string, removing quotes around the string. |
||
48 | void SetStringStringified(const LispChar * aString); |
||
49 | |||
50 | // Access |
||
51 | inline LispChar * c_str() const; // pointer to asciz 'C-string' |
||
52 | |||
53 | // Comparison |
||
54 | // If the string is in the hash table it is faster to compare the pointers to the strings |
||
55 | // (instead of calling this routine), since in that case if they |
||
56 | // are equal they should in fact be literally the same object. |
||
57 | LispInt operator==(const LispString& aString); |
||
58 | |||
59 | ~LispString(); |
||
60 | private: |
||
61 | inline void SetString(LispChar * aString, LispBoolean aStringOwnedExternally); |
||
62 | void SetString(const LispChar * aString); |
||
63 | public: |
||
64 | ReferenceCount iReferenceCount; |
||
65 | }; |
||
66 | |||
67 | |||
68 | /** \class LispStringSmartPtr for managing strings outside |
||
69 | of normal objects. This is the equivalent of LispPtr, maintaining |
||
70 | a reference count for the string object. |
||
71 | */ |
||
72 | class LispStringSmartPtr |
||
73 | { |
||
74 | public: |
||
75 | // Default constructor (not explicit, so it auto-initializes) |
||
76 | LispStringSmartPtr() : iString(NULL) {} |
||
77 | |||
78 | // Construct from pointer to LispString |
||
79 | LispStringSmartPtr(LispString * aString) : iString(NULL) |
||
80 | { |
||
81 | this->operator=(aString); |
||
82 | } |
||
83 | |||
84 | // Copy constructor |
||
85 | LispStringSmartPtr(const LispStringSmartPtr& aOther) : iString() |
||
86 | { |
||
87 | this->operator=(aOther.iString); |
||
88 | } |
||
89 | |||
90 | // Destructor |
||
91 | ~LispStringSmartPtr(); |
||
92 | |||
93 | // Assignment from pointer. (PDG - new method) |
||
94 | // (we return void, not *this). |
||
95 | LispStringSmartPtr& operator=(LispString * aString); |
||
96 | |||
97 | // Assignment from another (the *default* simply assigns members, not what we want). |
||
98 | // (we return void, not *this). |
||
99 | LispStringSmartPtr& operator=(const LispStringSmartPtr &aOther) { this->operator=(aOther.iString); return *this; } |
||
100 | |||
101 | // Expected pointer behavior. |
||
102 | operator LispString*() const { return iString; } // implicit conversion to pointer to T |
||
103 | LispString *operator->() const { return iString; } // so (smartPtr->member) accesses T's member |
||
104 | |||
105 | // Operators below are not used yet, so they are commented out. If you want to use them you need to test if they work. |
||
106 | //LispString &operator*() const { return *iString; } // so (*smartPtr) is a reference to T |
||
107 | //LispString *ptr() const { return iString; } // so (smartPtr.ptr()) returns the pointer to T (boost calls this method 'get') |
||
108 | //bool operator!() const { return !iString; } // is null pointer |
||
109 | |||
110 | private: |
||
111 | LispString * iString; |
||
112 | }; |
||
113 | |||
114 | #include "lispstring.inl" |
||
115 | #endif |
||
116 |