MCPcopy Create free account
hub / github.com/CppMicroServices/CppMicroServices / Start

Function Start

doc/src/tutorial/spellcheckclient/Activator.cpp:67–124  ·  view source on GitHub ↗

* Implements BundleActivator::Start(). Creates a service * tracker object to monitor spell checker services. Enters * a spell check loop where it reads passages from standard * input and checks their spelling using the spell checker service. * * \note It is very bad practice to use the calling thread to perform a * lengthy process like th

Source from the content-addressed store, hash-verified

65 * @param context the bundle context for this bundle.
66 */
67 void
68 Start(BundleContext context)
69 {
70 m_context = context;
71
72 // Create a service tracker to monitor spell check services.
73 m_tracker = new ServiceTracker<ISpellCheckService>(m_context);
74 m_tracker->Open();
75
76 // std::cout << "Tracker count is :" << m_tracker->GetTrackingCount() << std::endl;
77 std::cout << "Enter a blank line to exit." << std::endl;
78
79 // Loop endlessly until the user enters a blank line
80 while (std::cin)
81 {
82 // Ask the user to enter a passage.
83 std::cout << "Enter passage: ";
84
85 std::string passage;
86 std::getline(std::cin, passage);
87
88 // Get the selected spell check service, if available.
89 std::shared_ptr<ISpellCheckService> checker = m_tracker->GetService();
90
91 // If the user entered a blank line, then
92 // exit the loop.
93 if (passage.empty())
94 {
95 break;
96 }
97 // If there is no spell checker, then say so.
98 else if (checker == nullptr)
99 {
100 std::cout << "No spell checker available." << std::endl;
101 }
102 // Otherwise check passage and print misspelled words.
103 else
104 {
105 std::vector<std::string> errors = checker->Check(passage);
106
107 if (errors.empty())
108 {
109 std::cout << "Passage is correct." << std::endl;
110 }
111 else
112 {
113 std::cout << "Incorrect word(s):" << std::endl;
114 for (std::size_t i = 0; i < errors.size(); ++i)
115 {
116 std::cout << " " << errors[i] << std::endl;
117 }
118 }
119 }
120 }
121
122 // This automatically closes the tracker
123 delete m_tracker;
124 }

Callers 1

Shutdown0Method · 0.50

Calls 5

CheckMethod · 0.80
OpenMethod · 0.45
GetServiceMethod · 0.45
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected