Tech Off Post

Single Post Permalink

View Thread: New C++ runtime library
  • User profile image
    sysrpl

    I am working on a library for C++ which is small, has no dependencies other than itself (i'm not using boost), works on all platforms, and most importantly is as friendly as C#.

    This morning I finished testing my C++ implementation of linq. Here is an example of what it looks like. Your feedback is appreciated:

    Edit: I expanded the examples

    // Enumerable queries in C++
    // This program searches the current directory
    // And selects results using expression nodes
    
    #include <Codebot/Packages/System.h>
    #include <Codebot/Packages/Expressions.h>
    
    using namespace Codebot;
    using namespace Codebot::IO;
    using namespace Codebot::Text;
    using namespace Codebot::Expressions;
    
    void TestFileFind()
    {
      String SearchPath = "*";
      if (Application::Args().Length() > 0)
        SearchPath = Application::Args()[0];
      FileFind find;
      find.Search(SearchPath);
      auto query = Query(find)
        .OrderBy([] (FileResult a, FileResult b)
        {
          if (a.Attributes.Contains(FileAttribute::Directory))
          {
            if (b.Attributes.Contains(FileAttribute::Directory))
              return a.Name < b.Name;
            return true;
          }
          if (b.Attributes.Contains(FileAttribute::Directory))
            return false;
          return a.Name < b.Name;
        });
      ForEach(item, query)
        WriteLine(item.ToString());
      auto bytes = query
        .Select<int>([] (FileResult a) { return a.Size; })
        .Sum();
      WriteLine("{0|-31} total bytes\n", bytes);
    }
    
    void TestNumbers()
    {
      Array<int> numbers = {7, 4, 2, 8, 3, 6};
      auto q = Query(numbers);
      WriteLine("The first value is {0}",
        q.FirstOrDefault());
      WriteLine("The count value is {0}",
        q.Count());
      WriteLine("The sum value is {0}",
        q.Where([] (int i) { return IsOdd(i); }).Sum());
      WriteLine("The average value is {0}",
        q.Average());
      WriteLine("The min value is {0}",
        q.Min());
      WriteLine("The max value is {0}",
        q.Max());
      q = q.Where([] (int i) { return i > 2 && i < 8; })
        .OrderBy([] (int a, int b) { return a > b; });
      WriteLine("List: {0:list}\n", q);
    }
    
    void TestStrings()
    {
      Array<String> names = {"John", "Terrance", "Ralph", "Carl", "Stanley"};
      auto q = Query(names);
      WriteLine("The first value is {0}",
        q.FirstOrDefault());
      WriteLine("There are {0} items",
        q.Count());
      WriteLine("The sum is {0}", q
        .Where([] (String s) { return s.Contains("a"); }).Sum());
      WriteLine("The minimum value is {0}",
        q.Min());
      WriteLine("The maximum value is {0}",
        q.Max());
      q = q.Where([] (String s) { return s > "Mary"; })
        .OrderBy([] (String a, String b) { return a.Length() < b.Length(); });
      WriteLine("List: {0:list}\n", q);
    }
    
    int main()
    {
      return Application::Run({TestFileFind, TestNumbers, TestStrings});
    }
    
    
    /*
    Output:
    
    .                           Dir
    ..                          Dir
    Debug                       Dir
    .cproject                 14030  2012-04-12 14:41:33
    .project                   2508  2012-04-14 08:20:42
    Person.h                   1726  2012-04-12 22:59:28
    Widget.cpp                  871  2012-04-12 01:34:43
    Widget.h                   1663  2012-04-12 22:59:36
    main.cpp                   2415  2012-04-15 20:32:45
                              23213 total bytes
    
    The first value is 7
    The count value is 6
    The sum value is 10
    The average value is 5
    The min value is 2
    The max value is 8
    List: {7, 6, 4, 3}
    
    The first value is John
    There are 5 items
    The sum is TerranceRalphCarlStanley
    The minimum value is Carl
    The maximum value is Terrance
    List: {Ralph, Stanley, Terrance}
    
    */