From 0819616be7c980481cd570ce39448d84408d7859 Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 28 Feb 2012 21:58:24 +0000 Subject: Add support for C++ static constructors (at least to a few platforms) git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4438 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/README.txt | 13 ++++++-- apps/examples/helloxx/main.cxx | 73 +++++++++++++++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 15 deletions(-) (limited to 'apps/examples') diff --git a/apps/examples/README.txt b/apps/examples/README.txt index a89a9097c..fb947cd71 100644 --- a/apps/examples/README.txt +++ b/apps/examples/README.txt @@ -362,12 +362,19 @@ examples/helloxx library suupport is available, and that class are instantiated correctly. - NuttX configuration settings: + NuttX configuration prerequisites: + + CONFIG_HAVE_CXX -- Enable C++ Support + + Optional NuttX configuration settings: + + CONFIG_HAVE_CXXINITIALIZE -- Enable support for static constructors + (may not be available on all platforms). + + NuttX configuration settings specific to this examp;le: CONFIG_EXAMPLES_HELLOXX_BUILTIN -- Build the helloxx example as a "built-in" that can be executed from the NSH command line. - CONFIG_EXAMPLES_HELLOXX_NOSTATICCONST - Set if system does not support - static constructors. CONFIG_EXAMPLES_HELLOXX_NOSTACKCONST - Set if the system does not support construction of objects on the stack. diff --git a/apps/examples/helloxx/main.cxx b/apps/examples/helloxx/main.cxx index ed48f4961..8514fead2 100644 --- a/apps/examples/helloxx/main.cxx +++ b/apps/examples/helloxx/main.cxx @@ -1,8 +1,8 @@ //*************************************************************************** -// examples/helloxx/main.c +// examples/helloxx/main.cxx // -// Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved. -// Author: Gregory Nutt +// Copyright (C) 2009, 2011-2012 Gregory Nutt. All rights reserved. +// Author: Gregory Nutt // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions @@ -38,13 +38,39 @@ //*************************************************************************** #include -#include + #include #include +#include +#include + //*************************************************************************** // Definitions //*************************************************************************** +// Debug ******************************************************************** +// Non-standard debug that may be enabled just for testing the constructors + +#ifndef CONFIG_DEBUG +# undef CONFIG_DEBUG_CXX +#endif + +#ifdef CONFIG_DEBUG_CXX +# define cxxdbg dbg +# define cxxlldbg lldbg +# ifdef CONFIG_DEBUG_VERBOSE +# define cxxvdbg vdbg +# define cxxllvdbg llvdbg +# else +# define cxxvdbg(x...) +# define cxxllvdbg(x...) +# endif +#else +# define cxxdbg(x...) +# define cxxlldbg(x...) +# define cxxvdbg(x...) +# define cxxllvdbg(x...) +#endif //*************************************************************************** // Private Classes @@ -53,11 +79,20 @@ class CHelloWorld { public: - CHelloWorld(void) : mSecret(42) { lldbg("Constructor\n"); }; - ~CHelloWorld(void) { lldbg("Destructor\n"); }; + CHelloWorld(void) : mSecret(42) + { + cxxdbg("Constructor: mSecret=%d\n", mSecret); + } + + ~CHelloWorld(void) + { + cxxdbg("Destructor\n"); + } bool HelloWorld(void) { + cxxdbg("HelloWorld: mSecret=%d\n", mSecret); + if (mSecret != 42) { printf("CHelloWorld::HelloWorld: CONSTRUCTION FAILED!\n"); @@ -68,7 +103,7 @@ class CHelloWorld printf("CHelloWorld::HelloWorld: Hello, World!!\n"); return true; } - }; + } private: int mSecret; @@ -78,7 +113,10 @@ class CHelloWorld // Private Data //*************************************************************************** -#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTATICCONST +// Define a statically constructed CHellowWorld instance if C++ static +// initializers are supported by the platform + +#ifdef CONFIG_HAVE_CXXINITIALIZE static CHelloWorld g_HelloWorld; #endif @@ -105,20 +143,31 @@ extern "C" int helloxx_main(int argc, char *argv[]); int MAIN_NAME(int argc, char *argv[]) { -#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTACKCONST - CHelloWorld HelloWorld; + // If C++ initialization for static constructors is supported, then do + // that first + +#ifdef CONFIG_HAVE_CXXINITIALIZE + up_cxxinitialize(); #endif - CHelloWorld *pHelloWorld = new CHelloWorld; + // Exercise an explictly instantiated C++ object + + CHelloWorld *pHelloWorld = new CHelloWorld; printf(MAIN_STRING "Saying hello from the dynamically constructed instance\n"); pHelloWorld->HelloWorld(); + // Exercise an C++ object instantiated on the stack + #ifndef CONFIG_EXAMPLES_HELLOXX_NOSTACKCONST + CHelloWorld HelloWorld; + printf(MAIN_STRING "Saying hello from the instance constructed on the stack\n"); HelloWorld.HelloWorld(); #endif -#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTATICCONST + // Exercise an statically constructed C++ object + +#ifdef CONFIG_HAVE_CXXINITIALIZE printf(MAIN_STRING "Saying hello from the statically constructed instance\n"); g_HelloWorld.HelloWorld(); #endif -- cgit v1.2.3