1 // Copyright 2011 The Kyua Authors. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors 14 // may be used to endorse or promote products derived from this software 15 // without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #include "model/context.hpp" 30 31 #include "utils/format/macros.hpp" 32 #include "utils/fs/path.hpp" 33 #include "utils/noncopyable.hpp" 34 #include "utils/text/operations.ipp" 35 36 namespace fs = utils::fs; 37 namespace text = utils::text; 38 39 40 /// Internal implementation of a context. 41 struct model::context::impl : utils::noncopyable { 42 /// The current working directory. 43 fs::path _cwd; 44 45 /// The environment variables. 46 std::map< std::string, std::string > _env; 47 48 /// Constructor. 49 /// 50 /// \param cwd_ The current working directory. 51 /// \param env_ The environment variables. 52 impl(const fs::path& cwd_, 53 const std::map< std::string, std::string >& env_) : 54 _cwd(cwd_), 55 _env(env_) 56 { 57 } 58 59 /// Equality comparator. 60 /// 61 /// \param other The object to compare to. 62 /// 63 /// \return True if the two objects are equal; false otherwise. 64 bool 65 operator==(const impl& other) const 66 { 67 return _cwd == other._cwd && _env == other._env; 68 } 69 }; 70 71 72 /// Constructs a new context. 73 /// 74 /// \param cwd_ The current working directory. 75 /// \param env_ The environment variables. 76 model::context::context(const fs::path& cwd_, 77 const std::map< std::string, std::string >& env_) : 78 _pimpl(new impl(cwd_, env_)) 79 { 80 } 81 82 83 /// Destructor. 84 model::context::~context(void) 85 { 86 } 87 88 89 /// Returns the current working directory of the context. 90 /// 91 /// \return A path. 92 const fs::path& 93 model::context::cwd(void) const 94 { 95 return _pimpl->_cwd; 96 } 97 98 99 /// Returns the environment variables of the context. 100 /// 101 /// \return A variable name to variable value mapping. 102 const std::map< std::string, std::string >& 103 model::context::env(void) const 104 { 105 return _pimpl->_env; 106 } 107 108 109 /// Equality comparator. 110 /// 111 /// \param other The other object to compare this one to. 112 /// 113 /// \return True if this object and other are equal; false otherwise. 114 bool 115 model::context::operator==(const context& other) const 116 { 117 return *_pimpl == *other._pimpl; 118 } 119 120 121 /// Inequality comparator. 122 /// 123 /// \param other The other object to compare this one to. 124 /// 125 /// \return True if this object and other are different; false otherwise. 126 bool 127 model::context::operator!=(const context& other) const 128 { 129 return !(*this == other); 130 } 131 132 133 /// Injects the object into a stream. 134 /// 135 /// \param output The stream into which to inject the object. 136 /// \param object The object to format. 137 /// 138 /// \return The output stream. 139 std::ostream& 140 model::operator<<(std::ostream& output, const context& object) 141 { 142 output << F("context{cwd=%s, env=[") 143 % text::quote(object.cwd().str(), '\''); 144 145 const std::map< std::string, std::string >& env = object.env(); 146 bool first = true; 147 for (std::map< std::string, std::string >::const_iterator 148 iter = env.begin(); iter != env.end(); ++iter) { 149 if (!first) 150 output << ", "; 151 first = false; 152 153 output << F("%s=%s") % (*iter).first 154 % text::quote((*iter).second, '\''); 155 } 156 157 output << "]}"; 158 return output; 159 } 160