COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
tut1.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
12 
13 #include "common/Log.hpp"
14 #include "common/Component.hpp"
15 #include "common/Group.hpp"
16 #include "common/Link.hpp"
18 
19 using namespace boost;
20 using namespace cf3;
21 using namespace cf3::common;
22 
24 
25 int main(int argc, char * argv[])
26 {
27  // Create a main component to become a root.
28  // Note that root has to be a shared_ptr, as it is not owned.
29  shared_ptr<Group> world = allocate_component<Group>("World");
30 
31 
32 
33  // Create a few continents and countries
34  // Note that the created components are stored in Handle,
35  // as it is a non-owning pointer. The ownership is inside the parent component.
36  Handle<Group> europe = world->create_component<Group>("Europe");
37  Handle<Group> africa = world->create_component<Group>("Africa");
38  Handle<Group> america = world->create_component<Group>("America");
39  Handle<Group> asia = world->create_component<Group>("Asia");
40 
41  Handle<Group> belgium = europe->create_component<Group>("Belgium");
42  Handle<Group> uk = europe->create_component<Group>("United_Kingdom");
43 
44  Handle<Group> canada = america->create_component<Group>("Canada");
45  Handle<Group> usa = america->create_component<Group>("USA");
46 
47  Handle<Group> india = asia->create_component<Group>("India");
48  Handle<Group> thailand = asia->create_component<Group>("Thailand");
49  Handle<Group> china = asia->create_component<Group>("China");
50 
51  Handle<Group> south_africa = africa->create_component<Group>("South_Africa");
52 
53 
54 
55  // Create a link pointing to a country
56  Handle<Link> current_country = world->create_component<Link>("Current_Country");
57  current_country->link_to(*belgium);
58  CFinfo << "Current country : " << current_country->follow()->uri() << CFendl;
59 
60 
61 
62  // Mistakenly add Germany to Africa
63  Handle<Group> germany = africa->create_component<Group>("Germany");
64  CFinfo << "Germany's wrong path : " << germany->uri() << CFendl;
65 
66  // Correctly add Germany to Europe
67  germany->move_to(*europe);
68  CFinfo << "Germany's correct path: " << germany->uri() << CFendl;
69 
70 
71 
72  // Check if the handle to Thailand valid
73  CFinfo << "Is Thailand there? --> " << is_not_null(thailand) << CFendl;
74 
75  // Remove Thailand from the structure
76  asia->remove_component("Thailand");
77  CFinfo << "Is Thailand there? --> " << is_not_null(thailand) << CFendl;
78 
79 
80 
81  // Print the whole data structure. Useful to visualize the structure, as
82  // a file-system analogy
83  CFinfo << world->tree() << CFendl;
84 
85 
86 
87  // Access United Kingdom by its path
88  Handle<Component> a_country = world->access_component("cpath:/Europe/United_Kingdom");
89  CFinfo << "Accessed country " << a_country->name() << CFendl;
90 
91  // Find Canada in the structure
92  Handle<Group> found_canada = find_component_ptr_recursively_with_name<Group>(*world,"Canada");
93  CFinfo << "Found canada at " << found_canada->uri() << CFendl;
94 
95  return 0;
96 }
97 
98 /* OUTPUT:
99 
100 Current country : cpath:/Europe/Belgium
101 Germany's wrong path : cpath:/Africa/Germany
102 Germany's correct path: cpath:/Europe/Germany
103 Is Thailand there? --> 1
104 Is Thailand there? --> 0
105 World
106  Europe
107  Belgium
108  United_Kingdom
109  Germany
110  Africa
111  South_Africa
112  America
113  Canada
114  USA
115  Asia
116  India
117  China
118  Current_Country -> cpath:/Europe/Belgium
119 
120 Accessed country United_Kingdom
121 Found canada at cpath:/America/Canada
122 */
#define CFinfo
these are always defined
Definition: Log.hpp:104
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
external boost library namespace
void move_to(Component &to_parent)
Definition: Component.cpp:477
URI uri() const
Construct the full path.
Definition: Component.cpp:248
const std::string & name() const
Access the name of the component.
Definition: Component.hpp:146
#define CFendl
Definition: Log.hpp:109
int main(int argc, char *argv[])
Definition: tut1.cpp:25
Holds the Component class, as well as the ComponentIterator class plus some functions related to comp...
Component for grouping other components.
Definition: Group.hpp:25
Top-level namespace for coolfluid.
Definition: Action.cpp:18
Most basic kernel library.
Definition: Action.cpp:19
bool is_not_null(T ptr)
predicate for comparison to nullptr
Definition: CF.hpp:147
Send comments to:
COOLFluiD Web Admin