[PATCH rtems-examples 05/23] cxx_stdmap: Add example of using std::map with RTEMS

Joel Sherrill joel at rtems.org
Wed Feb 23 22:21:33 UTC 2022


---
 cxx/Makefile                  |  6 +++++-
 cxx/cxx_stdmap/Makefile       | 41 +++++++++++++++++++++++++++++++++++++
 cxx/cxx_stdmap/main.cc        | 33 ++++++++++++++++++++++++++++++
 cxx/cxx_stdmap/rtems_config.c | 47 +++++++++++++++++++++++++++++++++++++++++++
 cxx/cxx_stdmap/wscript        | 14 +++++++++++++
 cxx/wscript                   |  1 +
 6 files changed, 141 insertions(+), 1 deletion(-)
 create mode 100644 cxx/cxx_stdmap/Makefile
 create mode 100644 cxx/cxx_stdmap/main.cc
 create mode 100644 cxx/cxx_stdmap/rtems_config.c
 create mode 100644 cxx/cxx_stdmap/wscript

diff --git a/cxx/Makefile b/cxx/Makefile
index e5fa10b..d97f794 100644
--- a/cxx/Makefile
+++ b/cxx/Makefile
@@ -4,5 +4,9 @@ include $(RTEMS_SHARE)/make/directory.cfg
 
 # These tests require C++
 ifneq ($(CXX_FOR_TARGET),)
-  SUBDIRS= cxx_throw libcpp
+  SUBDIRS  = cxx_throw
+  SUBDIRS += cxx_stdmap
+
+  # This is specific to application Makefile support
+  SUBDIRS += libcpp
 endif
diff --git a/cxx/cxx_stdmap/Makefile b/cxx/cxx_stdmap/Makefile
new file mode 100644
index 0000000..a2a43a1
--- /dev/null
+++ b/cxx/cxx_stdmap/Makefile
@@ -0,0 +1,41 @@
+#
+#  Makefile
+#
+
+#
+#  RTEMS_MAKEFILE_PATH is typically set in an environment variable
+#
+
+EXEC=cxx_stdmap.exe
+PGM=${ARCH}/$(EXEC)
+
+# C source names
+CSRCS = rtems_config.c
+COBJS_ = $(CSRCS:.c=.o)
+COBJS = $(COBJS_:%=${ARCH}/%)
+
+# C++ source names
+CXXSRCS = main.cc
+CXXOBJS_ = $(CXXSRCS:.cc=.o)
+CXXOBJS = $(CXXOBJS_:%=${ARCH}/%)
+
+# AS source names
+ASSRCS =
+ASOBJS_ = $(ASSRCS:.s=.o)
+ASOBJS = $(ASOBJS_:%=${ARCH}/%)
+
+# Libraries
+LIBS = 
+
+include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
+
+include $(RTEMS_CUSTOM)
+include $(PROJECT_ROOT)/make/leaf.cfg
+
+OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS)
+
+all:    ${ARCH} $(PGM)
+
+$(PGM): $(OBJS)
+	$(make-cxx-exe)
+
diff --git a/cxx/cxx_stdmap/main.cc b/cxx/cxx_stdmap/main.cc
new file mode 100644
index 0000000..f7cb30a
--- /dev/null
+++ b/cxx/cxx_stdmap/main.cc
@@ -0,0 +1,33 @@
+#include <iostream>
+#include <map>
+#include <set>
+#include <algorithm>
+#include <functional>
+ 
+int main() {
+ 
+  // Creating & Initializing a map of String & Ints
+  std::map<std::string, int> mapOfWordCount = { { "aaa", 10 }, { "ddd", 41 },
+  		{ "bbb", 62 }, { "ccc", 13 } };
+ 
+  // Declaring the type of Predicate that accepts 2 pairs and return a bool
+  typedef std::function<bool(std::pair<std::string, int>, std::pair<std::string, int>)> Comparator;
+ 
+  // Defining a lambda function to compare two pairs. It will compare two pairs using second field
+  Comparator compFunctor =
+  		[](std::pair<std::string, int> elem1 ,std::pair<std::string, int> elem2)
+  		{
+  			return elem1.second < elem2.second;
+  		};
+ 
+  // Declaring a set that will store the pairs using above comparision logic
+  std::set<std::pair<std::string, int>, Comparator> setOfWords(
+  		mapOfWordCount.begin(), mapOfWordCount.end(), compFunctor);
+ 
+  // Iterate over a set using range base for loop
+  // It will display the items in sorted order of values
+  for (std::pair<std::string, int> element : setOfWords)
+  	std::cout << element.first << " :: " << element.second << std::endl;
+ 
+  return 0;
+}
diff --git a/cxx/cxx_stdmap/rtems_config.c b/cxx/cxx_stdmap/rtems_config.c
new file mode 100644
index 0000000..f813838
--- /dev/null
+++ b/cxx/cxx_stdmap/rtems_config.c
@@ -0,0 +1,47 @@
+
+#include <stdlib.h>
+
+int main(int argc, char **argv);
+
+static char *argv_list[] = {
+  "cxx_stdmap",
+  ""
+};
+
+
+static void *POSIX_Init(void *arg)
+{
+  (void) arg;  /* deliberately ignored */
+
+  /*
+   * Initialize optional services
+   */
+
+  /*
+   * Could get arguments from command line or have a static set.
+   */
+  (void) main(1, argv_list);
+
+  exit(0);
+
+  return NULL;
+}
+
+#include <bsp.h> /* for device driver prototypes */
+
+/* NOTICE: the clock driver is explicitly disabled */
+#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+
+#define CONFIGURE_MICROSECONDS_PER_TICK     1000
+
+#define CONFIGURE_POSIX_INIT_THREAD_TABLE
+
+#define CONFIGURE_UNLIMITED_OBJECTS
+#define CONFIGURE_UNIFIED_WORK_AREAS
+#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 32
+
+#define CONFIGURE_MINIMUM_TASK_STACK_SIZE (64 * 1024)
+
+#define CONFIGURE_INIT
+#include <rtems/confdefs.h>
diff --git a/cxx/cxx_stdmap/wscript b/cxx/cxx_stdmap/wscript
new file mode 100644
index 0000000..9e2e42d
--- /dev/null
+++ b/cxx/cxx_stdmap/wscript
@@ -0,0 +1,14 @@
+# Copyright 2013 Gedare Bloom (gedare at rtems.org)
+#
+# This file's license is 2-clause BSD as in this distribution's LICENSE.2 file.
+#
+
+# Waf build script for std::map RTEMS example
+import rtems_waf.rtems as rtems
+
+def build(bld):
+    rtems.build(bld)
+
+    bld(features = 'cxx cxxprogram',
+        target = 'cxx_stdmap.exe',
+        source = ['main.cc', 'rtems_config.c'])
diff --git a/cxx/wscript b/cxx/wscript
index 874c130..14cc13d 100644
--- a/cxx/wscript
+++ b/cxx/wscript
@@ -10,4 +10,5 @@ def build(bld):
     if bld.env['CXX'] is not None:
         bld.recurse('libcpp')
         bld.recurse('cxx_throw')
+        bld.recurse('cxx_stdmap')
 
-- 
1.8.3.1



More information about the devel mailing list