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

Function Start

doc/src/tutorial/dictionaryclient2/Activator.cpp:75–140  ·  view source on GitHub ↗

* Implements BundleActivator::Start(). Adds itself as a listener for service * events, then queries for available dictionary services. If any * dictionaries are found it gets a reference to the first one available and * then starts its "word checking loop". If no dictionaries are found, then * it just goes directly into its "word checking loop", but it will not

Source from the content-addressed store, hash-verified

73 * @param context the bundle context for this bundle.
74 */
75 void
76 Start(BundleContext context)
77 {
78 m_context = context;
79
80 {
81 // Use your favorite thread library to synchronize member
82 // variable access within this scope while registering
83 // the service listener and performing our initial
84 // dictionary service lookup since we
85 // don't want to receive service events when looking up the
86 // dictionary service, if one exists.
87 // MutexLocker lock(&m_mutex);
88
89 // Listen for events pertaining to dictionary services.
90 m_context.AddServiceListener(std::bind(&Activator::ServiceChanged, this, std::placeholders::_1),
91 std::string("(&(") + Constants::OBJECTCLASS + "="
92 + us_service_interface_iid<IDictionaryService>() + ")"
93 + "(Language=*))");
94
95 // Query for any service references matching any language.
96 std::vector<ServiceReference<IDictionaryService>> refs
97 = context.GetServiceReferences<IDictionaryService>("(Language=*)");
98
99 // If we found any dictionary services, then just get
100 // a reference to the first one so we can use it.
101 if (!refs.empty())
102 {
103 m_ref = refs.front();
104 m_dictionary = m_context.GetService(m_ref);
105 }
106 }
107
108 std::cout << "Enter a blank line to exit." << std::endl;
109
110 // Loop endlessly until the user enters a blank line
111 while (std::cin)
112 {
113 // Ask the user to enter a word.
114 std::cout << "Enter word: ";
115
116 std::string word;
117 std::getline(std::cin, word);
118
119 // If the user entered a blank line, then
120 // exit the loop.
121 if (word.empty())
122 {
123 break;
124 }
125 // If there is no dictionary, then say so.
126 else if (m_dictionary == nullptr)
127 {
128 std::cout << "No dictionary available." << std::endl;
129 }
130 // Otherwise print whether the word is correct or not.
131 else if (m_dictionary->CheckWord(word))
132 {

Callers

nothing calls this directly

Calls 5

frontMethod · 0.80
AddServiceListenerMethod · 0.45
emptyMethod · 0.45
GetServiceMethod · 0.45
CheckWordMethod · 0.45

Tested by

no test coverage detected