Parametrisable.h revision 10428
1/* 2 * Copyright (c) 2012-2014, TU Delft 3 * Copyright (c) 2012-2014, TU Eindhoven 4 * Copyright (c) 2012-2014, TU Kaiserslautern 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * 3. Neither the name of the copyright holder nor the names of its 19 * contributors may be used to endorse or promote products derived from 20 * this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * Authors: Andreas Hansson 35 * 36 */ 37 38#ifndef DATA_PARAMETRISABLE_H 39#define DATA_PARAMETRISABLE_H 40 41#include <string> 42#include <vector> 43 44#include "Parameter.h" 45 46namespace Data { 47/** 48 * Convenience class for the architectural components that are 49 * parametrisable. The interface is shared and implemented only in 50 * this class. 51 */ 52class Parametrisable { 53 public: 54 Parametrisable() 55 { 56 } 57 58 virtual ~Parametrisable() 59 { 60 } 61 62 /** 63 * Push a new parameter into the in-order vector without checking 64 * for duplicates. 65 */ 66 virtual void pushParameter(const Parameter& parameter); 67 68 /** 69 * Set a parameter with a given index (default 0). This could for 70 * example be a queue size for a given channel id. 71 */ 72 void setParameter(const Parameter& parameter, 73 unsigned int index = 0); 74 75 /** 76 * Get a parameter of a given name and of a certain index. Calling 77 * this method on an object that has no parameter of that name 78 * will result in application exit. 79 */ 80 Parameter getParameter(const std::string& id, 81 unsigned int index = 0) const; 82 83 /** 84 * Remove a parameter with a specific name and index. If a parameter 85 * is removed this method returns true, otherwise false. 86 */ 87 bool removeParameter(const std::string& id, 88 unsigned int index = 0); 89 90 /** 91 * Simply get all the parameters. 92 */ 93 std::vector<Parameter> getParameters() const; 94 95 /** 96 * Check if a parameter of a certain name exists in the object. 97 */ 98 bool hasParameter(const std::string& id, 99 unsigned int index = 0) const; 100 101 /** 102 * Convenience function to set a variable to the value stored in a parameter 103 * with built in error detection/assertion. Returns true if the value was 104 * successfully set. 105 * @param m 106 * @param paramName 107 * @param assertOnFail 108 * @return 109 */ 110 template<typename T> 111 bool setVarFromParam(T* m, const char* paramName) 112 { 113 if (hasParameter(paramName)) { 114 *m = static_cast<T>(getParameter(paramName)); 115 return true; 116 } else { 117 *m = static_cast<T>(0); 118 return false; 119 } 120 } 121 122 template<typename T> 123 T getParamValWithDefault(const char* paramName, T defaultVal) const 124 { 125 if (hasParameter(paramName)) { 126 return static_cast<T>(getParameter(paramName)); 127 } else { 128 return defaultVal; 129 } 130 } 131 132 protected: 133 std::vector<Parameter> parameters; 134}; 135} 136#endif // ifndef DATA_PARAMETRISABLE_H 137