1 /* 2 * Copyright 1993, 2000 Christopher Seiwald. 3 * 4 * This file is part of Jam - see jam.c for Copyright information. 5 */ 6 7 /* 8 * variable.h - handle jam multi-element variables 9 */ 10 11 #ifndef VARIABLE_SW20111119_H 12 #define VARIABLE_SW20111119_H 13 14 #include "config.h" 15 #include "lists.h" 16 #include "object.h" 17 #include "modules.h" 18 19 #include <string> 20 21 22 struct module_t; 23 24 void var_defines( struct module_t *, const char * const * e, int preprocess ); 25 LIST * var_get( struct module_t *, OBJECT * symbol ); 26 void var_set( struct module_t *, OBJECT * symbol, LIST * value, int flag ); 27 LIST * var_swap( struct module_t *, OBJECT * symbol, LIST * value ); 28 void var_done( struct module_t * ); 29 30 /* 31 * Defines for var_set(). 32 */ 33 34 #define VAR_SET 0 /* override previous value */ 35 #define VAR_APPEND 1 /* append to previous value */ 36 #define VAR_DEFAULT 2 /* set only if no previous value */ 37 38 namespace b2 { namespace jam { 39 struct variable 40 { variablevariable41 inline variable(const variable &v) 42 : var_module(v.var_module), var_symbol(object_copy(v.var_symbol)) {} variablevariable43 inline variable(struct module_t *m, const char *v) 44 : var_module(m), var_symbol(object_new(v)) {} variablevariable45 inline variable(const char *m, const char *v) 46 : variable(bindmodule(object_new(m)), v) {} variablevariable47 inline variable(const std::string &m, const std::string &v) 48 : variable(bindmodule(object_new(m.c_str())), v.c_str()) {} variablevariable49 inline explicit variable(const char *v) : variable(root_module(), v) {} variablevariable50 inline explicit variable(const std::string &v) : variable(v.c_str()) {} ~variablevariable51 inline ~variable() 52 { 53 if (var_symbol) object_free(var_symbol); 54 } 55 listvariable56 inline operator list() const { return list{var_get(var_module, var_symbol)}; } 57 58 inline variable & operator=(list && v) 59 { 60 var_set(var_module, var_symbol, v.release(), VAR_SET); 61 return *this; 62 } 63 inline variable & operator=(const list & v) { return *this = list{v}; } 64 inline variable & operator=(const char *v) { return *this = list{object{v}}; } 65 inline variable & operator=(const std::string &v) { return *this = list{object{v}}; } 66 67 inline variable & operator+=(list & v) 68 { 69 var_set(var_module, var_symbol, v.release(), VAR_APPEND); 70 return *this; 71 } 72 inline variable & operator+=(list && v) 73 { 74 var_set(var_module, var_symbol, v.release(), VAR_APPEND); 75 return *this; 76 } 77 inline variable & operator+=(const char *v) { return *this += list{object{v}}; } 78 inline variable & operator+=(const std::string &v) { return *this += list{object{v}}; } 79 80 inline variable & operator|=(list & v) 81 { 82 var_set(var_module, var_symbol, v.release(), VAR_DEFAULT); 83 return *this; 84 } 85 inline variable & operator|=(list && v) 86 { 87 var_set(var_module, var_symbol, v.release(), VAR_DEFAULT); 88 return *this; 89 } 90 inline variable & operator|=(const char *v) { return *this |= list{object{v}}; } 91 inline variable & operator|=(const std::string &v) { return *this |= list{object{v}}; } 92 93 inline operator bool() const 94 { 95 LIST * l = var_get(var_module, var_symbol); 96 return (!list_empty(l)) && (list_length(l) > 0); 97 } 98 99 private: 100 101 struct module_t * var_module = nullptr; 102 OBJECT * var_symbol = nullptr; 103 }; 104 }} 105 106 #endif 107