1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "model/context.hpp"
31 #include "utils/format/macros.hpp"
32 #include "utils/fs/path.hpp"
33 #include "utils/noncopyable.hpp"
34 #include "utils/text/operations.ipp"
36 namespace fs = utils::fs;
37 namespace text = utils::text;
40 /// Internal implementation of a context.
41 struct model::context::impl : utils::noncopyable {
42 /// The current working directory.
45 /// The environment variables.
46 std::map< std::string, std::string > _env;
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_) :
59 /// Equality comparator.
61 /// \param other The object to compare to.
63 /// \return True if the two objects are equal; false otherwise.
65 operator==(const impl& other) const
67 return _cwd == other._cwd && _env == other._env;
72 /// Constructs a new context.
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_))
84 model::context::~context(void)
89 /// Returns the current working directory of the context.
93 model::context::cwd(void) const
99 /// Returns the environment variables of the context.
101 /// \return A variable name to variable value mapping.
102 const std::map< std::string, std::string >&
103 model::context::env(void) const
109 /// Equality comparator.
111 /// \param other The other object to compare this one to.
113 /// \return True if this object and other are equal; false otherwise.
115 model::context::operator==(const context& other) const
117 return *_pimpl == *other._pimpl;
121 /// Inequality comparator.
123 /// \param other The other object to compare this one to.
125 /// \return True if this object and other are different; false otherwise.
127 model::context::operator!=(const context& other) const
129 return !(*this == other);
133 /// Injects the object into a stream.
135 /// \param output The stream into which to inject the object.
136 /// \param object The object to format.
138 /// \return The output stream.
140 model::operator<<(std::ostream& output, const context& object)
142 output << F("context{cwd=%s, env=[")
143 % text::quote(object.cwd().str(), '\'');
145 const std::map< std::string, std::string >& env = object.env();
147 for (std::map< std::string, std::string >::const_iterator
148 iter = env.begin(); iter != env.end(); ++iter) {
153 output << F("%s=%s") % (*iter).first
154 % text::quote((*iter).second, '\'');