Morse Micro IoT SDK  2.9.7
cplusplus_demo.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022-2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
20#include <iostream>
21#include "mmosal.h"
22
27{
28 public:
33 explicit ClassDemo(const char *p_comment)
34 {
35 std::cout << "ClassDemo constructor is running: " << p_comment << std::endl;
36 count = 0;
37 comment = p_comment;
38 }
39
44 {
45 std::cout << "ClassDemo destructor is running: " << comment << std::endl;
46 }
47
53 {
54 return count;
55 }
56
62 {
63 count++;
64 return count;
65 }
66
71 void SetDemoCount(int value)
72 {
73 count = value;
74 }
75
76 private:
77 int count;
78 const char *comment;
79};
80
84ClassDemo global_demo("global demo class");
85
90extern "C" void app_init(void)
91{
92 printf("\n\nC++ Demo Example (Built " __DATE__ " " __TIME__ ")\n\n");
93
94 while (true)
95 {
97 int demo_loop_count = global_demo.GetDemoCount();
98
99 ClassDemo demo("local demo class");
100 demo.SetDemoCount(demo_loop_count);
101 int old_count = demo.IncrementDemoCount();
102 int current_count = demo.GetDemoCount();
103
104 std::cout << "Demo cout in loop " << demo_loop_count
105 << "... next count will be " << old_count
106 << "/" << current_count << std::endl;
107
108#ifdef __EXCEPTIONS
109 /* Note: Exceptions are not supported by libc-nano.
110 * See app.mk on how to enable exception support using libc-nosys.
111 * If you use exceptions in libc-nano, the application
112 * will simply exit when you call throw().
113 *
114 * Also note that exceptions come with a huge cost and
115 * can add 30k to over 100k to your application code even if
116 * you use no C++ code (As exceptions like divide by 0 can be
117 * thrown by C code too). So use libc-nano and avoid using
118 * exception handlers in your C++ code if size is a constraint.
119 */
120 try
121 {
122 if (demo_loop_count % 2 == 0)
123 {
124 std::cout << "About to throw..." << std::endl;
125 throw("count was even");
126 }
127 else
128 {
129 std::cout << "Odd" << std::endl;;
130 }
131 }
132 catch(const char *reason)
133 {
134 std::cout << "Exception: " << reason << std::endl;
135 }
136#endif
137
138 mmosal_task_sleep(5000);
139 }
140}
The ClassDemo class.
~ClassDemo()
Destructor for ClassDemo.
int GetDemoCount()
Getter for count value.
int IncrementDemoCount()
Increment count value.
void SetDemoCount(int value)
Set the count value.
ClassDemo(const char *p_comment)
Constructor for ClassDemo.
ClassDemo global_demo("global demo class")
A global instance of the ClassDemo class.
void app_init(void)
Main entry point to the application.
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.