InFact
Interpreter and factory for easily creating C++ objects at run-time
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
example.h
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 //
39 
40 #ifndef INFACT_EXAMPLE_H_
41 #define INFACT_EXAMPLE_H_
42 
43 #include <string>
44 
45 #include "factory.h"
46 
47 namespace infact {
48 
50 class Date : public FactoryConstructible {
51  public:
53  Date() {}
55  virtual ~Date() {}
56 
58  virtual int year() const = 0;
60  virtual int month() const = 0;
62  virtual int day() const = 0;
63 };
64 
68 #define REGISTER_DATE(TYPE) \
69  REGISTER_NAMED(TYPE,TYPE,Date)
70 
75 class DateImpl : public Date {
76  public:
78  DateImpl() : Date() {}
80  virtual ~DateImpl() {}
81 
83  virtual void RegisterInitializers(Initializers &initializers) {
87  }
88 
90  virtual int year() const { return year_; }
92  virtual int month() const { return month_; }
94  virtual int day() const { return day_; }
95 
96  private:
97  int year_;
98  int month_;
99  int day_;
100 };
101 
103 class Person : public FactoryConstructible {
104  public:
106  Person() {}
108  virtual ~Person() {}
109 
111  virtual const string &name() const = 0;
113  virtual const int cm_height() const = 0;
115  virtual shared_ptr<const Date> birthday() const = 0;
116 };
117 
122 #define REGISTER_PERSON(TYPE) \
123  REGISTER_NAMED(TYPE,TYPE,Person)
124 
129 class PersonImpl : public Person {
130  public:
134  virtual ~PersonImpl() {}
135 
137  virtual void RegisterInitializers(Initializers &initializers) {
141  }
142 
144  virtual const string &name() const { return name_; }
146  virtual const int cm_height() const { return cm_height_; }
148  virtual shared_ptr<const Date> birthday() const { return birthday_; }
149 
150  private:
151  string name_;
152  int cm_height_;
153  shared_ptr<Date> birthday_;
154 };
155 
157 class Animal : public FactoryConstructible {
158  public:
160  Animal() {}
162  virtual ~Animal() {}
163 
165  virtual const string &name() const = 0;
166 
168  virtual int age() const = 0;
169 };
170 
175 #define REGISTER_ANIMAL(TYPE) \
176  REGISTER_NAMED(TYPE,TYPE,Animal)
177 
179 class Cow : public Animal {
180 public:
182  Cow() : Animal() {
183  age_ = 2; // default age, since age is optional
184  }
185 
186  // Destroys this instance.
187  virtual ~Cow() {}
188 
189  virtual void RegisterInitializers(Initializers &initializers) {
192  }
193 
195  virtual const string &name() const { return name_; }
196  virtual int age() const { return age_; }
197 private:
198  string name_;
199  int age_;
200 };
201 
207 class Sheep : public Animal {
208  public:
210  Sheep() : Animal() {}
212  virtual ~Sheep() {}
213 
218  virtual void RegisterInitializers(Initializers &initializers) {
221  // We can pass in a nullptr, if we don't want to directly
222  // initialize a data member of this class, but instead want to
223  // simply grab something from the Environment after this method
224  // has been invoked; see the PostInit method declared below
225  // (defined in example.cc).
226  //
227  // As of v1.0.6, the new INFACT_ADD_TEMPORARY macro makes it very
228  // easy to add such temporary variables in a very readable way.
230  }
231 
236  virtual void PostInit(const Environment *env, const string &init_str);
237 
239  virtual const string &name() const { return name_; }
241  virtual int age() const {
242  return -1; // Always returns -1, because sheep are timeless.
243  }
245  const vector<int> &counts() { return counts_; }
246  private:
247  string name_;
248  int age_;
249  // The various times people have counted this sheep when falling asleep.
250  vector<int> counts_;
251 };
252 
255  public:
257  PetOwner() {}
258 
260  virtual ~PetOwner() {}
261 
263  virtual int GetNumberOfPets() = 0;
264 
266  virtual shared_ptr<Animal> GetPet(int i) = 0;
267 };
268 
273 #define REGISTER_PET_OWNER(TYPE) \
274  REGISTER_NAMED(TYPE,TYPE,PetOwner)
275 
278 class HumanPetOwner : public PetOwner {
279  public:
282 
284  virtual ~HumanPetOwner() {}
285 
287  virtual void RegisterInitializers(Initializers &initializers) {
288  initializers.Add("pets", &pets_, true);
289  }
290 
292  virtual int GetNumberOfPets() {
293  return pets_.size();
294  }
295 
297  virtual shared_ptr<Animal> GetPet(int i) {
298  return pets_.at(i);
299  }
300 
301  private:
302  vector<shared_ptr<Animal> > pets_;
303 };
304 
305 } // namespace infact
306 
307 #endif
virtual shared_ptr< const Date > birthday() const =0
Returns the birthday of this person.
virtual void RegisterInitializers(Initializers &initializers)
Registers a single required initializer.
Definition: example.h:287
virtual int age() const
Returns the age of this animal.
Definition: example.h:196
virtual int GetNumberOfPets()
Returns the number of pets owned by this pet owner.
Definition: example.h:292
virtual ~PersonImpl()
Destroys this person.
Definition: example.h:134
virtual ~PetOwner()
Destroys this pet owner.
Definition: example.h:260
DateImpl()
Constructs this instance.
Definition: example.h:78
virtual int age() const
Returns the age of this animal.
Definition: example.h:241
virtual const string & name() const
Returns the name of this animal.
Definition: example.h:239
virtual ~Cow()
Definition: example.h:187
Sheep()
Constructs a sheep.
Definition: example.h:210
#define INFACT_ADD_PARAM_(param)
A macro to make it easy to register a parameter for initialization inside a RegisterInitializers impl...
Definition: factory.h:75
virtual int age() const =0
Returns the age of this animal.
virtual int month() const =0
Returns the month.
virtual ~HumanPetOwner()
Destroys a human pet owner.
Definition: example.h:284
HumanPetOwner()
Constructs a human pet owner.
Definition: example.h:281
virtual int GetNumberOfPets()=0
Returns the number of pets owned by this pet owner.
virtual int year() const
Returns the year.
Definition: example.h:90
virtual const string & name() const =0
Returns the name of this person.
PetOwner()
Constructs a generic pet owner.
Definition: example.h:257
A very simple class to represent an animal.
Definition: example.h:157
Animal()
Constructs a generic animal.
Definition: example.h:160
virtual void PostInit(const Environment *env, const string &init_str)
Grabs the variable named "age" from the Environment (set up by RegisterInitializers) and sets this sh...
Definition: example.cc:59
An implementation of the Date interface that can be constructed by a Factory (because of the REGISTER...
Definition: example.h:75
virtual const int cm_height() const
Returns the height in centimeters of this person.
Definition: example.h:146
Cow()
Constructs a cow.
Definition: example.h:182
A sheep.
Definition: example.h:207
virtual shared_ptr< const Date > birthday() const
Returns the birthday of this person.
Definition: example.h:148
virtual int day() const
Returns the day.
Definition: example.h:94
Date()
Constructs this date.
Definition: example.h:53
virtual int day() const =0
Returns the day.
virtual int year() const =0
Returns the year.
An owner of a pet.
Definition: example.h:254
An interface to represent a date.
Definition: example.h:50
A container for all the member initializers for a particular Factory-constructible instance...
Definition: factory.h:337
PersonImpl()
Constructs this person.
Definition: example.h:132
virtual void RegisterInitializers(Initializers &initializers)
Registers one required and two optional initializers.
Definition: example.h:137
An interface representing a person.
Definition: example.h:103
Provides a generic dynamic object factory.
virtual void RegisterInitializers(Initializers &initializers)
Registers one required and two optional inititalizers.
Definition: example.h:218
virtual ~Animal()
Destroys this animal.
Definition: example.h:162
const vector< int > & counts()
Returns the counts of this sheep.
Definition: example.h:245
virtual ~DateImpl()
Destroys this instance.
Definition: example.h:80
An interface for an environment in which variables of various types are mapped to their values...
Definition: environment.h:126
virtual ~Date()
Destroys this date.
Definition: example.h:55
virtual ~Person()
Destroys this abstract person.
Definition: example.h:108
virtual const string & name() const
Returns the name of this animal.
Definition: example.h:195
Person()
Constructs this abstract person.
Definition: example.h:106
virtual int month() const
Returns the month.
Definition: example.h:92
A class to represent a cow.
Definition: example.h:179
An interface simply to make it easier to implement Factory-constructible types by implementing both r...
Definition: factory.h:549
virtual shared_ptr< Animal > GetPet(int i)
Gets the pet with the specified index owned by this pet owner.
Definition: example.h:297
virtual const string & name() const
Returns the name of this person.
Definition: example.h:144
A concrete implementation of the Person interface that can be constructed by a Factory (because of th...
Definition: example.h:129
virtual shared_ptr< Animal > GetPet(int i)=0
Gets the pet with the specified index owned by this pet owner.
#define INFACT_ADD_TEMPORARY(type, var)
A macro to make it easier to register a temporary variable inside a RegisterInitializers implementati...
Definition: factory.h:93
void Add(const string &name, T *member, bool required=false)
This method is the raison d'etre of this class: a method to make it easy to add all supported types o...
Definition: factory.h:376
virtual void RegisterInitializers(Initializers &initializers)
Registers three required initializers.
Definition: example.h:83
virtual void RegisterInitializers(Initializers &initializers)
Registers data members of this class for initialization when an instance is constructed via the Facto...
Definition: example.h:189
virtual const string & name() const =0
Returns the name of this animal.
virtual const int cm_height() const =0
Returns the height in centimeters of this person.
A concrete type of PetOwner that can be constructed by a Factory<PetOwner> instance.
Definition: example.h:278
#define INFACT_ADD_REQUIRED_PARAM_(param)
Identical to INFACT_ADD_PARAM_ but for a required parameter.
Definition: factory.h:83
virtual ~Sheep()
Destroys a sheep.
Definition: example.h:212