custom for >5 resources
Fork of mbedConnectorInterface by
api/Resource.h@57:ef0fdadde9eb, 2015-05-20 (annotated)
- Committer:
- michaeljkoster
- Date:
- Wed May 20 00:46:01 2015 +0000
- Revision:
- 57:ef0fdadde9eb
- Parent:
- 48:4b9ee3e32f93
10 resources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:b438482ebbfc | 1 | /** |
ansond | 0:b438482ebbfc | 2 | * @file Resource.h |
ansond | 0:b438482ebbfc | 3 | * @brief mbed CoAP Endpoint Resource base class template |
ansond | 0:b438482ebbfc | 4 | * @author Doug Anson/Chris Paola |
ansond | 0:b438482ebbfc | 5 | * @version 1.0 |
sam_grove | 2:853f9ecc12df | 6 | * @see |
ansond | 0:b438482ebbfc | 7 | * |
ansond | 0:b438482ebbfc | 8 | * Copyright (c) 2014 |
ansond | 0:b438482ebbfc | 9 | * |
ansond | 0:b438482ebbfc | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 0:b438482ebbfc | 11 | * you may not use this file except in compliance with the License. |
ansond | 0:b438482ebbfc | 12 | * You may obtain a copy of the License at |
ansond | 0:b438482ebbfc | 13 | * |
ansond | 0:b438482ebbfc | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 0:b438482ebbfc | 15 | * |
ansond | 0:b438482ebbfc | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 0:b438482ebbfc | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 0:b438482ebbfc | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 0:b438482ebbfc | 19 | * See the License for the specific language governing permissions and |
ansond | 0:b438482ebbfc | 20 | * limitations under the License. |
ansond | 0:b438482ebbfc | 21 | */ |
sam_grove | 2:853f9ecc12df | 22 | |
sam_grove | 2:853f9ecc12df | 23 | #ifndef __RESOURCE_H__ |
sam_grove | 2:853f9ecc12df | 24 | #define __RESOURCE_H__ |
sam_grove | 2:853f9ecc12df | 25 | |
sam_grove | 2:853f9ecc12df | 26 | // logging facility |
sam_grove | 2:853f9ecc12df | 27 | #include "Logger.h" |
sam_grove | 2:853f9ecc12df | 28 | |
sam_grove | 2:853f9ecc12df | 29 | // string support |
sam_grove | 2:853f9ecc12df | 30 | #include <string> |
sam_grove | 2:853f9ecc12df | 31 | |
sam_grove | 2:853f9ecc12df | 32 | /** Resource class |
sam_grove | 2:853f9ecc12df | 33 | */ |
sam_grove | 2:853f9ecc12df | 34 | template <typename InnerType> class Resource |
sam_grove | 2:853f9ecc12df | 35 | { |
sam_grove | 2:853f9ecc12df | 36 | public: |
sam_grove | 2:853f9ecc12df | 37 | /** |
sam_grove | 2:853f9ecc12df | 38 | Default constructor |
sam_grove | 2:853f9ecc12df | 39 | @param logger input the Logger instance this Resource is a part of |
sam_grove | 2:853f9ecc12df | 40 | @param name input the Resource URI/Name |
sam_grove | 2:853f9ecc12df | 41 | @param value input the Resource value |
sam_grove | 2:853f9ecc12df | 42 | */ |
sam_grove | 2:853f9ecc12df | 43 | Resource(const Logger *logger,const string name,InnerType value) { |
sam_grove | 2:853f9ecc12df | 44 | this->init(logger); |
sam_grove | 2:853f9ecc12df | 45 | this->m_name = name; |
sam_grove | 2:853f9ecc12df | 46 | this->m_value = value; |
ansond | 46:cc6076ac5026 | 47 | this->m_implements_observation = false; |
sam_grove | 2:853f9ecc12df | 48 | } |
sam_grove | 2:853f9ecc12df | 49 | |
sam_grove | 2:853f9ecc12df | 50 | /** |
sam_grove | 2:853f9ecc12df | 51 | Copy constructor |
sam_grove | 2:853f9ecc12df | 52 | @param resource input the Resource that is to be deep copied |
sam_grove | 2:853f9ecc12df | 53 | */ |
sam_grove | 2:853f9ecc12df | 54 | Resource(const Resource<InnerType> &resource) { |
sam_grove | 2:853f9ecc12df | 55 | this->init(resource.m_logger); |
sam_grove | 2:853f9ecc12df | 56 | this->m_endpoint = resource.m_endpoint; |
sam_grove | 2:853f9ecc12df | 57 | this->m_name = resource.m_name; |
sam_grove | 2:853f9ecc12df | 58 | this->m_value = resource.m_value; |
ansond | 46:cc6076ac5026 | 59 | this->m_implements_observation = resource.m_implements_observation; |
sam_grove | 2:853f9ecc12df | 60 | } |
sam_grove | 2:853f9ecc12df | 61 | |
sam_grove | 2:853f9ecc12df | 62 | /** |
sam_grove | 2:853f9ecc12df | 63 | Destructor |
sam_grove | 2:853f9ecc12df | 64 | */ |
sam_grove | 2:853f9ecc12df | 65 | virtual ~Resource() { |
sam_grove | 2:853f9ecc12df | 66 | } |
ansond | 0:b438482ebbfc | 67 | |
sam_grove | 2:853f9ecc12df | 68 | /** |
sam_grove | 2:853f9ecc12df | 69 | Get the resource name |
sam_grove | 2:853f9ecc12df | 70 | @return the name of the resource |
sam_grove | 2:853f9ecc12df | 71 | */ |
sam_grove | 2:853f9ecc12df | 72 | string getName() { |
sam_grove | 2:853f9ecc12df | 73 | return this->m_name; |
sam_grove | 2:853f9ecc12df | 74 | } |
sam_grove | 2:853f9ecc12df | 75 | |
sam_grove | 2:853f9ecc12df | 76 | /** |
sam_grove | 2:853f9ecc12df | 77 | Get the resource value |
sam_grove | 2:853f9ecc12df | 78 | @return the value of the resource |
sam_grove | 2:853f9ecc12df | 79 | */ |
sam_grove | 2:853f9ecc12df | 80 | InnerType getValue() { |
sam_grove | 2:853f9ecc12df | 81 | return this->m_value; |
sam_grove | 2:853f9ecc12df | 82 | } |
sam_grove | 2:853f9ecc12df | 83 | |
sam_grove | 2:853f9ecc12df | 84 | /** |
sam_grove | 2:853f9ecc12df | 85 | Set the resource name |
sam_grove | 2:853f9ecc12df | 86 | @param name input the resource name |
sam_grove | 2:853f9ecc12df | 87 | */ |
sam_grove | 2:853f9ecc12df | 88 | void setName(const string name) { |
sam_grove | 2:853f9ecc12df | 89 | this->m_name = name; |
sam_grove | 2:853f9ecc12df | 90 | } |
ansond | 0:b438482ebbfc | 91 | |
sam_grove | 2:853f9ecc12df | 92 | /** |
sam_grove | 2:853f9ecc12df | 93 | Set the resource value |
sam_grove | 2:853f9ecc12df | 94 | @param value input the resource value |
sam_grove | 2:853f9ecc12df | 95 | */ |
sam_grove | 2:853f9ecc12df | 96 | void setValue(const InnerType value) { |
sam_grove | 2:853f9ecc12df | 97 | this->m_value = value; |
sam_grove | 2:853f9ecc12df | 98 | } |
sam_grove | 2:853f9ecc12df | 99 | |
sam_grove | 2:853f9ecc12df | 100 | /** |
sam_grove | 2:853f9ecc12df | 101 | Bind resource to endpoint |
sam_grove | 2:853f9ecc12df | 102 | */ |
sam_grove | 2:853f9ecc12df | 103 | virtual void bind(void *p) = 0; |
sam_grove | 2:853f9ecc12df | 104 | |
sam_grove | 2:853f9ecc12df | 105 | // access the logger() |
sam_grove | 2:853f9ecc12df | 106 | Logger *logger() { |
sam_grove | 2:853f9ecc12df | 107 | return this->m_logger; |
sam_grove | 2:853f9ecc12df | 108 | } |
ansond | 46:cc6076ac5026 | 109 | |
ansond | 48:4b9ee3e32f93 | 110 | /** |
ansond | 48:4b9ee3e32f93 | 111 | set the options |
ansond | 48:4b9ee3e32f93 | 112 | */ |
ansond | 48:4b9ee3e32f93 | 113 | void setOptions(const void *options) { |
ansond | 48:4b9ee3e32f93 | 114 | this->m_options = (void *)options; |
ansond | 48:4b9ee3e32f93 | 115 | } |
ansond | 48:4b9ee3e32f93 | 116 | |
ansond | 46:cc6076ac5026 | 117 | // this resource implements its own observation handler |
ansond | 46:cc6076ac5026 | 118 | bool implementsObservation() { return this->m_implements_observation; } |
sam_grove | 2:853f9ecc12df | 119 | |
sam_grove | 2:853f9ecc12df | 120 | protected: |
sam_grove | 2:853f9ecc12df | 121 | // initialize internals to Resource |
sam_grove | 2:853f9ecc12df | 122 | void init(const Logger *logger) { |
sam_grove | 2:853f9ecc12df | 123 | this->m_logger = (Logger *)logger; |
sam_grove | 2:853f9ecc12df | 124 | this->m_endpoint = NULL; |
sam_grove | 2:853f9ecc12df | 125 | this->m_name = ""; |
sam_grove | 2:853f9ecc12df | 126 | this->m_value = ""; |
sam_grove | 2:853f9ecc12df | 127 | } |
ansond | 46:cc6076ac5026 | 128 | |
ansond | 48:4b9ee3e32f93 | 129 | // get our options |
ansond | 48:4b9ee3e32f93 | 130 | void *getOptions() { |
ansond | 48:4b9ee3e32f93 | 131 | return this->m_options; |
ansond | 48:4b9ee3e32f93 | 132 | } |
ansond | 48:4b9ee3e32f93 | 133 | |
sam_grove | 2:853f9ecc12df | 134 | Logger *m_logger; |
sam_grove | 2:853f9ecc12df | 135 | void *m_endpoint; |
sam_grove | 2:853f9ecc12df | 136 | string m_name; |
sam_grove | 2:853f9ecc12df | 137 | InnerType m_value; |
ansond | 46:cc6076ac5026 | 138 | bool m_implements_observation; |
ansond | 48:4b9ee3e32f93 | 139 | void *m_options; |
sam_grove | 2:853f9ecc12df | 140 | }; |
sam_grove | 2:853f9ecc12df | 141 | |
sam_grove | 2:853f9ecc12df | 142 | #endif // __RESOURCE_H__ |