InFact
Interpreter and factory for easily creating C++ objects at run-time
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
example.cc
Go to the documentation of this file.
1 // Copyright 2014, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
34 
35 #include "example.h"
36 #include "environment-impl.h"
37 
38 namespace infact {
39 
40 // Normally, the various IMPLEMENT_FACTORY declarations would be
41 // separate from the various REGISTER_* declarations, which would
42 // usually appear in the separate concrete implementations' .C files.
43 // For compactness, we have this single .C file, so everything is
44 // lumped together.
45 
48 
51 
55 
58 
59 void Sheep::PostInit(const Environment *env, const string &init_str) {
60  int env_age;
61  // Note how we need to cast Environment down to EnvironmentImpl,
62  // because only the implementation has the templated \link
63  // EnvironmentImpl::Get \endlink method, because only an
64  // implementation can be aware of the all the \link
65  // Factory\endlink-constructible types.
66  const EnvironmentImpl *env_impl = dynamic_cast<const EnvironmentImpl *>(env);
67  if (env_impl != nullptr &&
68  env_impl->Get("age", &env_age)) {
69  age_ = env_age * 2;
70  }
71 }
72 
73 } // namespace infact
#define REGISTER_DATE(TYPE)
The macro by which Date implementations may register themselves with the Factory that can construct...
Definition: example.h:68
Provides various example class headers.
#define IMPLEMENT_FACTORY(BASE)
Provides the necessary implementation for a factory for the specified BASE class type.
Definition: factory.h:1051
bool Get(const string &varname, T *value) const
Retrieves the value of the variable with the specified name and puts into into the object pointed to ...
A very simple class to represent an animal.
Definition: example.h:157
An implementation of the Date interface that can be constructed by a Factory (because of the REGISTER...
Definition: example.h:75
A sheep.
Definition: example.h:207
#define REGISTER_ANIMAL(TYPE)
Registers the Animal with the specified subtype TYPE and NAME with the Animal Factory.
Definition: example.h:175
An owner of a pet.
Definition: example.h:254
#define REGISTER_PET_OWNER(TYPE)
The macro by which PetOwner implementations may register themselves with the Factory that can const...
Definition: example.h:273
An interface representing a person.
Definition: example.h:103
#define REGISTER_PERSON(TYPE)
The macro by which Person implementations may register themselves with the Factory that can constru...
Definition: example.h:122
An interface for an environment in which variables of various types are mapped to their values...
Definition: environment.h:126
A class to represent a cow.
Definition: example.h:179
Provides an environment for variables and their values, either primitive or Factory-constructible obj...
A concrete implementation of the Person interface that can be constructed by a Factory (because of th...
Definition: example.h:129
Provides a set of named variables and their types, as well as the values for those variables...
A concrete type of PetOwner that can be constructed by a Factory<PetOwner> instance.
Definition: example.h:278