|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
3 |
|
4 // Copyright (c) 2011, 2013 Google Inc. |
|
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 // * Redistributions of source code must retain the above copyright |
|
12 // notice, this list of conditions and the following disclaimer. |
|
13 // * Redistributions in binary form must reproduce the above |
|
14 // copyright notice, this list of conditions and the following disclaimer |
|
15 // in the documentation and/or other materials provided with the |
|
16 // distribution. |
|
17 // * Neither the name of Google Inc. nor the names of its |
|
18 // contributors may be used to endorse or promote products derived from |
|
19 // this software without specific prior written permission. |
|
20 // |
|
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
|
33 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
|
34 |
|
35 |
|
36 // This file is derived from the following files in |
|
37 // toolkit/crashreporter/google-breakpad: |
|
38 // src/common/module.cc |
|
39 // src/common/unique_string.cc |
|
40 |
|
41 // There's no internal-only interface for LulCommon. Hence include |
|
42 // the external interface directly. |
|
43 #include "LulCommonExt.h" |
|
44 |
|
45 #include <stdlib.h> |
|
46 #include <string.h> |
|
47 |
|
48 #include <string> |
|
49 #include <map> |
|
50 |
|
51 |
|
52 namespace lul { |
|
53 |
|
54 using std::string; |
|
55 |
|
56 //////////////////////////////////////////////////////////////// |
|
57 // Module |
|
58 // |
|
59 Module::Module(const string &name, const string &os, |
|
60 const string &architecture, const string &id) : |
|
61 name_(name), |
|
62 os_(os), |
|
63 architecture_(architecture), |
|
64 id_(id) { } |
|
65 |
|
66 Module::~Module() { |
|
67 } |
|
68 |
|
69 |
|
70 //////////////////////////////////////////////////////////////// |
|
71 // UniqueString |
|
72 // |
|
73 class UniqueString { |
|
74 public: |
|
75 UniqueString(string str) { str_ = strdup(str.c_str()); } |
|
76 ~UniqueString() { free(reinterpret_cast<void*>(const_cast<char*>(str_))); } |
|
77 const char* str_; |
|
78 }; |
|
79 |
|
80 class UniqueStringUniverse { |
|
81 public: |
|
82 UniqueStringUniverse() {}; |
|
83 const UniqueString* FindOrCopy(string str) { |
|
84 std::map<string, UniqueString*>::iterator it = map_.find(str); |
|
85 if (it == map_.end()) { |
|
86 UniqueString* ustr = new UniqueString(str); |
|
87 map_[str] = ustr; |
|
88 return ustr; |
|
89 } else { |
|
90 return it->second; |
|
91 } |
|
92 } |
|
93 private: |
|
94 std::map<string, UniqueString*> map_; |
|
95 }; |
|
96 |
|
97 |
|
98 const UniqueString* ToUniqueString(string str) { |
|
99 // For the initialisation of sUniverse to be threadsafe, |
|
100 // this relies on C++11's semantics. |
|
101 static UniqueStringUniverse sUniverse; |
|
102 return sUniverse.FindOrCopy(str); |
|
103 } |
|
104 |
|
105 const char* const FromUniqueString(const UniqueString* ustr) |
|
106 { |
|
107 return ustr->str_; |
|
108 } |
|
109 |
|
110 } // namespace lul |