OpFlex Framework  1.3.0
MO.h
Go to the documentation of this file.
1 /* -*- C++ -*-; c-basic-offset: 4; indent-tabs-mode: nil */
6 /*
7  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
8  *
9  * This program and the accompanying materials are made available under the
10  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
11  * and is available at http://www.eclipse.org/legal/epl-v10.html
12  */
13 
14 #ifndef OPFLEX_CORE_MO_H
15 #define OPFLEX_CORE_MO_H
16 
17 #include <string>
18 
19 #include <boost/make_shared.hpp>
20 #include <boost/ref.hpp>
21 #include <boost/optional.hpp>
22 #include <boost/shared_ptr.hpp>
23 #include <boost/noncopyable.hpp>
24 
28 
29 namespace opflex {
30 namespace modb {
31 namespace mointernal {
32 
49 class MO : private boost::noncopyable {
50 public:
54  virtual ~MO();
55 
64  class_id_t getClassId() const;
65 
72  const URI& getURI() const;
73 
74 protected:
82  MO(class_id_t class_id,
83  const URI& uri,
84  const boost::shared_ptr<const ObjectInstance>& oi);
85 
94  MO(ofcore::OFFramework& framework,
95  class_id_t class_id,
96  const URI& uri,
97  const boost::shared_ptr<const ObjectInstance>& oi);
98 
105 
111  const ObjectInstance& getObjectInstance() const;
112 
124  static boost::shared_ptr<const ObjectInstance>
125  resolveOI(ofcore::OFFramework& framework,
126  class_id_t class_id,
127  const URI& uri);
128 
132  static StoreClient& getStoreClient(ofcore::OFFramework& framework);
133 
140 
144  static void registerListener(ofcore::OFFramework& framework,
145  ObjectListener* listener,
146  class_id_t class_id);
147 
151  static void unregisterListener(ofcore::OFFramework& framework,
152  ObjectListener* listener,
153  class_id_t class_id);
154 
165  template <class T> static
166  boost::optional<boost::shared_ptr<T> >
168  class_id_t class_id,
169  const URI& uri) {
170  try {
171  return boost::make_shared<T>(boost::ref(framework), uri,
172  resolveOI(framework, class_id, uri));
173  } catch (std::out_of_range e) {
174  return boost::none;
175  }
176  }
177 
182  template <class T> static
184  class_id_t parent_class,
185  const URI& parent_uri,
186  prop_id_t parent_prop,
187  class_id_t child_class,
188  /* out */ std::vector<boost::shared_ptr<T> >& out) {
189  std::vector<URI> childURIs;
190  MO::getStoreClient(framework)
191  .getChildren(parent_class, parent_uri, parent_prop,
192  child_class, childURIs);
193  std::vector<URI>::const_iterator it;
194  for (it = childURIs.begin(); it != childURIs.end(); ++it) {
195  boost::optional<boost::shared_ptr<T> > child =
196  resolve<T>(framework, child_class, *it);
197  if (child) out.push_back(child.get());
198  }
199  }
200 
205  template <class T>
206  boost::shared_ptr<T> addChild(class_id_t parent_class,
207  const URI& parent_uri,
208  prop_id_t parent_prop,
209  class_id_t child_class,
210  const URI& child_uri) {
211  return boost::make_shared<T>(boost::ref(getFramework()),
212  child_uri,
213  getTLMutator().addChild(parent_class,
214  parent_uri,
215  parent_prop,
216  child_class,
217  child_uri));
218  }
219 
223  template <class T>
224  static boost::shared_ptr<T>
226  class_id_t class_id) {
227  return boost::make_shared<T>(boost::ref(framework),
228  URI::ROOT,
229  framework.getTLMutator()
230  .modify(class_id, URI::ROOT));
231  }
232 
236  static void remove(ofcore::OFFramework& framework,
237  class_id_t class_id,
238  const modb::URI& uri) {
239  framework.getTLMutator().remove(class_id, uri);
240  }
241 
242 private:
243  class MOImpl;
244  friend class MOImpl;
245  MOImpl* pimpl;
246 
247  friend bool operator==(const MO& lhs, const MO& rhs);
248  friend bool operator!=(const MO& lhs, const MO& rhs);
249 };
250 
254 bool operator==(const MO& lhs, const MO& rhs);
258 bool operator!=(const MO& lhs, const MO& rhs);
259 
260 } /* namespace mointernal */
261 } /* namespace modb */
262 } /* namespace opflex */
263 
264 #endif /* OPFLEX_CORE_MO_H */
A client for accessing the object store scoped to an owner.
Definition: StoreClient.h:39
bool operator==(const MAC &lhs, const MAC &rhs)
Check for MAC equality.
This is the base class for all managed objects, which are the primary point of interface with data st...
Definition: MO.h:49
static StoreClient & getStoreClient(ofcore::OFFramework &framework)
Get a read-only store client for the framework instance.
A mutator represents a set of changes to apply to the data store.
Definition: Mutator.h:60
MO(class_id_t class_id, const URI &uri, const boost::shared_ptr< const ObjectInstance > &oi)
Construct an MO.
class_id_t getClassId() const
Get the class ID associated with this managed object.
Interface definition file for MODB.
const URI & getURI() const
Get the URI associated with this managed object.
Interface for an object interested in updates to objects in the data store.
Definition: ObjectListener.h:41
Main interface to the OpFlex framework.
Definition: OFFramework.h:644
static const URI ROOT
Static root URI.
Definition: URI.h:82
friend bool operator==(const MO &lhs, const MO &rhs)
Check for MO equality.
const ObjectInstance & getObjectInstance() const
Get the raw object instance associated with this managed object.
static boost::optional< boost::shared_ptr< T > > resolve(ofcore::OFFramework &framework, class_id_t class_id, const URI &uri)
Resolve the specified URI to its managed object wrapper class, if it exists.
Definition: MO.h:167
void getChildren(class_id_t parent_class, const URI &parent_uri, prop_id_t parent_prop, class_id_t child_class, std::vector< URI > &output)
Get the children of the parent URI and property and put the result into the supplied vector...
static void registerListener(ofcore::OFFramework &framework, ObjectListener *listener, class_id_t class_id)
Register the listener for the specified class ID.
boost::shared_ptr< T > addChild(class_id_t parent_class, const URI &parent_uri, prop_id_t parent_prop, class_id_t child_class, const URI &child_uri)
Add a child of the specified type to the mutator and instantiate the correct wrapper class...
Definition: MO.h:206
static boost::shared_ptr< T > createRootElement(ofcore::OFFramework &framework, class_id_t class_id)
Add a root element of the given type to the framework.
Definition: MO.h:225
ofcore::OFFramework & getFramework() const
Get the framework instance associated with this managed object.
virtual ~MO()
Destroy an MO.
friend bool operator!=(const MO &lhs, const MO &rhs)
Check for MO inequality.
An internal instance of an object in the managed object store.
Definition: ObjectInstance.h:58
void remove(class_id_t class_id, const URI &uri)
Delete the child object specified along with its link to its parents.
boost::shared_ptr< mointernal::ObjectInstance > & addChild(class_id_t parent_class, const URI &parent_uri, prop_id_t parent_prop, class_id_t child_class, const URI &child_uri)
Create a new child object with the specified class and URI, and make it a child of the given parent...
Mutator & getTLMutator()
Get the currently-active mutator.
static void resolveChildren(ofcore::OFFramework &framework, class_id_t parent_class, const URI &parent_uri, prop_id_t parent_prop, class_id_t child_class, std::vector< boost::shared_ptr< T > > &out)
Resolve any children of the specified parent object to their managed object wrapper classes...
Definition: MO.h:183
uint64_t class_id_t
A unique class ID.
Definition: PropertyInfo.h:36
bool operator!=(const MAC &lhs, const MAC &rhs)
Check for MAC inequality.
boost::shared_ptr< mointernal::ObjectInstance > & modify(class_id_t class_id, const URI &uri)
Create a new mutable object with the given URI which is a copy of any existing object with the specif...
A URI is used to identify managed objects in the MODB.
Definition: URI.h:41
uint64_t prop_id_t
A unique property ID.
Definition: PropertyInfo.h:41
Interface definition file for OFFramework.
static boost::shared_ptr< const ObjectInstance > resolveOI(ofcore::OFFramework &framework, class_id_t class_id, const URI &uri)
Resolve the specified URI to the underlying object instance, if it exists.
static void unregisterListener(ofcore::OFFramework &framework, ObjectListener *listener, class_id_t class_id)
Unregister the listener for the specified class ID.
Interface definition file for ObjectListeners.