From: Dean M. B. <mik...@gm...> - 2008-10-26 13:14:34
|
Hi John, On Fri, Oct 24, 2008 at 5:38 AM, John P. Feltz <jf...@ov...> wrote: > > I implemented this in a similar network client project some time ago: > > namespace rfc3986 { > template < > typename ParserInput = std::string, > typename Scheme = std::string, > typename Authority = std::string, > typename Path = std::string, > typename Query = std::string, > typename Fragment =std::string > > class URI { > public: We use traits for these things, and have a simple 'Tag' template argument. > > ///Public type access > typedef Scheme scheme_type; > typedef Authority authority_type; > typedef Path path_type; > typedef Query query_type; > typedef Fragment fragment_type; > These are unnecessary since the traits+tag will make the types accessible even without having access to the definition of the class. > Scheme getScheme() const { return m_scheme; } > Authority getAuthority() const { return m_auth; } > Path getPath() const { return m_path; } > Query getQuery() const { return m_query; } > Fragment getFragment() const { return m_fragment; } > > private: > typedef URI< ParserInput, Scheme, Authority, Path, Query, > Fragment> Output; > > friend Output* parse< ParserInput, Output> > (std::stack<std::string>& errors, const ParserInput& input); > Why a friend class for the parsing? > URI(Scheme scheme, Authority auth, Path path, Query query, > Fragment fragment) : > m_scheme(scheme), m_auth(auth), m_path(path), m_query(query), > m_fragment(fragment) {} > > const Scheme m_scheme; > const Authority m_auth; > const Path m_path; > const Query m_query; > const Fragment m_fragment; > }; > }; > There's no way to create a URI object from a constructor? > > The advantage of this is: > > 1) The relationship between the parser and the validated URI object is > explicit. Which is something you don't need to expose, and makes things complicated. This means I can't do something like: uri root("http://www.boost.org/"); > 2) This is a flexible class with a precise interface. IE: > > typedef URI< > URI<>, > std::string, > HostConnection, > std::string, > std::string > > HTTP; > > class HostConnection { > public: > std::string getName() const; > const int* getPort() const; > > HostConnection(const HostConnection& other); > ~HostConnection(); > > private: > friend HostConnection* parse<std::string, HostConnection> > (std::stack<std::string>& errors, const std::string& input); > > HostConnection(const std::string& name, const int* port); > > const std::string m_name; > const int* m_port; > }; > > I will provide full sources with parser if requested. > Why does the parser have to be a free function when it can be part of the constructor which makes the interface a lot simpler? -- Dean Michael C. Berris Software Engineer, Friendster, Inc. |