"Fossies" - the Fresh Open Source Software Archive

Member "muscle/html/muscle-by-example/examples/queue/example_3_idioms.cpp" (28 Nov 2019, 2424 Bytes) of package /linux/privat/muscle7.52.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "system/SetupSystem.h"  // for CompleteSetupSystem
    2 #include "util/Queue.h"
    3 #include "util/String.h"
    4 
    5 using namespace muscle;
    6 
    7 static void PrintExampleDescription()
    8 {
    9    printf("\n");
   10    printf("This program demonstrates various minor convenience methods of the Queue class\n");
   11    printf("\n");
   12 }
   13 
   14 void PrintQueue(const char * desc, const Queue<String> & q); // forward declaration (see bottom of file)
   15 
   16 int main(int argc, char ** argv)
   17 {
   18    CompleteSetupSystem css;
   19 
   20    PrintExampleDescription();
   21 
   22 #ifdef MUSCLE_AVOID_CPLUSPLUS11
   23    Queue<String> sq;  // sigh
   24    sq.AddTail("Pear");
   25    sq.AddTail("banana");
   26    sq.AddTail("Apple");
   27    sq.AddTail("orange");
   28    sq.AddTail("grape");
   29    sq.AddTail("Berry 31");
   30    sq.AddTail("Berry 5");
   31    sq.AddTail("Berry 12");
   32 #else
   33    Queue<String> sq = {"Pear", "banana", "Apple", "orange", "grape", "Berry 31", "Berry 5", "Berry 12"};
   34 #endif
   35 
   36    PrintQueue("Initial Queue state", sq);
   37 
   38    String firstVal = sq.RemoveHeadWithDefault();  // removes first item and returns it, or "" if sq was empty
   39    printf("Popped firstVal:  [%s]\n", firstVal());
   40 
   41    String lastVal = sq.RemoveTailWithDefault();  // removes last item and returns it, or "" if sq was empty
   42    printf("Popped lastVal:   [%s]\n", lastVal());
   43 
   44    printf("\n");
   45    PrintQueue("Current Queue state A", sq);
   46 
   47    const String & firstValRef = sq.HeadWithDefault();  // reference to first item, or "" if sq was empty
   48    printf("Current first value is [%s]\n", firstValRef());
   49 
   50    const String & lastValRef = sq.TailWithDefault();  // reference to last item, or "" if sq was empty
   51    printf("Current last value is [%s]\n", lastValRef());
   52    printf("\n");
   53 
   54    String * newTail = sq.AddTailAndGet();   // adds new tail item and returns pointer to added item
   55    if (newTail) *newTail = "Schnozzberry";
   56            else printf("sq.AddTailAndGet() failed!?\n");
   57 
   58    PrintQueue("Current Queue state B", sq);
   59 
   60    printf("sq currently contains " UINT32_FORMAT_SPEC " items.\n", sq.GetNumItems());
   61    printf("\n");
   62 
   63    for (uint32 i=0; i<10; i++)
   64    {
   65       printf("sq.GetWithDefault(" UINT32_FORMAT_SPEC ", \"<doh>\") returned:  %s\n", i, sq.GetWithDefault(i, "<doh>")());
   66    }
   67    return 0;
   68 }
   69 
   70 void PrintQueue(const char * desc, const Queue<String> & q)
   71 {
   72    printf("%s (" UINT32_FORMAT_SPEC " items in Queue):\n", desc, q.GetNumItems());
   73    for (uint32 i=0; i<q.GetNumItems(); i++) printf("   %s\n", q[i]());
   74    printf("\n");
   75 }
   76