Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_model_location.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework Model Location
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 
15 #include "afw_internal.h"
16 
17 
18 
19 /* Get object type and model objects callback. */
20 static afw_boolean_t
21 impl_get_model_object_types_cb(
22  const afw_object_t *object,
23  void *context,
24  afw_xctx_t *xctx)
25 {
26  const afw_adaptor_t *model_location_adaptor;
27  afw_model_location_t *model_location;
28  const afw_pool_t *p;
29  afw_model_t *model;
30 
31  if (!object) {
32  AFW_THROW_ERROR_Z(general, "Map not found", xctx);
33  }
34 
35  model_location_adaptor = context;
36  p = model_location_adaptor->p;
37 
38  /* Compile model and add to object types associative array. */
39  model = afw_model_compile(&model_location_adaptor->adaptor_id,
40  object, p, xctx);
41 
42  /* Add model to loaded models. */
43  model_location = (afw_model_location_t *)
44  model_location_adaptor->impl->model_location;
45  model->next_model = model_location->first_model;
46  model_location->first_model = model;
47 
48  /* Return indicating not to short circuit */
49  return false;
50 }
51 
52 
53 
54 static const afw_model_t *
55 impl_load_model(
56  const afw_adaptor_t *model_location_adaptor,
57  const afw_utf8_t *model_id,
58  afw_xctx_t *xctx)
59 {
60  const afw_adaptor_session_t *session;
61 
63  model_location_adaptor, xctx);
64  AFW_TRY {
65 
66  afw_adaptor_session_get_object(session, NULL,
67  &afw_s__AdaptiveModel_, model_id,
68  (void *)model_location_adaptor,
69  impl_get_model_object_types_cb, NULL,
70  model_location_adaptor->p, xctx);
71 
72  }
73 
75  afw_adaptor_session_destroy(session, xctx);
76  }
77 
78  AFW_ENDTRY;
79 
80  return model_location_adaptor->impl->model_location->first_model;
81 }
82 
83 
84 /* Create model location. */
86 afw_model_location_create(
87  const afw_adaptor_t *adaptor,
88  const afw_pool_t *p, afw_xctx_t *xctx)
89 {
90  afw_model_location_t *model_location;
91  apr_status_t rv;
92 
93  model_location = afw_pool_calloc_type(p, afw_model_location_t, xctx);
94  model_location->model_location_adaptor = adaptor;
95  rv = apr_thread_mutex_create(&model_location->mutex,
96  APR_THREAD_MUTEX_UNNESTED, afw_pool_get_apr_pool(p));
97  if (rv != APR_SUCCESS) {
98  AFW_THROW_ERROR_RV_Z(general, apr, rv,
99  "apr_thread_mutex_create() failed",
100  xctx);
101  }
102 
103  return model_location;
104 }
105 
106 
107 AFW_DEFINE(const afw_model_t *)
108 afw_model_location_get_model(
109  const afw_adaptor_t *model_location_adaptor,
110  const afw_utf8_t *model_id,
111  afw_xctx_t *xctx)
112 {
113  const afw_model_t *model;
114  afw_model_location_t *model_location;
115 
116  model = NULL;
117  model_location = NULL;
118 
119  if (model_location_adaptor->impl) {
120  model_location = (afw_model_location_t *)
121  model_location_adaptor->impl->model_location;
122  }
123 
124  if (model_location) {
125 
126  AFW_THREAD_MUTEX_LOCK(model_location->mutex, xctx) {
127  for (model = (afw_model_t *)model_location->first_model;
128  model && !afw_utf8_equal(model_id, model->model_id);
129  model = (afw_model_t *)model->next_model);
130 
131  if (!model) {
132  model = impl_load_model(model_location_adaptor, model_id,
133  xctx);
134  }
135  }
137  }
138 
139  return model;
140 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
#define afw_adaptor_create_adaptor_session(instance, xctx)
Call method create_adaptor_session of interface afw_adaptor.
#define afw_adaptor_session_destroy(instance, xctx)
Call method destroy of interface afw_adaptor_session.
#define afw_adaptor_session_get_object(instance, impl_request, object_type_id, object_id, context, callback, adaptor_type_specific, p, xctx)
Call method get_object of interface afw_adaptor_session.
_Bool afw_boolean_t
Definition: afw_common.h:373
struct afw_model_internal_s afw_model_t
#define AFW_FINALLY
Always executed regardless of error.
Definition: afw_error.h:702
#define AFW_THROW_ERROR_RV_Z(code, rv_source_id, rv, message_z, xctx)
Macro used to set error and rv in xctx and throw it.
Definition: afw_error.h:301
#define AFW_ENDTRY
Ends an AFW try block.
Definition: afw_error.h:727
#define AFW_TRY
Begin an AFW TRY block.
Definition: afw_error.h:634
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
afw_model_t * afw_model_compile(const afw_utf8_t *adaptor_id, const afw_object_t *model_object, const afw_pool_t *p, afw_xctx_t *xctx)
Compile a model and add object types to associative array.
#define afw_pool_get_apr_pool(instance)
Call method get_apr_pool of interface afw_pool.
#define afw_pool_calloc_type(instance, type, xctx)
Macro to allocate cleared memory to hold type in pool.
Definition: afw_pool.h:167
#define AFW_THREAD_MUTEX_UNLOCK()
Macro to end a mutex lock.
Definition: afw_thread.h:151
#define AFW_THREAD_MUTEX_LOCK(mutex, xctx)
Macro to begin a mutex lock section.
Definition: afw_thread.h:137
afw_boolean_t afw_utf8_equal(const afw_utf8_t *s1, const afw_utf8_t *s2)
Check to see if a string equals another string.
const afw_model_location_t * model_location
Model location struct if this adaptor is a model location.
Interface afw_adaptor public struct.
Interface afw_adaptor_session public struct.
Struct used by adaptor referenced by modelLocationAdaptorId.
const afw_model_t *AFW_ATOMIC first_model
First loaded model.
afw_thread_mutex_t * mutex
Mutex for locking access to this struct.
const afw_adaptor_t * model_location_adaptor
Model location adaptor.
Interface afw_object public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_xctx public struct.