OSDN Git Service

Update samples to nxtOSEK_v212.zip (I did not check their licenses.)
[nxt-jsp/etrobo-atk.git] / nxtOSEK / samples_c++ / cpp / SmartPointer / sample.cpp
1 // Slightly modified Boost C++ library\r
2 #include "scoped_ptr.hpp"\r
3 #include "scoped_array.hpp"\r
4 \r
5 #include <memory>       //auto_ptr\r
6 #include <stdio.h>\r
7 #include <stdlib.h>\r
8 #include <string.h>\r
9 \r
10 // ECRobot++ API\r
11 #include "Lcd.h"\r
12 #include "Clock.h"\r
13 using namespace ecrobot; \r
14 \r
15 Lcd lcd;\r
16 \r
17 extern "C"\r
18 {\r
19 #include "ecrobot_interface.h"\r
20 #include "kernel.h"\r
21 \r
22 // nxtOSEK hook to be invoked from an ISR in category 2\r
23 void user_1ms_isr_type2(void)\r
24 {\r
25         SleeperMonitor(); // needed for I2C device and Clock classes\r
26 }\r
27 \r
28 // test class\r
29 class Foo\r
30 {\r
31 public:\r
32         Foo(){  lcd.putf("sn", "Foo::Foo()"); }\r
33         ~Foo(){ lcd.putf("sn", "Foo::~Foo()"); }\r
34         void doNothing(){}\r
35 };\r
36 \r
37 void updateWaitReset(Clock& c)\r
38 {\r
39         lcd.disp();\r
40         c.wait(5000);\r
41         lcd.clear();\r
42 }\r
43 \r
44 TASK(TaskMain)\r
45 {\r
46         Clock nxtClock;\r
47 \r
48         lcd.clear();\r
49         lcd.putf("sn", "new/delete");   \r
50         Foo *foo = new Foo();\r
51         lcd.cursor(0, 7);\r
52         lcd.putf("x", (U32)foo,8);\r
53         lcd.cursor(0, 2);\r
54         delete foo;\r
55 \r
56         updateWaitReset(nxtClock);\r
57 \r
58         //std::auto_ptr\r
59         {\r
60                 lcd.putf("sn", "auto_ptr");\r
61                 std::auto_ptr<Foo> autoPtr(new Foo());\r
62                 lcd.cursor(0, 7);\r
63                 lcd.putf("x", (U32)autoPtr.get(),8);\r
64                 lcd.cursor(0, 2);\r
65 \r
66                 //autoPtr deleted automatically when the current scope ends\r
67         }\r
68 \r
69         updateWaitReset(nxtClock);\r
70 \r
71         //transfer from std::auto_ptr to boost::scoped_ptr\r
72         {\r
73                 lcd.putf("sn", "auto->scoped");\r
74                 std::auto_ptr<Foo> autoPtr(new Foo());\r
75 \r
76                 lcd.cursor(0, 6);\r
77                 //show that the raw pointer address is the same with get()\r
78                 lcd.putf("sxn", "autoPtr ", (U32)autoPtr.get(),8);\r
79                 boost::scoped_ptr<Foo> scopedPtr(autoPtr);\r
80                 lcd.putf("sx",  "scpdPtr ", (U32)scopedPtr.get(),8);\r
81 \r
82                 lcd.cursor(0, 2);\r
83 \r
84                 //scoped_ptr deleted automatically when the current scope ends\r
85         }\r
86 \r
87         updateWaitReset(nxtClock);\r
88         \r
89         //boost::scoped_array\r
90         {\r
91                 lcd.putf("sn", "scoped_array");\r
92                 boost::scoped_array<Foo> scopedArray(new Foo[2]);\r
93                 lcd.cursor(0, 3);\r
94 \r
95                 //scoped_array deleted automatically when the current scope ends\r
96         }\r
97 \r
98         updateWaitReset(nxtClock);\r
99 \r
100         lcd.putf("s", "Assert test");\r
101         lcd.disp();\r
102         nxtClock.wait(2000);\r
103 \r
104         //should produce an assert for null deref\r
105         boost::scoped_ptr<Foo> scopedPtr;\r
106         scopedPtr->doNothing();\r
107         lcd.disp();\r
108 \r
109         while(true){}\r
110 }\r
111 }\r
112 \r
113 \r