xref: /AOO41X/main/odk/examples/cpp/counter/countermain.cxx (revision 27b2fc91b67b282ef25e5c8fc07f05afd8a62640)
1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 /*************************************************************************
36  *************************************************************************
37  *
38  * simple client application registering and using the counter component.
39  *
40  *************************************************************************
41  *************************************************************************/
42 
43 #include <stdio.h>
44 
45 #include <sal/main.h>
46 #include <rtl/ustring.hxx>
47 
48 #include <osl/diagnose.h>
49 
50 #include <cppuhelper/bootstrap.hxx>
51 
52 // generated c++ interfaces
53 #include <com/sun/star/lang/XComponent.hpp>
54 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
55 #include <com/sun/star/registry/XImplementationRegistration.hpp>
56 #include <foo/XCountable.hpp>
57 
58 
59 using namespace foo;
60 using namespace cppu;
61 using namespace com::sun::star::uno;
62 using namespace com::sun::star::lang;
63 using namespace com::sun::star::registry;
64 
65 using namespace ::rtl;
66 
67 
68 //=======================================================================
69 SAL_IMPLEMENT_MAIN()
70 {
71     try {
72 
73 		Reference< XComponentContext > xContext(::cppu::defaultBootstrap_InitialComponentContext());
74 		OSL_ENSURE( xContext.is(), "### bootstrap failed!\n" );
75 
76 		Reference< XMultiComponentFactory > xMgr = xContext->getServiceManager();
77 		OSL_ENSURE( xMgr.is(), "### cannot get initial service manager!" );
78 
79 		Reference< XInterface > xx = xMgr->createInstanceWithContext(
80 			OUString::createFromAscii("foo.Counter"), xContext);
81 
82 		OSL_ENSURE( xx.is(), "### cannot get service instance of \"foo.Counter\"!" );
83 
84 		Reference< XCountable > xCount( xx, UNO_QUERY );
85 		OSL_ENSURE( xCount.is(), "### cannot query XCountable interface of service instance \"foo.Counter\"!" );
86 
87 		if (xCount.is())
88 		{
89 			 xCount->setCount( 42 );
90 			 fprintf( stdout , "%d," , (int)xCount->getCount() );
91 			 fprintf( stdout , "%d," , (int)xCount->increment() );
92 			 fprintf( stdout , "%d\n" , (int)xCount->decrement() );
93 		}
94 
95 		Reference< XComponent >::query( xContext )->dispose();
96 
97 	} catch( Exception& e) {
98 	    printf("Error: caught exception:\n       %s\n",
99                OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US).getStr());
100 		exit(1);
101 	}
102 
103 	return 0;
104 }
105