CLI  2.9
tk_stl.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2006-2018, Alexis Royer, http://alexis.royer.free.fr/CLI
3 
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of the CLI library project nor the names of its contributors may be used to endorse or promote products derived from this software
12  without specific prior written permission.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 
31 
32 #ifndef _CLI_TK_STL_H_
33 #define _CLI_TK_STL_H_
34 
35 #include <ctype.h> // toupper, tolower
36 #include <string.h> // memset
37 
38 #include <string>
39 #include <deque>
40 #include <map>
41 
42 #include "cli/object.h"
43 
44 
46 
47 
48  CLI_NS_BEGIN(tk)
49 
50 
51  class String : public cli::Object
52  {
53  public:
56  static const tk::String Concat(
57  const unsigned int UI_MaxLength,
58  const char* const STR_1,
59  const char* const STR_2
60  )
61  {
62  tk::String str_Result(UI_MaxLength);
63  str_Result.Append(STR_1);
64  str_Result.Append(STR_2);
65  return str_Result;
66  }
67 
70  static const tk::String Concat(
71  const unsigned int UI_MaxLength,
72  const char* const STR_1,
73  const char* const STR_2,
74  const char* const STR_3
75  )
76  {
77  tk::String str_Result(Concat(UI_MaxLength, STR_1, STR_2));
78  str_Result.Append(STR_3);
79  return str_Result;
80  }
81 
84  static const tk::String Concat(
85  const unsigned int UI_MaxLength,
86  const char* const STR_1,
87  const char* const STR_2,
88  const char* const STR_3,
89  const char* const STR_4
90  )
91  {
92  tk::String str_Result(Concat(UI_MaxLength, STR_1, STR_2, STR_3));
93  str_Result.Append(STR_4);
94  return str_Result;
95  }
96 
99  static const tk::String Concat(
100  const unsigned int UI_MaxLength,
101  const char* const STR_1,
102  const char* const STR_2,
103  const char* const STR_3,
104  const char* const STR_4,
105  const char* const STR_5
106  )
107  {
108  tk::String str_Result(Concat(UI_MaxLength, STR_1, STR_2, STR_3, STR_4));
109  str_Result.Append(STR_5);
110  return str_Result;
111  }
112 
113  private:
115  explicit String(void);
116 
117  public:
119  explicit String(
120  const unsigned int UI_MaxLen
121  )
122  : Object(), m_stlString()
123  {
124  UnusedParameter(UI_MaxLen); // [contrib: Oleg Smolsky, 2010, based on CLI 2.5]
125  }
126 
130  explicit String(
131  const unsigned int UI_MaxLen,
132  const char* const STR_String
133  )
134  : Object(), m_stlString(STR_String)
135  {
136  UnusedParameter(UI_MaxLen); // [contrib: Oleg Smolsky, 2010, based on CLI 2.5]
137  }
138 
141  const String& TK_String
142  )
143  : Object(), m_stlString((const char* const) TK_String)
144  {
145  }
146 
148  virtual ~String(void)
149  {
150  }
151 
152  public:
155  const unsigned int GetLength(void) const
156  {
157  return (unsigned int) m_stlString.size();
158  }
159 
162  const bool IsEmpty(void) const
163  {
164  return (GetLength() <= 0);
165  }
166 
168  operator const char* const(void) const
169  {
170  return m_stlString.c_str();
171  }
172 
175  const char GetChar(
176  const unsigned int UI_Pos
177  ) const
178  {
179  char c_Char = '\0';
180  if (UI_Pos < m_stlString.size())
181  {
182  c_Char = m_stlString[UI_Pos];
183  }
184  return c_Char;
185  }
186 
189  const tk::String SubString(
190  const unsigned int UI_FirstCharacter,
191  const int I_SubStringLength
192  ) const
194  {
195  tk::String str_SubString(0);
196 
197  // Determine copy length.
198  unsigned int ui_CopyLen = I_SubStringLength;
199  if (GetLength() - UI_FirstCharacter < ui_CopyLen)
200  ui_CopyLen = GetLength() - UI_FirstCharacter;
201 
202  // Copy.
203  if ((UI_FirstCharacter < GetLength()) && (ui_CopyLen > 0))
204  {
205  str_SubString.Append(m_stlString.substr(UI_FirstCharacter, ui_CopyLen).c_str());
206  }
207 
208  return str_SubString;
209  }
210 
213  const String ToUpper(void) const
214  {
215  tk::String str_Upper(0);
216 
217  const unsigned int ui_StrLen = GetLength() + 1;
218  if (char* const pc_Upper = new char[ui_StrLen])
219  {
220  memset(pc_Upper, '\0', ui_StrLen);
221  if (const char* const pc_String = (const char*) *this) {
222  for (unsigned int ui=0; ui<ui_StrLen; ui++)
223  {
224  pc_Upper[ui] = toupper(pc_String[ui]);
225  }
226  }
227  str_Upper.Append(pc_Upper);
228  delete [] pc_Upper;
229  }
230 
231  return str_Upper;
232  }
233 
236  const String ToLower(void) const
237  {
238  tk::String str_Lower(0);
239 
240  const unsigned int ui_StrLen = GetLength() + 1;
241  if (char* const pc_Lower = new char[ui_StrLen])
242  {
243  memset(pc_Lower, '\0', ui_StrLen);
244  if (const char* const pc_String = (const char*) *this)
245  {
246  for (unsigned int ui=0; ui<ui_StrLen; ui++)
247  {
248  pc_Lower[ui] = tolower(pc_String[ui]);
249  }
250  }
251  str_Lower.Append(pc_Lower);
252  delete [] pc_Lower;
253  }
254 
255  return str_Lower;
256  }
257 
258  public:
261  const bool Reset(void)
262  {
263  m_stlString.erase();
264  return true;
265  }
266 
269  const bool Set(
270  const char* const STR_String
271  )
272  {
273  m_stlString = STR_String;
274  return true;
275  }
276 
279  const bool Append(
280  const char* const STR_String
281  )
282  {
283  m_stlString += STR_String;
284  return true;
285  }
286 
289  const bool Append(
290  const char C_Character
291  )
292  {
293  m_stlString += C_Character;
294  return true;
295  }
296 
300  tk::String& operator=(
301  const tk::String& STR_String
302  )
303  {
304  m_stlString = STR_String.m_stlString;
305  return *this;
306  }
307 
308  private:
310  std::string m_stlString;
311  };
312 
313 
315  template <class T> class Queue : public cli::Object
316  {
317  private:
319  explicit Queue(void);
320 
321  public:
323  explicit Queue(
324  const unsigned int UI_MaxCount
325  )
326  : Object(), m_stlQueue()
327  {
328  UnusedParameter(UI_MaxCount); // [contrib: Oleg Smolsky, 2010, based on CLI 2.5]
329  }
330 
333  const Queue<T>& TK_Queue
334  )
335  : Object(), m_stlQueue(TK_Queue.m_stlQueue)
336  {
337  }
338 
340  virtual ~Queue(void)
341  {
342  }
343 
344  private:
346  Queue& operator=(const Queue&);
347 
348  public:
351  const bool IsEmpty(void) const
352  {
353  return m_stlQueue.empty();
354  }
355 
358  const unsigned int GetCount(void) const
359  {
360  return (unsigned int) m_stlQueue.size();
361  }
362 
363  public:
367  const bool Reset(void)
368  {
369  m_stlQueue.clear();
370  return true;
371  }
372 
373  public:
375  class Iterator : public std::deque<T>::iterator
376  {
377  private:
379  explicit Iterator(void) : std::deque<T>::iterator() {}
380 
381  public:
384  template <class U>
386  const U& it
387  )
388  : std::deque<T>::iterator(it) {}
389  };
390 
393  Iterator GetIterator(void) const
394  {
395  return const_cast<std::deque<T>&>(m_stlQueue).begin();
396  }
397 
400  const bool IsValid(
401  const Iterator& it
402  ) const
403  {
404  return (it != m_stlQueue.end());
405  }
406 
409  const bool MovePrevious(
410  Iterator& it
411  ) const
412  {
413  if (it != m_stlQueue.begin())
414  {
415  it --;
416  return true;
417  }
418  else
419  {
420  return false;
421  }
422  }
423 
426  const bool MoveNext(
427  Iterator& it
428  ) const
429  {
430  it ++;
431  return IsValid(it);
432  }
433 
436  const T& GetAt(
437  const Iterator& it
438  ) const
439  {
440  return *it;
441  }
442 
445  T& GetAt(
446  const Iterator& it
447  )
448  {
449  return *it;
450  }
451 
455  const T Remove(
456  Iterator& it
457  )
458  {
459  const T t_Element = *it;
460  it = m_stlQueue.erase(it);
461  return t_Element;
462  }
463 
464  public:
467  const bool AddHead(
468  const T& T_Element
469  )
470  {
471  m_stlQueue.push_front(T_Element);
472  return true;
473  }
474 
477  const bool AddTail(
478  const T& T_Element
479  )
480  {
481  m_stlQueue.push_back(T_Element);
482  return true;
483  }
484 
488  const T& GetHead(void) const
489  {
490  return m_stlQueue.front();
491  }
492 
496  T& GetHead(void)
497  {
498  return const_cast<T&>(
499  const_cast<const Queue<T>*>(this)->GetHead()
500  );
501  }
502 
506  const T& GetTail(void) const
507  {
508  return m_stlQueue.back();
509  }
510 
514  T& GetTail(void)
515  {
516  return const_cast<T&>(
517  const_cast<const Queue<T>*>(this)->GetTail()
518  );
519  }
520 
524  const T RemoveHead(void)
525  {
526  const T t_Element = m_stlQueue.front();
527  m_stlQueue.pop_front();
528  return t_Element;
529  }
530 
534  const T RemoveTail(void)
535  {
536  const T t_Element = m_stlQueue.back();
537  m_stlQueue.pop_back();
538  return t_Element;
539  }
540 
541  public:
544  const bool Sort(
545  const int (*cmp)(const T&, const T&)
546  )
548  {
549  bool b_Result = true;
550  if (GetCount() < 2)
551  {
552  b_Result = true;
553  }
554  else
555  {
556  // Get a reference element.
557  T t_Ref = RemoveTail();
558 
559  // Dispatch elements in q_1 and q_2.
560  Queue<T> q_1(0), q_2(0);
561  while (! IsEmpty())
562  {
563  T t_Sort = RemoveTail();
564  if (cmp(t_Sort, t_Ref) > 0)
565  {
566  q_1.AddTail(t_Sort);
567  }
568  else
569  {
570  q_2.AddTail(t_Sort);
571  }
572  }
573 
574  // Sort each list q_1 and q_2
575  if (q_1.Sort(cmp) && q_2.Sort(cmp))
576  {
577  // Eventually restore q_1...
578  b_Result = true;
579  for ( Iterator it_1 = q_1.GetIterator();
580  q_1.IsValid(it_1);
581  q_1.MoveNext(it_1))
582  {
583  if (! AddTail(q_1.GetAt(it_1)))
584  {
585  b_Result = false;
586  }
587  }
588  // ... then the reference element...
589  if (! AddTail(t_Ref))
590  {
591  b_Result = false;
592  }
593  // ... and then q_2.
594  for ( Iterator it_2 = q_2.GetIterator();
595  q_2.IsValid(it_2);
596  q_2.MoveNext(it_2))
597  {
598  if (! AddTail(q_2.GetAt(it_2)))
599  {
600  b_Result = false;
601  }
602  }
603  }
604  }
605  return b_Result;
606  }
607 
608  private:
611  std::deque<T> m_stlQueue;
612  };
613 
615  template <class K, class T> class Map : public cli::Object
616  {
617  private:
619  explicit Map(void);
620 
621  public:
623  explicit Map(
624  const unsigned int UI_MaxCount
625  )
626  : Object(), m_stlMap()
627  {
628  }
629 
632  const Map<K,T>& TK_Map
633  )
634  : Object(), m_stlMap(TK_Map.m_stlMap)
635  {
636  }
637 
639  virtual ~Map(void)
640  {
641  }
642 
643  private:
645  Map& operator=(const Map&);
646 
647  public:
650  const bool IsEmpty(void) const
651  {
652  return m_stlMap.empty();
653  }
654 
657  const unsigned int GetCount(void) const
658  {
659  return (unsigned int) m_stlMap.size(); // cast to avoid warnings
660  }
661 
662  public:
666  const bool Reset(void)
667  {
668  m_stlMap.clear();
669  return true;
670  }
671 
672  public:
675  const bool SetAt(
676  const K& K_Key,
677  const T& T_Value
678  )
679  {
680  if (m_stlMap.count(K_Key) > 0)
681  {
682  m_stlMap.erase(K_Key);
683  }
684  m_stlMap.insert(std::make_pair(K_Key, T_Value));
685  return true;
686  }
687 
690  const bool Unset(
691  const K& K_Key
692  )
693  {
694  m_stlMap.erase(K_Key);
695  return true;
696  }
697 
700  const bool IsSet(
701  const K& K_Key
702  ) const
703  {
704  return (m_stlMap.count(K_Key) > 0);
705  }
706 
709  const T* const GetAt(
710  const K& K_Key
711  ) const
712  {
713  typename std::map<K,T>::const_iterator it = m_stlMap.find(K_Key);
714  if (it != m_stlMap.end())
715  {
716  return & it->second;
717  }
718  else
719  {
720  return NULL;
721  }
722  }
723 
724  public:
726  class Iterator : public std::map<K,T>::iterator
727  {
728  private:
730  explicit Iterator(void) : std::map<K,T>::iterator() {}
731 
732  public:
737  const Iterator& it
738  )
739  : std::map<K,T>::iterator(it) {}
740 
741  private:
744  template <class U>
745  Iterator& operator=(const U& any) {
746  std::map<K,T>::iterator::operator=(any);
747  return *this;
748  }
749 
750  private:
751  // In order to allow access to privet members for iteration.
752  friend class Map;
753  };
754 
755 
758  Iterator GetIterator(void) const
759  {
760  Iterator it;
761  it = const_cast<std::map<K,T>&>(m_stlMap).begin();
762  return it;
763  }
764 
767  const bool IsValid(
768  const Iterator& it
769  ) const
770  {
771  return (it != m_stlMap.end());
772  }
773 
776  const bool MoveNext(
777  Iterator& it
778  ) const
779  {
780  it ++;
781  return IsValid(it);
782  }
783 
786  const K& GetKey(
787  const Iterator& it
788  ) const
789  {
790  return it->first;
791  }
792 
795  const T& GetAt(
796  const Iterator& it
797  ) const
798  {
799  return it->second;
800  }
801 
804  T& GetAt(
805  const Iterator& it
806  )
807  {
808  return it->second;
809  }
810 
814  const T Remove(
815  Iterator& it
816  )
817  {
818  const T t_Element = GetAt(it);
819  /*it =*/ m_stlMap.erase(it);
820  return t_Element;
821  }
822 
823  private:
825  std::map<K,T> m_stlMap;
826  };
827 
828  CLI_NS_END(tk)
829 
831 
832 #endif // _CLI_TK_STL_H_
833 
Generic object.
Definition: object.h:42
const bool Reset(void)
Resets the queue.
Definition: tk_stl.h:367
const bool AddTail(const T &T_Element)
Add a new element at the tail of the queue.
Definition: tk_stl.h:477
Main namespace of the CLI library.
virtual ~Map(void)
Destructor.
Definition: tk_stl.h:639
static const tk::String Concat(const unsigned int UI_MaxLength, const char *const STR_1, const char *const STR_2, const char *const STR_3)
Concatenation.
Definition: tk_stl.h:70
virtual ~Queue(void)
Destructor.
Definition: tk_stl.h:340
const T *const GetAt(const K &K_Key) const
Element accessor.
Definition: tk_stl.h:709
static const tk::String Concat(const unsigned int UI_MaxLength, const char *const STR_1, const char *const STR_2, const char *const STR_3, const char *const STR_4, const char *const STR_5)
Concatenation.
Definition: tk_stl.h:99
Queue(const unsigned int UI_MaxCount)
Main constructor.
Definition: tk_stl.h:323
const K & GetKey(const Iterator &it) const
Key retrieval.
Definition: tk_stl.h:786
const bool MoveNext(Iterator &it) const
Iterates forward the iterator.
Definition: tk_stl.h:426
const bool Unset(const K &K_Key)
Unset an item.
Definition: tk_stl.h:690
String(const String &TK_String)
Copy constructor.
Definition: tk_stl.h:140
const char GetChar(const unsigned int UI_Pos) const
Single character accessor.
Definition: tk_stl.h:175
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
const String ToUpper(void) const
Upper string transformation.
Definition: tk_stl.h:213
static const tk::String Concat(const unsigned int UI_MaxLength, const char *const STR_1, const char *const STR_2, const char *const STR_3, const char *const STR_4)
Concatenation.
Definition: tk_stl.h:84
const bool Sort(const int(*cmp)(const T &, const T &))
Sort the list according to the given comparison function.
Definition: tk_stl.h:544
T & GetAt(const Iterator &it)
Modifiable item retrieval.
Definition: tk_stl.h:804
Basic queue object.
Definition: tk_stl.h:315
CLI classes toolkit.
Definition: tk_stl.h:51
T & GetHead(void)
First item accessor of the modifiable queue.
Definition: tk_stl.h:496
const bool Reset(void)
String resetting.
Definition: tk_stl.h:261
Queue(const Queue< T > &TK_Queue)
Copy constructor.
Definition: tk_stl.h:332
Iterator object.
Definition: tk_stl.h:375
const void * UnusedParameter(const T &T_UnusedParam)
Dummy function that aims to avoid warnings for unused parameters.
Definition: tk.h:49
const bool IsEmpty(void) const
Determines whether the queue is empty.
Definition: tk_stl.h:351
const bool Append(const char *const STR_String)
String appending.
Definition: tk_stl.h:279
Map(const Map< K, T > &TK_Map)
Copy constructor.
Definition: tk_stl.h:631
const bool IsEmpty(void) const
Checks whether the string is empty or not.
Definition: tk_stl.h:162
const bool SetAt(const K &K_Key, const T &T_Value)
Set a new item.
Definition: tk_stl.h:675
const bool Append(const char C_Character)
String appending.
Definition: tk_stl.h:289
const T Remove(Iterator &it)
Item removal.
Definition: tk_stl.h:455
const T & GetAt(const Iterator &it) const
Read-only item retrieval.
Definition: tk_stl.h:436
const bool AddHead(const T &T_Element)
Add a new element at the head of the queue.
Definition: tk_stl.h:467
Iterator GetIterator(void) const
Iterator retrieval.
Definition: tk_stl.h:758
tk::String & operator=(const tk::String &STR_String)
Assignment operator.
Definition: tk_stl.h:300
const bool MoveNext(Iterator &it) const
Iterates the iterator.
Definition: tk_stl.h:776
const T RemoveHead(void)
Add a new element at the head of the queue.
Definition: tk_stl.h:524
Iterator(const Iterator &it)
Copy constructor.
Definition: tk_stl.h:736
const bool IsSet(const K &K_Key) const
Checks whether an element is set for this key.
Definition: tk_stl.h:700
const T & GetHead(void) const
First item accessor of the read-only queue.
Definition: tk_stl.h:488
const bool MovePrevious(Iterator &it) const
Iterates backward the iterator.
Definition: tk_stl.h:409
Basic map object.
Definition: tk_stl.h:615
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
const unsigned int GetCount(void) const
Item count.
Definition: tk_stl.h:358
static const tk::String Concat(const unsigned int UI_MaxLength, const char *const STR_1, const char *const STR_2)
Concatenation.
Definition: tk_stl.h:56
const bool Set(const char *const STR_String)
String setting.
Definition: tk_stl.h:269
const tk::String SubString(const unsigned int UI_FirstCharacter, const int I_SubStringLength) const
Sub-string computation.
Definition: tk_stl.h:189
Object class definition.
Iterator(const U &it)
Copy constructor from an STL iterator.
Definition: tk_stl.h:385
const T Remove(Iterator &it)
Item removal.
Definition: tk_stl.h:814
const unsigned int GetCount(void) const
Item count.
Definition: tk_stl.h:657
const bool Reset(void)
Resets the map.
Definition: tk_stl.h:666
const T & GetAt(const Iterator &it) const
Read-only item retrieval.
Definition: tk_stl.h:795
const String ToLower(void) const
Lower string transformation.
Definition: tk_stl.h:236
virtual ~String(void)
Destructor.
Definition: tk_stl.h:148
Map(const unsigned int UI_MaxCount)
Main constructor.
Definition: tk_stl.h:623
const bool IsEmpty(void) const
Determines whether the map is empty.
Definition: tk_stl.h:650
const T RemoveTail(void)
Add a new element at the tail of the queue.
Definition: tk_stl.h:534
Iterator GetIterator(void) const
Iterator retrieval.
Definition: tk_stl.h:393
const bool IsValid(const Iterator &it) const
Checks the element at the given position is valid.
Definition: tk_stl.h:400
T & GetAt(const Iterator &it)
Modifiable item retrieval.
Definition: tk_stl.h:445
const T & GetTail(void) const
Last item accessor of the read-only queue.
Definition: tk_stl.h:506
const unsigned int GetLength(void) const
String length accessor.
Definition: tk_stl.h:155
Iterator object.
Definition: tk_stl.h:726
String(const unsigned int UI_MaxLen, const char *const STR_String)
Initial value constructor.
Definition: tk_stl.h:130
T & GetTail(void)
Last item accessor of the modifiable queue.
Definition: tk_stl.h:514
String(const unsigned int UI_MaxLen)
Constructor.
Definition: tk_stl.h:119
const bool IsValid(const Iterator &it) const
Checks the element at the given position is valid.
Definition: tk_stl.h:767